-
Notifications
You must be signed in to change notification settings - Fork 125
/
runtime_test.go
76 lines (69 loc) · 1.61 KB
/
runtime_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
// +build ignore
package goflow
import (
"encoding/json"
"testing"
"github.com/gorilla/websocket"
)
var (
r *Runtime
started bool
)
func ensureRuntimeStarted() {
if !started {
r = new(Runtime)
r.Init("goflow")
go r.Listen("localhost:13014")
started = true
<-r.Ready()
}
}
func sendJSONE(ws *websocket.Conn, msg interface{}) error {
bytes, err := json.Marshal(msg)
if err != nil {
return err
}
err = ws.WriteMessage(websocket.TextMessage, bytes)
return err
}
// Tests runtime information support
func TestRuntimeGetRuntime(t *testing.T) {
ensureRuntimeStarted()
// Create a WebSocket client
ws, _, err := websocket.DefaultDialer.Dial("ws://localhost:13014/", nil)
defer ws.Close()
if err != nil {
t.Error(err.Error())
}
// Send a runtime request and check the response
if err = sendJSONE(ws, &Message{"runtime", "getruntime", nil}); err != nil {
t.Error(err.Error())
}
var msg runtimeMessage
var bytes []byte
if _, bytes, err = ws.ReadMessage(); err != nil {
t.Error(err.Error())
return
}
if err = json.Unmarshal(bytes, &msg); err != nil {
t.Error(err.Error())
return
}
if msg.Protocol != "runtime" || msg.Command != "runtime" {
t.Errorf("Invalid protocol (%s) or command (%s)", msg.Protocol, msg.Command)
return
}
res := msg.Payload
if res.Type != "goflow" {
t.Errorf("Invalid protocol type: %s\n", res.Type)
}
if res.Version != "0.4" {
t.Errorf("Invalid protocol version: %s\n", res.Version)
}
if len(res.Capabilities) != 5 {
t.Errorf("Invalid number of supported capabilities: %v\n", res.Capabilities)
}
if res.Id == "" {
t.Error("Runtime Id is empty")
}
}