-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcontext_test.go
More file actions
37 lines (34 loc) · 860 Bytes
/
context_test.go
File metadata and controls
37 lines (34 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package sqlcomment
import (
"context"
"github.com/stretchr/testify/assert"
"sync"
"testing"
)
func TestWithTagThreadSafety(t *testing.T) {
ctx := context.Background()
var wg sync.WaitGroup
const numGoroutines = 100
results := make(map[int]Tags)
mu := sync.Mutex{}
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
// Add a unique tag for each goroutine
newCtx := WithTag(ctx, "goroutine", string(rune('A'+i)))
tags := FromContext(newCtx)
// Store the result in the map
mu.Lock()
results[i] = tags
mu.Unlock()
}(i)
}
wg.Wait()
// Verify that each goroutine has its own independent tags
assert.Equal(t, numGoroutines, len(results))
for i := 0; i < numGoroutines; i++ {
assert.Contains(t, results[i], "goroutine")
assert.Equal(t, string(rune('A'+i)), results[i]["goroutine"])
}
}