-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter_test.go
71 lines (52 loc) · 1.29 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
package kredis
import (
"time"
)
func (s *KredisTestSuite) TestCounter() {
c, err := NewCounter("counter")
s.NoError(err)
v, err := c.Increment(1)
s.NoError(err)
s.Equal(int64(1), v)
v, err = c.Increment(2)
s.NoError(err)
s.Equal(int64(3), v)
v, err = c.Decrement(1)
s.NoError(err)
s.Equal(int64(2), v)
v, err = c.Decrement(1)
s.NoError(err)
s.Equal(int64(1), v)
v, err = c.Decrement(2)
s.NoError(err)
s.Equal(int64(-1), v)
s.NoError(c.Reset())
s.Empty(c.Value())
}
func (s *KredisTestSuite) TestCounterWithExpiry() {
c, _ := NewCounter("counter", WithExpiry("1ms"))
c.Increment(1)
c.Increment(2)
c.Increment(3)
time.Sleep(5 * time.Millisecond)
s.Empty(c.Value())
}
func (s *KredisTestSuite) TestCounterWithDefault() {
c, _ := NewCounterWithDefault("counter", 23)
s.Equal(int64(23), c.Value())
}
func (s *KredisTestSuite) TestCounterWithDefaultAndExpiry() {
c, _ := NewCounterWithDefault("counter", 23, WithExpiry("1ms"))
c.Increment(1)
c.Increment(2)
c.Increment(3)
time.Sleep(5 * time.Millisecond)
s.Empty(c.Value())
}
func (s *KredisTestSuite) TestCounterWIthBadConnection() {
c, _ := NewCounter("counter", WithConfigName("badconn"))
s.Empty(c.Value())
s.Equal([]string{
"Counter#Value dial tcp [::1]:1234: connect: connection refused",
}, testWarnings)
}