forked from fatih/kite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkite_test.go
279 lines (221 loc) · 6.2 KB
/
kite_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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package kite
import (
"fmt"
"math/rand"
"net/http"
"os"
"strconv"
"sync"
"testing"
"time"
"github.com/koding/kite/config"
"github.com/koding/kite/dnode"
_ "github.com/koding/kite/testutil"
)
func TestMultiple(t *testing.T) {
testDuration := time.Second * 10
// number of available mathworker kites to be called
kiteNumber := 10
// number of exp kites that will call mathwork kites
clientNumber := 10
// ports are starting from 6000 up to 6000 + kiteNumber
port := 6000
fmt.Printf("Creating %d mathworker kites\n", kiteNumber)
var transport config.Transport
if transportName := os.Getenv("KITE_TRANSPORT"); transportName != "" {
tr, ok := config.Transports[transportName]
if !ok {
t.Fatalf("transport '%s' doesn't exists", transportName)
}
transport = tr
}
for i := 0; i < kiteNumber; i++ {
m := New("mathworker"+strconv.Itoa(i), "0.1."+strconv.Itoa(i))
m.Config.DisableAuthentication = true
m.Config.Transport = transport
m.HandleFunc("square", Square)
go http.ListenAndServe("127.0.0.1:"+strconv.Itoa(port+i), m)
}
// Wait until it's started
time.Sleep(time.Second * 2)
fmt.Printf("Creating %d exp clients\n", clientNumber)
clients := make([]*Client, clientNumber)
for i := 0; i < clientNumber; i++ {
cn := New("exp"+strconv.Itoa(i), "0.0.1")
cn.Config.Transport = transport
c := cn.NewClient("http://127.0.0.1:" + strconv.Itoa(port+i) + "/kite")
if err := c.Dial(); err != nil {
t.Fatal(err)
}
clients[i] = c
}
var wg sync.WaitGroup
fmt.Printf("Calling mathworker kites with %d conccurent clients randomly\n", clientNumber)
timeout := time.After(testDuration)
// every one second
for {
select {
case <-time.Tick(time.Second):
for i := 0; i < clientNumber; i++ {
wg.Add(1)
go func(i int, t *testing.T) {
defer wg.Done()
time.Sleep(time.Millisecond * time.Duration(rand.Intn(500)))
_, err := clients[i].TellWithTimeout("square", 4*time.Second, 2)
if err != nil {
t.Error(err)
}
}(i, t)
}
case <-timeout:
fmt.Println("test stopped")
t.SkipNow()
}
}
wg.Wait()
}
// Call a single method with multiple clients. This test is implemented to be
// sure the method is calling back with in the same time and not timing out.
func TestConcurrency(t *testing.T) {
// Create a mathworker kite
mathKite := newXhrKite("mathworker", "0.0.1")
mathKite.Config.DisableAuthentication = true
mathKite.HandleFunc("ping", func(r *Request) (interface{}, error) {
time.Sleep(time.Second)
return "pong", nil
})
go http.ListenAndServe("127.0.0.1:3637", mathKite)
// Wait until it's started
time.Sleep(time.Second)
// number of exp kites that will call mathworker kite
clientNumber := 30
fmt.Printf("Creating %d exp clients\n", clientNumber)
clients := make([]*Client, clientNumber)
for i := 0; i < clientNumber; i++ {
c := newXhrKite("exp", "0.0.1").NewClient("http://127.0.0.1:3637/kite")
if err := c.Dial(); err != nil {
t.Fatal(err)
}
clients[i] = c
}
var wg sync.WaitGroup
for i := range clients {
wg.Add(1)
go func(i int) {
defer wg.Done()
result, err := clients[i].TellWithTimeout("ping", 4*time.Second)
if err != nil {
t.Fatal(err)
}
if result.MustString() != "pong" {
t.Errorf("Got %s want: pong", result.MustString())
}
}(i)
}
wg.Wait()
}
// Test 2 way communication between kites.
func TestKite(t *testing.T) {
// Create a mathworker kite
mathKite := newXhrKite("mathworker", "0.0.1")
mathKite.Config.DisableAuthentication = true
mathKite.HandleFunc("square", Square)
mathKite.HandleFunc("squareCB", SquareCB)
mathKite.HandleFunc("sleep", Sleep)
go http.ListenAndServe("127.0.0.1:3636", mathKite)
// Wait until it's started
time.Sleep(time.Second)
// Create exp2 kite
exp2Kite := newXhrKite("exp2", "0.0.1")
fooChan := make(chan string)
exp2Kite.HandleFunc("foo", func(r *Request) (interface{}, error) {
s := r.Args.One().MustString()
t.Logf("Message received: %s\n", s)
fooChan <- s
return nil, nil
})
// exp2 connects to mathworker
remote := exp2Kite.NewClient("http://127.0.0.1:3636/kite")
err := remote.Dial()
if err != nil {
t.Fatal(err)
}
result, err := remote.TellWithTimeout("square", 4*time.Second, 2)
if err != nil {
t.Fatal(err)
}
number := result.MustFloat64()
t.Logf("rpc result: %f\n", number)
if number != 4 {
t.Fatalf("Invalid result: %f", number)
}
select {
case s := <-fooChan:
if s != "bar" {
t.Fatalf("Invalid message: %s", s)
}
case <-time.After(100 * time.Millisecond):
t.Fatal("Did not get the message")
}
resultChan := make(chan float64, 1)
resultCallback := func(args *dnode.Partial) {
n := args.One().MustFloat64()
resultChan <- n
}
result, err = remote.TellWithTimeout("squareCB", 4*time.Second, 3, dnode.Callback(resultCallback))
if err != nil {
t.Fatal(err)
}
select {
case n := <-resultChan:
if n != 9.0 {
t.Fatalf("Unexpected result: %f", n)
}
case <-time.After(100 * time.Millisecond):
t.Fatal("Did not get the message")
}
result, err = remote.TellWithTimeout("sleep", time.Second)
if err == nil {
t.Fatal("Did get message in 1 seconds, however the sleep method takes 2 seconds to response")
}
result, err = remote.Tell("sleep")
if err != nil {
t.Fatal(err)
}
if !result.MustBool() {
t.Fatal("sleep result must be true")
}
}
// Sleeps for 2 seconds and returns true
func Sleep(r *Request) (interface{}, error) {
time.Sleep(time.Second * 2)
return true, nil
}
// Returns the result. Also tests reverse call.
func Square(r *Request) (interface{}, error) {
a := r.Args.One().MustFloat64()
result := a * a
r.LocalKite.Log.Info("Kite call, sending result '%f' back\n", result)
// Reverse method call
r.Client.Go("foo", "bar")
return result, nil
}
// Calls the callback with the result. For testing requests with Callback.
func SquareCB(r *Request) (interface{}, error) {
args := r.Args.MustSliceOfLength(2)
a := args[0].MustFloat64()
cb := args[1].MustFunction()
result := a * a
r.LocalKite.Log.Info("Kite call, sending result '%f' back\n", result)
// Send the result.
err := cb.Call(result)
if err != nil {
return nil, err
}
return nil, nil
}
func newXhrKite(name, version string) *Kite {
k := New(name, version)
k.Config.Transport = config.XHRPolling
return k
}