generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
testing.go
98 lines (89 loc) · 2.34 KB
/
testing.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
package cachestore
import (
"context"
"testing"
"time"
"github.com/gomodule/redigo/redis"
"github.com/mrz1836/go-cache"
"github.com/newrelic/go-agent/v3/newrelic"
"github.com/rafaeljusto/redigomock"
"github.com/stretchr/testify/require"
)
// loadMockRedis will load a mocked redis connection
func loadMockRedis(
idleTimeout time.Duration, //nolint:unparam // same param but for testing
maxConnTime time.Duration, //nolint:unparam // same param but for testing
maxActive int, //nolint:unparam // same param but for testing
maxIdle int, //nolint:unparam // same param but for testing
) (client *cache.Client, conn *redigomock.Conn) {
conn = redigomock.NewConn()
client = &cache.Client{
DependencyScriptSha: "",
Pool: &redis.Pool{
Dial: func() (redis.Conn, error) { return conn, nil },
IdleTimeout: idleTimeout,
MaxActive: maxActive,
MaxConnLifetime: maxConnTime,
MaxIdle: maxIdle,
TestOnBorrow: func(c redis.Conn, t time.Time) error {
if time.Since(t) < time.Minute {
return nil
}
_, doErr := c.Do(cache.PingCommand)
return doErr
},
},
ScriptsLoaded: nil,
}
return
}
// loadRealRedis will load a real redis connection
func loadRealRedis(
ctx context.Context,
connectionURL string,
idleTimeout time.Duration,
maxConnTime time.Duration,
maxActive int,
maxIdle int,
dependency bool,
newRelic bool,
) (client *cache.Client, conn redis.Conn, err error) {
if client, err = cache.Connect(
ctx,
connectionURL,
maxActive,
maxIdle,
maxConnTime,
idleTimeout,
dependency,
newRelic,
); err != nil {
return
}
conn, err = client.GetConnectionWithContext(ctx)
return
}
// getNewRelicApp will return a dummy new relic app
func getNewRelicApp(appName string) (*newrelic.Application, error) {
if len(appName) == 0 {
return nil, ErrAppNameRequired
}
return newrelic.NewApplication(
func(config *newrelic.Config) {
config.AppName = appName
config.DistributedTracer.Enabled = true
config.Enabled = false
},
)
}
// getNewRelicCtx will return a dummy ctx
func getNewRelicCtx(t *testing.T, appName, txnName string) context.Context {
// Load new relic (dummy)
newRelic, err := getNewRelicApp(appName)
require.NoError(t, err)
require.NotNil(t, newRelic)
// Create new relic tx
return newrelic.NewContext(
context.Background(), newRelic.StartTransaction(txnName),
)
}