MCPcopy Index your code
hub / github.com/koding/kite / TestKite

Function TestKite

kite_test.go:381–488  ·  view source on GitHub ↗

Test 2 way communication between kites.

(t *testing.T)

Source from the content-addressed store, hash-verified

379
380// Test 2 way communication between kites.
381func TestKite(t *testing.T) {
382 // Create a mathworker kite
383 mathKite := newXhrKite("mathworker", "0.0.1")
384 mathKite.Config.DisableAuthentication = true
385 mathKite.Config.Port = 3636
386 mathKite.HandleFunc("square", Square)
387 mathKite.HandleFunc("squareCB", SquareCB)
388 mathKite.HandleFunc("sleep", Sleep)
389 mathKite.HandleFunc("sqrt", Sqrt)
390 mathKite.FinalFunc(func(r *Request, resp interface{}, err error) (interface{}, error) {
391 if r.Method != "sqrt" || err != ErrNegative {
392 return resp, err
393 }
394
395 a := r.Args.One().MustFloat64()
396
397 // JSON does not marshal complex128,
398 // for test purpose we use just string
399 return fmt.Sprintf("%di", int(math.Sqrt(-a)+0.5)), nil
400 })
401 go mathKite.Run()
402 <-mathKite.ServerReadyNotify()
403 defer mathKite.Close()
404
405 // Create exp2 kite
406 exp2Kite := newXhrKite("exp2", "0.0.1")
407 fooChan := make(chan string)
408 exp2Kite.HandleFunc("foo", func(r *Request) (interface{}, error) {
409 s := r.Args.One().MustString()
410 t.Logf("Message received: %s\n", s)
411 fooChan <- s
412 return nil, nil
413 })
414
415 // exp2 connects to mathworker
416 remote := exp2Kite.NewClient("http://127.0.0.1:3636/kite")
417 err := remote.Dial()
418 if err != nil {
419 t.Fatal(err)
420 }
421 defer remote.Close()
422
423 result, err := remote.TellWithTimeout("sqrt", 4*time.Second, -4)
424 if err != nil {
425 t.Fatal(err)
426 }
427
428 if s, err := result.String(); err != nil || s != "2i" {
429 t.Fatalf("want 2i, got %v (%v)", result, err)
430 }
431
432 result, err = remote.TellWithTimeout("square", 4*time.Second, 2)
433 if err != nil {
434 t.Fatal(err)
435 }
436
437 number := result.MustFloat64()
438

Callers

nothing calls this directly

Calls 15

DialMethod · 0.95
CloseMethod · 0.95
TellWithTimeoutMethod · 0.95
TellMethod · 0.95
newXhrKiteFunction · 0.85
HandleFuncMethod · 0.80
MustFloat64Method · 0.80
OneMethod · 0.80
ServerReadyNotifyMethod · 0.80
MustStringMethod · 0.80
NewClientMethod · 0.80
FatalMethod · 0.80

Tested by

no test coverage detected