generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_options_test.go
258 lines (209 loc) · 6.86 KB
/
client_options_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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package cachestore
import (
"context"
"testing"
"time"
"github.com/coocood/freecache"
"github.com/mrz1836/go-cache"
zLogger "github.com/mrz1836/go-logger"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestDefaultClientOptions will test the method defaultClientOptions()
func TestDefaultClientOptions(t *testing.T) {
t.Run("ensure default values", func(t *testing.T) {
defaults := defaultClientOptions()
require.NotNil(t, defaults)
assert.Equal(t, Empty, defaults.engine)
assert.False(t, defaults.newRelicEnabled)
assert.Nil(t, defaults.redis)
require.NotNil(t, defaults.redisConfig)
})
}
// TestClientOptions_GetTxnCtx will test the method getTxnCtx()
func TestClientOptions_GetTxnCtx(t *testing.T) {
t.Run("no txn found", func(t *testing.T) {
defaults := defaultClientOptions()
require.NotNil(t, defaults)
defaults.newRelicEnabled = true
ctx := defaults.getTxnCtx(context.Background())
require.NotNil(t, ctx)
txn := newrelic.FromContext(ctx)
assert.Nil(t, txn)
})
t.Run("txn found", func(t *testing.T) {
ctx := getNewRelicCtx(t, "test", "test-tx")
txn := newrelic.FromContext(ctx)
assert.NotNil(t, txn)
})
}
// TestWithNewRelic will test the method WithNewRelic()
func TestWithNewRelic(t *testing.T) {
t.Run("get opts", func(t *testing.T) {
opt := WithNewRelic()
assert.IsType(t, *new(ClientOps), opt)
})
t.Run("apply opts", func(t *testing.T) {
opts := []ClientOps{WithNewRelic(), WithFreeCache()}
c, err := NewClient(context.Background(), opts...)
require.NotNil(t, c)
require.NoError(t, err)
assert.True(t, c.IsNewRelicEnabled())
})
}
// TestWithDebugging will test the method WithDebugging()
func TestWithDebugging(t *testing.T) {
t.Run("get opts", func(t *testing.T) {
opt := WithDebugging()
assert.IsType(t, *new(ClientOps), opt)
})
t.Run("apply opts", func(t *testing.T) {
opts := []ClientOps{WithDebugging(), WithFreeCache()}
c, err := NewClient(context.Background(), opts...)
require.NotNil(t, c)
require.NoError(t, err)
assert.True(t, c.IsDebug())
})
}
// TestWithRedis will test the method WithRedis()
func TestWithRedis(t *testing.T) {
t.Run("get opts", func(t *testing.T) {
opt := WithRedis(nil)
assert.IsType(t, *new(ClientOps), opt)
})
t.Run("apply empty config, error", func(t *testing.T) {
config := &RedisConfig{}
opts := []ClientOps{WithDebugging(), WithRedis(config)}
c, err := NewClient(context.Background(), opts...)
assert.Nil(t, c)
require.Error(t, err)
})
t.Run("apply basic local config", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping live local redis tests")
}
config := &RedisConfig{
URL: testLocalConnectionURL,
}
opts := []ClientOps{WithDebugging(), WithRedis(config)}
c, err := NewClient(context.Background(), opts...)
require.NotNil(t, c)
require.NoError(t, err)
assert.Equal(t, DefaultRedisMaxIdleTimeout.String(), c.RedisConfig().MaxIdleTimeout.String())
assert.Equal(t, Redis, c.Engine())
})
t.Run("missing redis prefix", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping live local redis tests")
}
config := &RedisConfig{
URL: "localhost:" + DefaultRedisPort,
}
opts := []ClientOps{WithDebugging(), WithRedis(config)}
c, err := NewClient(context.Background(), opts...)
require.NotNil(t, c)
require.NoError(t, err)
assert.Equal(t, DefaultRedisMaxIdleTimeout.String(), c.RedisConfig().MaxIdleTimeout.String())
assert.Equal(t, Redis, c.Engine())
})
t.Run("apply verbose config", func(t *testing.T) {
if testing.Short() {
t.Skip("skipping live local redis tests")
}
config := &RedisConfig{
URL: testLocalConnectionURL,
MaxActiveConnections: 10,
MaxConnectionLifetime: 10 * time.Second,
MaxIdleConnections: 10,
MaxIdleTimeout: 10 * time.Second,
DependencyMode: true,
UseTLS: false,
}
opts := []ClientOps{WithDebugging(), WithRedis(config)}
c, err := NewClient(context.Background(), opts...)
require.NotNil(t, c)
require.NoError(t, err)
assert.Equal(t, Redis, c.Engine())
})
}
// TestWithRedisConnection will test the method WithRedisConnection()
func TestWithRedisConnection(t *testing.T) {
t.Run("get opts", func(t *testing.T) {
opt := WithRedisConnection(nil)
assert.IsType(t, *new(ClientOps), opt)
})
t.Run("apply empty redis connection", func(t *testing.T) {
opts := []ClientOps{WithDebugging(), WithRedisConnection(nil)}
c, err := NewClient(context.Background(), opts...)
assert.NotNil(t, c)
require.NoError(t, err)
assert.Equal(t, FreeCache, c.Engine())
})
t.Run("apply existing external redis connection", func(t *testing.T) {
newClient, err := loadRedisClient(context.Background(), &RedisConfig{
URL: testLocalConnectionURL,
}, false)
require.NoError(t, err)
opts := []ClientOps{WithDebugging(), WithRedisConnection(newClient)}
var c ClientInterface
c, err = NewClient(context.Background(), opts...)
require.NotNil(t, c)
require.NoError(t, err)
assert.Equal(t, Redis, c.Engine())
assert.IsType(t, &cache.Client{}, c.Redis())
assert.Nil(t, c.RedisConfig())
})
}
// TestWithFreeCache will test the method WithFreeCache()
func TestWithFreeCache(t *testing.T) {
t.Run("get opts", func(t *testing.T) {
opt := WithFreeCache()
assert.IsType(t, *new(ClientOps), opt)
})
t.Run("use the default configuration", func(t *testing.T) {
opts := []ClientOps{WithDebugging(), WithFreeCache()}
c, err := NewClient(context.Background(), opts...)
require.NotNil(t, c)
require.NoError(t, err)
assert.Equal(t, FreeCache, c.Engine())
assert.IsType(t, &freecache.Cache{}, c.FreeCache())
})
}
// TestWithFreeCacheConnection will test the method WithFreeCacheConnection()
func TestWithFreeCacheConnection(t *testing.T) {
t.Run("get opts", func(t *testing.T) {
opt := WithFreeCacheConnection(nil)
assert.IsType(t, *new(ClientOps), opt)
})
t.Run("use an existing connection", func(t *testing.T) {
freeClient := loadFreeCache(DefaultCacheSize, DefaultGCPercent)
opts := []ClientOps{WithDebugging(), WithFreeCacheConnection(freeClient)}
c, err := NewClient(context.Background(), opts...)
require.NotNil(t, c)
require.NoError(t, err)
assert.Equal(t, FreeCache, c.Engine())
assert.Equal(t, freeClient, c.FreeCache())
})
}
// TestWithLogger will test the method WithLogger()
func TestWithLogger(t *testing.T) {
t.Parallel()
t.Run("check type", func(t *testing.T) {
opt := WithLogger(nil)
assert.IsType(t, *new(ClientOps), opt)
})
t.Run("test applying nil", func(t *testing.T) {
options := &clientOptions{}
opt := WithLogger(nil)
opt(options)
assert.Nil(t, options.logger)
})
t.Run("test applying option", func(t *testing.T) {
options := &clientOptions{}
customClient := zLogger.NewGormLogger(true, 4)
opt := WithLogger(customClient)
opt(options)
assert.Equal(t, customClient, options.logger)
})
}