-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathroutine_storage_test.go
73 lines (64 loc) · 1.23 KB
/
routine_storage_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
package routine
import (
"github.com/stretchr/testify/assert"
"math/rand"
"runtime"
"sync"
"testing"
)
func TestStorage(t *testing.T) {
s := newStorage()
for i := 0; i < 1000; i++ {
str := "hello"
s.Set(str)
p := s.Get()
assert.True(t, p.(string) == str)
}
assert.True(t, s.Del() != nil)
assert.True(t, s.Get() == nil)
}
// test too many storages
func TestStorageTooMany(t *testing.T) {
var ss []*storage
for keyCount() < keyCnt {
ss = append(ss, newStorage())
}
assert.Panics(t, func() {
newStorage()
})
_ = ss
// Avoid affecting other tests
runtime.GC()
nap()
}
func TestStorageConcurrency(t *testing.T) {
const concurrency = 100
const loopTimes = 100000
var wg sync.WaitGroup
s := newStorage()
wg.Add(concurrency)
for i := 0; i < concurrency; i++ {
go func() {
v := rand.Uint64()
for i := 0; i < loopTimes; i++ {
s.Set(v)
tmp := s.Get()
assert.True(t, tmp.(uint64) == v)
}
wg.Done()
}()
}
wg.Wait()
}
// BenchmarkStorage-12 8102013 133.6 ns/op 16 B/op 1 allocs/op
func BenchmarkStorage(b *testing.B) {
s := newStorage()
var variable = "hello world"
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = s.Get()
s.Set(variable)
s.Del()
}
}