forked from influxdata/influxdb-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeApiBlocking_test.go
77 lines (70 loc) · 2.14 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
// Copyright 2020 InfluxData, Inc. All rights reserved.
// Use of this source code is governed by MIT
// license that can be found in the LICENSE file.
package influxdb2
import (
"context"
"github.com/influxdata/influxdb-client-go/internal/http"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"sync"
"testing"
"time"
)
func TestWritePoint(t *testing.T) {
client := newTestClient()
service := newTestService(t, client)
client.options.SetBatchSize(5)
writeApi := newWriteApiBlockingImpl("my-org", "my-bucket", service, client)
points := genPoints(10)
err := writeApi.WritePoint(context.Background(), points...)
require.Nil(t, err)
require.Len(t, service.lines, 10)
for i, p := range points {
line := p.ToLineProtocol(client.options.Precision())
//cut off last \n char
line = line[:len(line)-1]
assert.Equal(t, service.lines[i], line)
}
}
func TestWriteRecord(t *testing.T) {
client := newTestClient()
service := newTestService(t, client)
client.options.SetBatchSize(5)
writeApi := newWriteApiBlockingImpl("my-org", "my-bucket", service, client)
lines := genRecords(10)
err := writeApi.WriteRecord(context.Background(), lines...)
require.Nil(t, err)
require.Len(t, service.lines, 10)
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.replyError = &http.Error{Code: "invalid", Message: "data"}
err = writeApi.WriteRecord(context.Background(), lines...)
require.NotNil(t, err)
require.Equal(t, "invalid: data", err.Error())
}
func TestWriteContextCancel(t *testing.T) {
client := newTestClient()
service := newTestService(t, client)
client.options.SetBatchSize(5)
writeApi := newWriteApiBlockingImpl("my-org", "my-bucket", service, client)
lines := genRecords(10)
ctx, cancel := context.WithCancel(context.Background())
var err error
var wg sync.WaitGroup
wg.Add(1)
go func() {
time.Sleep(time.Second)
err = writeApi.WriteRecord(ctx, lines...)
wg.Done()
}()
cancel()
wg.Wait()
require.Equal(t, context.Canceled, err)
assert.Len(t, service.lines, 0)
}