1616
1717import * as webTypes from '@opencensus/web-types' ;
1818import { Tracer } from '../src/trace/model/tracer' ;
19+ import { WindowWithZone } from '../src/trace/model/types' ;
1920
2021describe ( 'Tracer' , ( ) => {
2122 let tracer : Tracer ;
2223 let listener : webTypes . SpanEventListener ;
2324 const options = { name : 'test' } ;
25+ let realZone : Function | undefined ;
26+ const windowWithZone = window as WindowWithZone ;
2427
2528 beforeEach ( ( ) => {
2629 tracer = new Tracer ( ) ;
@@ -31,37 +34,113 @@ describe('Tracer', () => {
3134 tracer . eventListeners = [ listener ] ;
3235 } ) ;
3336
34- /** Should get/set the current RootSpan from tracer instance */
35- describe ( 'get/set currentRootSpan()' , ( ) => {
36- it ( 'should get the current RootSpan from tracer instance' , ( ) => {
37- tracer . startRootSpan ( options , root => {
38- expect ( root ) . toBeTruthy ( ) ;
39- expect ( root ) . toBe ( tracer . currentRootSpan ) ;
37+ describe ( 'Zone is not present' , ( ) => {
38+ beforeEach ( ( ) => {
39+ realZone = windowWithZone . Zone ;
40+ windowWithZone . Zone = undefined ;
41+ } ) ;
42+
43+ afterEach ( ( ) => {
44+ windowWithZone . Zone = realZone ;
45+ } ) ;
46+
47+ it ( 'isZonePresent() is false' , ( ) => {
48+ expect ( tracer . isZonePresent ( ) ) . toBeFalsy ( ) ;
49+ } ) ;
50+
51+ describe ( 'get/set currentRootSpan()' , ( ) => {
52+ // As `Zone` is not present, the current root span is accesible outside
53+ // the callback as there is only one current root span.
54+ it ( 'should get the current RootSpan from tracer instance' , ( ) => {
55+ const onStartFn = jasmine . createSpy ( 'onStartFn' ) ;
56+ tracer . startRootSpan ( options , onStartFn ) ;
57+ const onStartRoot = onStartFn . calls . argsFor ( 0 ) [ 0 ] ;
58+ expect ( onStartRoot ) . toBe ( tracer . currentRootSpan ) ;
59+ } ) ;
60+ } ) ;
61+
62+ describe ( 'startRootSpan' , ( ) => {
63+ it ( 'should create start RootSpan' , ( ) => {
64+ const onStartFn = jasmine . createSpy ( 'onStartFn' ) ;
65+ const oldRoot = tracer . currentRootSpan ;
66+
67+ tracer . startRootSpan ( options , onStartFn ) ;
68+
69+ expect ( onStartFn ) . toHaveBeenCalled ( ) ;
70+ const onStartRoot = onStartFn . calls . argsFor ( 0 ) [ 0 ] ;
71+ expect ( onStartRoot . name ) . toBe ( 'test' ) ;
72+ expect ( onStartRoot ) . not . toBe ( oldRoot ) ;
73+ expect ( tracer . currentRootSpan ) . toBe ( onStartRoot ) ;
74+ expect ( listener . onStartSpan ) . toHaveBeenCalledWith ( onStartRoot ) ;
75+ } ) ;
76+ } ) ;
77+
78+ describe ( 'startChildSpan' , ( ) => {
79+ it ( 'starts a child span of the current root span' , ( ) => {
80+ spyOn ( tracer . currentRootSpan , 'startChildSpan' ) ;
81+ tracer . startChildSpan ( {
82+ name : 'child1' ,
83+ kind : webTypes . SpanKind . CLIENT ,
84+ } ) ;
85+ expect ( tracer . currentRootSpan . startChildSpan ) . toHaveBeenCalledWith ( {
86+ childOf : tracer . currentRootSpan ,
87+ name : 'child1' ,
88+ kind : webTypes . SpanKind . CLIENT ,
89+ } ) ;
4090 } ) ;
4191 } ) ;
4292 } ) ;
4393
44- describe ( 'startRootSpan' , ( ) => {
45- it ( 'should create a new RootSpan instance' , ( ) => {
46- tracer . startRootSpan ( options , rootSpan => {
47- expect ( rootSpan ) . toBeTruthy ( ) ;
94+ describe ( 'Zone is present' , ( ) => {
95+ /** Should get/set the current RootSpan from tracer instance */
96+ describe ( 'get/set currentRootSpan()' , ( ) => {
97+ it ( 'should get the current RootSpan from tracer instance' , ( ) => {
98+ tracer . startRootSpan ( options , root => {
99+ expect ( root ) . toBeTruthy ( ) ;
100+ expect ( root ) . toBe ( tracer . currentRootSpan ) ;
101+ } ) ;
48102 } ) ;
49103 } ) ;
50- it ( 'sets current root span' , ( ) => {
51- const oldRoot = tracer . currentRootSpan ;
52104
53- tracer . startRootSpan ( options , rootSpan => {
54- expect ( rootSpan . name ) . toBe ( 'test' ) ;
55- expect ( rootSpan ) . not . toBe ( oldRoot ) ;
56- expect ( tracer . currentRootSpan ) . toBe ( rootSpan ) ;
57- expect ( listener . onStartSpan ) . toHaveBeenCalledWith ( rootSpan ) ;
105+ describe ( 'startRootSpan' , ( ) => {
106+ it ( 'should create a new RootSpan instance' , ( ) => {
107+ tracer . startRootSpan ( options , rootSpan => {
108+ expect ( rootSpan ) . toBeTruthy ( ) ;
109+ } ) ;
110+ } ) ;
111+ it ( 'sets current root span' , ( ) => {
112+ const oldRoot = tracer . currentRootSpan ;
113+
114+ tracer . startRootSpan ( options , rootSpan => {
115+ expect ( rootSpan . name ) . toBe ( 'test' ) ;
116+ expect ( rootSpan ) . not . toBe ( oldRoot ) ;
117+ expect ( tracer . currentRootSpan ) . toBe ( rootSpan ) ;
118+ expect ( listener . onStartSpan ) . toHaveBeenCalledWith ( rootSpan ) ;
119+ } ) ;
120+ } ) ;
121+ it ( 'should create a new Zone and RootSpan associated to the zone' , ( ) => {
122+ tracer . startRootSpan ( options , rootSpan => {
123+ expect ( rootSpan ) . toBeTruthy ( ) ;
124+ expect ( Zone . current ) . not . toBe ( Zone . root ) ;
125+ expect ( Zone . current . get ( 'data' ) . rootSpan ) . toBe ( rootSpan ) ;
126+ } ) ;
58127 } ) ;
59128 } ) ;
60- it ( 'should create a new Zone and RootSpan an associated to the zone' , ( ) => {
61- tracer . startRootSpan ( options , rootSpan => {
62- expect ( rootSpan ) . toBeTruthy ( ) ;
63- expect ( Zone . current ) . not . toBe ( Zone . root ) ;
64- expect ( Zone . current . get ( 'data' ) . rootSpan ) . toBe ( rootSpan ) ;
129+
130+ describe ( 'startChildSpan' , ( ) => {
131+ let rootSpanLocal : webTypes . Span ;
132+ let span : webTypes . Span ;
133+ it ( 'starts a child span of the current root span' , ( ) => {
134+ tracer . startRootSpan ( options , rootSpan => {
135+ rootSpanLocal = rootSpan ;
136+ span = tracer . startChildSpan ( {
137+ name : 'child1' ,
138+ kind : webTypes . SpanKind . CLIENT ,
139+ } ) ;
140+ } ) ;
141+ expect ( span ) . toBeTruthy ( ) ;
142+ expect ( rootSpanLocal . numberOfChildren ) . toBe ( 1 ) ;
143+ expect ( rootSpanLocal . spans [ 0 ] ) . toBe ( span ) ;
65144 } ) ;
66145 } ) ;
67146 } ) ;
@@ -74,23 +153,6 @@ describe('Tracer', () => {
74153 } ) ;
75154 } ) ;
76155
77- describe ( 'startChildSpan' , ( ) => {
78- let rootSpanLocal : webTypes . Span ;
79- let span : webTypes . Span ;
80- it ( 'starts a child span of the current root span' , ( ) => {
81- tracer . startRootSpan ( options , rootSpan => {
82- rootSpanLocal = rootSpan ;
83- span = tracer . startChildSpan ( {
84- name : 'child1' ,
85- kind : webTypes . SpanKind . CLIENT ,
86- } ) ;
87- } ) ;
88- expect ( span ) . toBeTruthy ( ) ;
89- expect ( rootSpanLocal . numberOfChildren ) . toBe ( 1 ) ;
90- expect ( rootSpanLocal . spans [ 0 ] ) . toBe ( span ) ;
91- } ) ;
92- } ) ;
93-
94156 describe ( 'wrap' , ( ) => {
95157 it ( 'just returns the given function' , ( ) => {
96158 const wrapFn = jasmine . createSpy ( 'wrapFn' ) ;
0 commit comments