-
Notifications
You must be signed in to change notification settings - Fork 27
/
counter_test.go
110 lines (89 loc) · 2.55 KB
/
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
package riak
import (
"github.com/bmizerany/assert"
"testing"
)
func TestCounter(t *testing.T) {
// Preparations
client := setupConnection(t)
assert.T(t, client != nil)
_, version, err := client.ServerVersion()
assert.T(t, err == nil)
major, minor := parseVersion(version)
if (major < 1) || (major == 1 && minor < 4) {
t.Log("running a pre 1.4 version of riak - skipping counter tests.")
return
}
// Find bucket and set properties
bucket, err := client.NewBucket("counter_test.go")
assert.T(t, err == nil)
err = bucket.SetAllowMult(true)
assert.T(t, err == nil)
c1, err := bucket.GetCounter("counter_1")
assert.T(t, err == nil)
base := c1.Value
// Increment and refresh
err = c1.IncrementAndReload(5)
assert.T(t, err == nil)
assert.T(t, c1.Value == (base+5))
// Increment without refresh
err = c1.Increment(5)
assert.T(t, err == nil)
assert.T(t, c1.Value == (base+5))
// Reload
err = c1.Reload()
assert.T(t, err == nil)
assert.T(t, c1.Value == (base+10))
// Decrement multiple times
err = c1.Decrement(2)
assert.T(t, err == nil)
err = c1.Decrement(2)
assert.T(t, err == nil)
// Decrement and refresh
err = c1.DecrementAndReload(3)
assert.T(t, err == nil)
assert.T(t, c1.Value == (base+3))
c2, err := bucket.GetCounter("counter_2")
assert.T(t, err == nil)
base = c2.Value
// Increment another counter
err = c2.Increment(3)
assert.T(t, err == nil)
err = c2.Increment(5)
assert.T(t, err == nil)
// Reload the counter
err = c2.Reload()
assert.T(t, err == nil)
assert.T(t, c2.Value == (base+8))
// Get directly from bucket
c3, err := client.GetCounterFrom("counter_test.go", "counter_2")
assert.T(t, err == nil)
assert.T(t, c3.Value == (base+8))
// Destroy the first counter
err = c1.Destroy()
assert.T(t, err == nil)
err = c1.Reload()
assert.T(t, c1.Value == 0)
// Write a new value back to the first counter
err = c1.Increment(5)
assert.T(t, err == nil)
err = c1.Reload()
assert.T(t, c1.Value == 5)
// Increment a counter without a read
// First get the base (so we actually want to read in this case)... This is purely for testing purposes
c4, err := bucket.GetCounter("counter_3")
assert.T(t, err == nil)
base = c4.Value
// Grab the counter again, but this time don't load the value
c5, err := bucket.GetCounterWithoutLoad("counter_3")
assert.T(t, err == nil)
// Increment another counter
err = c5.Increment(3)
assert.T(t, err == nil)
err = c5.Increment(5)
assert.T(t, err == nil)
// Actually load the counter at this point
err = c5.Reload()
assert.T(t, err == nil)
assert.T(t, c5.Value == (base+8))
}