-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdumbBlock_test.go
96 lines (81 loc) · 1.75 KB
/
dumbBlock_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
package sputnik_test
import (
"github.com/g41797/kissngoqueue"
"github.com/g41797/sputnik"
)
// Satellite block
type dumbBlock struct {
// Block communicator
communicator sputnik.BlockCommunicator
// Main queue of test
q *kissngoqueue.Queue[sputnik.Msg]
// Used for synchronization
// between finish and run
// This pattern may be used in real application
stop chan struct{}
done chan struct{}
}
// dumbBlock support all callbacks of Block:
//
// Init
func (dmb *dumbBlock) init(_ sputnik.ConfFactory) error {
dmb.stop = make(chan struct{}, 1)
return nil
}
// Run:
func (dmb *dumbBlock) run(bc sputnik.BlockCommunicator) {
// Save for further communication with blocks
dmb.communicator = bc
dmb.done = make(chan struct{})
defer close(dmb.done)
// select isn't required for one channel
// in real application you can add "listening"
// on another channels here e.g. timeouts or
// redirected OnMsg|OnConnect|etc
select {
case <-dmb.stop:
break
}
return
}
// Finish:
func (dmb *dumbBlock) finish(init bool) {
close(dmb.stop) // Cancel Run
if init {
return
}
select {
case <-dmb.done: // Wait finish of Run
break
}
return
}
// OnServerConnect:
func (dmb *dumbBlock) serverConnected(connection sputnik.ServerConnection) {
//Inform test about event
m := make(sputnik.Msg)
m["__name"] = "serverConnected"
dmb.send(m)
return
}
// OnServerDisconnect:
func (dmb *dumbBlock) serverDisconnected() {
//Inform test about event
m := make(sputnik.Msg)
m["__name"] = "serverDisconnected"
dmb.send(m)
return
}
// OnMsg:
func (dmb *dumbBlock) eventReceived(msg sputnik.Msg) {
//Inform test about event
dmb.send(msg)
return
}
func (dmb *dumbBlock) send(msg sputnik.Msg) {
if dmb.q != nil {
//Send message to test
dmb.q.PutMT(msg)
}
return
}