forked from porthos-rpc/porthos-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
149 lines (115 loc) · 2.9 KB
/
server_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
package porthos
import (
"encoding/json"
"os"
"testing"
"time"
"github.com/streadway/amqp"
)
type ResponseTest struct {
Original float64 `json:"original"`
Sum float64 `json:"sum"`
}
func TestNewServer(t *testing.T) {
broker, err := NewBroker(os.Getenv("AMQP_URL"))
if err != nil {
t.Fatal("NewBroker failed.", err)
}
userService, err := NewServer(broker, "UserService", Options{false})
s := userService.(*server)
defer userService.Close()
if err != nil {
t.Fatal("NewServer failed.", err)
}
if s.serviceName != "UserService" {
t.Errorf("Wrong serviceName, expected: 'UserService', got: '%s'", s.serviceName)
}
if s.channel == nil {
t.Error("Service channel is nil.")
}
if s.requestChannel == nil {
t.Error("Service requestChannel is nil.")
}
if s.methods == nil {
t.Error("Service methods is nil.")
}
}
func TestServerProcessRequest(t *testing.T) {
b, err := NewBroker(os.Getenv("AMQP_URL"))
if err != nil {
t.Fatal("NewBroker failed.", err)
}
userService, err := NewServer(b, "UserService", Options{false})
if err != nil {
t.Fatal("NewServer failed.", err)
}
defer userService.Close()
// register the method that we will test.
userService.Register("doSomething", func(req Request, res Response) {
form, _ := req.Form()
x, _ := form.GetArg(0).AsFloat64()
res.JSON(200, ResponseTest{x, x + 1})
})
go userService.ListenAndServe()
// This code below is to simulate the client invoking the remote method.
// create the request message body.
body, _ := json.Marshal([]interface{}{10})
ch, err := b.openChannel()
if err != nil {
t.Fatal("Queue declare failed.", err)
}
defer ch.Close()
// declare the response queue.
q, err := ch.QueueDeclare("", false, false, true, false, nil)
if err != nil {
t.Fatal("Queue declare failed.", err)
}
// start consuming from the response queue.
dc, err := ch.Consume(
q.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
if err != nil {
t.Fatal("Consume failed.", err)
}
// publish the request.
err = ch.Publish(
"",
"UserService",
false,
false,
amqp.Publishing{
Headers: amqp.Table{
"X-Method": "doSomething",
},
ContentType: "application/json",
CorrelationId: "1",
ReplyTo: q.Name,
Body: body,
})
if err != nil {
t.Fatal("Publish failed.", err)
}
// wait the response or timeout.
for {
select {
case response := <-dc:
if response.Headers["statusCode"].(int32) != 200 {
t.Errorf("Expected status code was 200, got: %d", response.Headers["statusCode"])
}
var responseTest ResponseTest
err = json.Unmarshal(response.Body, &responseTest)
if responseTest.Sum != 11 {
t.Errorf("Response failed, expected: 11, got: %s", responseTest.Sum)
}
return
case <-time.After(5 * time.Second):
t.Fatal("No response receive. Timedout.")
}
}
}