-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathwriteAPIBlocking_test.go
160 lines (143 loc) · 4.52 KB
/
writeAPIBlocking_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
// Copyright 2020-2021 InfluxData, Inc. All rights reserved.
// Use of this source code is governed by MIT
// license that can be found in the LICENSE file.
package api
import (
"context"
"net"
"net/http"
"strings"
"sync"
"testing"
"time"
http2 "github.com/influxdata/influxdb-client-go/v2/api/http"
"github.com/influxdata/influxdb-client-go/v2/api/write"
"github.com/influxdata/influxdb-client-go/v2/internal/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWritePoint(t *testing.T) {
service := test.NewTestService(t, "http://localhost:8888")
opts := write.DefaultOptions().SetBatchSize(5)
writeAPI := NewWriteAPIBlocking("my-org", "my-bucket", service, opts)
points := test.GenPoints(10)
err := writeAPI.WritePoint(context.Background(), points...)
require.Nil(t, err)
require.Len(t, service.Lines(), 10)
for i, p := range points {
line := write.PointToLineProtocol(p, opts.Precision())
//cut off last \n char
line = line[:len(line)-1]
assert.Equal(t, service.Lines()[i], line)
}
}
func TestWriteRecord(t *testing.T) {
service := test.NewTestService(t, "http://localhost:8888")
writeAPI := NewWriteAPIBlocking("my-org", "my-bucket", service, write.DefaultOptions().SetBatchSize(5))
lines := test.GenRecords(10)
for _, line := range lines {
err := writeAPI.WriteRecord(context.Background(), line)
require.Nil(t, err)
}
require.Len(t, service.Lines(), 10)
require.Equal(t, 10, service.Requests())
for i, l := range lines {
assert.Equal(t, l, service.Lines()[i])
}
service.Close()
err := writeAPI.WriteRecord(context.Background(), lines...)
require.Nil(t, err)
require.Equal(t, 1, service.Requests())
for i, l := range lines {
assert.Equal(t, l, service.Lines()[i])
}
service.Close()
err = writeAPI.WriteRecord(context.Background())
require.Nil(t, err)
require.Len(t, service.Lines(), 0)
service.SetReplyError(&http2.Error{Code: "invalid", Message: "data"})
err = writeAPI.WriteRecord(context.Background(), lines...)
require.NotNil(t, err)
require.Equal(t, "invalid: data", err.Error())
}
func TestWriteRecordBatch(t *testing.T) {
service := test.NewTestService(t, "http://localhost:8888")
writeAPI := NewWriteAPIBlocking("my-org", "my-bucket", service, write.DefaultOptions().SetBatchSize(5))
lines := test.GenRecords(10)
batch := strings.Join(lines, "\n")
err := writeAPI.WriteRecord(context.Background(), batch)
require.Nil(t, err)
require.Len(t, service.Lines(), 10)
for i, l := range lines {
assert.Equal(t, l, service.Lines()[i])
}
service.Close()
}
func TestWriteParallel(t *testing.T) {
service := test.NewTestService(t, "http://localhost:8888")
writeAPI := NewWriteAPIBlocking("my-org", "my-bucket", service, write.DefaultOptions().SetBatchSize(5))
lines := test.GenRecords(1000)
chanLine := make(chan string)
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
for l := range chanLine {
err := writeAPI.WriteRecord(context.Background(), l)
assert.Nil(t, err)
}
wg.Done()
}()
}
for _, l := range lines {
chanLine <- l
}
close(chanLine)
wg.Wait()
assert.Len(t, service.Lines(), len(lines))
service.Close()
}
func TestWriteErrors(t *testing.T) {
service := http2.NewService("http://locl:866", "", http2.DefaultOptions().SetHTTPClient(&http.Client{
Timeout: 100 * time.Millisecond,
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: 100 * time.Millisecond,
}).DialContext,
},
}))
writeAPI := NewWriteAPIBlocking("my-org", "my-bucket", service, write.DefaultOptions().SetBatchSize(5))
points := test.GenPoints(10)
errors := 0
for _, p := range points {
err := writeAPI.WritePoint(context.Background(), p)
if assert.Error(t, err) {
errors++
}
}
require.Equal(t, 10, errors)
}
func TestWriteBatchIng(t *testing.T) {
service := test.NewTestService(t, "http://localhost:8888")
writeAPI := NewWriteAPIBlockingWithBatching("my-org", "my-bucket", service, write.DefaultOptions().SetBatchSize(5))
lines := test.GenRecords(10)
for i, line := range lines {
err := writeAPI.WriteRecord(context.Background(), line)
require.Nil(t, err)
if i == 4 || i == 9 {
assert.Equal(t, 1, service.Requests())
require.Len(t, service.Lines(), 5)
service.Close()
}
}
for i := 0; i < 4; i++ {
err := writeAPI.WriteRecord(context.Background(), lines[i])
require.Nil(t, err)
}
assert.Equal(t, 0, service.Requests())
require.Len(t, service.Lines(), 0)
err := writeAPI.Flush(context.Background())
require.Nil(t, err)
assert.Equal(t, 1, service.Requests())
require.Len(t, service.Lines(), 4)
}