This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
client_internal_it_test.go
182 lines (160 loc) · 4.48 KB
/
client_internal_it_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
// +build !nointegration
package pilosa
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"reflect"
"strings"
"testing"
"testing/iotest"
"time"
)
func TestNewClientFromAddresses(t *testing.T) {
cli, err := NewClient([]string{":10101", "node0.pilosa.com:10101", "node2.pilosa.com"})
if err != nil {
t.Fatalf("Creating client from addresses: %v", err)
}
expectedHosts := []URI{
{scheme: "http", port: 10101, host: "localhost"},
{scheme: "http", port: 10101, host: "node0.pilosa.com"},
{scheme: "http", port: 10101, host: "node2.pilosa.com"},
}
actualHosts := cli.cluster.Hosts()
if !reflect.DeepEqual(actualHosts, expectedHosts) {
t.Fatalf("Unexpected hosts in client's cluster, got: %v, expected: %v", actualHosts, expectedHosts)
}
cli, err = NewClient([]string{"://"})
if err == nil {
t.Fatalf("Did not get expected error when creating client: %v", cli.cluster.Hosts())
}
cli, err = NewClient([]string{})
if err != nil {
t.Fatalf("Got error when creating empty client from addresses: %v", err)
}
cli, err = NewClient(nil)
if err != nil {
t.Fatalf("Got error when creating empty client from addresses: %v", err)
}
}
func TestAnyError(t *testing.T) {
err := anyError(
&http.Response{StatusCode: 400,
Body: ioutil.NopCloser(iotest.TimeoutReader(bytes.NewBuffer([]byte("asdf"))))},
nil)
if err == nil {
t.Fatalf("should have gotten an error")
}
}
func TestImportWithReplay(t *testing.T) {
buf := &bytes.Buffer{}
client := getClient(ExperimentalOptClientLogImports(buf))
// the first iterator for creating the target
iterator := &ColumnGenerator{numRows: 10, numColumns: 1000}
target := map[uint64][]uint64{}
for {
rec, err := iterator.NextRecord()
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
if col, ok := rec.(Column); ok {
target[col.RowID] = append(target[col.RowID], col.ColumnID)
}
}
// the second iterator for the actual import
iterator = &ColumnGenerator{numRows: 10, numColumns: 1000}
field := index.Field("importfield-batchsize")
err := client.EnsureField(field)
if err != nil {
t.Fatal(err)
}
statusChan := make(chan ImportStatusUpdate, 10)
err = client.ImportField(field, iterator, OptImportStatusChannel(statusChan), OptImportThreadCount(2), OptImportBatchSize(1000))
if err != nil {
t.Fatal(err)
}
// delete index and recreate
Reset()
field = index.Field("importfield-batchsize")
err = client.EnsureField(field)
if err != nil {
t.Fatal(err)
}
client.logLock.Lock()
defer client.logLock.Unlock()
if client.importLogEncoder == nil {
t.Fatalf("should have a log encoder")
}
err = client.ExperimentalReplayImport(buf, 2)
if err != nil {
t.Fatalf("replaying import: %v", err)
}
for rowID, colIDs := range target {
resp, err := client.Query(field.Row(rowID))
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(colIDs, resp.Result().Row().Columns) {
t.Fatalf("row: %v: %v != %v", rowID, colIDs, resp.Result().Row().Columns)
}
}
}
func TestImportWithReplayErrors(t *testing.T) {
buf := &bytes.Buffer{}
client := getClient(ExperimentalOptClientLogImports(buf))
// the first iterator for creating the target
iterator := &ColumnGenerator{numRows: 10, numColumns: 1000}
target := map[uint64][]uint64{}
for {
rec, err := iterator.NextRecord()
if err == io.EOF {
break
}
if err != nil {
t.Fatal(err)
}
if col, ok := rec.(Column); ok {
target[col.RowID] = append(target[col.RowID], col.ColumnID)
}
}
// the second iterator for the actual import
iterator = &ColumnGenerator{numRows: 10, numColumns: 1000}
field := index.Field("importfield-batchsize")
err := client.EnsureField(field)
if err != nil {
t.Fatal(err)
}
statusChan := make(chan ImportStatusUpdate, 1000)
err = client.ImportField(field, iterator, OptImportStatusChannel(statusChan), OptImportThreadCount(2), OptImportBatchSize(100))
if err != nil {
t.Fatal(err)
}
// delete index, but don't recreate
Reset()
result := make(chan error)
go func() {
result <- client.ExperimentalReplayImport(buf, 2)
}()
select {
case err := <-result:
if err == nil {
t.Fatalf("import shouldn't have worked")
}
if !strings.Contains(err.Error(), "waiting: ") {
t.Fatalf("unexpected error: %v", err)
}
case <-time.After(time.Second * 5):
t.Fatal("import replay hanging when no schema created")
}
}
func TestDetectClusterChanges(t *testing.T) {
c := getClient()
defer c.Close()
c.shardNodes.data["blah"] = make(map[uint64][]*URI)
c.shardNodes.data["blah"][1] = []*URI{{scheme: "zzz"}}
c.detectClusterChanges()
}