-
Notifications
You must be signed in to change notification settings - Fork 415
/
memcache_test.go
217 lines (178 loc) · 5.09 KB
/
memcache_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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
package memcache
import (
"bufio"
"context"
"fmt"
"net"
"os"
"strings"
"testing"
"github.com/bradfitz/gomemcache/memcache"
"github.com/stretchr/testify/assert"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/mocktracer"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/globalconfig"
)
func TestMemcache(t *testing.T) {
li := makeFakeServer(t)
defer li.Close()
testMemcache(t, li.Addr().String())
}
func TestMemcacheIntegration(t *testing.T) {
if _, ok := os.LookupEnv("INTEGRATION"); !ok {
t.Skip("to enable integration test, set the INTEGRATION environment variable")
}
testMemcache(t, "localhost:11211")
}
func testMemcache(t *testing.T, addr string) {
client := WrapClient(memcache.New(addr), WithServiceName("test-memcache"))
defer client.DeleteAll()
validateMemcacheSpan := func(t *testing.T, span mocktracer.Span, resourceName string) {
assert.Equal(t, "test-memcache", span.Tag(ext.ServiceName),
"service name should be set to test-memcache")
assert.Equal(t, "memcached.query", span.OperationName(),
"operation name should be set to memcached.query")
assert.Equal(t, resourceName, span.Tag(ext.ResourceName),
"resource name should be set to the memcache command")
assert.Equal(t, "bradfitz/gomemcache/memcache", span.Tag(ext.Component),
"component should be set to gomemcache")
assert.Equal(t, ext.SpanKindClient, span.Tag(ext.SpanKind),
"span.kind should be set to client")
}
t.Run("default", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
err := client.
Add(&memcache.Item{
Key: "key1",
Value: []byte("value1"),
})
assert.NoError(t, err)
spans := mt.FinishedSpans()
assert.Len(t, spans, 1)
validateMemcacheSpan(t, spans[0], "Add")
})
t.Run("context", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
ctx := context.Background()
span, ctx := tracer.StartSpanFromContext(ctx, "parent")
err := client.
WithContext(ctx).
Add(&memcache.Item{
Key: "key2",
Value: []byte("value2"),
})
assert.NoError(t, err)
span.Finish()
spans := mt.FinishedSpans()
assert.Len(t, spans, 2)
validateMemcacheSpan(t, spans[0], "Add")
assert.Equal(t, span, spans[1])
assert.Equal(t, spans[1].TraceID(), spans[0].TraceID(),
"memcache span should be part of the parent trace")
})
}
func TestFakeServer(t *testing.T) {
li := makeFakeServer(t)
defer li.Close()
conn, err := net.Dial("tcp", li.Addr().String())
if err != nil {
t.Fatal(err)
}
defer conn.Close()
fmt.Fprintf(conn, "add %s\r\n%s\r\n", "key", "value")
s := bufio.NewScanner(conn)
assert.True(t, s.Scan())
assert.Equal(t, "STORED", s.Text())
}
func TestAnalyticsSettings(t *testing.T) {
li := makeFakeServer(t)
defer li.Close()
addr := li.Addr().String()
assertRate := func(t *testing.T, mt mocktracer.Tracer, rate interface{}, opts ...ClientOption) {
client := WrapClient(memcache.New(addr), opts...)
defer client.DeleteAll()
err := client.Add(&memcache.Item{Key: "key1", Value: []byte("value1")})
assert.NoError(t, err)
spans := mt.FinishedSpans()
assert.Len(t, spans, 1)
assert.Equal(t, rate, spans[0].Tag(ext.EventSampleRate))
}
t.Run("defaults", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
assertRate(t, mt, nil)
})
t.Run("global", func(t *testing.T) {
t.Skip("global flag disabled")
mt := mocktracer.Start()
defer mt.Stop()
rate := globalconfig.AnalyticsRate()
defer globalconfig.SetAnalyticsRate(rate)
globalconfig.SetAnalyticsRate(0.4)
assertRate(t, mt, 0.4)
})
t.Run("enabled", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
assertRate(t, mt, 1.0, WithAnalytics(true))
})
t.Run("disabled", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
assertRate(t, mt, nil, WithAnalytics(false))
})
t.Run("override", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
rate := globalconfig.AnalyticsRate()
defer globalconfig.SetAnalyticsRate(rate)
globalconfig.SetAnalyticsRate(0.4)
assertRate(t, mt, 0.23, WithAnalyticsRate(0.23))
})
}
func makeFakeServer(t *testing.T) net.Listener {
li, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
go func() {
for {
c, err := li.Accept()
if err != nil {
break
}
go func() {
defer c.Close()
// the memcache textual protocol is line-oriented with each
// command being space separated:
//
// command1 arg1 arg2
// command2 arg1 arg2
// ...
//
s := bufio.NewScanner(c)
for s.Scan() {
args := strings.Split(s.Text(), " ")
switch args[0] {
case "add":
if !s.Scan() {
return
}
fmt.Fprintf(c, "STORED\r\n")
default:
fmt.Fprintf(c, "SERVER ERROR unknown command: %v \r\n", args[0])
return
}
}
}()
}
}()
return li
}