6
6
"fmt"
7
7
"net/http"
8
8
"net/http/httptest"
9
+ "sync"
9
10
"testing"
10
11
11
12
instana "github.com/instana/go-sensor"
@@ -16,22 +17,25 @@ import (
16
17
)
17
18
18
19
func Test_Collector_Noop (t * testing.T ) {
19
- assert .NotNil (t , instana .C , "instana.C should never be nil and be initialized as noop" )
20
+ c , err := instana .GetCollector ()
21
+ assert .Error (t , err , "should return error as collector has not been initialized yet." )
20
22
21
- sc , err := instana .C .Extract (nil , nil )
23
+ assert .NotNil (t , c , "instana collector should never be nil and be initialized as noop" )
24
+
25
+ sc , err := c .Extract (nil , nil )
22
26
assert .Nil (t , sc )
23
27
assert .Error (t , err )
24
- assert .Nil (t , instana . C .StartSpan ("" ))
25
- assert .Nil (t , instana . C .LegacySensor ())
28
+ assert .Nil (t , c .StartSpan ("" ))
29
+ assert .Nil (t , c .LegacySensor ())
26
30
}
27
31
28
32
func Test_Collector_LegacySensor (t * testing.T ) {
29
33
recorder := instana .NewTestRecorder ()
30
34
c := instana .InitCollector (& instana.Options {AgentClient : alwaysReadyClient {}, Recorder : recorder })
31
35
s := c .LegacySensor ()
32
- defer instana .ShutdownSensor ()
36
+ defer instana .ShutdownCollector ()
33
37
34
- assert .NotNil (t , instana . C .LegacySensor ())
38
+ assert .NotNil (t , c .LegacySensor ())
35
39
36
40
h := instana .TracingHandlerFunc (s , "/{action}" , func (w http.ResponseWriter , req * http.Request ) {
37
41
fmt .Fprintln (w , "Ok" )
@@ -41,30 +45,55 @@ func Test_Collector_LegacySensor(t *testing.T) {
41
45
42
46
h .ServeHTTP (httptest .NewRecorder (), req )
43
47
44
- assert .Len (t , recorder .GetQueuedSpans (), 1 , "Instrumentations should still work fine with instana.C. LegacySensor()" )
48
+ assert .Len (t , recorder .GetQueuedSpans (), 1 , "Instrumentations should still work fine with LegacySensor() method " )
45
49
}
46
50
47
51
func Test_Collector_Singleton (t * testing.T ) {
48
- instana .C = nil
49
52
var ok bool
50
53
var instance instana.TracerLogger
51
54
52
- _ , ok = instana .C .(* instana.Collector )
53
- assert .False (t , ok , "instana.C is noop before InitCollector is called" )
55
+ c , err := instana .GetCollector ()
56
+ assert .Error (t , err , "should return error as collector has not been initialized yet." )
57
+
58
+ defer instana .ShutdownCollector ()
59
+
60
+ _ , ok = c .(* instana.Collector )
61
+ assert .False (t , ok , "instana collector is noop before InitCollector is called" )
62
+
63
+ c = instana .InitCollector (instana .DefaultOptions ())
64
+
65
+ instance , ok = c .(* instana.Collector )
66
+ assert .True (t , ok , "instana collector is of type instana.Collector after InitCollector is called" )
67
+
68
+ c = instana .InitCollector (instana .DefaultOptions ())
54
69
55
- instana .InitCollector (instana .DefaultOptions ())
70
+ assert .Equal (t , c , instance , "instana collector is singleton and should not be reassigned if InitCollector is called again" )
71
+ }
72
+
73
+ func Test_InitCollector_With_Goroutines (t * testing.T ) {
74
+
75
+ defer instana .ShutdownCollector ()
56
76
57
- instance , ok = instana . C .( * instana. Collector )
58
- assert . True ( t , ok , "instana.C is of type instana.Collector after InitCollector is called" )
77
+ var wg sync. WaitGroup
78
+ wg . Add ( 3 )
59
79
60
- instana .InitCollector (instana .DefaultOptions ())
80
+ for i := 0 ; i < 3 ; i ++ {
81
+ go func (id int ) {
82
+ defer wg .Done ()
83
+ c := instana .InitCollector (instana .DefaultOptions ())
61
84
62
- assert .Equal (t , instana .C , instance , "instana.C is singleton and should not be reassigned if InitCollector is called again" )
85
+ _ , ok := c .(* instana.Collector )
86
+ assert .True (t , ok , "instana collector is of type instana.Collector after InitCollector is called" )
87
+
88
+ assert .NotNil (t , c )
89
+ }(i )
90
+ }
91
+ wg .Wait ()
63
92
}
64
93
65
94
func Test_Collector_EmbeddedTracer (t * testing.T ) {
66
- instana .C = nil
67
95
c := instana .InitCollector (nil )
96
+ defer instana .ShutdownCollector ()
68
97
69
98
sp := c .StartSpan ("my-span" )
70
99
@@ -92,18 +121,18 @@ func Test_Collector_EmbeddedTracer(t *testing.T) {
92
121
}
93
122
94
123
func Test_Collector_Logger (t * testing.T ) {
95
- instana . C = nil
96
- instana .InitCollector ( nil )
124
+ c := instana . InitCollector ( nil )
125
+ defer instana .ShutdownCollector ( )
97
126
98
127
l := & mylogger {}
99
128
100
- instana . C .SetLogger (l )
129
+ c .SetLogger (l )
101
130
102
- instana . C .Debug ()
103
- instana . C .Info ()
104
- instana . C .Warn ()
105
- instana . C .Error ()
106
- instana . C .Error ()
131
+ c .Debug ()
132
+ c .Info ()
133
+ c .Warn ()
134
+ c .Error ()
135
+ c .Error ()
107
136
108
137
assert .Equal (t , 1 , l .counter ["debug" ])
109
138
assert .Equal (t , 1 , l .counter ["info" ])
0 commit comments