-
Notifications
You must be signed in to change notification settings - Fork 0
/
reloader_test.go
127 lines (94 loc) · 1.74 KB
/
reloader_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
package reloader_test
import (
"fmt"
"sync"
"testing"
"time"
"github.com/starboard-nz/reloader"
)
var i int32
func intLoad() (*int32, error) {
n := i
i++
return &n, nil
}
func TestReloaderInt32(t *testing.T) {
l := reloader.NewLoader[int32](intLoad, 100 * time.Millisecond)
wg := &sync.WaitGroup{}
// start 100 goroutines that read the same data
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
var prev int32
for {
var d int32
err := l.Do(func(n *int32) error {
if n == nil {
return fmt.Errorf("nil pointer")
}
d = *n
return nil
})
if err != nil {
t.Errorf("Error returned: %v", err)
}
if prev != d {
fmt.Printf("Data = %d\n", d)
prev = d
}
time.Sleep(time.Millisecond)
if d >= 10 {
wg.Done()
return
}
}
}()
}
// wait for all goroutines to return
wg.Wait()
}
type Foo struct {
m map[string]int32
}
var x int32
func fooLoad() (*Foo, error) {
m := make(map[string]int32)
m["hello"] = x
x++
return &Foo{m: m}, nil
}
func TestReloaderFoo(t *testing.T) {
l := reloader.NewLoader[Foo](fooLoad, 100 * time.Millisecond)
wg := &sync.WaitGroup{}
// start 100 goroutines that read the same data
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
var prev int32
for {
var d int32
err := l.Do(func(n *Foo) error {
if n == nil {
return fmt.Errorf("nil pointer")
}
d = n.m["hello"]
return nil
})
if err != nil {
t.Errorf("Error returned: %v", err)
}
if prev != d {
fmt.Printf("Data = %d\n", d)
prev = d
}
time.Sleep(time.Millisecond)
if d >= 10 {
wg.Done()
return
}
}
}()
}
// wait for all goroutines to return
wg.Wait()
}