/
test.go
215 lines (190 loc) · 4.92 KB
/
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2020-present Open Networking Foundation.
//
// 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 test
import (
"fmt"
protocolapi "github.com/atomix/atomix-api/go/atomix/protocol"
"github.com/atomix/atomix-go-client/pkg/atomix"
"github.com/atomix/atomix-go-framework/pkg/atomix/cluster"
"github.com/atomix/atomix-go-framework/pkg/atomix/logging"
"sync"
)
// Protocol is a test protocol implementation
type Protocol interface {
// NewReplica creates a new test replica
NewReplica(network cluster.Network, replica protocolapi.ProtocolReplica, protocol protocolapi.ProtocolConfig) Replica
// NewClient creates a new test client
NewClient(network cluster.Network, clientID string, protocol protocolapi.ProtocolConfig) Client
}
// Option is a test option
type Option interface {
apply(*testOptions)
}
// testOptions is the set of all test options
type testOptions struct {
replicas int
partitions int
debug bool
}
// WithReplicas sets the number of replicas to test
func WithReplicas(replicas int) Option {
return replicasOption{
replicas: replicas,
}
}
type replicasOption struct {
replicas int
}
func (o replicasOption) apply(options *testOptions) {
options.replicas = o.replicas
}
// WithPartitions sets the number of partitions to test
func WithPartitions(partitions int) Option {
return partitionsOption{
partitions: partitions,
}
}
type partitionsOption struct {
partitions int
}
func (o partitionsOption) apply(options *testOptions) {
options.partitions = o.partitions
}
// WithDebugLogs sets whether to enable debug logs for the test
func WithDebugLogs() Option {
return debugOption{
debug: true,
}
}
type debugOption struct {
debug bool
}
func (o debugOption) apply(options *testOptions) {
options.debug = o.debug
}
// NewTest creates a new Atomix test
func NewTest(protocol Protocol, opts ...Option) *Test {
network := cluster.NewLocalNetwork()
options := testOptions{
replicas: 1,
partitions: 1,
}
for _, opt := range opts {
opt.apply(&options)
}
config := newTestConfig(options)
replicas := make([]*testReplica, len(config.Replicas))
for i, r := range config.Replicas {
replicas[i] = newReplica(protocol.NewReplica(network, r, config))
}
return &Test{
network: network,
protocol: protocol,
config: config,
replicas: replicas,
debug: options.debug,
}
}
func newTestConfig(options testOptions) protocolapi.ProtocolConfig {
var config protocolapi.ProtocolConfig
var replicas []string
var port int
for i := 1; i <= options.replicas; i++ {
replicaID := fmt.Sprintf("replica-%d", i)
nodeID := fmt.Sprintf("node-%d", i)
port = nextPort()
config.Replicas = append(config.Replicas, protocolapi.ProtocolReplica{
ID: replicaID,
NodeID: nodeID,
APIPort: int32(port),
})
replicas = append(replicas, replicaID)
}
for i := 1; i <= options.partitions; i++ {
config.Partitions = append(config.Partitions, protocolapi.ProtocolPartition{
PartitionID: uint32(i),
Replicas: replicas,
APIPort: int32(port),
})
}
return config
}
// Test is an Atomix test utility
type Test struct {
network cluster.Network
protocol Protocol
config protocolapi.ProtocolConfig
replicas []*testReplica
clients []*testClient
debug bool
mu sync.Mutex
}
// Start starts the test
func (t *Test) Start() error {
if t.debug {
logging.SetLevel(logging.DebugLevel)
}
t.mu.Lock()
defer t.mu.Unlock()
for _, replica := range t.replicas {
if err := replica.Start(); err != nil {
return err
}
}
return nil
}
// NewClient creates a new test client
func (t *Test) NewClient(clientID string) (atomix.Client, error) {
t.mu.Lock()
defer t.mu.Unlock()
client := newClient(clientID, t.protocol.NewClient(t.network, clientID, t.config))
driverPort := nextPort()
agentPort := nextPort()
if err := client.Start(driverPort, agentPort); err != nil {
return nil, err
}
t.clients = append(t.clients, client)
return client, nil
}
// Stop stops the test
func (t *Test) Stop() error {
t.mu.Lock()
defer t.mu.Unlock()
for _, client := range t.clients {
if err := client.Stop(); err != nil {
return err
}
}
for _, replica := range t.replicas {
if err := replica.Stop(); err != nil {
return err
}
}
return nil
}
var ports = &portAllocator{port: 5000}
func nextPort() int {
return ports.next()
}
type portAllocator struct {
port int
mu sync.Mutex
}
func (p *portAllocator) next() int {
p.mu.Lock()
defer p.mu.Unlock()
port := p.port
p.port++
return port
}