-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathatomic_counter_test.go
120 lines (94 loc) · 2.96 KB
/
atomic_counter_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
116
117
118
119
120
package chotki
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/cockroachdb/pebble"
"github.com/drpcorg/chotki/protocol"
"github.com/drpcorg/chotki/rdx"
"github.com/stretchr/testify/assert"
)
func TestAtomicCounter(t *testing.T) {
dir, err := os.MkdirTemp("", "*")
assert.NoError(t, err)
a, err := Open(dir, Options{
Src: 0x1a,
Name: "test replica",
Options: pebble.Options{ErrorIfExists: true},
})
assert.NoError(t, err)
cid, err := a.NewClass(context.Background(), rdx.ID0, Field{Name: "test", RdxType: rdx.Natural})
assert.NoError(t, err)
rid, err := a.NewObjectTLV(context.Background(), cid, protocol.Records{protocol.Record('N', rdx.Ntlv(0))})
assert.NoError(t, err)
counterA := NewAtomicCounter(a, rid, 1, 0)
counterB := NewAtomicCounter(a, rid, 1, 0)
res, err := counterA.Increment(context.Background(), 1)
assert.NoError(t, err)
assert.EqualValues(t, 1, res)
res, err = counterB.Increment(context.Background(), 1)
assert.NoError(t, err)
assert.EqualValues(t, 2, res)
res, err = counterA.Increment(context.Background(), 1)
assert.NoError(t, err)
assert.EqualValues(t, 3, res)
}
func TestAtomicCounterWithPeriodicUpdate(t *testing.T) {
dira, err := os.MkdirTemp("", "*")
assert.NoError(t, err)
a, err := Open(dira, Options{
Src: 0x1a,
Name: "test replica",
Options: pebble.Options{ErrorIfExists: true},
})
assert.NoError(t, err)
dirb, err := os.MkdirTemp("", "*")
assert.NoError(t, err)
b, err := Open(dirb, Options{
Src: 0x1b,
Name: "test replica2",
Options: pebble.Options{ErrorIfExists: true},
})
assert.NoError(t, err)
cid, err := a.NewClass(
context.Background(), rdx.ID0,
Field{Name: "test", RdxType: rdx.Natural},
Field{Name: "test2", RdxType: rdx.ZCounter},
)
assert.NoError(t, err)
rid, err := a.NewObjectTLV(
context.Background(), cid,
protocol.Records{
protocol.Record('N', rdx.Ntlv(0)),
protocol.Record('Z', rdx.Ztlv(0)),
},
)
assert.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for i := 1; i <= 2; i++ {
counterA := NewAtomicCounter(a, rid, uint64(i), 100*time.Millisecond)
counterB := NewAtomicCounter(b, rid, uint64(i), 0)
// first increment
res, err := counterA.Increment(ctx, 1)
assert.NoError(t, err)
assert.EqualValues(t, 1, res, fmt.Sprintf("iteration %d", i))
syncData(a, b)
// increment from another replica
res, err = counterB.Increment(ctx, 1)
assert.NoError(t, err)
assert.EqualValues(t, 2, res, fmt.Sprintf("iteration %d", i))
syncData(a, b)
// this increment does not account data from other replica because current value is cached
res, err = counterA.Increment(ctx, 1)
assert.NoError(t, err)
assert.EqualValues(t, 2, res, fmt.Sprintf("iteration %d", i))
time.Sleep(100 * time.Millisecond)
// after wait we increment, and we get actual value
res, err = counterA.Increment(ctx, 1)
assert.NoError(t, err)
assert.EqualValues(t, 4, res, fmt.Sprintf("iteration %d", i))
}
}