forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelemetry_test.go
112 lines (105 loc) · 2.45 KB
/
telemetry_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
// Copyright (c) 2021-2022 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"context"
"math/rand"
"sync"
"testing"
"time"
)
func TestTelemetryAddLog(t *testing.T) {
config, err := ParseDSN(dsn)
if err != nil {
t.Error(err)
}
sc, err := buildSnowflakeConn(context.Background(), *config)
if err != nil {
t.Fatal(err)
}
if err = authenticateWithConfig(sc); err != nil {
t.Fatal(err)
}
st := &snowflakeTelemetry{
sr: sc.rest,
mutex: &sync.Mutex{},
enabled: true,
flushSize: defaultFlushSize,
}
rand.Seed(time.Now().UnixNano())
randNum := rand.Int() % 10000
for i := 0; i < randNum; i++ {
if err = st.addLog(&telemetryData{
Message: map[string]string{
typeKey: "client_telemetry_type",
queryIDKey: "123",
},
Timestamp: time.Now().UnixNano() / int64(time.Millisecond),
}); err != nil {
t.Fatal(err)
}
}
if len(st.logs) != randNum%defaultFlushSize {
t.Errorf("length of remaining logs does not match. expected: %v, got: %v",
randNum%defaultFlushSize, len(st.logs))
}
if err = st.sendBatch(); err != nil {
t.Fatal(err)
}
}
func TestTelemetrySQLException(t *testing.T) {
config, err := ParseDSN(dsn)
if err != nil {
t.Error(err)
}
sc, err := buildSnowflakeConn(context.Background(), *config)
if err != nil {
t.Fatal(err)
}
if err = authenticateWithConfig(sc); err != nil {
t.Fatal(err)
}
st := &snowflakeTelemetry{
sr: sc.rest,
mutex: &sync.Mutex{},
enabled: true,
flushSize: defaultFlushSize,
}
sc.telemetry = st
sfa := &snowflakeFileTransferAgent{
sc: sc,
commandType: uploadCommand,
srcFiles: make([]string, 0),
data: &execResponseData{
SrcLocations: make([]string, 0),
},
}
if err = sfa.initFileMetadata(); err == nil {
t.Fatal("this should have thrown an error")
}
if len(st.logs) != 1 {
t.Errorf("there should be 1 telemetry data in log. found: %v", len(st.logs))
}
if sendErr := st.sendBatch(); sendErr != nil {
t.Fatal(sendErr)
}
if len(st.logs) != 0 {
t.Errorf("there should be no telemetry data in log. found: %v", len(st.logs))
}
}
func TestDisableTelemetry(t *testing.T) {
config, err := ParseDSN(dsn)
if err != nil {
t.Error(err)
}
config.DisableTelemetry = true
sc, err := buildSnowflakeConn(context.Background(), *config)
if err != nil {
t.Fatal(err)
}
if err = authenticateWithConfig(sc); err != nil {
t.Fatal(err)
}
if _, err = sc.Query("select 1", nil); err != nil {
t.Fatal(err)
}
}