forked from jcelliott/turnpike
-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_test.go
150 lines (130 loc) · 3.42 KB
/
client_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
// Copyright (c) 2013 Joshua Elliott
// Released under the MIT License
// http://opensource.org/licenses/MIT
package turnpike
import (
"errors"
"net/http"
"runtime"
"testing"
"time"
)
func TestClient_CallResult(t *testing.T) {
s := NewServer(false)
s.RegisterRPC("rpc:test_result",
func(client, uri string, args ...interface{}) (interface{}, error) {
return "ok", nil
})
http.Handle("/ws1", s.Handler)
// TODO: needs better way of running multiple listen and serve.
// Currently there is no way of closing the listener. A custom server and
// handler will work but requires more work. TBD.
go func() {
err := http.ListenAndServe(":8001", nil)
if err != nil {
t.Fatal("ListenAndServe: " + err.Error())
}
}()
// Let the server goroutine start.
runtime.Gosched()
c := NewClient()
err := c.Connect("ws://127.0.0.1:8001/ws1", "http://localhost/")
if err != nil {
t.Fatal("error connecting: " + err.Error())
}
resultCh := c.Call("rpc:test_result")
r := <-resultCh
if r.Error != nil {
t.Error(r.Error)
}
if r.Result != "ok" {
t.Fail()
}
}
func TestClient_CallResultGenericError(t *testing.T) {
s := NewServer(false)
s.RegisterRPC("rpc:test_generic_error",
func(client, uri string, args ...interface{}) (interface{}, error) {
return nil, errors.New("error")
})
http.Handle("/ws2", s.Handler)
go func() {
err := http.ListenAndServe(":8002", nil)
if err != nil {
t.Fatal("ListenAndServe: " + err.Error())
}
}()
runtime.Gosched()
c := NewClient()
err := c.Connect("ws://127.0.0.1:8002/ws2", "http://localhost/")
if err != nil {
t.Fatal("error connecting: " + err.Error())
}
resultCh := c.Call("rpc:test_generic_error")
r := <-resultCh
if r.Result != nil {
t.Fail()
}
if r.Error.Error() != "turnpike: RPC error with URI rpc:test_generic_error#generic-error: error" {
t.Fail()
}
}
func TestClient_CallRestultCustomError(t *testing.T) {
s := NewServer(false)
s.RegisterRPC("rpc:test_custom_error",
func(client, uri string, args ...interface{}) (interface{}, error) {
return nil, RPCError{uri, "custom error", nil}
})
http.Handle("/ws3", s.Handler)
go func() {
err := http.ListenAndServe(":8003", nil)
if err != nil {
t.Fatal("ListenAndServe: " + err.Error())
}
}()
runtime.Gosched()
c := NewClient()
err := c.Connect("ws://127.0.0.1:8003/ws3", "http://localhost/")
if err != nil {
t.Fatal("error connecting: " + err.Error())
}
resultCh := c.Call("rpc:test_custom_error")
r := <-resultCh
if r.Result != nil {
t.Fail()
}
if r.Error.Error() != "turnpike: RPC error with URI rpc:test_custom_error: custom error" {
t.Fail()
}
}
func TestClient_Event(t *testing.T) {
s := NewServer(false)
http.Handle("/ws4", s.Handler)
// TODO: needs better way of running multiple listen and serve.
// Currently there is no way of closing the listener. A custom server and
// handler will work but requires more work. TBD.
go func() {
err := http.ListenAndServe(":8004", nil)
if err != nil {
t.Fatal("ListenAndServe: " + err.Error())
}
}()
// Let the server goroutine start.
runtime.Gosched()
c := NewClient()
err := c.Connect("ws://127.0.0.1:8004/ws4", "http://localhost/")
if err != nil {
t.Fatal("error connecting: " + err.Error())
}
eventCh := make(chan bool)
c.Subscribe("event:test", func(uri string, event interface{}) {
eventCh <- true
})
c.Publish("event:test", "test")
select {
case <-eventCh:
return
case <-time.After(time.Second):
t.Fail()
}
}