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

Commit

Permalink
test: improve console exporter tests (#71)
Browse files Browse the repository at this point in the history
* chore: improves console exporter tests

* refactor(fix): changes to address review comments
  • Loading branch information
eduardoemery authored and kjin committed Jul 13, 2018
1 parent e1078b1 commit 4067e92
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 38 deletions.
4 changes: 2 additions & 2 deletions packages/opencensus-core/src/exporters/console-exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class NoopExporter implements types.Exporter {
/** Format and sends span data to the console. */
export class ConsoleExporter implements types.Exporter {
/** Buffer object to store the spans. */
private buffer: ExporterBuffer;
private logger: loggerTypes.Logger;
private buffer: ExporterBuffer;

/**
* Constructs a new ConsoleLogExporter instance.
Expand Down Expand Up @@ -67,8 +67,8 @@ export class ConsoleExporter implements types.Exporter {
const SPANS_STR: string[] = root.spans.map(
(span) => [`\t\t{spanId: ${span.id}, name: ${span.name}}`].join(
'\n'));
const result: string[] = [];

const result: string[] = [];
result.push(
ROOT_STR + '\n\tChildSpans:\n' +
`${SPANS_STR.join('\n')}`);
Expand Down
76 changes: 40 additions & 36 deletions packages/opencensus-core/test/test-console-exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,16 @@ import {ExporterBuffer} from '../src/exporters/exporter-buffer';
import {RootSpan} from '../src/trace/model/root-span';
import {CoreTracer} from '../src/trace/model/tracer';

const tracer = new CoreTracer().start({});
const DEFAULT_BUFFER_SIZE = 3;
const DEFAULT_BUFFER_TIMEOUT = 20000; // time in milliseconds
const tracer = new CoreTracer().start({samplingRate: 1.0});
const defaultBufferConfig = {
bufferSize: DEFAULT_BUFFER_SIZE,
bufferTimeout: DEFAULT_BUFFER_TIMEOUT
bufferSize: 1,
bufferTimeout: 20000 // time in milliseconds
};

const createRootSpans = (): RootSpan[] => {
const rootSpans = [];
for (let i = 0; i < DEFAULT_BUFFER_SIZE + 1; i++) {
const rootSpan = new RootSpan(tracer, {name: `rootSpan.${i}`});
rootSpan.start();
for (let j = 0; j < 10; j++) {
rootSpan.startChildSpan(`childSpan.${i}.${j}`, 'client');
}
rootSpans.push(rootSpan);
}
return rootSpans;
};


describe('NoopExporter', () => {
/** Should do nothing when calling onEndSpan() */
describe('onEndSpan()', () => {
it('should do anything', () => {
it('should do nothing', () => {
const exporter = new NoopExporter();
const rootSpan = new RootSpan(tracer);
exporter.onEndSpan(rootSpan);
Expand All @@ -57,14 +41,12 @@ describe('NoopExporter', () => {

/** Should do anything when calling publish() */
describe('publish()', () => {
it('should do anything', () => {
it('should do nothing', () => {
const exporter = new NoopExporter();
const rootSpan = new RootSpan(tracer);
const queue: RootSpan[] = [];
queue.push(rootSpan);
const queue: RootSpan[] = [rootSpan];

exporter.publish(queue);
assert.ok(true);
return exporter.publish(queue);
});
});
});
Expand All @@ -73,28 +55,50 @@ describe('ConsoleLogExporter', () => {
/** Should end a span */
describe('onEndSpan()', () => {
it('should end a span', () => {
const intercept = require('intercept-stdout');
let capturedText = '';
const unhookIntercept = intercept((txt: string) => {
capturedText += txt;
});

const exporter = new ConsoleExporter(defaultBufferConfig);
tracer.registerSpanEventListener(exporter);
// const rootSpan = new RootSpan(tracer);
const rootSpans = createRootSpans();
for (const rootSpan of rootSpans) {
rootSpan.end();
}
assert.ok(true);

const rootSpan1 = new RootSpan(tracer);
exporter.onEndSpan(rootSpan1);
assert.strictEqual(capturedText, '');

const rootSpan2 = new RootSpan(tracer);
exporter.onEndSpan(rootSpan2);
[rootSpan1, rootSpan2].map(rootSpan => {
assert.ok(capturedText.indexOf(rootSpan.traceId) >= 0);
assert.ok(capturedText.indexOf(rootSpan.id) >= 0);
assert.ok(capturedText.indexOf(rootSpan.name) >= 0);
});
});
});

/** Should publish the rootspan in queue */
describe('publish()', () => {
it('should publish the rootspans in queue', () => {
const intercept = require('intercept-stdout');
let capturedText = '';
const unhookIntercept = intercept((txt: string) => {
capturedText += txt;
});

const exporter = new ConsoleExporter(defaultBufferConfig);
const rootSpan = new RootSpan(tracer);
rootSpan.start();
rootSpan.startChildSpan('name', 'type', rootSpan.traceId);
const queue: RootSpan[] = [];
queue.push(rootSpan);
const queue: RootSpan[] = [rootSpan];

exporter.publish(queue);
assert.ok(true);
return exporter.publish(queue).then(() => {
assert.ok(capturedText.indexOf(rootSpan.traceId) >= 0);
assert.ok(capturedText.indexOf(rootSpan.id) >= 0);
assert.ok(capturedText.indexOf(rootSpan.name) >= 0);
assert.ok(capturedText.indexOf(rootSpan.spans[0].name) >= 0);
assert.ok(capturedText.indexOf(rootSpan.spans[0].id) >= 0);
});
});
});
});

0 comments on commit 4067e92

Please sign in to comment.