Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #55 from oibe/split_opentracing_compatibility_tests
Browse files Browse the repository at this point in the history
Separated opentracing mock tracer tests from opentracing api
  • Loading branch information
oibe committed Sep 7, 2016
2 parents 76a10b1 + ed19e42 commit 22c433f
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 70 deletions.
88 changes: 18 additions & 70 deletions test/api_compatibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,17 @@ var expect = require('chai').expect;
// library. Again, globals are used purely for convenience.
var opentracing = require('../debug.js');

module.exports = function apiCompatibilityChecks(createTracer) {
describe('OpenTracing API', function() {
describe('Constants', function() {
var constStrings = [
'FORMAT_TEXT_MAP',
'FORMAT_BINARY',
'FORMAT_HTTP_HEADERS',
'REFERENCE_CHILD_OF',
'REFERENCE_FOLLOWS_FROM',
];
_.each(constStrings, function(name) {
it(name + ' should be a constant string', function() {
expect(opentracing[name]).to.be.a('string');
});
});
});

describe('Standalone functions', function() {
var funcs = [
'childOf',
'followsFrom',
'initGlobalTracer',
'globalTracer',

];
_.each(funcs, function(name) {
it(name + ' should be a function', function() {
expect(opentracing[name]).to.be.a('function');
});
});
});
module.exports = function apiCompatibilityChecks(_createTracer) {
var createTracer = _createTracer || function() { return new opentracing.Tracer(); };

describe('OpenTracing API Compatibility', function() {
describe('Tracer', function() {
it('should be a class', function() {
expect(opentracing.Tracer).to.be.a('function');
expect(new opentracing.Tracer()).to.be.an('object');
expect(createTracer).to.be.a('function');
expect(createTracer()).to.be.an('object');
});

var tracer = new opentracing.Tracer();
var tracer = createTracer();
var funcs = [
'startSpan',
'inject',
Expand All @@ -59,22 +31,19 @@ module.exports = function apiCompatibilityChecks(createTracer) {

describe('Tracer#startSpan', function() {
it('should handle Spans and SpanContexts', function() {
var tracer = new opentracing.Tracer();
var tracer = createTracer();
var span = tracer.startSpan('test_operation');
expect(function() { tracer.startSpan('child', { childOf : span }); }).to.not.throw(Error);
});
});

describe('Tracer#inject', function() {
it('should enforce the required carrier types', function() {
var tracer = new opentracing.Tracer();
it('should not throw exception on required carrier types', function() {
var tracer = createTracer();
var spanContext = tracer.startSpan('test_operation').context();
var textCarrier = {};
expect(function() { tracer.inject(spanContext, opentracing.FORMAT_TEXT_MAP, textCarrier); }).to.not.throw(Error);
expect(function() { tracer.inject(spanContext, opentracing.FORMAT_TEXT_MAP, ''); }).to.throw(Error);
expect(function() { tracer.inject(spanContext, opentracing.FORMAT_TEXT_MAP, 5); }).to.throw(Error);

var binCarrier = new opentracing.BinaryCarrier();
expect(function() { tracer.inject(spanContext, opentracing.FORMAT_TEXT_MAP, textCarrier); }).to.not.throw(Error);
expect(function() { tracer.inject(spanContext, opentracing.FORMAT_BINARY, binCarrier); }).to.not.throw(Error);
expect(function() { tracer.inject(spanContext, opentracing.FORMAT_BINARY, new Object); }).to.not.throw(Error);
expect(function() { tracer.inject(spanContext, opentracing.FORMAT_BINARY, {}); }).to.not.throw(Error);
Expand All @@ -83,11 +52,10 @@ module.exports = function apiCompatibilityChecks(createTracer) {
expect(function() { tracer.extract(opentracing.FORMAT_BINARY, binCarrier); }).to.not.throw(Error);
expect(function() { tracer.extract(opentracing.FORMAT_BINARY, {}); }).to.not.throw(Error);
expect(function() { tracer.extract(opentracing.FORMAT_BINARY, { buffer : null }); }).to.not.throw(Error);
expect(function() { tracer.extract(opentracing.FORMAT_BINARY, { buffer : '' }); }).to.throw(Error);
expect(function() { tracer.extract(opentracing.FORMAT_BINARY, { buffer : 5 }); }).to.throw(Error);
});

it('should handle Spans and SpanContexts', function() {
var tracer = new opentracing.Tracer();
var tracer = createTracer();
var span = tracer.startSpan('test_operation');
var textCarrier = {};
expect(function() { tracer.inject(span, opentracing.FORMAT_TEXT_MAP, textCarrier); }).to.not.throw(Error);
Expand All @@ -96,7 +64,7 @@ module.exports = function apiCompatibilityChecks(createTracer) {
});

describe('Span', function() {
var tracer = new opentracing.Tracer();
var tracer = createTracer();
var span = tracer.startSpan('test_span');

it('should be a class', function() {
Expand All @@ -121,13 +89,8 @@ module.exports = function apiCompatibilityChecks(createTracer) {
});

describe('Span#finish', function() {
it('should return undefined', function() {
var tracer = new opentracing.Tracer();
var span = tracer.startSpan('test_span');
expect(span.finish()).to.be.undefined;
});
it('should only accept numbers as an argument', function() {
var tracer = new opentracing.Tracer();
it('should not throw exceptions on valid arguments', function() {
var tracer = createTracer();
function f(arg) {
return function() {
var span = tracer.startSpan('test_span');
Expand All @@ -136,20 +99,12 @@ module.exports = function apiCompatibilityChecks(createTracer) {
}
expect(f(10)).to.not.throw(Error);
expect(f(Date.now())).to.not.throw(Error);
expect(f('1234567')).to.throw(Error);
expect(f([])).to.throw(Error);
expect(f({})).to.throw(Error);
});
it('should throw an Error if given more than 1 argument', function() {
var tracer = new opentracing.Tracer();
var span = tracer.startSpan('test_span');
expect(function() { span.finish(Date.now(), { extra : 123 }) }).to.be.throw(Error);
});
});
});

describe('SpanContext', function() {
var tracer = new opentracing.Tracer();
var tracer = createTracer();
var span = tracer.startSpan('test_span');
var spanContext = span.context();

Expand All @@ -160,26 +115,19 @@ module.exports = function apiCompatibilityChecks(createTracer) {


describe('Reference', function() {
var tracer = new opentracing.Tracer();
var tracer = createTracer();
var span = tracer.startSpan('test_span');
var ref = new opentracing.Reference(opentracing.REFERENCE_CHILD_OF, span.context());

it('should be a class', function() {
expect(ref).to.be.an('object');
});

it('should handle Spans and SpanContexts', function() {
var tracer = new opentracing.Tracer();
var span = tracer.startSpan('test_operation');
expect(function() { new opentracing.Reference(opentracing.REFERENCE_CHILD_OF, span); }).to.not.throw(Error);
});
});
});

describe('Miscellaneous', function() {
describe('Memory usage', function() {
it('should not report leaks after setting the global tracer', function() {
opentracing.initGlobalTracer(new opentracing.Tracer());
});
});
});
}
67 changes: 67 additions & 0 deletions test/noop_implementation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// For the convenience of unit testing, add these to the global namespace
var _ = require('underscore');
var expect = require('chai').expect;

// Unit testing is done against the debug version of the library as it has
// additional conformance checks that are optimized out of the production
// library. Again, globals are used purely for convenience.
var opentracing = require('../debug.js');

module.exports = function noopImplementationTests(_createTracer) {
var createTracer = _createTracer || function() { return new opentracing.Tracer(); };

describe('Noop Tracer Implementation', function() {
describe('Tracer#inject', function() {
it('should enforce the required carrier types', function() {
var tracer = createTracer();
var spanContext = tracer.startSpan('test_operation').context();
expect(function() { tracer.inject(spanContext, opentracing.FORMAT_TEXT_MAP, ''); }).to.throw(Error);
expect(function() { tracer.inject(spanContext, opentracing.FORMAT_TEXT_MAP, 5); }).to.throw(Error);

expect(function() { tracer.extract(opentracing.FORMAT_BINARY, { buffer : '' }); }).to.throw(Error);
expect(function() { tracer.extract(opentracing.FORMAT_BINARY, { buffer : 5 }); }).to.throw(Error);
});

it('should handle Spans and SpanContexts', function() {
var tracer = createTracer();
var span = tracer.startSpan('test_operation');
var textCarrier = {};
expect(function() { tracer.inject(span, opentracing.FORMAT_TEXT_MAP, textCarrier); }).to.not.throw(Error);
});
});

describe('Span#finish', function() {
it('should return undefined', function() {
var tracer = createTracer();
var span = tracer.startSpan('test_span');
expect(span.finish()).to.be.undefined;
});

it('should only accept numbers as an argument', function() {
var tracer = createTracer();
function f(arg) {
return function() {
var span = tracer.startSpan('test_span');
span.finish(arg);
}
}
expect(f('1234567')).to.throw(Error);
expect(f([])).to.throw(Error);
expect(f({})).to.throw(Error);
});
it('should throw an Error if given more than 1 argument', function() {
var tracer = createTracer();
var span = tracer.startSpan('test_span');
expect(function() { span.finish(Date.now(), { extra : 123 }) }).to.be.throw(Error);
});
});

describe('Miscellaneous', function() {
describe('Memory usage', function() {
it('should not report leaks after setting the global tracer', function() {
opentracing.initGlobalTracer(createTracer());
});
});
});
});
}
37 changes: 37 additions & 0 deletions test/opentracing_api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var _ = require('underscore');
var expect = require('chai').expect;
var opentracing = require('../lib');

module.exports = function opentracingAPITests() {
describe('Opentracing API', function() {
describe('Constants', function() {
var constStrings = [
'FORMAT_TEXT_MAP',
'FORMAT_BINARY',
'FORMAT_HTTP_HEADERS',
'REFERENCE_CHILD_OF',
'REFERENCE_FOLLOWS_FROM',
];
_.each(constStrings, function(name) {
it(name + ' should be a constant string', function() {
expect(opentracing[name]).to.be.a('string');
});
});
});

describe('Standalone functions', function() {
var funcs = [
'childOf',
'followsFrom',
'initGlobalTracer',
'globalTracer',

];
_.each(funcs, function(name) {
it(name + ' should be a function', function() {
expect(opentracing[name]).to.be.a('function');
});
});
});
});
}
8 changes: 8 additions & 0 deletions test/unittest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
require('source-map-support').install();

var apiCompatibilityChecks = require('./api_compatibility.js');
var noopImplementationTests = require('./noop_implementation.js');
var opentracingAPITests = require('./opentracing_api.js');
var opentracing = require('../debug.js');

// Run the tests on the default OpenTracing no-op Tracer.
noopImplementationTests();

// Run the api conformance tests on the default Opentracing no-op Tracer.
apiCompatibilityChecks(function() {
return new opentracing.Tracer();
});

opentracingAPITests();

0 comments on commit 22c433f

Please sign in to comment.