Skip to content

Commit 86dfc35

Browse files
committed
feat: Re-initialise telemetry handles to fix race conditions during test execution
1 parent 18a4358 commit 86dfc35

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

aitelemetry/telemetrywrapper_test.go

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import (
1515
)
1616

1717
var (
18-
th1 TelemetryHandle
19-
th2 TelemetryHandle
2018
aiConfig AIConfig
2119
hostAgentUrl = "localhost:3501"
2220
getCloudResponse = "AzurePublicCloud"
@@ -68,16 +66,6 @@ func TestMain(m *testing.M) {
6866
DisableMetadataRefreshThread: true,
6967
}
7068

71-
th1, err = NewAITelemetry(httpURL, "00ca2a73-c8d6-4929-a0c2-cf84545ec225", aiConfig)
72-
if th1 == nil {
73-
fmt.Printf("Error initializing AI telemetry: %v", err)
74-
}
75-
76-
th2, err = NewWithConnectionString(connectionString, aiConfig)
77-
if th2 == nil {
78-
fmt.Printf("Error initializing AI telemetry with connection string: %v", err)
79-
}
80-
8169
exitCode := m.Run()
8270

8371
if runtime.GOOS == "linux" {
@@ -99,6 +87,20 @@ func handleGetCloud(w http.ResponseWriter, req *http.Request) {
9987
w.Write([]byte(getCloudResponse))
10088
}
10189

90+
func initTelemetry(t *testing.T) (TelemetryHandle, TelemetryHandle) {
91+
th1, err1 := NewAITelemetry(httpURL, "00ca2a73-c8d6-4929-a0c2-cf84545ec225", aiConfig)
92+
if err1 != nil {
93+
fmt.Printf("Error initializing AI telemetry: %v", err1)
94+
}
95+
96+
th2, err2 := NewWithConnectionString(connectionString, aiConfig)
97+
if err2 != nil {
98+
fmt.Printf("Error initializing AI telemetry with connection string: %v", err2)
99+
}
100+
101+
return th1, th2
102+
}
103+
102104
func TestEmptyAIKey(t *testing.T) {
103105
var err error
104106

@@ -116,18 +118,19 @@ func TestEmptyAIKey(t *testing.T) {
116118
func TestNewAITelemetry(t *testing.T) {
117119
var err error
118120

119-
th1, err := NewAITelemetry(httpURL, "00ca2a73-c8d6-4929-a0c2-cf84545ec225", aiConfig)
121+
th1, th2 := initTelemetry(t)
120122
if th1 == nil {
121123
t.Errorf("Error initializing AI telemetry: %v", err)
122124
}
123125

124-
th2, err := NewWithConnectionString(connectionString, aiConfig)
125126
if th2 == nil {
126127
t.Errorf("Error initializing AI telemetry with connection string: %v", err)
127128
}
128129
}
129130

130131
func TestTrackMetric(t *testing.T) {
132+
th1, th2 := initTelemetry(t)
133+
131134
metric := Metric{
132135
Name: "test",
133136
Value: 1.0,
@@ -140,6 +143,8 @@ func TestTrackMetric(t *testing.T) {
140143
}
141144

142145
func TestTrackLog(t *testing.T) {
146+
th1, th2 := initTelemetry(t)
147+
143148
report := Report{
144149
Message: "test",
145150
Context: "10a",
@@ -152,6 +157,8 @@ func TestTrackLog(t *testing.T) {
152157
}
153158

154159
func TestTrackEvent(t *testing.T) {
160+
th1, th2 := initTelemetry(t)
161+
155162
event := Event{
156163
EventName: "testEvent",
157164
ResourceID: "SomeResourceId",
@@ -165,28 +172,15 @@ func TestTrackEvent(t *testing.T) {
165172
}
166173

167174
func TestFlush(t *testing.T) {
175+
th1, th2 := initTelemetry(t)
176+
168177
th1.Flush()
169178
th2.Flush()
170179
}
171180

172181
func TestClose(t *testing.T) {
182+
th1, th2 := initTelemetry(t)
183+
173184
th1.Close(10)
174185
th2.Close(10)
175186
}
176-
177-
func TestClosewithoutSend(t *testing.T) {
178-
var err error
179-
180-
thtest, err := NewAITelemetry(httpURL, "00ca2a73-c8d6-4929-a0c2-cf84545ec225", aiConfig)
181-
if thtest == nil {
182-
t.Errorf("Error initializing AI telemetry:%v", err)
183-
}
184-
185-
thtest2, err := NewWithConnectionString(connectionString, aiConfig)
186-
if thtest2 == nil {
187-
t.Errorf("Error initializing AI telemetry with connection string:%v", err)
188-
}
189-
190-
thtest.Close(10)
191-
thtest2.Close(10)
192-
}

0 commit comments

Comments
 (0)