Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
fever-ch
GitHub Repository: fever-ch/go-google-sites-proxy
Path: blob/master/common/lazycontainer_test.go
508 views
1
package common
2
3
import (
4
"github.com/stretchr/testify/assert"
5
"testing"
6
7
"strconv"
8
"sync"
9
"sync/atomic"
10
"time"
11
"unsafe"
12
)
13
14
func TestLazyContainer(t *testing.T) {
15
16
const entries = 100000
17
const iterations = 20
18
const extra = 100
19
20
var wg sync.WaitGroup
21
22
var containers [entries + extra]*LazyContainer
23
24
var computations int32
25
26
for i := 0; i < entries+extra; i++ {
27
z := i
28
containers[i] = NewLazyContainer(
29
func() unsafe.Pointer {
30
time.Sleep(5 * time.Millisecond)
31
pt := strconv.Itoa(z)
32
atomic.AddInt32(&computations, 1)
33
return unsafe.Pointer(&pt)
34
})
35
}
36
37
for i := 0; i < entries; i += 2 {
38
for j := 0; j < iterations; j++ {
39
wg.Add(1)
40
go func(idx int) {
41
assert.Equal(t, strconv.Itoa(idx), *(*string)(containers[idx].Get()))
42
wg.Done()
43
}(i)
44
}
45
}
46
47
wg.Wait()
48
49
assert.Equal(t, entries/2, int(computations))
50
}
51
52