-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_test.go
91 lines (73 loc) · 1.79 KB
/
helper_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
package syslog2nats
import (
"os"
"testing"
"time"
"github.com/g41797/sputnik"
"github.com/g41797/sputnik/sidecar"
"github.com/nats-io/nats-server/v2/server"
)
func shutdownJSServerAndRemoveStorage(t *testing.T, s *server.Server) {
t.Helper()
var sd string
if config := s.JetStreamConfig(); config != nil {
sd = config.StoreDir
}
s.Shutdown()
if sd != "" {
if err := os.RemoveAll(sd); err != nil {
t.Fatalf("Unable to remove storage %q: %v", sd, err)
}
}
s.WaitForShutdown()
}
func ConfFact() sputnik.ConfFactory {
return sidecar.ConfigFactory(CONFPATH)
}
// connector always returns Shared connection
// for usage this connection with tests we need more flexible approach
func NewServerConnection(Shared bool) sputnik.ServerConnection {
cntr := NewConnector()
scn, err := cntr.Connect(ConfFact())
if err != nil {
return nil
}
nonshared := scn.(*NatsConnection)
nonshared.Shared = Shared
return nonshared
}
func CloseServerConnection(sconn sputnik.ServerConnection) {
if sconn == nil {
return
}
sc := sconn.(*NatsConnection)
if sc.NatsConn != nil {
sc.NatsConn.Close()
}
return
}
var _ sputnik.BlockCommunicator = &dumbCommunicator{}
type dumbCommunicator struct {
msgs chan sputnik.Msg
}
func newCommunicator(maxMsgs int) *dumbCommunicator {
return &dumbCommunicator{msgs: make(chan sputnik.Msg, maxMsgs+2)}
}
func (c *dumbCommunicator) Communicator(resp string) (bc sputnik.BlockCommunicator, exists bool) {
return nil, false
}
func (c *dumbCommunicator) Descriptor() sputnik.BlockDescriptor {
return sputnik.BlockDescriptor{}
}
func (c *dumbCommunicator) Send(msg sputnik.Msg) bool {
c.msgs <- msg
return true
}
func (c *dumbCommunicator) Recv(to time.Duration) sputnik.Msg {
select {
case msg := <-c.msgs:
return msg
case <-time.After(to):
return nil
}
}