forked from honeycombio/libhoney-go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_test.go
220 lines (193 loc) · 5.95 KB
/
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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package libhoney
import (
"fmt"
"sync"
"testing"
"time"
"github.com/honeycombio/libhoney-go/transmission"
"github.com/stretchr/testify/assert"
)
func TestClientAdding(t *testing.T) {
b := &Builder{
dynFields: make([]dynamicField, 0, 0),
fieldHolder: fieldHolder{
data: make(map[string]interface{}),
},
}
c := &Client{
builder: b,
}
//c.AddDynamicField("dynamo", func() interface{} { return nil })
//assert.Equal(t, 1, len(b.dynFields), "after adding dynamic field, builder should have it")
c.AddField("pine", 34)
assert.Equal(t, 34, b.data["pine"], "after adding field, builder should have it")
//c.Add(map[string]interface{}{"birch": 45})
//assert.Equal(t, 45, b.data["birch"], "after adding complex field, builder should have it")
//
ev := c.NewEvent()
assert.Equal(t, 34, ev.data["pine"], "with default content, created events should be prepopulated")
b2 := c.NewBuilder()
assert.Equal(t, 34, b2.data["pine"], "with default content, cloned builders should be prepopulated")
}
func TestNewClient(t *testing.T) {
conf := ClientConfig{}
c, err := NewClient(conf)
assert.NoError(t, err, "new client should not error")
assert.Equal(t, "Oliver", c.builder.WriteKey, "initialized client should respect config")
}
func TestClientIsolated(t *testing.T) {
c1, _ := NewClient(ClientConfig{})
c2, _ := NewClient(ClientConfig{})
AddField("Mary", 83)
c1.AddField("Ursula", 88)
c2.AddField("Philip", 53)
ed := NewEvent()
assert.Equal(t, 83, ed.data["Mary"], "global libhoney should have global content")
assert.Equal(t, nil, ed.data["Ursula"], "global libhoney should not have client content")
assert.Equal(t, nil, ed.data["Philip"], "global libhoney should not have client content")
e1 := c1.NewEvent()
assert.Equal(t, 88, e1.data["Ursula"], "client events should have client-scoped date")
assert.Equal(t, nil, e1.data["Philip"], "client events should not have other client's content")
assert.Equal(t, nil, e1.data["Mary"], "client events should not have global content")
e2 := c2.NewEvent()
assert.Equal(t, 53, e2.data["Philip"], "client events should have client-scoped data")
assert.Equal(t, nil, e2.data["Ursula"], "client events should not have other client's content")
assert.Equal(t, nil, e2.data["Mary"], "client events should not have global content")
}
func TestClientRaces(t *testing.T) {
t.Parallel()
wg := sync.WaitGroup{}
wg.Add(3)
for i := 0; i < 3; i++ {
go func() {
c, _ := NewClient(ClientConfig{})
c.AddField("name", "val")
ev := c.NewEvent()
ev.AddField("argel", "bargle")
// ev.Send()
c.Close()
wg.Done()
}()
}
wg.Wait()
}
// dirtySender is a transmisison Sender that reads and writes all the event's
// fields in an attempt to create a data race
type dirtySender struct{}
func (ds *dirtySender) Start() error { return nil }
func (ds *dirtySender) Stop() error { return nil }
func (ds *dirtySender) Flush() error { return nil }
func (ds *dirtySender) TxResponses() chan transmission.Response { return nil }
func (ds *dirtySender) SendResponse(transmission.Response) bool { return true }
func (ds *dirtySender) Add(ev *transmission.Event) {
// fmt.Printf("transmission address of ev.Data is %v", ev.Data)
oldDataset := ev.Dataset
ev.Dataset = "some different value"
oldSampleRate := ev.SampleRate
ev.SampleRate = 10
oldAPIHost := ev.APIHost
ev.APIHost = "some wacky value"
oldTimestamp := ev.Timestamp
ev.Timestamp = time.Now()
oldMetadata := ev.Metadata
ev.Metadata = "some intertype value"
ev.Data["new value"] = fmt.Sprintf("%s %d %s %s %+v",
oldDataset, oldSampleRate, oldAPIHost, oldTimestamp.Format(time.RFC3339), oldMetadata)
vals := make([]interface{}, 0)
keys := make([]string, 0)
for key, val := range ev.Data {
time.Sleep(1)
keys = append(keys, key)
vals = append(vals, val)
}
for _, key := range keys {
time.Sleep(1)
ev.Data[key] = "overwrite all keys in the event"
}
}
func TestAddSendRaces(t *testing.T) {
t.Parallel()
wg := sync.WaitGroup{}
wg.Add(1)
tx := &dirtySender{}
c, _ := NewClient(ClientConfig{
Transmission: tx,
})
ev := c.NewEvent()
//ev.WriteKey = "oh my, a write"
ev.Dataset = "there is no data in this set"
ev.APIHost = "APIHostess with the mostess"
ev.AddField("preexisting", "condition")
for i := 0; i < 10; i++ {
wg.Add(1)
go func(num int) {
ev.AddField(fmt.Sprintf("argle%d", num), fmt.Sprintf("bargle%d", num))
wg.Done()
}(i)
}
go func() {
// fmt.Printf("libh address of ev.data is %v", &ev.data)
//err := ev.Send()
//assert.NoError(t, err, "sending event shouldn't error")
wg.Done()
}()
for i := 0; i < 10; i++ {
wg.Add(1)
go func(num int) {
ev.AddField(fmt.Sprintf("foogle%d", num), fmt.Sprintf("boogle%d", num))
wg.Done()
}(i)
}
wg.Wait()
// fmt.Printf("after, event data is %v", ev.data)
}
// Expected to error, but not panic.
func TestDefaultClient(t *testing.T) {
t.Parallel()
c := Client{}
e := c.NewEvent()
e.AddField("foo", "bar")
//err := e.Send()
//assert.Error(t, err)
b := c.NewBuilder()
e = b.NewEvent()
e.AddField("foo", "bar")
//err = e.Send()
//assert.Error(t, err)
}
func TestEnsureLoggerRaces(t *testing.T) {
t.Parallel()
c := Client{}
// Close() ensures the Logger exists.
wg := sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(2)
go func() { c.Close(); wg.Done() }()
go func() { c.Close(); wg.Done() }()
}
wg.Wait()
}
func TestEnsureTransmissionRaces(t *testing.T) {
t.Parallel()
c := Client{}
// TxResponses() ensures the Transmission exists.
wg := sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(2)
go func() { c.TxResponses(); wg.Done() }()
go func() { c.TxResponses(); wg.Done() }()
}
wg.Wait()
}
func TestEnsureBuilderRaces(t *testing.T) {
t.Parallel()
c := Client{}
// AddField() ensures the Builder exists.
wg := sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(2)
go func() { c.AddField("ready, set", "GO"); wg.Done() }()
go func() { c.AddField("ready, and", "GO"); wg.Done() }()
}
wg.Wait()
}