-
Notifications
You must be signed in to change notification settings - Fork 0
/
g_counter_test.go
86 lines (78 loc) · 2.09 KB
/
g_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
package crdt
import "testing"
func TestGCounterInitialization(t *testing.T) {
counter := NewGCounter("counter1", "srv1")
if counter.Value() != 0 {
t.Fatalf("counter value should initialize to 0")
}
}
func TestGCounterSingleIncrement(t *testing.T) {
counter := NewGCounter("counter1", "srv1")
counter.Increment()
if counter.Value() != 1 {
t.Fatalf("counter value should be 1")
}
}
func TestGCounterMultiIncrement(t *testing.T) {
counter := NewGCounter("counter1", "srv1")
counter.Increment()
counter.Increment()
counter.Increment()
counter.Increment()
if counter.Value() != 4 {
t.Fatalf("counter value should be 4")
}
}
func TestGCounterMerge(t *testing.T) {
counter1 := NewGCounter("counter1", "srv1")
counter1.Increment()
counter1.Increment()
counter2 := NewGCounter("counter1", "srv2")
counter2.Increment()
counter1.Merge(counter2)
if counter1.Value() != 3 {
t.Fatalf("merged counter value should be 3")
}
}
func TestGCounterMergeMismatchedId(t *testing.T) {
counter1 := NewGCounter("counter1", "srv1")
counter1.Increment()
counter1.Increment()
counter2 := NewGCounter("counter2", "srv2")
counter2.Increment()
counter1.Merge(counter2)
if counter1.Value() != 2 {
t.Fatalf("merged counter value should be 2 since names did not match")
}
}
func TestGCounterMergeIdempotent(t *testing.T) {
counter1 := NewGCounter("counter1", "srv1")
counter1.Increment()
counter1.Increment()
counter2 := NewGCounter("counter1", "srv2")
counter2.Increment()
counter1.Merge(counter2)
if counter1.Value() != 3 {
t.Fatalf("merged counter value should be 3")
}
counter1.Merge(counter2)
if counter1.Value() != 3 {
t.Fatalf("merged counter value should be 3")
}
}
func TestGCounterMergeSameNode(t *testing.T) {
counter1 := NewGCounter("counter1", "srv1")
counter1.Increment()
counter1.Increment()
counter2 := NewGCounter("counter1", "srv2")
counter2.Increment()
counter1.Merge(counter2)
if counter1.Value() != 3 {
t.Fatalf("merged counter value should be 3")
}
counter2.Increment()
counter1.Merge(counter2)
if counter1.Value() != 4 {
t.Fatalf("merged counter value should be 4")
}
}