-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebouncer_test.go
170 lines (145 loc) · 4.49 KB
/
debouncer_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
package debouncer
import (
"context"
"fmt"
"sync/atomic"
"testing"
"time"
"github.com/bradfitz/gomemcache/memcache"
redigo "github.com/gomodule/redigo/redis"
"github.com/moeryomenko/debouncer/adapters"
"github.com/moeryomenko/synx"
cache "github.com/moeryomenko/ttlcache"
"github.com/orlangure/gnomock"
"github.com/orlangure/gnomock/preset/memcached"
nomockredis "github.com/orlangure/gnomock/preset/redis"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/require"
)
type Data struct {
IntValue int `json:"int_value"`
StringValue string `json:"string_value"`
}
var value = map[string]Data{
`key1`: {IntValue: 10, StringValue: `test`},
`key2`: {IntValue: 15, StringValue: `test`},
}
func TestDebouncer(t *testing.T) {
mem, err := gnomock.Start(memcached.Preset())
if err != nil {
t.Fatalf("could not start memcached: %s", err)
}
defer func() {
gnomock.Stop(mem)
}()
red, err := gnomock.Start(nomockredis.Preset())
if err != nil {
t.Fatalf("could not start redis : %s", err)
}
defer func() {
gnomock.Stop(red)
}()
memCache, memLock := adapters.NewMemcachedDriver(memcache.New(mem.DefaultAddress()))
redisCache, redisLock := adapters.NewRedisDriver(redis.NewClient(&redis.Options{Addr: red.DefaultAddress()}))
redigoCache, redigoLock := adapters.NewRedigoDriver(&redigo.Pool{
MaxIdle: 3,
IdleTimeout: time.Second,
Dial: func() (redigo.Conn, error) { return redigo.Dial("tcp", red.DefaultAddress()) },
})
testcases := map[string]Distributed[map[string]Data]{
"Memcached": {
Cache: memCache,
Locker: memLock,
Retry: 20 * time.Millisecond,
TTL: 3 * time.Second,
Serializer: JSONSerializer[map[string]Data]{},
},
"Redis": {
Cache: redisCache,
Locker: redisLock,
Retry: 20 * time.Millisecond,
TTL: 3 * time.Second,
Serializer: JSONSerializer[map[string]Data]{},
},
"Redigo": {
Cache: redigoCache,
Locker: redigoLock,
Retry: 20 * time.Millisecond,
TTL: 3 * time.Second,
Serializer: JSONSerializer[map[string]Data]{},
},
}
t.Parallel()
for name, testcase := range testcases {
name := name
testcase := testcase
t.Run(name, func(t *testing.T) {
key := `test` + name
counter := int32(0)
testService := func(context.Context) (map[string]Data, error) {
<-time.After(time.Second)
atomic.AddInt32(&counter, 1)
return value, nil
}
// run instances.
instanceGroup := synx.NewCtxGroup(context.Background())
for instance := 0; instance < 3; instance++ {
instance := instance + 1
instanceGroup.Go(func(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
localCache := Local[map[string]Data]{
TTL: time.Second,
Cache: cache.NewCache[string, map[string]Data](ctx, 100),
}
d, err := NewDebouncer(Config[map[string]Data]{
Local: localCache,
Distributed: testcase,
})
require.NoError(t, err, `create debouncer failed`)
// do concurrent waitRequests.
requests := 10
group := synx.NewCtxGroup(context.Background())
for requestID := 0; requestID < requests; requestID++ {
requestID := requestID + 1
group.Go(func(ctx context.Context) error {
timedRun(t, fmt.Sprintf(`instance%d_request%d`, instance, requestID), func(t *testing.T) {
result, err := d.Do(ctx, key, testService)
require.NoError(t, err)
require.Equal(t, value, result)
})
// take from local cache.
<-time.After(100 * time.Millisecond)
timedRun(t, fmt.Sprintf(`instance%d_request%d_after_first_request`, instance, requestID), func(t *testing.T) {
result, err := d.Do(ctx, key, testService)
require.NoError(t, err)
require.Equal(t, value, result)
})
// take from distributed cache.
<-time.After(1 * time.Second)
timedRun(t, fmt.Sprintf(`instance%d_request%d_distributed_cache`, instance, requestID), func(t *testing.T) {
result, err := d.Do(ctx, key, testService)
require.NoError(t, err)
require.Equal(t, value, result)
})
return nil
})
}
return group.Wait()
})
}
err = instanceGroup.Wait()
require.NoError(t, err)
if counter != 1 {
t.Fatal("call's more than once")
}
})
}
}
func timedRun(t *testing.T, name string, fn func(t *testing.T)) {
start := time.Now()
defer func() {
t.Logf(`execution time of request %s: %s`, name, time.Since(start).String())
}()
fn(t)
}