-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive_test.go
189 lines (171 loc) · 3.87 KB
/
live_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
189
// +build live
package bubbles
import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"sync"
"testing"
"time"
)
const (
addr = "localhost" // no port, default is :9200
index = "bubbles"
)
func cleanES(t *testing.T) {
req, err := http.NewRequest("DELETE", fmt.Sprintf("http://%s:9200/%s", addr, index), nil)
if err != nil {
t.Fatal(err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
if !(res.StatusCode == 200 || res.StatusCode == 404) {
t.Fatal(fmt.Sprintf("DELETE err: %d", res.StatusCode))
}
res.Body.Close()
}
func TestLiveIndex(t *testing.T) {
cleanES(t)
b := New([]string{addr}, OptFlush(10*time.Millisecond))
ins := Action{
Type: Index,
MetaData: MetaData{
Index: index,
Type: "type1",
ID: "1",
},
Document: `{"field1": "value1"}`,
}
b.Enqueue(ins)
b.Wait(10 * time.Second)
pending := b.Stop()
if have, want := len(pending), 0; have != want {
t.Fatalf("have %d, want %d: %v", have, want, pending)
}
}
func TestLiveIndexError(t *testing.T) {
cleanES(t)
// Index with errors.
errs := NewTestErrs(t)
b := New([]string{addr},
OptFlush(100*time.Millisecond),
OptServerTimeout(3*time.Second),
OptErrer(errs),
)
ins1 := Action{
Type: Index,
MetaData: MetaData{
Index: index,
Type: "type1",
ID: "1",
},
Document: `{"field1": "value1"}`,
}
ins2 := Action{
Type: Index,
MetaData: MetaData{
Index: index,
Type: "type1",
ID: "2",
},
Document: `{"field1": `, // <-- invalid!
}
b.Enqueue(ins1)
b.Enqueue(ins2)
b.Wait(500 * time.Millisecond)
pending := b.Stop()
if have, want := len(pending), 0; have != want {
t.Fatalf("have %d, want %d: %v", have, want, pending)
}
// ins2 has a fatal error and should be reported via the error cb.
errored := <-errs.Errors
if have, want := errored.Action, ins2; have != want {
t.Fatalf("have %v, want %v", have, want)
}
if have, want := errored.Elasticsearch.Type, "mapper_parsing_exception"; have != want {
t.Fatalf("have %q, want %q", have, want)
}
if have, want := errored.Error(), "http://localhost:9200/_bulk index status 400: mapper_parsing_exception failed to parse"; have != want {
t.Fatalf("have %q, want %q", have, want)
}
close(errs.Errors)
// That should have been our only error.
if _, ok := <-errs.Errors; ok {
t.Fatalf("error channel should have been closed")
}
}
func TestLiveMany(t *testing.T) {
cleanES(t)
var (
b = New([]string{addr},
OptFlush(10*time.Millisecond),
)
clients = 10
documents = 10000
)
var wg sync.WaitGroup
for i := 0; i < clients; i++ {
wg.Add(1)
go func() {
for j := 0; j < documents/clients; j++ {
b.Enqueue(Action{
Type: Create,
MetaData: MetaData{
Index: index,
Type: "type1",
},
Document: `{"field1": "value1"}`,
})
}
wg.Done()
}()
}
wg.Wait()
b.Wait(10 * time.Second)
pending := b.Stop()
if have, want := len(pending), 0; have != want {
t.Fatalf("have %d, want %d: %v", have, want, pending)
}
// Wait for ES to index our documents.
done := make(chan struct{}, 1)
go func() {
for {
if getDocCount() >= documents {
break
}
time.Sleep(10 * time.Millisecond)
}
done <- struct{}{}
}()
select {
case <-done:
case <-time.After(10 * time.Second):
t.Fatalf("Timeout waiting for documents")
}
if have, want := getDocCount(), documents; have != want {
t.Fatalf("have %d, want %d", have, want)
}
}
func getDocCount() int {
// _cat/indices gives a line such as:
// 'yellow open bubbles 5 1 10001 0 314.8kb 314.8kb\n'
res, err := http.Get(fmt.Sprintf("http://%s:9200/_cat/indices/%s", addr, index))
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
// fmt.Printf("res: %d - %s\n", res.StatusCode, string(body))
cols := strings.Fields(string(body))
docCount, err := strconv.Atoi(cols[5])
if err != nil {
panic(err)
}
return docCount
}