-
Notifications
You must be signed in to change notification settings - Fork 29
/
local_test.go
192 lines (165 loc) · 4.28 KB
/
local_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
package onet
import (
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.dedis.ch/kyber/v3/suites"
"go.dedis.ch/onet/v3/log"
"go.dedis.ch/onet/v3/network"
)
var tSuite = suites.MustFind("Ed25519")
const clientServiceName = "ClientService"
var clientServiceID ServiceID
func init() {
var err error
clientServiceID, err = RegisterNewService(clientServiceName, newClientService)
log.ErrFatal(err)
}
func Test_panicClose(t *testing.T) {
l := NewLocalTest(tSuite)
l.CloseAll()
require.Panics(t, func() { l.genLocalHosts(2) })
}
func Test_showPanic(t *testing.T) {
l := NewLocalTest(tSuite)
c := make(chan bool)
go func() {
<-c
}()
defer func() {
require.NotNil(t, recover())
c <- true
}()
defer l.CloseAll()
panic("this should be caught")
}
func Test_showFail(t *testing.T) {
t.Skip("I have no idea how I can have this test passing... It tests that CloseAll doesn't test goroutines when a test fails.")
l := NewLocalTest(tSuite)
c := make(chan bool)
go func() {
<-c
}()
defer l.CloseAll()
defer func() {
if !t.Failed() {
t.Fail()
}
c <- true
}()
l.T = t
require.Nil(t, "not nil")
}
func TestGenLocalHost(t *testing.T) {
l := NewLocalTest(tSuite)
hosts := l.genLocalHosts(2)
defer l.CloseAll()
log.Lvl4("Hosts are:", hosts[0].Address(), hosts[1].Address())
if hosts[0].Address() == hosts[1].Address() {
t.Fatal("Both addresses are equal")
}
}
func TestGenLocalHostAfter(t *testing.T) {
l := NewLocalTest(tSuite)
defer l.CloseAll()
hosts := l.genLocalHosts(2)
hosts2 := l.genLocalHosts(2)
require.NotEqual(t, hosts2[0].Address(), hosts[0].Address())
}
// This tests the client-connection in the case of a non-garbage-collected
// client that stays in the service.
func TestNewTCPTest(t *testing.T) {
l := NewTCPTest(tSuite)
_, el, _ := l.GenTree(3, true)
defer l.CloseAll()
c1 := NewClient(tSuite, clientServiceName)
err := c1.SendProtobuf(el.List[0], &SimpleMessage{}, nil)
log.ErrFatal(err)
}
func TestLocalTCPGenConnectableRoster(t *testing.T) {
l := NewTCPTest(tSuite)
defer l.CloseAll()
servers := l.GenServers(3)
roster := *l.GenRosterFromHost(servers...)
for _, serverIdent := range roster.List {
got, err := http.Get(serverIdent.URL)
require.NoError(t, err)
got.Body.Close()
}
}
// Tests whether TestClose is called in the service.
func TestTestClose(t *testing.T) {
l := NewTCPTest(tSuite)
servers, _, _ := l.GenTree(1, true)
services := l.GetServices(servers, clientServiceID)
pingpong := make(chan bool, 1)
go func() {
pingpong <- true
for _, s := range services {
<-s.(*clientService).closed
}
pingpong <- true
}()
// Wait for the go-routine to be started
<-pingpong
l.CloseAll()
// Wait for all services to be clsoed
<-pingpong
}
func TestWaitDone(t *testing.T) {
l := NewTCPTest(tSuite)
servers, ro, _ := l.GenTree(1, true)
defer l.CloseAll()
services := l.GetServices(servers, clientServiceID)
service := services[0].(*clientService)
require.Nil(t, service.SendRaw(ro.List[0], &RawMessage{}))
<-service.click
select {
case <-service.click:
log.Fatal("service is already done")
default:
}
require.Nil(t, l.WaitDone(5*time.Second))
select {
case <-service.click:
default:
log.Fatal("service should be done by now")
}
}
type clientService struct {
*ServiceProcessor
cl *Client
click chan bool
closed chan bool
}
type SimpleMessage2 struct{}
type RawMessage struct{}
func (c *clientService) SimpleMessage(msg *SimpleMessage) (network.Message, error) {
log.Lvl3("Got request", msg)
c.cl.SendProtobuf(c.ServerIdentity(), &SimpleMessage2{}, nil)
return nil, nil
}
func (c *clientService) SimpleMessage2(msg *SimpleMessage2) (network.Message, error) {
log.Lvl3("Got request", msg)
return nil, nil
}
func (c *clientService) TestClose() {
c.closed <- true
}
func newClientService(c *Context) (Service, error) {
s := &clientService{
ServiceProcessor: NewServiceProcessor(c),
cl: NewClient(c.server.Suite(), clientServiceName),
click: make(chan bool, 1),
closed: make(chan bool, 1),
}
log.ErrFatal(s.RegisterHandlers(s.SimpleMessage, s.SimpleMessage2))
s.RegisterProcessorFunc(network.RegisterMessage(RawMessage{}), func(arg1 *network.Envelope) error {
s.click <- true
time.Sleep(100 * time.Millisecond)
s.click <- true
return nil
})
return s, nil
}