-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction_test.go
133 lines (105 loc) · 3.32 KB
/
action_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
123
124
125
126
127
128
129
130
131
132
133
package tdb
import (
"os"
"testing"
"time"
"github.com/drkaka/lg"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)
func TestActions(t *testing.T) {
lg.InitLogger(true)
folder := "test_actions"
db, err := Open(folder)
if !assert.NoError(t, err, "should have no error") {
t.Fatal(err)
}
defer os.RemoveAll(folder)
testFastInsert(t, db)
testAddAction(t, db)
// test load actions
db2, err := Open(folder)
if !assert.NoError(t, err, "should have no error") {
t.Fatal(err)
}
targets, allStarts, allLasts, err := db2.GetActions()
assert.NoError(t, err, "get all actions wrong")
assert.Len(t, targets, 1, "targets count wrong")
assert.Len(t, allStarts, 1, "starts count wrong")
assert.Len(t, allLasts, 1, "lasts count wrong")
// after test, should have no item
testCheckEXP(t, db)
testReloadEmptyInfo(t, folder)
}
func testFastInsert(t *testing.T, db *TDB) {
target := string(randBytes(6))
now := uint32(time.Now().Unix())
p("test fast insert", zap.String("target", target))
var g errgroup.Group
for i := 0; i < 100; i++ {
g.Go(func() error {
return db.AddAction(target, now)
})
}
err := g.Wait()
assert.NoError(t, err, "error when wait")
starts, howlongs, err := db.GetSlots(target, 0, 0)
assert.NoError(t, err, "get slots wrong")
assert.Len(t, starts, 0, "starts count wrong")
assert.Len(t, howlongs, 0, "howlong count wrong")
delete(db.action.content, target)
}
func testAddAction(t *testing.T, db *TDB) {
target := string(randBytes(6))
now := uint32(time.Now().Unix())
// add new action
db.AddAction(target, now)
// should create a slot
db.AddAction(target, now+17)
starts, howlongs, err := db.GetSlots(target, 0, 0)
assert.NoError(t, err, "get slots wrong")
assert.Len(t, starts, 1, "starts count wrong")
assert.Len(t, howlongs, 1, "howlong count wrong")
assert.Len(t, starts[0], 1, "starts count wrong")
assert.Len(t, howlongs[0], 1, "howlong count wrong")
assert.Equal(t, now, starts[0][0], "start wrong")
assert.Equal(t, uint32(1), howlongs[0][0], "last wrong")
targets, allStarts, allLasts, err := db.GetActions()
assert.NoError(t, err, "get all actions wrong")
assert.Len(t, targets, 1, "targets count wrong")
assert.Len(t, allStarts, 1, "starts count wrong")
assert.Len(t, allLasts, 1, "lasts count wrong")
}
func testCheckEXP(t *testing.T, db *TDB) {
// delete all actions
var keys []string
for k := range db.action.content {
keys = append(keys, k)
}
for i := 0; i < len(keys); i++ {
delete(db.action.content, keys[i])
}
target := string(randBytes(6))
now := uint32(time.Now().Unix())
// add a should be expired action
err := db.AddAction(target, now-actionExp-2)
assert.NoError(t, err, "error add an action")
err = db.CheckExpirations()
assert.NoError(t, err, "error check expiration")
targets, allStarts, allLasts, err := db.GetActions()
assert.NoError(t, err, "get all actions wrong")
assert.Len(t, targets, 0, "targets count wrong")
assert.Len(t, allStarts, 0, "starts count wrong")
assert.Len(t, allLasts, 0, "lasts count wrong")
}
func testReloadEmptyInfo(t *testing.T, folder string) {
db, err := Open(folder)
if !assert.NoError(t, err, "should have no error") {
t.Fatal(err)
}
target := string(randBytes(6))
now := uint32(time.Now().Unix())
err = db.AddAction(target, now)
assert.NoError(t, err, "error add an action")
}