Skip to content

Commit 3860239

Browse files
committed
Refactor antithesis robustness main
Signed-off-by: Marek Siarkowicz <[email protected]>
1 parent 1585023 commit 3860239

File tree

1 file changed

+45
-48
lines changed
  • tests/antithesis/test-template/robustness

1 file changed

+45
-48
lines changed

tests/antithesis/test-template/robustness/main.go

+45-48
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,12 @@ package main
1818

1919
import (
2020
"context"
21-
"log"
21+
"fmt"
2222
"os"
2323
"sync"
2424
"time"
2525

2626
"github.com/antithesishq/antithesis-sdk-go/assert"
27-
"github.com/antithesishq/antithesis-sdk-go/random"
2827
"golang.org/x/time/rate"
2928

3029
"go.etcd.io/etcd/tests/v3/robustness/client"
@@ -34,54 +33,43 @@ import (
3433
"go.etcd.io/etcd/tests/v3/robustness/traffic"
3534
)
3635

37-
var (
38-
// Please keep the sum of weights equal 100.
39-
profile = traffic.Profile{
40-
MinimalQPS: 100,
41-
MaximalQPS: 1000,
42-
BurstableQPS: 1000,
43-
ClientCount: 3,
44-
MaxNonUniqueRequestConcurrency: 3,
45-
}
46-
IDProvider = identity.NewIDProvider()
47-
LeaseIDStorage = identity.NewLeaseIDStorage()
48-
ConcurrencyLimiter = traffic.NewConcurrencyLimiter(profile.MaxNonUniqueRequestConcurrency)
49-
)
50-
51-
// Connect returns a client connection to an etcd node
52-
func Connect() *client.RecordingClient {
53-
hosts := []string{"etcd0:2379", "etcd1:2379", "etcd2:2379"}
54-
cli, err := client.NewRecordingClient(hosts, IDProvider, time.Now())
55-
if err != nil {
56-
log.Fatalf("Failed to connect to etcd: %v", err)
57-
// Antithesis Assertion: client should always be able to connect to an etcd host
58-
host := random.RandomChoice(hosts)
59-
assert.Unreachable("Client failed to connect to an etcd host", map[string]any{"host": host, "error": err})
60-
os.Exit(1)
61-
}
62-
return cli
36+
var profile = traffic.Profile{
37+
MinimalQPS: 100,
38+
MaximalQPS: 1000,
39+
BurstableQPS: 1000,
40+
ClientCount: 3,
41+
MaxNonUniqueRequestConcurrency: 3,
6342
}
6443

65-
func testRobustness() {
44+
func main() {
6645
ctx := context.Background()
67-
var wg sync.WaitGroup
68-
var mux sync.Mutex
69-
runfor := time.Duration(robustnessrand.RandRange(5, 60) * int64(time.Second))
46+
baseTime := time.Now()
47+
duration := time.Duration(robustnessrand.RandRange(5, 60) * int64(time.Second))
48+
testRobustness(ctx, baseTime, duration)
49+
}
50+
51+
func testRobustness(ctx context.Context, baseTime time.Time, duration time.Duration) {
7052
limiter := rate.NewLimiter(rate.Limit(profile.MaximalQPS), profile.BurstableQPS)
71-
finish := wrap(time.After(runfor))
72-
reports := []report.ClientReport{}
53+
finish := closeAfter(ctx, duration)
54+
ids := identity.NewIDProvider()
55+
storage := identity.NewLeaseIDStorage()
56+
concurrencyLimiter := traffic.NewConcurrencyLimiter(profile.MaxNonUniqueRequestConcurrency)
57+
hosts := []string{"etcd0:2379", "etcd1:2379", "etcd2:2379"}
7358

74-
for range profile.ClientCount {
59+
reports := []report.ClientReport{}
60+
var mux sync.Mutex
61+
var wg sync.WaitGroup
62+
for i := 0; i < profile.ClientCount; i++ {
63+
c := connect(hosts[i%len(hosts)], ids, baseTime)
7564
wg.Add(1)
76-
c := Connect()
7765
go func(c *client.RecordingClient) {
7866
defer wg.Done()
7967
defer c.Close()
8068

8169
traffic.EtcdAntithesis.RunTrafficLoop(ctx, c, limiter,
82-
IDProvider,
83-
LeaseIDStorage,
84-
ConcurrencyLimiter,
70+
ids,
71+
storage,
72+
concurrencyLimiter,
8573
finish,
8674
)
8775
mux.Lock()
@@ -90,21 +78,30 @@ func testRobustness() {
9078
}(c)
9179
}
9280
wg.Wait()
93-
assert.Reachable("Completion robustness traffic generation", nil)
81+
fmt.Println("Completed robustness traffic generation")
82+
assert.Reachable("Completed robustness traffic generation", nil)
83+
}
84+
85+
func connect(endpoint string, ids identity.Provider, baseTime time.Time) *client.RecordingClient {
86+
cli, err := client.NewRecordingClient([]string{endpoint}, ids, baseTime)
87+
if err != nil {
88+
// Antithesis Assertion: client should always be able to connect to an etcd host
89+
assert.Unreachable("Client failed to connect to an etcd host", map[string]any{"host": endpoint, "error": err})
90+
os.Exit(1)
91+
}
92+
return cli
9493
}
9594

96-
// wrap converts a receive-only channel to receive-only struct{} channel
97-
func wrap[T any](from <-chan T) <-chan struct{} {
95+
func closeAfter(ctx context.Context, t time.Duration) <-chan struct{} {
9896
out := make(chan struct{})
9997
go func() {
10098
for {
101-
<-from
102-
out <- struct{}{}
99+
select {
100+
case <-time.After(t):
101+
case <-ctx.Done():
102+
}
103+
close(out)
103104
}
104105
}()
105106
return out
106107
}
107-
108-
func main() {
109-
testRobustness()
110-
}

0 commit comments

Comments
 (0)