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

Commit

Permalink
Export mock tracer via standard package; add options to API test harn…
Browse files Browse the repository at this point in the history
…ess (#98)
  • Loading branch information
AloisReitbauer authored and yurishkuro committed Feb 26, 2018
1 parent 1f32ae4 commit 1b32dfd
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
3 changes: 1 addition & 2 deletions src/examples/demo/demo.ts
@@ -1,7 +1,6 @@
/* eslint-disable */

import '../../index';
import { MockTracer } from '../../mock_tracer';
import {MockTracer} from '../../index';

console.log('\nRunning demo...\n');

Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Expand Up @@ -6,13 +6,16 @@ import Span from './span';
import SpanContext from './span_context';
import Tracer from './tracer';

import {MockTracer} from './mock_tracer';

export {
BinaryCarrier,
Reference,
SpanContext,
Span,
Tracer,
Tags
Tags,
MockTracer
};

export * from './global_tracer';
Expand Down
19 changes: 9 additions & 10 deletions src/test/api_compatibility.ts
Expand Up @@ -3,8 +3,9 @@ import { assert, expect } from 'chai';
import { BinaryCarrier, FORMAT_BINARY, FORMAT_TEXT_MAP, Reference, REFERENCE_CHILD_OF, Span, Tracer } from '../index';

export interface ApiCompatibilityChecksOptions {
/** a boolean that controls whether or not to verify baggage values */
checkBaggageValues?: boolean;
/** a boolean that controls whether or not to verify certain API functionality */
skipBaggageChecks?: boolean;
skipInjectExtractChecks?: boolean;
}

/**
Expand All @@ -14,7 +15,7 @@ export interface ApiCompatibilityChecksOptions {
* @param {object} createTracer - a factory function that allocates a tracer.
* @param {object} [options] - the options to be set on api compatibility
*/
export function apiCompatibilityChecks(createTracer = () => new Tracer(), options: ApiCompatibilityChecksOptions = {}): void {
function apiCompatibilityChecks(createTracer = () => new Tracer(), options: ApiCompatibilityChecksOptions = {skipBaggageChecks: false, skipInjectExtractChecks: false}): void {

describe('OpenTracing API Compatibility', () => {
let tracer: Tracer;
Expand All @@ -35,7 +36,7 @@ export function apiCompatibilityChecks(createTracer = () => new Tracer(), option
});

describe('inject', () => {
it('should not throw exception on required carrier types', () => {
(options.skipInjectExtractChecks ? it.skip : it)('should not throw exception on required carrier types', () => {
const spanContext = span.context();
const textCarrier = {};
const binCarrier = new BinaryCarrier([1, 2, 3]);
Expand All @@ -44,15 +45,15 @@ export function apiCompatibilityChecks(createTracer = () => new Tracer(), option
expect(() => { tracer.inject(spanContext, FORMAT_BINARY, {}); }).to.not.throw(Error);
});

it('should handle Spans and SpanContexts', () => {
(options.skipInjectExtractChecks ? it.skip : it)('should handle Spans and SpanContexts', () => {
const textCarrier = {};
expect(() => { tracer.inject(span, FORMAT_TEXT_MAP, textCarrier); }).to.not.throw(Error);
expect(() => { tracer.inject(span.context(), FORMAT_TEXT_MAP, textCarrier); }).to.not.throw(Error);
});
});

describe('extract', () => {
it('should not throw exception on required carrier types', () => {
(options.skipInjectExtractChecks ? it.skip : it)('should not throw exception on required carrier types', () => {
const textCarrier = {};
const binCarrier = new BinaryCarrier([1, 2, 3]);
expect(() => { tracer.extract(FORMAT_TEXT_MAP, textCarrier); }).to.not.throw(Error);
Expand All @@ -65,12 +66,10 @@ export function apiCompatibilityChecks(createTracer = () => new Tracer(), option

describe('Span', () => {

it('should set baggage and retrieve baggage', () => {
(options.skipBaggageChecks ? it.skip : it)('should set baggage and retrieve baggage', () => {
span.setBaggageItem('some-key', 'some-value');
const val = span.getBaggageItem('some-key');
if (options.checkBaggageValues) {
assert.equal('some-value', val);
}
assert.equal('some-value', val);
});

describe('finish', () => {
Expand Down
27 changes: 27 additions & 0 deletions src/test/mocktracer_implemenation.ts
@@ -0,0 +1,27 @@

import { expect } from 'chai';
import {MockTracer } from '../index';

function mockTracerimplementationTests(): void {

describe('Mock Tracer API tests', () => {

describe ('Tracer#report', () => {

it ('should not throw exceptions when running report', () => {
const tracer = new MockTracer();
const span = tracer.startSpan('test_operation');
span.addTags ({key: 'value'});
span.finish ();
expect (() => {
const report = tracer.report();
for (const span of report.spans) {
span.tags();
}
}).to.not.throw (Error);
});
});
});
}

export default mockTracerimplementationTests;
9 changes: 8 additions & 1 deletion src/test/unittest.ts
Expand Up @@ -2,14 +2,21 @@
require('source-map-support').install();

import apiCompatibilityChecks from './api_compatibility';
import mocktracerImplementationTests from './mocktracer_implemenation';
import noopImplementationTests from './noop_implementation';
import opentracingAPITests from './opentracing_api';

import {MockTracer, Tracer} from '../index.js';

mocktracerImplementationTests ();

apiCompatibilityChecks( () => new MockTracer (), {skipInjectExtractChecks: true, skipBaggageChecks: true} );

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

// Run the api conformance tests on the default Opentracing no-op Tracer.
apiCompatibilityChecks();
apiCompatibilityChecks( () => new Tracer (), {skipBaggageChecks: true});

// Basic unittests for opentracing
opentracingAPITests();

0 comments on commit 1b32dfd

Please sign in to comment.