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

Commit 4067e92

Browse files
eduardoemerykjin
authored andcommitted
test: improve console exporter tests (#71)
* chore: improves console exporter tests * refactor(fix): changes to address review comments
1 parent e1078b1 commit 4067e92

2 files changed

Lines changed: 42 additions & 38 deletions

File tree

packages/opencensus-core/src/exporters/console-exporter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ export class NoopExporter implements types.Exporter {
3333
/** Format and sends span data to the console. */
3434
export class ConsoleExporter implements types.Exporter {
3535
/** Buffer object to store the spans. */
36-
private buffer: ExporterBuffer;
3736
private logger: loggerTypes.Logger;
37+
private buffer: ExporterBuffer;
3838

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

71+
const result: string[] = [];
7272
result.push(
7373
ROOT_STR + '\n\tChildSpans:\n' +
7474
`${SPANS_STR.join('\n')}`);

packages/opencensus-core/test/test-console-exporter.ts

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,16 @@ import {ExporterBuffer} from '../src/exporters/exporter-buffer';
2222
import {RootSpan} from '../src/trace/model/root-span';
2323
import {CoreTracer} from '../src/trace/model/tracer';
2424

25-
const tracer = new CoreTracer().start({});
26-
const DEFAULT_BUFFER_SIZE = 3;
27-
const DEFAULT_BUFFER_TIMEOUT = 20000; // time in milliseconds
25+
const tracer = new CoreTracer().start({samplingRate: 1.0});
2826
const defaultBufferConfig = {
29-
bufferSize: DEFAULT_BUFFER_SIZE,
30-
bufferTimeout: DEFAULT_BUFFER_TIMEOUT
27+
bufferSize: 1,
28+
bufferTimeout: 20000 // time in milliseconds
3129
};
3230

33-
const createRootSpans = (): RootSpan[] => {
34-
const rootSpans = [];
35-
for (let i = 0; i < DEFAULT_BUFFER_SIZE + 1; i++) {
36-
const rootSpan = new RootSpan(tracer, {name: `rootSpan.${i}`});
37-
rootSpan.start();
38-
for (let j = 0; j < 10; j++) {
39-
rootSpan.startChildSpan(`childSpan.${i}.${j}`, 'client');
40-
}
41-
rootSpans.push(rootSpan);
42-
}
43-
return rootSpans;
44-
};
45-
46-
4731
describe('NoopExporter', () => {
4832
/** Should do nothing when calling onEndSpan() */
4933
describe('onEndSpan()', () => {
50-
it('should do anything', () => {
34+
it('should do nothing', () => {
5135
const exporter = new NoopExporter();
5236
const rootSpan = new RootSpan(tracer);
5337
exporter.onEndSpan(rootSpan);
@@ -57,14 +41,12 @@ describe('NoopExporter', () => {
5741

5842
/** Should do anything when calling publish() */
5943
describe('publish()', () => {
60-
it('should do anything', () => {
44+
it('should do nothing', () => {
6145
const exporter = new NoopExporter();
6246
const rootSpan = new RootSpan(tracer);
63-
const queue: RootSpan[] = [];
64-
queue.push(rootSpan);
47+
const queue: RootSpan[] = [rootSpan];
6548

66-
exporter.publish(queue);
67-
assert.ok(true);
49+
return exporter.publish(queue);
6850
});
6951
});
7052
});
@@ -73,28 +55,50 @@ describe('ConsoleLogExporter', () => {
7355
/** Should end a span */
7456
describe('onEndSpan()', () => {
7557
it('should end a span', () => {
58+
const intercept = require('intercept-stdout');
59+
let capturedText = '';
60+
const unhookIntercept = intercept((txt: string) => {
61+
capturedText += txt;
62+
});
63+
7664
const exporter = new ConsoleExporter(defaultBufferConfig);
77-
tracer.registerSpanEventListener(exporter);
78-
// const rootSpan = new RootSpan(tracer);
79-
const rootSpans = createRootSpans();
80-
for (const rootSpan of rootSpans) {
81-
rootSpan.end();
82-
}
83-
assert.ok(true);
65+
66+
const rootSpan1 = new RootSpan(tracer);
67+
exporter.onEndSpan(rootSpan1);
68+
assert.strictEqual(capturedText, '');
69+
70+
const rootSpan2 = new RootSpan(tracer);
71+
exporter.onEndSpan(rootSpan2);
72+
[rootSpan1, rootSpan2].map(rootSpan => {
73+
assert.ok(capturedText.indexOf(rootSpan.traceId) >= 0);
74+
assert.ok(capturedText.indexOf(rootSpan.id) >= 0);
75+
assert.ok(capturedText.indexOf(rootSpan.name) >= 0);
76+
});
8477
});
8578
});
8679

8780
/** Should publish the rootspan in queue */
8881
describe('publish()', () => {
8982
it('should publish the rootspans in queue', () => {
83+
const intercept = require('intercept-stdout');
84+
let capturedText = '';
85+
const unhookIntercept = intercept((txt: string) => {
86+
capturedText += txt;
87+
});
88+
9089
const exporter = new ConsoleExporter(defaultBufferConfig);
9190
const rootSpan = new RootSpan(tracer);
91+
rootSpan.start();
9292
rootSpan.startChildSpan('name', 'type', rootSpan.traceId);
93-
const queue: RootSpan[] = [];
94-
queue.push(rootSpan);
93+
const queue: RootSpan[] = [rootSpan];
9594

96-
exporter.publish(queue);
97-
assert.ok(true);
95+
return exporter.publish(queue).then(() => {
96+
assert.ok(capturedText.indexOf(rootSpan.traceId) >= 0);
97+
assert.ok(capturedText.indexOf(rootSpan.id) >= 0);
98+
assert.ok(capturedText.indexOf(rootSpan.name) >= 0);
99+
assert.ok(capturedText.indexOf(rootSpan.spans[0].name) >= 0);
100+
assert.ok(capturedText.indexOf(rootSpan.spans[0].id) >= 0);
101+
});
98102
});
99103
});
100104
});

0 commit comments

Comments
 (0)