Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

Commit

Permalink
added test for tchannel json as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Onwukike Ibe committed Oct 27, 2016
1 parent beaaa99 commit 479d9bb
Showing 1 changed file with 86 additions and 72 deletions.
158 changes: 86 additions & 72 deletions test/tchannel_bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,92 +34,106 @@ import TChannel from 'tchannel';
import TChannelThrift from 'tchannel/as/thrift';
import TChannelBridge from '../src/tchannel_bridge.js';
import TChannelAsThrift from 'tchannel/as/thrift';
import TChannelAsJSON from 'tchannel/as/json';
import Utils from '../src/util.js';

describe ('test tchannel span bridge', () => {
// BIG_TIMEOUT is useful for debugging purposes
let BIG_TIMEOUT = 15000000;

let tracer = new Tracer(
'test-service',
new InMemoryReporter(),
new ConstSampler(true)
);

it ('spans propagate through tchannel and preserve parent span properties', function tchTest1(done) {
let span = tracer.startSpan('futurama');
span.setBaggageItem('leela', 'fry');
let headers = TChannelBridge.saveTracerStateToCarrier(span);
TChannelBridge.saveBaggageToCarrier(span, headers);
let server = new TChannel({
serviceName: 'server',
// We don't want tchannel to generate any spans.
trace: true,
forceTrace: true
});
// Server calls client channel after it starts listening.
server.listen(4040, '127.0.0.1', onServerListening);


// Create the toplevel client channel.
let client = new TChannel({
// We don't want tchannel to generate any spans.
trace: true,
forceTrace: true
});


// Create the client subchannel that makes requests.
let clientSubChannel = client.makeSubChannel({
serviceName: 'server',
peers: ['127.0.0.1:4040']
});

// Wrap the subchannel in an encoding
let encodedChannel = TChannelAsThrift({
channel: clientSubChannel,
entryPoint: path.join(__dirname, 'thrift', 'echo.thrift')
});

// register the server request function.
let context = {};
encodedChannel.register(server, 'Echo::echo', context, handleServerReq);
function handleServerReq(context, req, head, body, callback) {

callback(null, {
ok: true,
head: head,
body: body
let options = [
{ description: 'tchannelAsJson', channelEncoding: TChannelAsJSON },
{ description: 'tchannelAsThrift', channelEncoding: TChannelAsThrift }
];

_.each(options, (o) => {

it (o.description + ' spans propagate through tchannel and preserve parent span properties', function tchTest1(done) {
let span = tracer.startSpan('futurama');
span.setBaggageItem('leela', 'fry');
let headers = TChannelBridge.saveTracerStateToCarrier(span);
TChannelBridge.saveBaggageToCarrier(span, headers);
let server = new TChannel({
serviceName: 'server',
trace: true,
forceTrace: true
});
}
// Server calls client channel after it starts listening.
server.listen(4040, '127.0.0.1', onServerListening);


function onServerListening() {
let req = encodedChannel.request({
// Create the toplevel client channel.
let client = new TChannel({
trace: true,
forceTrace: true
});


// Create the client subchannel that makes requests.
let clientSubChannel = client.makeSubChannel({
serviceName: 'server',
parent: {span: TChannelBridge.getTChannelParentSpan() },
headers: { cn: 'echo' },
timeout: 15000000
peers: ['127.0.0.1:4040']
});
clientSubChannel.trace = false;
server.trace = false;
req.trace = false;

req.send('Echo::echo', headers, { value: 'some-string' }, (err, res, arg2, arg3) => {
assert.isNotOk(err);
let receivedSpan = TChannelBridge.getSpanFromTChannelRequest(tracer, 'nibbler', res.head);
assert.equal(receivedSpan.name, 'nibbler');
assert.equal(span.context().traceIdStr, receivedSpan.context().traceIdStr);

let headers = res.head[constants.TCHANNEL_TRACING_PREFIX + constants.TRACER_STATE_HEADER_NAME].split(':');
let traceId = headers[0];
let spanId = headers[1];
let flags = parseInt(headers[3]);
assert.equal(span.context().traceIdStr, traceId);
assert.equal(span.context().spanIdStr, spanId);
assert.equal(span.context().parentId, null);
assert.equal(span.context().flags, flags);

done();

// Wrap the subchannel in an encoding
let encodedChannel = o.channelEncoding({
channel: clientSubChannel,
entryPoint: path.join(__dirname, 'thrift', 'echo.thrift') // ignored in json case
});
}
}).timeout(1500000);

// register the server request function.
let context = {};
encodedChannel.register(server, 'Echo::echo', context, handleServerReq);
function handleServerReq(context, req, head, body, callback) {

callback(null, {
ok: true,
head: head,
body: body
});
}

function onServerListening() {
let req = encodedChannel.request({
serviceName: 'server',
// We don't want tchannel to generate any spans.
parent: {span: TChannelBridge.getTChannelParentSpan() },
headers: { cn: 'echo' },
timeout: BIG_TIMEOUT
});
clientSubChannel.trace = false;
server.trace = false;
req.trace = false;

req.send('Echo::echo', headers, { value: 'some-string' }, (err, res, arg2, arg3) => {
assert.isNotOk(err);
let receivedSpan = TChannelBridge.getSpanFromTChannelRequest(tracer, 'nibbler', res.head);
assert.equal(receivedSpan.name, 'nibbler');
assert.equal(span.context().traceIdStr, receivedSpan.context().traceIdStr);

let headers = res.head[constants.TCHANNEL_TRACING_PREFIX + constants.TRACER_STATE_HEADER_NAME].split(':');
let traceId = headers[0];
let spanId = headers[1];
let flags = parseInt(headers[3]);

assert.equal(span.context().traceIdStr, traceId);
assert.equal(span.context().spanIdStr, spanId);
assert.equal(span.context().parentId, null);
assert.equal(span.context().flags, flags);
server.close();
client.close();

done();
});
}
}).timeout(BIG_TIMEOUT);
});

it ('saveBaggageToCarrier', () => {
let headers = {
Expand Down

0 comments on commit 479d9bb

Please sign in to comment.