This repository has been archived by the owner on Dec 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
endpoint_test.go
181 lines (165 loc) · 5.43 KB
/
endpoint_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
// Copyright (C) 2015 Nippon Telegraph and Telephone Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package endpoint
import (
"flag"
"fmt"
log "github.com/cihub/seelog"
"github.com/osrg/earthquake/earthquake/endpoint/rest"
"github.com/osrg/earthquake/earthquake/inspector/transceiver"
"github.com/osrg/earthquake/earthquake/signal"
"github.com/osrg/earthquake/earthquake/util/config"
logutil "github.com/osrg/earthquake/earthquake/util/log"
"github.com/osrg/earthquake/earthquake/util/mockorchestrator"
restutil "github.com/osrg/earthquake/earthquake/util/rest"
testutil "github.com/osrg/earthquake/earthquake/util/test"
"github.com/stretchr/testify/assert"
"os"
"sync"
"testing"
"time"
)
var (
localTransceiver transceiver.Transceiver
restTransceivers []transceiver.Transceiver
)
const (
maxRESTEntities = 4
)
func TestMain(m *testing.M) {
flag.Parse()
logutil.InitLog("", true)
signal.RegisterKnownSignals()
// Instantiate the orchestrator
cfg, err := config.NewFromString("{\"restPort\":0}", "json")
if err != nil {
panic(err)
}
actionCh := make(chan signal.Action)
eventCh := StartAll(actionCh, cfg)
mockOrc := mockorchestrator.NewMockOrchestrator(eventCh, actionCh)
mockOrc.Start()
defer mockOrc.Shutdown()
// Instantiate the local transceiver
localTransceiver, err = transceiver.NewTransceiver("local://", "")
if err != nil {
panic(err)
}
localTransceiver.Start()
// Pre-allocate REST transceivers for maxEntities.
// This pre-allocation is needed because there is currently no way to
// "unregister" a transceiver entity in REST.
// FIXME: transceivers should be a local variable.
restTransceivers = make([]transceiver.Transceiver, maxRESTEntities)
url := fmt.Sprintf("http://127.0.0.1:%d%s", rest.ActualPort, restutil.APIRoot)
for i := 0; i < maxRESTEntities; i++ {
var err error
entityID := fmt.Sprintf("restentity-%d", i)
restTransceivers[i], err = transceiver.NewTransceiver(url,
entityID)
if err != nil {
panic(err)
}
restTransceivers[i].Start()
}
// we need to wait here.
// otherwise the test can hang due to an error from restendpoint.go:
// "Ignored action for unknown entity %s. You sent the action before registration done?"
// FIXME: there should be some notification
log.Debugf("FIXME: sleeping for 10 seconds, but we should not sleep here..")
time.Sleep(10 * time.Second)
os.Exit(m.Run())
}
func TestEndpointWithPacketEvent_1_1(t *testing.T) {
testEndpointWithPacketEvent(t, 1, 1)
}
func TestEndpointWithPacketEvent_2_2(t *testing.T) {
testEndpointWithPacketEvent(t, 2, 2)
}
func TestEndpointWithPacketEvent_10_2(t *testing.T) {
testEndpointWithPacketEvent(t, 10, 2)
}
func TestEndpointWithPacketEvent_256_4(t *testing.T) {
testEndpointWithPacketEvent(t, 256, 4)
}
func testEndpointWithPacketEvent(t *testing.T, n, entities int) {
assert.True(t, entities <= maxRESTEntities)
var wg sync.WaitGroup
wg.Add(2 * n)
go func() {
for i := 0; i < n; i++ {
var entityID string
var trans transceiver.Transceiver
entityNum := i % entities
if entityNum%2 == 0 {
t.Logf("Test %d: Sending (using local trans)", i)
entityID = fmt.Sprintf("localentity-%d", entityNum)
trans = localTransceiver
} else {
t.Logf("Test %d: Sending (using REST trans)", i)
entityID = fmt.Sprintf("restentity-%d", entityNum)
trans = restTransceivers[entityNum]
}
event := testutil.NewPacketEvent(t, entityID, i)
actionCh, err := trans.SendEvent(event)
t.Logf("Test %d: Sent %s", i, event)
assert.NoError(t, err)
wg.Done()
go func(i int) {
t.Logf("Test %d: Receiving", i)
action := <-actionCh
restoredEvent := action.Event()
t.Logf("Test %d: Received action %s (event: %s)", i, action, restoredEvent)
assert.Equal(t, event.EntityID(), restoredEvent.EntityID())
wg.Done()
}(i)
}
}()
wg.Wait()
// TODO: clean up transceivers
}
func TestEndpointShouldNotBlockWithPacketEvent_10_2(t *testing.T) {
testEndpointShouldNotBlockWithPacketEvent(t, 10, 2)
}
func testEndpointShouldNotBlockWithPacketEvent(t *testing.T, n, entities int) {
assert.True(t, entities <= maxRESTEntities)
actionChs := make(map[int]chan signal.Action)
for i := 0; i < n; i++ {
var entityID string
var trans transceiver.Transceiver
entityNum := i % entities
if entityNum%2 == 0 {
t.Logf("Test %d: Sending (using local trans)", i)
entityID = fmt.Sprintf("localentity-%d", entityNum)
trans = localTransceiver
} else {
t.Logf("Test %d: Sending (using REST trans)", i)
entityID = fmt.Sprintf("restentity-%d", entityNum)
trans = restTransceivers[entityNum]
}
event := testutil.NewPacketEvent(t, entityID, i)
actionCh, err := trans.SendEvent(event)
t.Logf("Test %d: Sent %s", i, event)
assert.NoError(t, err)
actionChs[i] = actionCh
}
for i := 0; i < n; i++ {
actionCh := actionChs[i]
t.Logf("Test %d: Receiving", i)
action := <-actionCh
restoredEvent := action.Event()
t.Logf("Test %d: Received action %s (event: %s)", i, action, restoredEvent)
}
}