forked from reo7sp/go-tarantool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tnt_test.go
60 lines (52 loc) · 1.21 KB
/
tnt_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
package tarantool
import (
"math/rand"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestVectorClockClone(t *testing.T) {
require := require.New(t)
vc := NewVectorClock(1, 2, 3, 4, 5)
clone := vc.Clone()
require.Equal(vc, clone)
}
func TestVectorClockFollow(t *testing.T) {
require := require.New(t)
vc := NewVectorClock(1, 2, 3, 4, 5)
require.Equal(VectorClock{0, 1, 2, 3, 4, 5}, vc)
vc.Follow(0, 1)
require.Equal(VectorClock{1, 1, 2, 3, 4, 5}, vc)
vc.Follow(1, 2)
require.Equal(VectorClock{1, 2, 2, 3, 4, 5}, vc)
vc.Follow(2, 3)
require.Equal(VectorClock{1, 2, 3, 3, 4, 5}, vc)
vc.Follow(7, 42)
require.Equal(VectorClock{1, 2, 3, 3, 4, 5, 0, 42}, vc)
}
// makes sense only with -race flag
func TestVectorClockRace(t *testing.T) {
vc := NewVectorClock(1, 2, 3, 4, 5)
var wg sync.WaitGroup
wg.Add(1)
go func(vc VectorClock) {
defer wg.Done()
for id := uint32(1); id < 10; id++ {
for i := 0; i < 10; i++ {
vc.Follow(id, rand.Uint64())
time.Sleep(10 * time.Millisecond)
}
}
}(vc)
wg.Add(1)
go func(vc VectorClock) {
defer wg.Done()
for i := 1; i < 100; i++ {
clone := vc.Clone()
_ = clone
time.Sleep(10 * time.Millisecond)
}
}(vc)
wg.Wait()
}