/
test_mocks.go
127 lines (104 loc) · 2.9 KB
/
test_mocks.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
// Licensed under the Apache License, Version 2.0
// Details: https://raw.githubusercontent.com/square/quotaservice/master/LICENSE
package quotaservice
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/square/quotaservice/config"
"github.com/square/quotaservice/events"
pbconfig "github.com/square/quotaservice/protos/config"
)
var _ Bucket = (*MockBucket)(nil)
type MockBucket struct {
sync.RWMutex
DefaultBucket
WaitTime time.Duration
namespace, bucketName string
dyn bool
cfg *pbconfig.BucketConfig
simulateFailure bool
}
func (b *MockBucket) Take(_ context.Context, numTokens int64, maxWaitTime time.Duration) (time.Duration, bool, error) {
if b.simulateFailure {
return 0, false, errors.New("mock bucket had an error!")
}
b.RLock()
defer b.RUnlock()
if b.WaitTime > maxWaitTime {
return 0, false, nil
}
return b.WaitTime, true, nil
}
func (b *MockBucket) Config() *pbconfig.BucketConfig {
return b.cfg
}
func (b *MockBucket) Dynamic() bool {
return b.dyn
}
type MockBucketFactory struct {
buckets map[string]*MockBucket
SimulateFailure bool
}
func (bf *MockBucketFactory) SetWaitTime(namespace, name string, d time.Duration) {
bucket := bf.bucket(namespace, name)
bucket.Lock()
defer bucket.Unlock()
bucket.WaitTime = d
}
func (bf *MockBucketFactory) bucket(namespace, name string) *MockBucket {
fqn := config.FullyQualifiedName(namespace, name)
bucket := bf.buckets[fqn]
if bucket == nil {
panic(fmt.Sprintf("No such bucket %v", fqn))
}
return bucket
}
func (bf *MockBucketFactory) Init(cfg *pbconfig.ServiceConfig) {}
func (bf *MockBucketFactory) Client() interface{} { return nil }
func (bf *MockBucketFactory) NewBucket(namespace, bucketName string, cfg *pbconfig.BucketConfig, dyn bool) Bucket {
b := &MockBucket{
WaitTime: 0,
namespace: namespace,
bucketName: bucketName,
dyn: dyn,
cfg: cfg,
simulateFailure: bf.SimulateFailure,
}
if bf.buckets == nil {
bf.buckets = make(map[string]*MockBucket)
}
bf.buckets[config.FullyQualifiedName(namespace, bucketName)] = b
return b
}
type MockEmitter struct {
Events chan events.Event
}
func (m *MockEmitter) Emit(e events.Event) {
if m.Events != nil {
m.Events <- e
}
}
type MockEndpoint struct {
QuotaService QuotaService
}
func (d *MockEndpoint) Init(qs QuotaService) {
d.QuotaService = qs
}
func (d *MockEndpoint) Start() {}
func (d *MockEndpoint) Stop() {}
func NewBucketContainerWithMocks(cfg *pbconfig.ServiceConfig) (*bucketContainer, *MockBucketFactory, *MockEmitter) {
bf := &MockBucketFactory{}
e := &MockEmitter{}
bc := NewBucketContainer(bf, e, NewReaperConfigForTests())
bc.Init(cfg)
return bc, bf, e
}
func NewReaperConfigForTests() config.ReaperConfig {
r := config.NewReaperConfig()
r.MinFrequency = 100 * time.Millisecond
r.InitSleep = 100 * time.Millisecond
return r
}