-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacceptance_test.go
112 lines (103 loc) · 3.43 KB
/
acceptance_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
// Copyright © 2022 Meroxa, Inc. & Gophers Lab Technologies Pvt. Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package redis
import (
"fmt"
"math/rand"
"strings"
"testing"
"time"
"github.com/alicebob/miniredis/v2"
"github.com/conduitio-labs/conduit-connector-redis/config"
"github.com/conduitio/conduit-commons/opencdc"
sdk "github.com/conduitio/conduit-connector-sdk"
"go.uber.org/goleak"
)
const (
key = "dummy_key"
)
func TestAcceptance(t *testing.T) {
mr, err := miniredis.Run()
if err != nil {
t.Fatal(err)
}
defer mr.Close()
sourceConf := map[string]string{
config.KeyHost: mr.Host(),
config.KeyPort: mr.Port(),
config.KeyRedisKey: key,
config.KeyMode: string(config.ModeStream),
config.KeyPollingPeriod: time.Millisecond.String(),
}
destConf := map[string]string{
config.KeyHost: mr.Host(),
config.KeyPort: mr.Port(),
config.KeyRedisKey: key,
config.KeyMode: string(config.ModeStream),
}
sdk.AcceptanceTest(t, AcceptanceTestDriver{
ConfigurableAcceptanceTestDriver: sdk.ConfigurableAcceptanceTestDriver{
Config: sdk.ConfigurableAcceptanceTestDriverConfig{
Connector: Connector,
SourceConfig: sourceConf,
DestinationConfig: destConf,
GoleakOptions: []goleak.Option{goleak.IgnoreCurrent()},
AfterTest: func(*testing.T) {
mr.FlushAll()
},
},
},
rand: rand.New(rand.NewSource(time.Now().UnixNano())), //nolint:gosec // only used for testing purpose
})
}
type AcceptanceTestDriver struct {
rand *rand.Rand
sdk.ConfigurableAcceptanceTestDriver
}
// GenerateRecord overrides the pre-defined generate record function to generate the records in required format.
func (d AcceptanceTestDriver) GenerateRecord(*testing.T, opencdc.Operation) opencdc.Record {
return opencdc.Record{
Operation: opencdc.OperationCreate,
Metadata: opencdc.Metadata{"key": key},
Key: opencdc.RawData(key),
Payload: opencdc.Change{After: opencdc.RawData(
fmt.Sprintf(`{"%s":"%s"}`, d.randString(32), d.randString(32)),
)},
}
}
// randString generates a random string of length n.
// (source: https://stackoverflow.com/a/31832326)
func (d AcceptanceTestDriver) randString(n int) string {
const letterBytes = `0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
sb := strings.Builder{}
sb.Grow(n)
// src.Int63() generates 63 random bits, enough for letterIdxMax characters
for i, cache, remain := n-1, d.rand.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = d.rand.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
sb.WriteByte(letterBytes[idx])
i--
}
cache >>= letterIdxBits
remain--
}
return sb.String()
}