-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestBlocks_test.go
165 lines (137 loc) · 3.4 KB
/
testBlocks_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
package sputnik_test
import (
"time"
"github.com/g41797/kissngoqueue"
"github.com/g41797/sputnik"
)
// Test helper:
type testBlocks struct {
// All blocks
dbl []*dumbBlock
// Test queue
q *kissngoqueue.Queue[sputnik.Msg]
// Launcher
launch sputnik.Launch
// ShootDown
kill sputnik.ShootDown
// Signalling channel
done chan struct{}
// ServerConnector
conntr sputnik.DummyConnector
to time.Duration
}
func NewTestBlocks() *testBlocks {
tb := new(testBlocks)
tb.q = kissngoqueue.NewQueue[sputnik.Msg]()
tb.conntr = sputnik.DummyConnector{}
tb.to = time.Millisecond * 100
return tb
}
// Expectation:
// - get n messages from blocks
// - with "__name" == <name>
func (tb *testBlocks) expect(n int, name string) bool {
for i := 0; i < n; i++ {
msg, ok := tb.q.Get()
if !ok {
return false
}
mn, exists := msg["__name"]
if !exists {
return false
}
mname, ok := mn.(string)
if !ok {
return false
}
if mname != name {
return false
}
}
return true
}
// Send msg to block using it's responsibility
// Use this pattern in real application for
// negotiation between blocks
func (tb *testBlocks) sendTo(resp string, msg sputnik.Msg) bool {
cn := tb.dbl[0].communicator
bc, exists := cn.Communicator(resp)
if !exists {
return false
}
sok := bc.Send(msg)
return sok
}
func (tb *testBlocks) mainCntrl() sputnik.BlockCommunicator {
mcn, _ := tb.dbl[0].communicator.Communicator(sputnik.InitiatorResponsibility)
return mcn
}
// Run Launcher on dedicated goroutine
// Test controls execution via sputnik API
// Results received using queue
func (tb *testBlocks) run() {
tb.done = make(chan struct{})
go func(l sputnik.Launch, done chan struct{}) {
defer close(done)
l()
}(tb.launch, tb.done)
return
}
// Registration of factories for test environment
// For this case init() isn't used
// use this pattern for the case when you don't need
// dynamic registration: all blocks (and factories) are
// known in advance.
func (tb *testBlocks) factories() sputnik.BlockFactories {
res := make(sputnik.BlockFactories)
finfct, _ := sputnik.Factory(sputnik.DefaultFinisherName)
confct, _ := sputnik.Factory(sputnik.DefaultConnectorName)
factList := []struct {
name string
fact sputnik.BlockFactory
}{
{"dumb", tb.dbFact},
{sputnik.DefaultFinisherName, finfct},
{sputnik.DefaultConnectorName, confct},
}
for _, fd := range factList {
sputnik.RegisterBlockFactoryInner(fd.name, fd.fact, res)
}
return res
}
func (tb *testBlocks) attachQueue() {
for i, _ := range tb.dbl {
tb.dbl[i].q = tb.q
}
return
}
// Block factory:
func (tb *testBlocks) dbFact() *sputnik.Block {
dmb := new(dumbBlock)
tb.dbl = append(tb.dbl, dmb)
return sputnik.NewBlock(
sputnik.WithInit(dmb.init),
sputnik.WithRun(dmb.run),
sputnik.WithFinish(dmb.finish),
sputnik.WithOnMsg(dmb.eventReceived),
sputnik.WithOnConnect(dmb.serverConnected),
sputnik.WithOnDisconnect(dmb.serverDisconnected),
)
}
// Satellite has 3 app. blocks:
var blkList []sputnik.BlockDescriptor = []sputnik.BlockDescriptor{
{"dumb", "1"},
{"dumb", "2"},
{"dumb", "3"},
}
// Configuration factory:
func dumbConf(confName string, result any) error { return nil }
func dumbSputnik(tb *testBlocks) sputnik.Sputnik {
sp, _ := sputnik.NewSputnik(
sputnik.WithConfFactory(dumbConf),
sputnik.WithAppBlocks(blkList),
sputnik.WithBlockFactories(tb.factories()),
sputnik.WithConnector(&tb.conntr, tb.to),
)
return *sp
}