@@ -18,7 +18,7 @@ import {CanonicalCode, CoreTracer, MessageEventType, Span, SpanKind, TracerConfi
1818import * as assert from 'assert' ;
1919import * as nock from 'nock' ;
2020
21- import { MICROS_PER_MILLI , ZipkinExporterOptions , ZipkinTraceExporter } from '../src/zipkin' ;
21+ import { MICROS_PER_MILLI , TranslatedSpan , ZipkinExporterOptions , ZipkinTraceExporter } from '../src/zipkin' ;
2222
2323/** Zipkin host url */
2424const zipkinHost = 'http://localhost:9411' ;
@@ -42,23 +42,6 @@ const defaultConfig: TracerConfig = {
4242 samplingRate : 1
4343} ;
4444
45- /** Run a nock server to replace zipkin service */
46- const runNockServer = ( ) => {
47- nock ( zipkinHost )
48- . persist ( )
49- . post ( postPath )
50- . reply ( 202 )
51- . post ( '/wrong' )
52- . reply ( 404 ) ;
53- } ;
54-
55- /** Checking if tests will use a real network, otherwise run a nock server */
56- before ( ( ) => {
57- if ( ! OPENCENSUS_NETWORK_TESTS ) {
58- runNockServer ( ) ;
59- }
60- } ) ;
61-
6245/** Zipkin tests */
6346describe ( 'Zipkin Exporter' , function ( ) {
6447 /** Desabling the timeout for tests */
@@ -89,14 +72,85 @@ describe('Zipkin Exporter', function() {
8972 const tracer = new CoreTracer ( ) ;
9073 tracer . start ( defaultConfig ) ;
9174
75+ let scope : nock . Scope ;
76+ let requestBody : [ TranslatedSpan ] ;
77+
78+ /**
79+ * Checking if tests will use a real network, otherwise run a nock server
80+ */
81+ if ( ! OPENCENSUS_NETWORK_TESTS ) {
82+ /** Run a nock server to replace zipkin service */
83+ scope = nock ( zipkinHost )
84+ . persist ( )
85+ . post (
86+ postPath ,
87+ ( body : [ TranslatedSpan ] ) => {
88+ requestBody = body ;
89+ return true ;
90+ } )
91+ . reply ( 202 ) ;
92+ }
93+
9294 return tracer . startRootSpan (
9395 { name : 'root-test' } , async ( rootSpan : Span ) => {
94- const span = rootSpan . startChildSpan (
96+ const span1 = rootSpan . startChildSpan (
9597 { name : 'spanTest' , kind : SpanKind . CLIENT } ) ;
96- span . end ( ) ;
98+ const span2 = tracer . startChildSpan (
99+ { name : 'spanTest' , kind : SpanKind . CLIENT , childOf : span1 } ) ;
100+ span2 . end ( ) ;
101+ span1 . end ( ) ;
97102 rootSpan . end ( ) ;
98- return exporter . publish ( [ rootSpan , rootSpan ] ) . then ( ( result ) => {
103+ return exporter . publish ( [ rootSpan ] ) . then ( ( result ) => {
99104 assert . strictEqual ( result . statusCode , 202 ) ;
105+
106+ if ( ! OPENCENSUS_NETWORK_TESTS ) {
107+ scope . done ( ) ;
108+ assert . deepStrictEqual ( requestBody , [
109+ {
110+ 'annotations' : [ ] ,
111+ 'debug' : true ,
112+ 'duration' :
113+ Math . round ( rootSpan . duration * MICROS_PER_MILLI ) ,
114+ 'id' : rootSpan . id ,
115+ 'kind' : 'SERVER' ,
116+ 'localEndpoint' : { 'serviceName' : 'opencensus-tests' } ,
117+ 'name' : 'root-test' ,
118+ 'shared' : true ,
119+ 'tags' : { 'census.status_code' : '0' } ,
120+ 'timestamp' :
121+ rootSpan . startTime . getTime ( ) * MICROS_PER_MILLI ,
122+ 'traceId' : rootSpan . traceId
123+ } ,
124+ {
125+ 'annotations' : [ ] ,
126+ 'debug' : true ,
127+ 'duration' : Math . round ( span1 . duration * MICROS_PER_MILLI ) ,
128+ 'id' : span1 . id ,
129+ 'kind' : 'CLIENT' ,
130+ 'localEndpoint' : { 'serviceName' : 'opencensus-tests' } ,
131+ 'name' : 'spanTest' ,
132+ 'parentId' : rootSpan . id ,
133+ 'shared' : false ,
134+ 'tags' : { 'census.status_code' : '0' } ,
135+ 'timestamp' : span1 . startTime . getTime ( ) * MICROS_PER_MILLI ,
136+ 'traceId' : span1 . traceId
137+ } ,
138+ {
139+ 'annotations' : [ ] ,
140+ 'debug' : true ,
141+ 'duration' : Math . round ( span2 . duration * MICROS_PER_MILLI ) ,
142+ 'id' : span2 . id ,
143+ 'kind' : 'CLIENT' ,
144+ 'localEndpoint' : { 'serviceName' : 'opencensus-tests' } ,
145+ 'name' : 'spanTest' ,
146+ 'parentId' : span1 . id ,
147+ 'shared' : false ,
148+ 'tags' : { 'census.status_code' : '0' } ,
149+ 'timestamp' : span2 . startTime . getTime ( ) * MICROS_PER_MILLI ,
150+ 'traceId' : span2 . traceId
151+ }
152+ ] ) ;
153+ }
100154 } ) ;
101155 } ) ;
102156 } ) ;
@@ -190,6 +244,17 @@ describe('Zipkin Exporter', function() {
190244 serviceName : 'opencensus-tests'
191245 } ;
192246
247+ let scope : nock . Scope ;
248+
249+ /**
250+ * Checking if tests will use a real network, otherwise run a nock
251+ * server
252+ */
253+ if ( ! OPENCENSUS_NETWORK_TESTS ) {
254+ /** Run a nock server to replace zipkin service */
255+ scope = nock ( zipkinHost ) . persist ( ) . post ( '/wrong' ) . reply ( 404 ) ;
256+ }
257+
193258 const exporter = new ZipkinTraceExporter ( options ) ;
194259 const tracer = new CoreTracer ( ) ;
195260 tracer . start ( defaultConfig ) ;
@@ -202,6 +267,10 @@ describe('Zipkin Exporter', function() {
202267 rootSpan . end ( ) ;
203268 return exporter . publish ( [ rootSpan ] ) . then ( ( result ) => {
204269 assert . strictEqual ( result . statusCode , 404 ) ;
270+
271+ if ( ! OPENCENSUS_NETWORK_TESTS ) {
272+ scope . done ( ) ;
273+ }
205274 } ) ;
206275 } ) ;
207276 } ) ;
0 commit comments