-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy paththread_local_test.go
219 lines (204 loc) · 4.58 KB
/
thread_local_test.go
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package routine
import (
"math"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestThreadLocal_Index(t *testing.T) {
tls := NewThreadLocal[string]()
assert.GreaterOrEqual(t, tls.(*threadLocal[string]).index, 0)
tls2 := NewThreadLocalWithInitial[string](func() string {
return "Hello"
})
assert.Greater(t, tls2.(*threadLocal[string]).index, tls.(*threadLocal[string]).index)
}
func TestThreadLocal_NextIndex(t *testing.T) {
backup := threadLocalIndex
defer func() {
threadLocalIndex = backup
}()
//
threadLocalIndex = math.MaxInt32
assert.Panics(t, func() {
nextThreadLocalIndex()
})
assert.Equal(t, math.MaxInt32, int(threadLocalIndex))
}
func TestThreadLocal_Common(t *testing.T) {
tls := NewThreadLocal[int]()
tls2 := NewThreadLocal[string]()
tls.Remove()
tls2.Remove()
assert.Equal(t, 0, tls.Get())
assert.Equal(t, "", tls2.Get())
//
tls.Set(1)
tls2.Set("World")
assert.Equal(t, 1, tls.Get())
assert.Equal(t, "World", tls2.Get())
//
tls.Set(0)
tls2.Set("")
assert.Equal(t, 0, tls.Get())
assert.Equal(t, "", tls2.Get())
//
tls.Set(2)
tls2.Set("!")
assert.Equal(t, 2, tls.Get())
assert.Equal(t, "!", tls2.Get())
//
tls.Remove()
tls2.Remove()
assert.Equal(t, 0, tls.Get())
assert.Equal(t, "", tls2.Get())
//
tls.Set(2)
tls2.Set("!")
assert.Equal(t, 2, tls.Get())
assert.Equal(t, "!", tls2.Get())
wg := &sync.WaitGroup{}
wg.Add(100)
for i := 0; i < 100; i++ {
Go(func() {
assert.Equal(t, 0, tls.Get())
assert.Equal(t, "", tls2.Get())
wg.Done()
})
}
wg.Wait()
assert.Equal(t, 2, tls.Get())
assert.Equal(t, "!", tls2.Get())
}
func TestThreadLocal_Mixed(t *testing.T) {
tls := NewThreadLocal[int]()
tls2 := NewThreadLocalWithInitial[string](func() string {
return "Hello"
})
assert.Equal(t, 0, tls.Get())
assert.Equal(t, "Hello", tls2.Get())
//
tls.Set(1)
tls2.Set("World")
assert.Equal(t, 1, tls.Get())
assert.Equal(t, "World", tls2.Get())
//
tls.Set(0)
tls2.Set("")
assert.Equal(t, 0, tls.Get())
assert.Equal(t, "", tls2.Get())
//
tls.Set(2)
tls2.Set("!")
assert.Equal(t, 2, tls.Get())
assert.Equal(t, "!", tls2.Get())
//
tls.Remove()
tls2.Remove()
assert.Equal(t, 0, tls.Get())
assert.Equal(t, "Hello", tls2.Get())
//
tls.Set(2)
tls2.Set("!")
assert.Equal(t, 2, tls.Get())
assert.Equal(t, "!", tls2.Get())
wg := &sync.WaitGroup{}
wg.Add(100)
for i := 0; i < 100; i++ {
Go(func() {
assert.Equal(t, 0, tls.Get())
assert.Equal(t, "Hello", tls2.Get())
wg.Done()
})
}
wg.Wait()
assert.Equal(t, 2, tls.Get())
assert.Equal(t, "!", tls2.Get())
}
func TestThreadLocal_WithInitial(t *testing.T) {
src := &person{Id: 1, Name: "Tim"}
tls := NewThreadLocalWithInitial[*person](nil)
tls2 := NewThreadLocalWithInitial[*person](func() *person {
var value *person
return value
})
tls3 := NewThreadLocalWithInitial[*person](func() *person {
return src
})
tls4 := NewThreadLocalWithInitial[person](func() person {
return *src
})
for i := 0; i < 100; i++ {
p := tls.Get()
assert.Nil(t, p)
//
p2 := tls2.Get()
assert.Nil(t, p2)
//
p3 := tls3.Get()
assert.Same(t, src, p3)
p4 := tls4.Get()
assert.NotSame(t, src, &p4)
assert.Equal(t, *src, p4)
wg := &sync.WaitGroup{}
wg.Add(1)
Go(func() {
assert.Same(t, src, tls3.Get())
p5 := tls4.Get()
assert.NotSame(t, src, &p5)
assert.Equal(t, *src, p5)
//
wg.Done()
})
wg.Wait()
}
tls3.Set(nil)
tls4.Set(person{})
assert.Nil(t, tls3.Get())
assert.Equal(t, person{}, tls4.Get())
tls3.Remove()
tls4.Remove()
assert.Same(t, src, tls3.Get())
p6 := tls4.Get()
assert.NotSame(t, src, &p6)
assert.Equal(t, *src, p6)
}
func TestThreadLocal_CrossCoroutine(t *testing.T) {
tls := NewThreadLocal[string]()
tls.Set("Hello")
assert.Equal(t, "Hello", tls.Get())
subWait := &sync.WaitGroup{}
subWait.Add(2)
finishWait := &sync.WaitGroup{}
finishWait.Add(2)
go func() {
subWait.Wait()
assert.Equal(t, "", tls.Get())
finishWait.Done()
}()
Go(func() {
subWait.Wait()
assert.Equal(t, "", tls.Get())
finishWait.Done()
})
tls.Remove() //remove in parent goroutine should not affect child goroutine
subWait.Done() //allow sub goroutine run
subWait.Done() //allow sub goroutine run
finishWait.Wait() //wait sub goroutine done
finishWait.Wait() //wait sub goroutine done
}
func TestThreadLocal_CreateBatch(t *testing.T) {
const count = 128
tlsList := make([]ThreadLocal[int], count)
for i := 0; i < count; i++ {
value := i
tlsList[i] = NewThreadLocalWithInitial[int](func() int { return value })
}
for i := 0; i < count; i++ {
assert.Equal(t, i, tlsList[i].Get())
}
}
type person struct {
Id int
Name string
}