forked from influxdata/influxdb-iox-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iox_client_test.go
131 lines (105 loc) · 3.53 KB
/
iox_client_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
package influxdbiox_test
import (
"bytes"
"context"
"fmt"
"net/http"
"net/url"
"os"
"strings"
"testing"
"time"
"google.golang.org/grpc"
"github.com/apache/arrow/go/v10/arrow/array"
"github.com/influxdata/line-protocol/v2/lineprotocol"
"github.com/stretchr/testify/require"
"github.com/influxdata/influxdb-iox-client-go/v2"
)
// Return the environment value for env, or default to the provided fallback
// value.
func envOrDefault(env string, fallback string) string {
v, ok := os.LookupEnv(env)
if !ok {
return fallback
}
return v
}
// Return the hostname of the test IOx instance.
func getTestHost() string {
return envOrDefault("INFLUXDB_IOX_HOST", "localhost")
}
// Return the HTTP port for the test IOx instance.
func getTestHttpPort() string {
return envOrDefault("INFLUXDB_IOX_HTTP_PORT", "8080")
}
// Return the gRPC port for the test IOx instance.
func getTestGRPCPort() string {
return envOrDefault("INFLUXDB_IOX_GRPC_PORT", "8082")
}
// Initialises the IOx client with a randomly generated database name.
//
// Returns the client & per-client database name.
func openNewDatabase(ctx context.Context, t *testing.T) (*influxdbiox.Client, string) {
databaseName := fmt.Sprintf("test_%d", time.Now().UnixNano())
if testing.Verbose() {
t.Logf("temporary database name: %q", databaseName)
}
host := getTestHost()
grpcPort := getTestGRPCPort()
config := influxdbiox.ClientConfig{
Address: fmt.Sprintf("%s:%s", host, grpcPort),
Namespace: databaseName,
DialOptions: []grpc.DialOption{grpc.WithBlock()},
}
client, err := influxdbiox.NewClient(ctx, &config)
require.NoError(t, err)
t.Cleanup(func() { _ = client.Close() })
require.NoError(t, client.Handshake(ctx))
return client, databaseName
}
// Write some data to the specified table, within the specified database.
func writeDataset(ctx context.Context, t *testing.T, databaseName string, table string) *http.Response {
writeURL, err := url.Parse(fmt.Sprintf("http://%s:%s/api/v2/write", getTestHost(), getTestHttpPort()))
require.NoError(t, err)
// Break the database name into an org/bucket pair.
orgBucket := strings.SplitN(databaseName, "_", 2)
require.Len(t, orgBucket, 2)
queryValues := writeURL.Query()
queryValues.Set("org", orgBucket[0])
queryValues.Set("bucket", orgBucket[1])
queryValues.Set("precision", "ns")
writeURL.RawQuery = queryValues.Encode()
e := new(lineprotocol.Encoder)
e.SetLax(false)
e.SetPrecision(lineprotocol.Nanosecond)
baseTime := time.Date(2021, time.April, 15, 0, 0, 0, 0, time.UTC)
for i := 0; i < 10; i++ {
e.StartLine(table)
e.AddTag("foo", "bar")
e.AddField("v", lineprotocol.MustNewValue(int64(i)))
e.EndLine(baseTime.Add(time.Minute * time.Duration(i)))
}
require.NoError(t, e.Err())
request, err := http.NewRequestWithContext(ctx, http.MethodPost, writeURL.String(), bytes.NewReader(e.Bytes()))
require.NoError(t, err)
request.Header.Set("Content-Type", "text/plain; charset=utf-8")
response, err := http.DefaultClient.Do(request)
require.NoError(t, err)
require.Equal(t, 2, response.StatusCode/100)
return response
}
func TestClient(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
t.Cleanup(cancel)
client, dbName := openNewDatabase(ctx, t)
writeDataset(ctx, t, dbName, "t")
req, err := client.PrepareQuery(ctx, "", "select count(*) from t;")
require.NoError(t, err)
reader, err := req.Query(ctx)
require.NoError(t, err)
t.Cleanup(reader.Release)
for reader.Next() {
record := reader.Record()
t.Logf("%v", record.Column(0).(*array.Int64).Int64Values())
}
}