-
Notifications
You must be signed in to change notification settings - Fork 16
/
dcp_test.go
187 lines (148 loc) · 3.73 KB
/
dcp_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
package godcpclient
import (
"context"
"crypto/rand"
"fmt"
"log"
"math"
"math/big"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/Trendyol/go-dcp-client/helpers"
"github.com/couchbase/gocbcore/v10"
"github.com/stretchr/testify/assert"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
)
func createConfigFile(t *testing.T) (string, func()) {
configStr := `hosts:
- localhost:8091
username: Administrator
password: password
bucketName: sample
metadataBucket: sample
dcp:
group:
name: groupName
membership:
type: static
memberNumber: 1
totalMembers: 1
api:
port: 8080
metric:
enabled: true
path: /metrics
leaderElector:
enabled: false`
tmpFile, err := os.CreateTemp("", "*.yml")
if err != nil {
t.Error(err)
}
if _, err = tmpFile.WriteString(configStr); err != nil {
t.Error(err)
}
return tmpFile.Name(), func() {
tmpFile.Close()
os.Remove(tmpFile.Name())
}
}
func setupContainer(t *testing.T, config helpers.Config) func() {
ctx := context.Background()
container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "docker.io/trendyoltech/couchbase-testcontainer:6.5.1",
ExposedPorts: []string{"8091:8091/tcp", "8093:8093/tcp", "11210:11210/tcp"},
WaitingFor: wait.ForLog("/entrypoint.sh couchbase-server").WithStartupTimeout(30 * time.Second),
Env: map[string]string{
"USERNAME": config.Username,
"PASSWORD": config.Password,
"BUCKET_NAME": config.BucketName,
},
},
Started: true,
})
if err != nil {
t.Error(err)
}
return func() {
_ = container.Terminate(ctx)
}
}
func insertDataToContainer(t *testing.T, mockDataSize int64, config helpers.Config) {
client := NewClient(config)
_ = client.Connect()
defer client.Close()
ids := make([]int, mockDataSize)
for i := range ids {
ids[i] = i
}
// 2048 is the default value for the max queue size of the client, so we need to make sure that we don't exceed that
chunks := helpers.ChunkSlice[int](ids, int(math.Ceil(float64(mockDataSize)/float64(2048))))
for _, chunk := range chunks {
wg := sync.WaitGroup{}
wg.Add(len(chunk))
for _, id := range chunk {
go func(i int) {
ch := make(chan error)
opm := NewAsyncOp(context.Background())
op, err := client.GetAgent().Set(gocbcore.SetOptions{
Key: []byte(fmt.Sprintf("my_key_%v", i)),
Value: []byte(fmt.Sprintf("my_value_%v", i)),
}, func(result *gocbcore.StoreResult, err error) {
opm.Resolve()
ch <- err
})
err = opm.Wait(op, err)
if err != nil {
t.Error(err)
}
err = <-ch
if err != nil {
t.Error(err)
}
wg.Done()
}(id)
}
wg.Wait()
}
log.Printf("Inserted %v items", mockDataSize)
}
func TestDcp(t *testing.T) {
b, err := rand.Int(rand.Reader, big.NewInt(24000-12000))
if err != nil {
t.Error(err)
}
mockDataSize := b.Int64() + 12000
configPath, configFileClean := createConfigFile(t)
defer configFileClean()
config := helpers.NewConfig(fmt.Sprintf("%v_data_insert", helpers.Name), configPath)
containerShutdown := setupContainer(t, config)
defer containerShutdown()
insertDataToContainer(t, mockDataSize, config)
var dcp Dcp
var counter int64
lock := sync.Mutex{}
dcp, err = NewDcp(configPath, func(event interface{}, err error) {
if err != nil {
return
}
if event, ok := event.(DcpMutation); ok {
lock.Lock()
counter++
lock.Unlock()
assert.True(t, strings.HasPrefix(string(event.Key), "my_key"))
assert.True(t, strings.HasPrefix(string(event.Value), "my_value"))
if counter == mockDataSize {
dcp.Close()
}
}
})
if err != nil {
t.Error(err)
}
dcp.Start()
}