-
Notifications
You must be signed in to change notification settings - Fork 2
/
bolt_provider_test.go
215 lines (182 loc) · 5.46 KB
/
bolt_provider_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package boltdb_test
import (
"fmt"
"io/ioutil"
"os"
"sync"
"testing"
"github.com/AsynkronIT/protoactor-go/actor"
"github.com/AsynkronIT/protoactor-go/persistence"
boltdb "github.com/artyomturkin/protoactor-go-persistence-boltdb"
"github.com/boltdb/bolt"
)
const ActorName = "test.actor"
type dataStore struct {
providerState persistence.ProviderState
db *bolt.DB
tpath string
snapshotInterval int
}
func (p *dataStore) GetState() persistence.ProviderState {
return p.providerState
}
func (d *dataStore) Close() error {
defer os.Remove(d.tpath)
return d.db.Close()
}
// initData sets up a data store
// it adds one event to set state for every sting passed in
// set the last snapshot to given index of those events
func initData(t *testing.T, c int, snapshotInterval, lastSnapshot int, states ...string) *dataStore {
actorName := fmt.Sprintf("%s.%d", ActorName, c)
tpath := tempfile()
db, err := bolt.Open(tpath, 0666, nil)
if err != nil {
t.Fatalf("failed to open db: %v", err)
}
// add all events
state := boltdb.NewBoltProvider(snapshotInterval, db)
for i, s := range states {
state.PersistEvent(actorName, i, &Event{State: s})
t.Logf("case %d - init event %v", c, &Event{State: s})
}
// mark one as a snapshot
if lastSnapshot < len(states) {
snapshot := states[lastSnapshot]
state.PersistSnapshot(
actorName, lastSnapshot, &Snapshot{State: snapshot},
)
t.Logf("case %d - init snap %d %v", c, lastSnapshot, &Snapshot{State: snapshot})
}
return &dataStore{providerState: state, db: db, tpath: tpath, snapshotInterval: snapshotInterval}
}
type myActor struct {
persistence.Mixin
state string
t *testing.T
mod string
}
var _ actor.Actor = (*myActor)(nil)
func makeActor(t *testing.T) func() actor.Actor {
return func() actor.Actor {
return &myActor{t: t}
}
}
var queryWg sync.WaitGroup
var mod string
var queryState map[string]string = map[string]string{}
func (a *myActor) Receive(ctx actor.Context) {
switch msg := ctx.Message().(type) {
case *persistence.RequestSnapshot:
// PersistSnapshot when requested
a.PersistSnapshot(&Snapshot{State: a.state})
a.t.Logf("%s-%s pers %v", ctx.Self().Id, mod, &Snapshot{State: a.state})
case *Snapshot:
// Restore from Snapshot
a.state = msg.State
a.t.Logf("%s-%s snap %v", ctx.Self().Id, mod, msg)
case *Event:
// Persist all events received outside of recovery
if !a.Recovering() {
a.PersistReceive(msg)
a.t.Logf("%s-%s even %v", ctx.Self().Id, mod, msg)
} else {
a.t.Logf("%s-%s reco %v", ctx.Self().Id, mod, msg)
}
// Set state to whatever message says
a.state = msg.State
case *Query:
queryState[ctx.Self().Id] = a.state
queryWg.Done()
}
}
type Query struct{}
func TestBoltProvider(t *testing.T) {
cases := []struct {
init *dataStore
msgs []string
afterMsgs string
}{
// replay with no state
0: {initData(t, 0, 5, 0), nil, ""},
// replay directly on snapshot, no more messages
1: {initData(t, 1, 8, 2, "a", "b", "c"), nil, "c"},
// replay with snapshot and events, add another event
2: {initData(t, 2, 8, 1, "a", "b", "c"), []string{"d"}, "d"},
// replay state and add an event, which triggers snapshot
3: {initData(t, 3, 4, 1, "a", "b", "c"), []string{"d"}, "d"},
// replay state and add an event, which triggers snapshot,
// and then another one
4: {initData(t, 4, 4, 1, "a", "b", "c"), []string{"d", "e"}, "e"},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("case-%d", i), func(t *testing.T) {
actorName := fmt.Sprintf("%s.%d", ActorName, i)
props := actor.FromProducer(makeActor(t)).
WithMiddleware(persistence.Using(tc.init))
mod = "orig"
pid, err := actor.SpawnNamed(props, actorName)
noError(t, "spawn actor", err)
// send a bunch of messages
for _, msg := range tc.msgs {
pid.Tell(&Event{State: msg})
}
queryWg.Add(1)
pid.Tell(&Query{})
queryWg.Wait()
// check the state after all these messages
equal(t, "spawned actor last message", tc.afterMsgs, queryState[actorName])
// wait for shutdown
pid.GracefulPoison()
//close bolt db, but don't delete it
tc.init.db.Close()
//respawn actor with new bolt db provider and db instance
mod = "resp"
db, err := bolt.Open(tc.init.tpath, 0666, nil)
if err != nil {
t.Fatalf("failed to open db: %v", err)
}
// add all events
state := boltdb.NewBoltProvider(tc.init.snapshotInterval, db)
ds := &dataStore{providerState: state, db: db, tpath: tc.init.tpath}
props = actor.FromProducer(makeActor(t)).
WithMiddleware(persistence.Using(ds))
if err != nil {
t.Fatalf("failed to reopen db: %v", err)
}
pid, err = actor.SpawnNamed(props, actorName)
noError(t, "respawn actor", err)
queryWg.Add(1)
pid.Tell(&Query{})
queryWg.Wait()
// check the state after all these messages
equal(t, "respawned actor last message", tc.afterMsgs, queryState[actorName])
// shutdown at end of test for cleanup
pid.GracefulPoison()
tc.init.Close()
})
}
}
func equal(t *testing.T, name string, expected, actual interface{}) {
if expected != actual {
t.Errorf("%s expected: %v, instead got: %v", name, expected, actual)
}
}
func noError(t *testing.T, name string, err error) {
if err != nil {
t.Fatalf("%s failed with error: %v", name, err)
}
}
func tempfile() string {
f, err := ioutil.TempFile("", "bolt-")
if err != nil {
panic(err)
}
if err := f.Close(); err != nil {
panic(err)
}
if err := os.Remove(f.Name()); err != nil {
panic(err)
}
return f.Name()
}