-
Notifications
You must be signed in to change notification settings - Fork 0
/
z_gomap_distributed_test.go
115 lines (84 loc) · 2.5 KB
/
z_gomap_distributed_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
package gomap
import (
"os"
"strconv"
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDistributedHashmapBasic(t *testing.T) {
folder, _ := os.MkdirTemp("", "hash")
var obj HashmapDistributed
obj.New(folder)
key := []byte{'w', 'x', 'r', 'l', 'q'}
value := []byte("awoiljfasdlfj")
err := obj.Add(key, value)
assert.Nil(t, err, "Error should be nil")
}
func TestDistributedHashmapAddGet1(t *testing.T) {
folder, _ := os.MkdirTemp("", "hash")
var obj HashmapDistributed
obj.New(folder)
key := []byte{'w', 'x', 'r', 'l', 'q'}
value := []byte("value")
err := obj.Add(key, value)
assert.Nil(t, err, "Error should be nil")
res, err := obj.Get(key)
assert.Nil(t, err, "Error should be nil")
assert.Equal(t, value, res, "they should be equal")
}
func TestDistributedHashmapAddGetN(t *testing.T) {
folder, _ := os.MkdirTemp("", "hash")
var obj HashmapDistributed
obj.New(folder)
for i := 0; i < Ntests; i++ {
key := []byte(strconv.Itoa(i))
value := key
err := obj.Add(key, value)
assert.Nil(t, err, "Error should be nil")
res, err := obj.Get(key)
assert.Nil(t, err, "Error should be nil")
assert.Equal(t, res, value, "they should be equal")
}
}
func TestDistributedHashmapAddGetNAsync(t *testing.T) {
folder, _ := os.MkdirTemp("", "hash")
var obj HashmapDistributed
obj.New(folder)
var wg sync.WaitGroup // create a WaitGroup
for i := 0; i < Ntests; i++ {
wg.Add(1) // increment the WaitGroup counter
go func(i int) { // capture loop variable
defer wg.Done() // defer the Done call
key := []byte(strconv.Itoa(i))
value := key
err := obj.Add(key, value)
assert.Nil(t, err, "Error should be nil")
}(i) // pass loop variable as argument
}
wg.Wait() // wait for all above goroutines to finish
for i := 0; i < 10; i++ {
wg.Add(1) // increment the WaitGroup counter
go func(i int) { // capture loop variable
defer wg.Done() // defer the Done call
key := []byte(strconv.Itoa(i))
value := key
res, err := obj.Get(key)
assert.Nil(t, err, "Error should be nil")
assert.Equal(t, res, value, "they should be equal")
}(i) // pass loop variable as argument
}
wg.Wait() // wait for all above goroutines to finish
}
func BenchmarkDistributedHashmapValue(b *testing.B) {
folder, _ := os.MkdirTemp("", "hash")
var obj HashmapDistributed
obj.New(folder)
b.ResetTimer()
for i := 0; i < b.N; i++ {
key := []byte(strconv.Itoa(i))
value := key
err := obj.Add(key, value)
assert.Nil(b, err, "Error should be nil")
}
}