-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
44 lines (37 loc) · 926 Bytes
/
cache_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
package props
import (
"context"
"fmt"
"testing"
"github.com/magiconair/properties"
)
type CtxKey string
var Key = new(CtxKey)
type SimpleSource struct{}
func (s *SimpleSource) Poll(ctx context.Context) (*properties.Properties, error) {
return properties.MustLoadString(fmt.Sprintf("key=%v", ctx.Value(Key))), nil
}
func TestAsyncPoller_Concurrent(t *testing.T) {
t.Run("Concurrent access of props should be safe", func(t *testing.T) {
c := &Cache{
Store: GetProperties(),
Source: &SimpleSource{},
}
ctx := context.TODO()
c.sync(context.WithValue(ctx, Key, "init"))
go func() {
for i := 0; i < 100; i++ {
c.sync(context.WithValue(ctx, Key, i))
}
}()
for i := 100; i < 200; i++ {
v := GetString("key", "")
c.sync(context.WithValue(ctx, Key, i))
nv := GetString("key", "")
if v == nv || v == "" || nv == "" {
t.Error("updating props failed")
return
}
}
})
}