forked from AlexMarco7/aclow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaclow-test.go
80 lines (66 loc) · 1.64 KB
/
aclow-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
package aclow
import (
"sync"
"testing"
)
type Tester struct {
app *App
address string
module string
assertFunc func(Message, error)
mocks map[string]bool
calledMocks map[string]bool
}
var lock = sync.RWMutex{}
func (t *Tester) Test(module string, node Node) {
t.app = &App{}
t.app.Start(StartOptions{
Local: true,
})
t.address = node.Address()[0]
t.module = module
t.mocks = map[string]bool{}
t.calledMocks = map[string]bool{}
t.app.RegisterModule(module, []Node{node})
}
func (t *Tester) Mock(module string, address string, mock func(Message) (Message, error)) {
lock.Lock()
defer lock.Unlock()
t.mocks[module+"@"+address] = true
t.app.RegisterModule(module, []Node{&MockNode{
Tester: t,
MockedModule: module,
MockedAddress: address,
Mock: mock,
}})
}
func (t *Tester) Run(msg Message, testing *testing.T) {
result, err := t.app.Call(t.module+"@"+t.address, msg)
lock.RLock()
defer lock.RUnlock()
for k := range t.mocks {
if !t.calledMocks[k] {
testing.Errorf("%s wasn't called!", k)
}
}
if t.assertFunc != nil {
t.assertFunc(result, err)
}
}
func (t *Tester) Assert(assertFunc func(Message, error)) {
t.assertFunc = assertFunc
}
type MockNode struct {
Tester *Tester
MockedModule string
MockedAddress string
Mock func(Message) (Message, error)
}
func (m *MockNode) Address() []string { return []string{m.MockedAddress} }
func (m *MockNode) Start(app *App) {}
func (m *MockNode) Execute(msg Message, call Caller) (Message, error) {
lock.Lock()
defer lock.Unlock()
m.Tester.calledMocks[m.MockedModule+"@"+m.MockedAddress] = true
return m.Mock(msg)
}