forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
broker_test.go
95 lines (84 loc) · 2.77 KB
/
broker_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
package influxdb_test
import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"time"
"github.com/influxdb/influxdb"
)
func TestBroker_WillRunQueries(t *testing.T) {
// TODO fix the raciness in this test
t.Skip()
// this handler should just work
testHandler := &BrokerTestHandler{}
server := httptest.NewServer(testHandler)
defer server.Close()
// this will timeout on the trigger request
timeoutHandler := &BrokerTestHandler{wait: 1100 * time.Millisecond}
timeoutServer := httptest.NewServer(timeoutHandler)
defer timeoutServer.Close()
// this will return a 500
badHandler := &BrokerTestHandler{sendError: true}
badServer := httptest.NewServer(badHandler)
defer badServer.Close()
b := influxdb.NewBroker()
// set the trigger times and failure sleeps for the test
b.TriggerInterval = 2 * time.Millisecond
b.TriggerTimeout = 100 * time.Millisecond
b.TriggerFailurePause = 2 * time.Millisecond
f := tempfile()
defer os.Remove(f)
if err := b.Open(f, &url.URL{Host: "127.0.0.1:8080"}); err != nil {
t.Fatalf("error opening broker: %s", err)
}
if err := b.Initialize(); err != nil {
t.Fatalf("error initializing broker: %s", err)
}
defer b.Close()
// set the data nodes (replicas) so all the failure cases get hit first
if err := b.Broker.CreateReplica(1, &url.URL{Host: "127.0.0.1:8090"}); err != nil {
t.Fatalf("couldn't create replica %s", err.Error())
}
b.Broker.CreateReplica(2, &url.URL{Host: timeoutServer.URL[7:]})
b.Broker.CreateReplica(3, &url.URL{Host: badServer.URL[7:]})
b.Broker.CreateReplica(4, &url.URL{Host: server.URL[7:]})
b.RunContinuousQueryLoop()
// every failure and success case should be hit in this time frame
time.Sleep(1400 * time.Millisecond)
if timeoutHandler.requestCount != 1 {
t.Fatal("broker should have only sent 1 request to the server that times out.")
}
if badHandler.requestCount != 1 {
t.Fatal("broker should have only sent 1 request to the bad server. i.e. it didn't keep the state to make request to the good server")
}
if testHandler.requestCount < 1 || testHandler.processRequestCount < 1 {
t.Fatal("broker failed to send multiple continuous query requests to the data node")
}
}
type BrokerTestHandler struct {
requestCount int
processRequestCount int
sendError bool
wait time.Duration
}
// ServeHTTP serves an HTTP request.
func (h *BrokerTestHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.requestCount++
<-time.After(h.wait)
if h.sendError {
w.WriteHeader(http.StatusInternalServerError)
}
switch r.URL.Path {
case "/process_continuous_queries":
if r.Method == "POST" {
h.processRequestCount++
w.WriteHeader(http.StatusAccepted)
} else {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
}
default:
http.NotFound(w, r)
}
}