-
Notifications
You must be signed in to change notification settings - Fork 7
/
counter_test.go
122 lines (108 loc) · 2.46 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
111
112
113
114
115
116
117
118
119
120
121
122
package riaken_core
import (
"testing"
"github.com/golang/protobuf/proto"
"github.com/riaken/riaken-core/rpb"
)
func init() {
client := dial()
defer client.Close()
session := client.Session()
defer session.Release()
bucket := session.GetBucket("b5")
props := &rpb.RpbBucketProps{
AllowMult: proto.Bool(true),
}
if _, err := bucket.SetBucketProps(props); err != nil {
panic(err.Error())
}
}
func TestCounter(t *testing.T) {
client := dial()
defer client.Close()
session := client.Session()
defer session.Release()
bucket := session.GetBucket("b5")
counter := bucket.Counter("c1")
if _, err := counter.Update(1); err != nil {
t.Error(err.Error())
}
data, err := counter.Get()
if err != nil {
t.Error(err.Error())
}
if data.GetValue() != 1 {
t.Errorf("got %d, expected 1", data.GetValue())
}
if _, err := counter.Update(10); err != nil {
t.Error(err.Error())
}
data, err = counter.Get()
if err != nil {
t.Error(err.Error())
}
if data.GetValue() != 11 {
t.Errorf("got %d, expected 11", data.GetValue())
}
if _, err := counter.Update(-5); err != nil {
t.Error(err.Error())
}
data, err = counter.Get()
if err != nil {
t.Error(err.Error())
}
if data.GetValue() != 6 {
t.Errorf("got %d, expected 6", data.GetValue())
}
object := bucket.Object("c1")
if ok, err := object.Delete(); !ok {
t.Error("deletion of object failed")
} else if err != nil {
t.Error(err.Error())
}
}
func TestCounterDo(t *testing.T) {
client := dial()
defer client.Close()
session := client.Session()
defer session.Release()
bucket := session.GetBucket("b5")
counter := bucket.Counter("c1")
opts1 := &rpb.RpbCounterUpdateReq{
Returnvalue: proto.Bool(true),
}
ret, err := counter.Do(opts1).Update(5)
if err != nil {
t.Error(err.Error())
}
if ret.GetValue() != 5 {
t.Errorf("got %d, expected 5", ret.GetValue())
}
opts2 := &rpb.RpbCounterGetReq{
NotfoundOk: proto.Bool(true),
}
data, err := counter.Do(opts2).Get()
if err != nil {
t.Error(err.Error())
}
if data.GetValue() != 5 {
t.Errorf("got %d, expected 5", ret.GetValue())
}
counter = bucket.Counter("c2")
opts3 := &rpb.RpbCounterGetReq{
NotfoundOk: proto.Bool(true),
}
data, err = counter.Do(opts3).Get()
if err != nil {
t.Error(err.Error())
}
if data.GetValue() != 0 {
t.Errorf("got %d, expected 0", ret.GetValue())
}
object := bucket.Object("c1")
if ok, err := object.Delete(); !ok {
t.Error("deletion of object failed")
} else if err != nil {
t.Error(err.Error())
}
}