From 1cab4e39aaebdd06581c6a077ac9e2d62a187635 Mon Sep 17 00:00:00 2001 From: Onwukike Ibe Date: Tue, 6 Sep 2016 18:12:59 -0400 Subject: [PATCH 1/5] Separated opentracing mock tracer tests from opentracing api compatibility tests. --- test/api_compatibility.js | 123 +++---------------------------------- test/mock_compatibility.js | 114 ++++++++++++++++++++++++++++++++++ test/unittest.js | 5 ++ 3 files changed, 128 insertions(+), 114 deletions(-) create mode 100644 test/mock_compatibility.js diff --git a/test/api_compatibility.js b/test/api_compatibility.js index a97803d..3c88206 100644 --- a/test/api_compatibility.js +++ b/test/api_compatibility.js @@ -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 opentracing.Tracer(); }; + describe('OpenTracing API', 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 = new createTracer(); var funcs = [ 'startSpan', 'inject', @@ -56,47 +28,10 @@ module.exports = function apiCompatibilityChecks(createTracer) { expect(tracer[name]).to.be.a('function'); }); }); - - describe('Tracer#startSpan', function() { - it('should handle Spans and SpanContexts', function() { - var tracer = new opentracing.Tracer(); - 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(); - 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_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); - expect(function() { tracer.inject(spanContext, opentracing.FORMAT_BINARY, { buffer : null }); }).to.not.throw(Error); - - 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 span = tracer.startSpan('test_operation'); - var textCarrier = {}; - expect(function() { tracer.inject(span, opentracing.FORMAT_TEXT_MAP, textCarrier); }).to.not.throw(Error); - }); - }); }); describe('Span', function() { - var tracer = new opentracing.Tracer(); + var tracer = new createTracer(); var span = tracer.startSpan('test_span'); it('should be a class', function() { @@ -119,37 +54,10 @@ module.exports = function apiCompatibilityChecks(createTracer) { expect(span[name]).to.be.a('function'); }); }); - - 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(); - function f(arg) { - return function() { - var span = tracer.startSpan('test_span'); - span.finish(arg); - } - } - 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 = new createTracer(); var span = tracer.startSpan('test_span'); var spanContext = span.context(); @@ -160,26 +68,13 @@ module.exports = function apiCompatibilityChecks(createTracer) { describe('Reference', function() { - var tracer = new opentracing.Tracer(); + var tracer = new 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()); - }); }); }); } diff --git a/test/mock_compatibility.js b/test/mock_compatibility.js new file mode 100644 index 0000000..a20f109 --- /dev/null +++ b/test/mock_compatibility.js @@ -0,0 +1,114 @@ +// 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 mockCompatibilityChecks() { + describe('Mock Opentracing Implementation', 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'); + }); + }); + }); + + describe('Tracer#startSpan', function() { + it('should handle Spans and SpanContexts', function() { + var tracer = new opentracing.Tracer(); + 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(); + 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_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); + expect(function() { tracer.inject(spanContext, opentracing.FORMAT_BINARY, { buffer : null }); }).to.not.throw(Error); + + 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 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 = 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(); + function f(arg) { + return function() { + var span = tracer.startSpan('test_span'); + span.finish(arg); + } + } + 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('Miscellaneous', function() { + describe('Memory usage', function() { + it('should not report leaks after setting the global tracer', function() { + opentracing.initGlobalTracer(new opentracing.Tracer()); + }); + }); + }); + }); +} diff --git a/test/unittest.js b/test/unittest.js index 8ba7150..3928228 100644 --- a/test/unittest.js +++ b/test/unittest.js @@ -2,8 +2,13 @@ require('source-map-support').install(); var apiCompatibilityChecks = require('./api_compatibility.js'); +var mockCompatibilityChecks = require('./mock_compatibility.js'); +var opentracing = require('../debug.js'); // Run the tests on the default OpenTracing no-op Tracer. +mockCompatibilityChecks(); + +// Run the api conformance tests on the default Opentracing no-op Tracer. apiCompatibilityChecks(function() { return new opentracing.Tracer(); }); From b596eab84315cba72bf6aa2f2d6b3474af146c7e Mon Sep 17 00:00:00 2001 From: Onwukike Ibe Date: Wed, 7 Sep 2016 10:44:19 -0400 Subject: [PATCH 2/5] Added "do not throw exception" tests to api compatibility tests --- test/api_compatibility.js | 67 +++++++++++++++++++++++++++++++++++--- test/mock_compatibility.js | 27 --------------- 2 files changed, 63 insertions(+), 31 deletions(-) diff --git a/test/api_compatibility.js b/test/api_compatibility.js index 3c88206..6567b88 100644 --- a/test/api_compatibility.js +++ b/test/api_compatibility.js @@ -11,13 +11,25 @@ module.exports = function apiCompatibilityChecks(_createTracer) { var createTracer = _createTracer || function() { return opentracing.Tracer(); }; describe('OpenTracing API', function() { + describe('Standalone functions', function() { + var funcs = [ + 'childOf', + 'followsFrom', + ]; + _.each(funcs, function(name) { + it(name + ' should be a function', function() { + expect(opentracing[name]).to.be.a('function'); + }); + }); + }); + describe('Tracer', function() { it('should be a class', function() { expect(createTracer).to.be.a('function'); expect(createTracer()).to.be.an('object'); }); - var tracer = new createTracer(); + var tracer = createTracer(); var funcs = [ 'startSpan', 'inject', @@ -30,8 +42,41 @@ module.exports = function apiCompatibilityChecks(_createTracer) { }); }); + describe('Tracer#startSpan', function() { + it('should handle Spans and SpanContexts', function() { + var tracer = new opentracing.Tracer(); + var span = tracer.startSpan('test_operation'); + expect(function() { tracer.startSpan('child', { childOf : span }); }).to.not.throw(Error); + }); + }); + + describe('Tracer#inject', function() { + it('should not throw exception on required carrier types', function() { + var tracer = new opentracing.Tracer(); + var spanContext = tracer.startSpan('test_operation').context(); + var textCarrier = {}; + 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); + expect(function() { tracer.inject(spanContext, opentracing.FORMAT_BINARY, { buffer : null }); }).to.not.throw(Error); + + 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); + }); + + it('should handle Spans and SpanContexts', function() { + var tracer = new opentracing.Tracer(); + var span = tracer.startSpan('test_operation'); + var textCarrier = {}; + expect(function() { tracer.inject(span, opentracing.FORMAT_TEXT_MAP, textCarrier); }).to.not.throw(Error); + }); + }); + describe('Span', function() { - var tracer = new createTracer(); + var tracer = createTracer(); var span = tracer.startSpan('test_span'); it('should be a class', function() { @@ -56,8 +101,22 @@ module.exports = function apiCompatibilityChecks(_createTracer) { }); }); + describe('Span#finish', function() { + it('should not throw exceptions on valid arguments', function() { + var tracer = new opentracing.Tracer(); + function f(arg) { + return function() { + var span = tracer.startSpan('test_span'); + span.finish(arg); + } + } + expect(f(10)).to.not.throw(Error); + expect(f(Date.now())).to.not.throw(Error); + }); + }); + describe('SpanContext', function() { - var tracer = new createTracer(); + var tracer = createTracer(); var span = tracer.startSpan('test_span'); var spanContext = span.context(); @@ -68,7 +127,7 @@ module.exports = function apiCompatibilityChecks(_createTracer) { describe('Reference', function() { - var tracer = new createTracer(); + var tracer = createTracer(); var span = tracer.startSpan('test_span'); var ref = new opentracing.Reference(opentracing.REFERENCE_CHILD_OF, span.context()); diff --git a/test/mock_compatibility.js b/test/mock_compatibility.js index a20f109..f716899 100644 --- a/test/mock_compatibility.js +++ b/test/mock_compatibility.js @@ -39,41 +39,16 @@ module.exports = function mockCompatibilityChecks() { }); }); - describe('Tracer#startSpan', function() { - it('should handle Spans and SpanContexts', function() { - var tracer = new opentracing.Tracer(); - 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(); 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_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); - expect(function() { tracer.inject(spanContext, opentracing.FORMAT_BINARY, { buffer : null }); }).to.not.throw(Error); - - 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 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() { @@ -90,8 +65,6 @@ module.exports = function mockCompatibilityChecks() { span.finish(arg); } } - 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); From 6454c519b62a36d70ff3b16f3a039e27aa7ee767 Mon Sep 17 00:00:00 2001 From: Onwukike Ibe Date: Wed, 7 Sep 2016 13:21:34 -0400 Subject: [PATCH 3/5] fixed indenting --- test/api_compatibility.js | 78 +++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/test/api_compatibility.js b/test/api_compatibility.js index 6567b88..8654f36 100644 --- a/test/api_compatibility.js +++ b/test/api_compatibility.js @@ -40,38 +40,38 @@ module.exports = function apiCompatibilityChecks(_createTracer) { expect(tracer[name]).to.be.a('function'); }); }); - }); - describe('Tracer#startSpan', function() { - it('should handle Spans and SpanContexts', function() { - var tracer = new opentracing.Tracer(); - var span = tracer.startSpan('test_operation'); - expect(function() { tracer.startSpan('child', { childOf : span }); }).to.not.throw(Error); + describe('Tracer#startSpan', function() { + it('should handle Spans and SpanContexts', function() { + var tracer = new opentracing.Tracer(); + var span = tracer.startSpan('test_operation'); + expect(function() { tracer.startSpan('child', { childOf : span }); }).to.not.throw(Error); + }); }); - }); - describe('Tracer#inject', function() { - it('should not throw exception on required carrier types', function() { - var tracer = new opentracing.Tracer(); - var spanContext = tracer.startSpan('test_operation').context(); - var textCarrier = {}; - 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); - expect(function() { tracer.inject(spanContext, opentracing.FORMAT_BINARY, { buffer : null }); }).to.not.throw(Error); - - 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); - }); + describe('Tracer#inject', function() { + it('should not throw exception on required carrier types', function() { + var tracer = new opentracing.Tracer(); + var spanContext = tracer.startSpan('test_operation').context(); + var textCarrier = {}; + 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); + expect(function() { tracer.inject(spanContext, opentracing.FORMAT_BINARY, { buffer : null }); }).to.not.throw(Error); + + 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); + }); - it('should handle Spans and SpanContexts', function() { - var tracer = new opentracing.Tracer(); - var span = tracer.startSpan('test_operation'); - var textCarrier = {}; - expect(function() { tracer.inject(span, opentracing.FORMAT_TEXT_MAP, textCarrier); }).to.not.throw(Error); + it('should handle Spans and SpanContexts', function() { + var tracer = new opentracing.Tracer(); + var span = tracer.startSpan('test_operation'); + var textCarrier = {}; + expect(function() { tracer.inject(span, opentracing.FORMAT_TEXT_MAP, textCarrier); }).to.not.throw(Error); + }); }); }); @@ -99,19 +99,19 @@ module.exports = function apiCompatibilityChecks(_createTracer) { expect(span[name]).to.be.a('function'); }); }); - }); - describe('Span#finish', function() { - it('should not throw exceptions on valid arguments', function() { - var tracer = new opentracing.Tracer(); - function f(arg) { - return function() { - var span = tracer.startSpan('test_span'); - span.finish(arg); + describe('Span#finish', function() { + it('should not throw exceptions on valid arguments', function() { + var tracer = new opentracing.Tracer(); + function f(arg) { + return function() { + var span = tracer.startSpan('test_span'); + span.finish(arg); + } } - } - expect(f(10)).to.not.throw(Error); - expect(f(Date.now())).to.not.throw(Error); + expect(f(10)).to.not.throw(Error); + expect(f(Date.now())).to.not.throw(Error); + }); }); }); From 2ab8becd6137fbb27116d68139a892c73e7defaa Mon Sep 17 00:00:00 2001 From: Onwukike Ibe Date: Wed, 7 Sep 2016 13:27:11 -0400 Subject: [PATCH 4/5] removed functions test from api compatibility --- test/api_compatibility.js | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/test/api_compatibility.js b/test/api_compatibility.js index 8654f36..ed2bb00 100644 --- a/test/api_compatibility.js +++ b/test/api_compatibility.js @@ -11,18 +11,6 @@ module.exports = function apiCompatibilityChecks(_createTracer) { var createTracer = _createTracer || function() { return opentracing.Tracer(); }; describe('OpenTracing API', function() { - describe('Standalone functions', function() { - var funcs = [ - 'childOf', - 'followsFrom', - ]; - _.each(funcs, function(name) { - it(name + ' should be a function', function() { - expect(opentracing[name]).to.be.a('function'); - }); - }); - }); - describe('Tracer', function() { it('should be a class', function() { expect(createTracer).to.be.a('function'); From ed19e425d843a4eacb8de361efa24099f24ee5bb Mon Sep 17 00:00:00 2001 From: Onwukike Ibe Date: Wed, 7 Sep 2016 15:28:53 -0400 Subject: [PATCH 5/5] addressing comments --- test/api_compatibility.js | 18 ++++--- ...ompatibility.js => noop_implementation.js} | 52 ++++++------------- test/opentracing_api.js | 37 +++++++++++++ test/unittest.js | 7 ++- 4 files changed, 70 insertions(+), 44 deletions(-) rename test/{mock_compatibility.js => noop_implementation.js} (63%) create mode 100644 test/opentracing_api.js diff --git a/test/api_compatibility.js b/test/api_compatibility.js index ed2bb00..fba47de 100644 --- a/test/api_compatibility.js +++ b/test/api_compatibility.js @@ -8,9 +8,9 @@ var expect = require('chai').expect; var opentracing = require('../debug.js'); module.exports = function apiCompatibilityChecks(_createTracer) { - var createTracer = _createTracer || function() { return opentracing.Tracer(); }; + var createTracer = _createTracer || function() { return new opentracing.Tracer(); }; - describe('OpenTracing API', function() { + describe('OpenTracing API Compatibility', function() { describe('Tracer', function() { it('should be a class', function() { expect(createTracer).to.be.a('function'); @@ -31,7 +31,7 @@ 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); }); @@ -39,7 +39,7 @@ module.exports = function apiCompatibilityChecks(_createTracer) { describe('Tracer#inject', function() { it('should not throw exception on required carrier types', function() { - var tracer = new opentracing.Tracer(); + var tracer = createTracer(); var spanContext = tracer.startSpan('test_operation').context(); var textCarrier = {}; var binCarrier = new opentracing.BinaryCarrier(); @@ -55,7 +55,7 @@ module.exports = function apiCompatibilityChecks(_createTracer) { }); 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); @@ -90,7 +90,7 @@ module.exports = function apiCompatibilityChecks(_createTracer) { describe('Span#finish', function() { it('should not throw exceptions on valid arguments', function() { - var tracer = new opentracing.Tracer(); + var tracer = createTracer(); function f(arg) { return function() { var span = tracer.startSpan('test_span'); @@ -122,6 +122,12 @@ module.exports = function apiCompatibilityChecks(_createTracer) { 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); + }); }); }); } diff --git a/test/mock_compatibility.js b/test/noop_implementation.js similarity index 63% rename from test/mock_compatibility.js rename to test/noop_implementation.js index f716899..201d513 100644 --- a/test/mock_compatibility.js +++ b/test/noop_implementation.js @@ -7,41 +7,13 @@ var expect = require('chai').expect; // library. Again, globals are used purely for convenience. var opentracing = require('../debug.js'); -module.exports = function mockCompatibilityChecks() { - describe('Mock Opentracing Implementation', 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 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 = new opentracing.Tracer(); + 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); @@ -49,16 +21,24 @@ module.exports = function mockCompatibilityChecks() { 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 = new opentracing.Tracer(); + 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 = new opentracing.Tracer(); + var tracer = createTracer(); function f(arg) { return function() { var span = tracer.startSpan('test_span'); @@ -70,7 +50,7 @@ module.exports = function mockCompatibilityChecks() { expect(f({})).to.throw(Error); }); it('should throw an Error if given more than 1 argument', function() { - var tracer = new opentracing.Tracer(); + var tracer = createTracer(); var span = tracer.startSpan('test_span'); expect(function() { span.finish(Date.now(), { extra : 123 }) }).to.be.throw(Error); }); @@ -79,7 +59,7 @@ module.exports = function mockCompatibilityChecks() { describe('Miscellaneous', function() { describe('Memory usage', function() { it('should not report leaks after setting the global tracer', function() { - opentracing.initGlobalTracer(new opentracing.Tracer()); + opentracing.initGlobalTracer(createTracer()); }); }); }); diff --git a/test/opentracing_api.js b/test/opentracing_api.js new file mode 100644 index 0000000..f1be329 --- /dev/null +++ b/test/opentracing_api.js @@ -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'); + }); + }); + }); + }); +} diff --git a/test/unittest.js b/test/unittest.js index 3928228..0e1b340 100644 --- a/test/unittest.js +++ b/test/unittest.js @@ -2,13 +2,16 @@ require('source-map-support').install(); var apiCompatibilityChecks = require('./api_compatibility.js'); -var mockCompatibilityChecks = require('./mock_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. -mockCompatibilityChecks(); +noopImplementationTests(); // Run the api conformance tests on the default Opentracing no-op Tracer. apiCompatibilityChecks(function() { return new opentracing.Tracer(); }); + +opentracingAPITests();