-
Notifications
You must be signed in to change notification settings - Fork 1
/
bucket_ping_test.go
188 lines (164 loc) · 4.73 KB
/
bucket_ping_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
188
package gocb
import (
"bytes"
"testing"
"time"
gocbcore "github.com/couchbase/gocbcore/v8"
"github.com/pkg/errors"
)
func TestPingAll(t *testing.T) {
results := map[string]gocbcore.PingResult{
"server1": {
Endpoint: "server1",
Latency: 25 * time.Millisecond,
Scope: "default",
},
"server2": {
Endpoint: "server2",
Latency: 42 * time.Millisecond,
Error: errors.New("something"),
Scope: "default",
},
"server3": {
Endpoint: "server3",
Latency: 100 * time.Millisecond,
Error: gocbcore.ErrRequestCanceled,
Scope: "default",
},
}
pingResult := &gocbcore.PingKvResult{
ConfigRev: 64,
Services: []gocbcore.PingResult{
results["server1"],
results["server2"],
results["server3"],
},
}
doHTTP := func(req *gocbcore.HTTPRequest) (*gocbcore.HTTPResponse, error) {
var endpoint string
switch req.Service {
case gocbcore.N1qlService:
<-time.After(50 * time.Millisecond)
endpoint = "http://localhost:8093"
req.Endpoint = endpoint
return &gocbcore.HTTPResponse{
Endpoint: endpoint,
StatusCode: 200,
Body: &testReadCloser{bytes.NewBufferString(""), nil},
}, nil
case gocbcore.FtsService:
req.Endpoint = "http://localhost:8094"
return nil, errors.New("some error occurred")
case gocbcore.CbasService:
<-time.After(20 * time.Millisecond)
endpoint = "http://localhost:8095"
req.Endpoint = endpoint
return &gocbcore.HTTPResponse{
Endpoint: endpoint,
StatusCode: 200,
Body: &testReadCloser{bytes.NewBufferString(""), nil},
}, nil
default:
return nil, errors.New("unexpected service type")
}
}
kvProvider := &mockKvProvider{
value: pingResult,
}
httpProvider := &mockHTTPProvider{
doFn: doHTTP,
}
clients := make(map[string]client)
cli := &mockClient{
bucketName: "mock",
collectionID: 0,
scopeID: 0,
useMutationTokens: false,
mockKvProvider: kvProvider,
mockHTTPProvider: httpProvider,
}
clients["mock"] = cli
c := &Cluster{
connections: clients,
sb: stateBlock{
KvTimeout: 1000 * time.Millisecond,
},
}
b := &Bucket{
sb: stateBlock{
clientStateBlock: clientStateBlock{
BucketName: "mock",
},
KvTimeout: c.sb.KvTimeout,
AnalyticsTimeout: c.sb.AnalyticsTimeout,
QueryTimeout: c.sb.QueryTimeout,
SearchTimeout: c.sb.SearchTimeout,
cachedClient: cli,
},
}
report, err := b.Ping(nil)
if err != nil {
t.Fatalf("Expected ping to not return error but was %v", err)
}
if report.ID == "" {
t.Fatalf("Report ID was empty")
}
if len(report.Services) != 4 {
t.Fatalf("Expected services length to be 6 but was %d", len(report.Services))
}
for serviceType, services := range report.Services {
for _, service := range services {
switch serviceType {
case ServiceTypeQuery:
if service.Remote != "http://localhost:8093" {
t.Fatalf("Expected service RemoteAddr to be http://localhost:8093 but was %s", service.Remote)
}
if service.State != PingStateOk {
t.Fatalf("Expected service state to be ok but was %d", service.State)
}
if service.Latency < 50*time.Millisecond {
t.Fatalf("Expected service latency to be over 50ms but was %d", service.Latency)
}
case ServiceTypeSearch:
if service.Remote != "http://localhost:8094" {
t.Fatalf("Expected service RemoteAddr to be http://localhost:8094 but was %s", service.Remote)
}
if service.State != PingStateError {
t.Fatalf("Expected service State to be error but was %d", service.State)
}
if service.Latency != 0 {
t.Fatalf("Expected service latency to be 0 but was %d", service.Latency)
}
case ServiceTypeAnalytics:
if service.Remote != "http://localhost:8095" {
t.Fatalf("Expected service RemoteAddr to be http://localhost:8095 but was %s", service.Remote)
}
if service.State != PingStateOk {
t.Fatalf("Expected service state to be ok but was %d", service.State)
}
if service.Latency < 20*time.Millisecond {
t.Fatalf("Expected service latency to be over 20ms but was %d", service.Latency)
}
case ServiceTypeKeyValue:
expected, ok := results[service.Remote]
if !ok {
t.Fatalf("Unexpected service endpoint: %s", service.Remote)
}
if service.Latency != expected.Latency {
t.Fatalf("Expected service Latency to be %s but was %s", expected.Latency, service.Latency)
}
if expected.Error != nil {
if service.State != PingStateError {
t.Fatalf("Service success should have been error, was %d", service.State)
}
} else {
if service.State != PingStateOk {
t.Fatalf("Service success should have been ok, was %d", service.State)
}
}
default:
t.Fatalf("Unexpected service type: %d", serviceType)
}
}
}
}