forked from kolo/xmlrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
187 lines (154 loc) · 3.91 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
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
// +build integration
package xmlrpc
import (
"context"
"io"
"net/http"
"net/http/httptest"
"runtime"
"sync"
"testing"
"time"
)
func Test_CallWithoutArgs(t *testing.T) {
client := newClient(t)
defer client.Close()
var result time.Time
if err := client.Call("service.time", nil, &result); err != nil {
t.Fatalf("service.time call error: %v", err)
}
}
func Test_CallWithOneArg(t *testing.T) {
client := newClient(t)
defer client.Close()
var result string
if err := client.Call("service.upcase", "xmlrpc", &result); err != nil {
t.Fatalf("service.upcase call error: %v", err)
}
if result != "XMLRPC" {
t.Fatalf("Unexpected result of service.upcase: %s != %s", "XMLRPC", result)
}
}
func Test_CallWithTwoArgs(t *testing.T) {
client := newClient(t)
defer client.Close()
var sum int
if err := client.Call("service.sum", []interface{}{2, 3}, &sum); err != nil {
t.Fatalf("service.sum call error: %v", err)
}
if sum != 5 {
t.Fatalf("Unexpected result of service.sum: %d != %d", 5, sum)
}
}
func Test_TwoCalls(t *testing.T) {
client := newClient(t)
defer client.Close()
var upcase string
if err := client.Call("service.upcase", "xmlrpc", &upcase); err != nil {
t.Fatalf("service.upcase call error: %v", err)
}
var sum int
if err := client.Call("service.sum", []interface{}{2, 3}, &sum); err != nil {
t.Fatalf("service.sum call error: %v", err)
}
}
func Test_FailedCall(t *testing.T) {
client := newClient(t)
defer client.Close()
var result int
if err := client.Call("service.error", nil, &result); err == nil {
t.Fatal("expected service.error returns error, but it didn't")
}
}
func Test_ConcurrentCalls(t *testing.T) {
client := newClient(t)
call := func() {
var result time.Time
client.Call("service.time", nil, &result)
}
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
call()
wg.Done()
}()
}
wg.Wait()
client.Close()
}
func Test_CloseMemoryLeak(t *testing.T) {
expected := runtime.NumGoroutine()
for i := 0; i < 3; i++ {
client := newClient(t)
client.Call("service.time", nil, nil)
client.Close()
}
var actual int
// It takes some time to stop running goroutinges. This function checks number of
// running goroutines. It finishes execution if number is same as expected or timeout
// has been reached.
func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
for {
select {
case <-ctx.Done():
return
default:
actual = runtime.NumGoroutine()
if actual == expected {
return
}
}
}
}()
if actual != expected {
t.Errorf("expected number of running goroutines to be %d, but got %d", expected, actual)
}
}
func Test_BadStatus(t *testing.T) {
// this is a mock xmlrpc server which sends an invalid status code on the first request
// and an empty methodResponse for all subsequence requests
first := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if first {
first = false
http.Error(w, "bad status", http.StatusInternalServerError)
} else {
io.WriteString(w, `
<?xml version="1.0" encoding="UTF-8"?>
<methodResponse>
<params>
<param>
<value>
<struct></struct>
</value>
</param>
</params>
</methodResponse>
`)
}
}))
client, err := NewClient(ts.URL, nil)
if err != nil {
t.Fatalf("Can't create client: %v", err)
}
defer client.Close()
var result interface{}
// expect an error due to the bad status code
if err := client.Call("method", nil, &result); err == nil {
t.Fatalf("Bad status didn't result in error")
}
// expect subsequent calls to succeed
if err := client.Call("method", nil, &result); err != nil {
t.Fatalf("Failed to recover after bad status: %v", err)
}
}
func newClient(t *testing.T) *Client {
client, err := NewClient("http://localhost:5001", nil)
if err != nil {
t.Fatalf("Can't create client: %v", err)
}
return client
}