-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathextension_test.go
203 lines (167 loc) · 4.92 KB
/
extension_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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package redisstorageextension
import (
"context"
"sync"
"testing"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/extension/extensiontest"
"go.opentelemetry.io/collector/extension/xextension/storage"
)
func TestExtensionIntegrity(t *testing.T) {
t.Skip("Requires a Redis cluster to be present at localhost:6379")
ctx := context.Background()
se := newTestExtension(t)
type mockComponent struct {
kind component.Kind
name component.ID
}
components := []mockComponent{
{kind: component.KindReceiver, name: newTestEntity("receiver_one")},
{kind: component.KindReceiver, name: newTestEntity("receiver_two")},
{kind: component.KindProcessor, name: newTestEntity("processor_one")},
{kind: component.KindProcessor, name: newTestEntity("processor_two")},
{kind: component.KindExporter, name: newTestEntity("exporter_one")},
{kind: component.KindExporter, name: newTestEntity("exporter_two")},
{kind: component.KindExtension, name: newTestEntity("extension_one")},
{kind: component.KindExtension, name: newTestEntity("extension_two")},
}
// Make a client for each component
clients := make(map[component.ID]storage.Client)
for _, c := range components {
client, err := se.GetClient(ctx, c.kind, c.name, "")
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, client.Close(ctx))
})
clients[c.name] = client
}
thrashClient := func(wg *sync.WaitGroup, n component.ID, c storage.Client) {
// keys and values
keys := []string{"a", "b", "c", "d", "e"}
myBytes := []byte(n.Name())
// Set my values
for i := 0; i < len(keys); i++ {
err := c.Set(ctx, keys[i], myBytes)
require.NoError(t, err)
}
// Repeatedly thrash client
for j := 0; j < 100; j++ {
// Make sure my values are still mine
for i := 0; i < len(keys); i++ {
v, err := c.Get(ctx, keys[i])
require.NoError(t, err)
require.Equal(t, myBytes, v)
}
// Delete my values
for i := 0; i < len(keys); i++ {
err := c.Delete(ctx, keys[i])
require.NoError(t, err)
}
// Reset my values
for i := 0; i < len(keys); i++ {
err := c.Set(ctx, keys[i], myBytes)
require.NoError(t, err)
}
}
wg.Done()
}
// Use clients concurrently
var wg sync.WaitGroup
for name, client := range clients {
wg.Add(1)
go thrashClient(&wg, name, client)
}
wg.Wait()
}
func TestClientHandlesSimpleCases(t *testing.T) {
t.Skip("Requires a Redis cluster to be present at localhost:6379")
ctx := context.Background()
se := newTestExtension(t)
client, err := se.GetClient(
ctx,
component.KindReceiver,
newTestEntity("my_component"),
"",
)
myBytes := []byte("value")
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, client.Close(ctx))
})
// Set the data
err = client.Set(ctx, "key", myBytes)
require.NoError(t, err)
// Set it again (nop does not error)
err = client.Set(ctx, "key", myBytes)
require.NoError(t, err)
// Get actual data
data, err := client.Get(ctx, "key")
require.NoError(t, err)
require.Equal(t, myBytes, data)
// Delete the data
err = client.Delete(ctx, "key")
require.NoError(t, err)
// Delete it again (nop does not error)
err = client.Delete(ctx, "key")
require.NoError(t, err)
// Get missing data
data, err = client.Get(ctx, "key")
require.NoError(t, err)
require.Nil(t, data)
}
func TestTwoClientsWithDifferentNames(t *testing.T) {
t.Skip("Requires a Redis cluster to be present at localhost:6379")
ctx := context.Background()
se := newTestExtension(t)
client1, err := se.GetClient(
ctx,
component.KindReceiver,
newTestEntity("my_component"),
"foo",
)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, client1.Close(ctx))
})
client2, err := se.GetClient(
ctx,
component.KindReceiver,
newTestEntity("my_component"),
"bar",
)
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, client2.Close(ctx))
})
myBytes1 := []byte("value1")
myBytes2 := []byte("value2")
// Set the data
err = client1.Set(ctx, "key", myBytes1)
require.NoError(t, err)
err = client2.Set(ctx, "key", myBytes2)
require.NoError(t, err)
// Check it was associated accordingly
data, err := client1.Get(ctx, "key")
require.NoError(t, err)
require.Equal(t, myBytes1, data)
data, err = client2.Get(ctx, "key")
require.NoError(t, err)
require.Equal(t, myBytes2, data)
}
func newTestExtension(t *testing.T) storage.Extension {
f := NewFactory()
cfg := f.CreateDefaultConfig().(*Config)
extension, err := f.Create(context.Background(), extensiontest.NewNopSettings(), cfg)
require.NoError(t, err)
se, ok := extension.(storage.Extension)
require.True(t, ok)
require.NoError(t, se.Start(context.Background(), componenttest.NewNopHost()))
return se
}
func newTestEntity(name string) component.ID {
return component.MustNewIDWithName("nop", name)
}