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

Commit 0e9774d

Browse files
djonathascardososilva-fabio
authored andcommitted
test: created rootspan test
1 parent 4a3d279 commit 0e9774d

4 files changed

Lines changed: 104 additions & 47 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ build/
3737
#log files
3838
*.log
3939

40-
40+
#istanbul files
41+
coverage/
4142

packages/opencensus-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"repository": "census-instrumentation/opencensus-node",
88
"scripts": {
99
"build": "node_modules/.bin/tsc --declaration",
10-
"test": "nyc --reporter=html --reporter=text mocha 'build/test/**/*.js'",
10+
"test": "nyc -x '**/test/**' --reporter=html --reporter=text mocha 'build/test/**/*.js'",
1111
"clean": "rimraf build/*",
1212
"check": "gts check",
1313
"compile": "tsc -p .",

packages/opencensus-core/src/trace/model/root-span.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ export class RootSpanImpl extends SpanBaseModel implements RootSpan {
6969

7070
// TODO - Define logic for list of spans
7171
for (const span of this.spansLocal) {
72-
if (span.ended || !span.started) return;
73-
span.truncate();
72+
if (!span.ended && span.started) {
73+
span.truncate();
74+
}
7475
}
7576

7677
this.tracer.onEndSpan(this);
@@ -98,18 +99,18 @@ export class RootSpanImpl extends SpanBaseModel implements RootSpan {
9899
* @param parentSpanId Span parent ID
99100
*/
100101
startSpan(name: string, type: string, parentSpanId?: string) {
101-
if (!this.started) {
102+
if (this.ended) {
102103
debug(
103-
'calling %s.startSpan() on un-started %s %o', this.className,
104+
'calling %s.startSpan() on ended %s %o', this.className,
104105
this.className, {id: this.id, name: this.name, type: this.type});
105106
return;
106107
}
107-
if (this.ended) {
108+
if (!this.started) {
108109
debug(
109-
'calling %s.startSpan() on ended %s %o', this.className,
110+
'calling %s.startSpan() on un-started %s %o', this.className,
110111
this.className, {id: this.id, name: this.name, type: this.type});
111112
return;
112-
}
113+
}
113114
const newSpan = new SpanImpl(this);
114115
if (name) {
115116
newSpan.name = name;

packages/opencensus-core/test/test-root-span.ts

Lines changed: 93 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,99 +17,154 @@
1717
import * as assert from 'assert';
1818
import * as mocha from 'mocha';
1919

20-
import {Span,RootSpan,Tracer} from '../src/trace/model/types';
21-
import {SpanImpl} from '../src/trace/model/span';
2220
import {RootSpanImpl} from '../src/trace/model/rootspan';
21+
import {SpanImpl} from '../src/trace/model/span';
2322
import {TracerImpl} from '../src/trace/model/tracer';
24-
let tracer = new TracerImpl();
23+
import {RootSpan, Span, TraceOptions, Tracer} from '../src/trace/model/types';
24+
25+
const tracer = new TracerImpl();
2526

26-
describe('RootSpan', function() {
27+
describe('RootSpan', () => {
2728
/**
2829
* Should create a RootSpan instance
2930
*/
30-
describe('new RootSpan()', function() {
31-
it('should create a RootSpan instance', function() {
32-
let root = new RootSpanImpl(tracer);
33-
assert.ok(root instanceof SpanImpl);
31+
describe('new RootSpan()', () => {
32+
it('should create a RootSpan instance', () => {
33+
const root = new RootSpanImpl(tracer);
34+
assert.ok(root instanceof RootSpanImpl);
35+
});
36+
});
37+
38+
/**
39+
* Should create a RootSpan instance with options
40+
*/
41+
describe('new RootSpan() with options', () => {
42+
it('should create a RootSpan instance with options', () => {
43+
const trace = new RootSpanImpl(tracer);
44+
const options = {name: 'test', traceContext: trace.traceContext} as
45+
TraceOptions;
46+
const root = new RootSpanImpl(tracer, options);
47+
assert.ok(root instanceof RootSpanImpl);
48+
});
49+
});
50+
51+
/**
52+
* Should get span list from rootspan instance
53+
*/
54+
describe('get spans()', () => {
55+
it('should get span list from rootspan instance', () => {
56+
const root = new RootSpanImpl(tracer);
57+
root.start();
58+
const span = root.startSpan('spanName', 'spanType');
59+
60+
for (const span of root.spans) {
61+
assert.ok(span instanceof SpanImpl);
62+
}
63+
});
64+
});
65+
66+
/**
67+
* Should get trace id from rootspan instance
68+
*/
69+
describe('new traceId()', () => {
70+
it('should get trace id from rootspan instance', () => {
71+
const root = new RootSpanImpl(tracer);
72+
assert.equal(root.traceId, root.traceContext.traceId);
3473
});
3574
});
3675

3776
/**
3877
* Should create and start a RootSpan instance
3978
*/
40-
describe('start()', function() {
41-
it('should start a RootSpan instance', function() {
42-
let root = new RootSpanImpl(tracer);
79+
describe('start()', () => {
80+
it('should start a RootSpan instance', () => {
81+
const root = new RootSpanImpl(tracer);
4382
root.start();
4483
assert.ok(root.started);
4584
});
4685
});
4786

4887
/**
49-
* Should check type and a span was started
88+
* Should create and start a new span instance
5089
*/
51-
describe('startSpan()', function() {
90+
describe('startSpan()', () => {
5291
let root, span;
5392

54-
before(function() {
93+
before(() => {
5594
root = new RootSpanImpl(tracer);
5695
root.start();
5796
span = root.startSpan('spanName', 'spanType');
5897
});
5998

60-
it('should check span instance type', function() {
99+
it('should create span instance', () => {
61100
assert.ok(span instanceof SpanImpl);
62101
});
63102

64-
it('should check if a new span was started', function() {
103+
it('should start a span instance', () => {
65104
assert.ok(span.started);
66105
});
67106
});
68107

69108
/**
70-
* Should start and end a rootspan properly
109+
* Should not start a span from a not started rootspan
110+
*/
111+
describe('startSpan() before start rootspan', () => {
112+
it('should not create span', () => {
113+
const root = new RootSpanImpl(tracer);
114+
const span = root.startSpan('spanName', 'spanType');
115+
assert.ok(span == null);
116+
});
117+
});
118+
119+
/**
120+
* Should not create a span from a ended rootspan
71121
*/
72-
describe('end()', function() {
73-
it('should end the trace', function() {
74-
let root = new RootSpanImpl(tracer);
122+
describe('startSpan() after rootspan ended', () => {
123+
it('should not create span', () => {
124+
const root = new RootSpanImpl(tracer);
75125
root.start();
76126
root.end();
77-
assert.ok(root.ended);
127+
const span = root.startSpan('spanName', 'spanType');
128+
assert.ok(span == null);
78129
});
79130
});
80131

81132
/**
82-
* Should not end a rootspan which was not started
133+
* Should end a rootspan instance
83134
*/
84-
describe('end() before trace started', function() {
85-
it('should not end trace', function() {
86-
let root = new RootSpanImpl(tracer);
135+
describe('end()', () => {
136+
it('should end the rootspan instance', () => {
137+
const root = new RootSpanImpl(tracer);
138+
root.start();
87139
root.end();
88-
assert.ok(!root.ended);
140+
assert.ok(root.ended);
89141
});
90142
});
91143

92144
/**
93-
* Should not start a span from a not started rootspan
145+
* Should not end a rootspan which was not started
94146
*/
95-
describe('startSpan() before trace started', function() {
96-
it('should not create span', function() {
97-
let root = new RootSpanImpl(tracer);
98-
let span = root.startSpan('spanName', 'spanType');
99-
assert.ok(span == null);
147+
describe('end() before start rootspan', () => {
148+
it('should not end rootspan', () => {
149+
const root = new RootSpanImpl(tracer);
150+
root.end();
151+
assert.ok(!root.ended);
100152
});
101153
});
102154

103155
/**
104-
* Should not create a span from a ended rootspan
156+
* Should end all spans inside rootspan
105157
*/
106-
describe('startSpan() after trace ended', function() {
107-
it('should not create span', function() {
108-
let root = new RootSpanImpl(tracer);
158+
describe('end() before end all spans', () => {
159+
it('should end all spans inside rootspan', () => {
160+
const root = new RootSpanImpl(tracer);
109161
root.start();
162+
const span = root.startSpan('spanName', 'spanType');
110163
root.end();
111-
let span = root.startSpan('spanName', 'spanType');
112-
assert.ok(span == null);
164+
165+
for (const span of root.spans) {
166+
assert.ok(span.ended);
167+
}
113168
});
114169
});
115170
});

0 commit comments

Comments
 (0)