Skip to content

Commit d04cea6

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

File tree

1 file changed

+38
-38
lines changed
  • tests/antithesis/test-template/robustness

1 file changed

+38
-38
lines changed

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

+38-38
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"
@@ -43,45 +42,37 @@ var (
4342
ClientCount: 3,
4443
MaxNonUniqueRequestConcurrency: 3,
4544
}
46-
IDProvider = identity.NewIDProvider()
47-
LeaseIDStorage = identity.NewLeaseIDStorage()
48-
ConcurrencyLimiter = traffic.NewConcurrencyLimiter(profile.MaxNonUniqueRequestConcurrency)
4945
)
5046

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
47+
func main() {
48+
ctx := context.Background()
49+
baseTime := time.Now()
50+
duration := time.Duration(robustnessrand.RandRange(5, 60) * int64(time.Second))
51+
testRobustness(ctx, baseTime, duration)
6352
}
6453

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

74-
for range profile.ClientCount {
62+
reports := []report.ClientReport{}
63+
var mux sync.Mutex
64+
var wg sync.WaitGroup
65+
for i := 0; i < profile.ClientCount; i++ {
66+
c := connect(hosts[i%len(hosts)], ids, baseTime)
7567
wg.Add(1)
76-
c := Connect()
7768
go func(c *client.RecordingClient) {
7869
defer wg.Done()
7970
defer c.Close()
8071

8172
traffic.EtcdAntithesis.RunTrafficLoop(ctx, c, limiter,
82-
IDProvider,
83-
LeaseIDStorage,
84-
ConcurrencyLimiter,
73+
ids,
74+
storage,
75+
concurrencyLimiter,
8576
finish,
8677
)
8778
mux.Lock()
@@ -90,21 +81,30 @@ func testRobustness() {
9081
}(c)
9182
}
9283
wg.Wait()
93-
assert.Reachable("Completion robustness traffic generation", nil)
84+
fmt.Println("Completed robustness traffic generation")
85+
assert.Reachable("Completed robustness traffic generation", nil)
9486
}
9587

96-
// wrap converts a receive-only channel to receive-only struct{} channel
97-
func wrap[T any](from <-chan T) <-chan struct{} {
88+
func connect(endpoint string, ids identity.Provider, baseTime time.Time) *client.RecordingClient {
89+
cli, err := client.NewRecordingClient([]string{endpoint}, ids, baseTime)
90+
if err != nil {
91+
// Antithesis Assertion: client should always be able to connect to an etcd host
92+
assert.Unreachable("Client failed to connect to an etcd host", map[string]any{"host": endpoint, "error": err})
93+
os.Exit(1)
94+
}
95+
return cli
96+
}
97+
98+
func closeAfter(ctx context.Context, t time.Duration) <-chan struct{} {
9899
out := make(chan struct{})
99100
go func() {
100101
for {
101-
<-from
102-
out <- struct{}{}
102+
select {
103+
case <-time.After(t):
104+
case <-ctx.Done():
105+
}
106+
close(out)
103107
}
104108
}()
105109
return out
106110
}
107-
108-
func main() {
109-
testRobustness()
110-
}

0 commit comments

Comments
 (0)