-
Notifications
You must be signed in to change notification settings - Fork 1
/
models_test.go
68 lines (58 loc) · 1.25 KB
/
models_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
package robinson_test
import (
"fmt"
"sync"
"testing"
"github.com/kaatinga/robinson"
)
func TestCrusoe_Call(t *testing.T) {
crusoe := robinson.NewCrusoePointer[int32]()
var wg sync.WaitGroup
f := func(i int32) int32 {
return i + 1
}
for j := 0; j < 1000; j++ {
wg.Add(1)
go func() {
defer wg.Done()
crusoe.Call(f)
}()
}
wg.Wait()
if crusoe.Get() != 1000 {
t.Errorf("strange value returned: %v", crusoe.Get())
}
}
func TestCrusoe_Get_Int(t *testing.T) {
var tests []struct {
value int
}
// fuzzing
for i := 0; i < 1000; i++ {
tests = append(tests, struct{ value int }{value: i})
}
crusoe := robinson.NewCrusoePointer[int]()
if fmt.Sprintf("%[1]T", crusoe) != "*robinson.Crusoe[int]" {
t.Errorf("strange cache type returned: %T", crusoe)
}
wg := sync.WaitGroup{}
for i := 0; i < 2; i++ {
wg.Add(1)
go func(i int) {
for _, scenario := range tests {
t.Run(fmt.Sprintf("%T %[1]v, loop %d", scenario.value, i), func(t *testing.T) {
crusoe.Set(scenario.value)
_ = crusoe.Get()
})
}
wg.Done()
}(i)
}
wg.Wait()
t.Run("get the last value", func(t *testing.T) {
cacheValue := crusoe.Get()
if cacheValue != tests[len(tests)-1].value {
t.Errorf("strange value returned: %v", cacheValue)
}
})
}