forked from gree/futurama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store_test.go
126 lines (102 loc) · 2.95 KB
/
store_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
package futurama
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
var evId string = ""
func TestStore_Save(t *testing.T) {
cfg := DefaultConfig()
TestOnly_ResetDb(&cfg.MySQLConfig)
store := NewMySQLStore(cfg)
store.Open()
defer store.Close()
triggerTime := time.Now()
data := map[string]interface{}{
"a": 1,
"b": "text",
}
ev := NewEvent(Test_TriggerType_Default, triggerTime, data)
evId = store.Save(ev)
assert := assert.New(t)
assert.NotEmpty(evId)
evList := TestOnly_SelectEvents(&cfg.MySQLConfig)
assert.Len(evList, 1)
gotEv := evList[0]
assert.Equal(gotEv.Id, evId)
assert.Equal(gotEv.TriggerType, Test_TriggerType_Default)
assert.WithinDuration(gotEv.TriggerTime, triggerTime, 50*time.Millisecond)
assert.Equal(gotEv.Attempts, 0)
assert.Equal(int(gotEv.Status), EventStatus_DEFAULT)
assert.Equal(gotEv.Owner, "")
}
func TestStore_Cancel(t *testing.T) {
TestStore_Save(t)
assert := assert.New(t)
assert.NotEmpty(evId)
cfg := DefaultConfig()
store := NewMySQLStore(cfg)
store.Open()
defer store.Close()
err := store.Cancel(evId)
assert.Empty(err)
evList := TestOnly_SelectEvents(&cfg.MySQLConfig)
assert.Len(evList, 1)
gotEv := evList[0]
assert.Equal(gotEv.Id, evId)
assert.Equal(gotEv.TriggerType, Test_TriggerType_Default)
assert.Equal(gotEv.Attempts, 0)
assert.Equal(int(gotEv.Status), EventStatus_CANCEL)
assert.Equal(gotEv.Owner, "")
}
func TestStore_UpdateStatus_Other(t *testing.T) {
for i := EventStatus_DEFAULT; i <= EventStatus_RETRY; i++ {
TestStore_Save(t)
assert := assert.New(t)
assert.NotEmpty(evId)
cfg := DefaultConfig()
store := NewMySQLStore(cfg)
store.Open()
defer store.Close()
err := store.UpdateStatus(evId, EventStatus(i))
assert.Empty(err)
evList := TestOnly_SelectEvents(&cfg.MySQLConfig)
assert.Len(evList, 0)
}
}
func TestStore_UpdateForRetry(t *testing.T) {
TestStore_Save(t)
assert := assert.New(t)
assert.NotEmpty(evId)
cfg := DefaultConfig()
store := NewMySQLStore(cfg)
store.Open()
defer store.Close()
sql := fmt.Sprintf(`UPDATE %s SET owner=? where id=?`, cfg.TableName)
if _, err := store.db.Exec(sql, cfg.ConsumerName, evId); err != nil {
assert.Fail(err.Error())
return
}
evList := TestOnly_SelectEvents(&cfg.MySQLConfig)
assert.Len(evList, 1)
assert.Equal(evList[0].Id, evId)
assert.Equal(evList[0].Owner, cfg.ConsumerName)
triggerTime := time.Now().Add(3 * time.Second)
ev := NewEvent(Test_TriggerType_Default, triggerTime, nil)
ev.Attempts = 10
ev.Id = evId
if err := store.UpdateForRetry(ev, nil); err != nil {
assert.Fail(err.Error())
return
}
evList = TestOnly_SelectEvents(&cfg.MySQLConfig)
assert.Len(evList, 1)
gotEv := evList[0]
assert.Equal(gotEv.Id, evId)
assert.Equal(gotEv.TriggerType, Test_TriggerType_Default)
assert.WithinDuration(gotEv.TriggerTime, triggerTime, 50*time.Millisecond)
assert.Equal(gotEv.Attempts, ev.Attempts)
assert.Equal(int(gotEv.Status), EventStatus_DEFAULT)
assert.Equal(gotEv.Owner, "")
}