tests: Refactor how linearizability test components are run in parallel

Signed-off-by: Marek Siarkowicz <siarkowicz@google.com>
This commit is contained in:
Marek Siarkowicz 2023-01-02 09:08:15 +01:00
parent 6821e226dd
commit 8a9f848d33

View File

@ -25,6 +25,7 @@ import (
"github.com/anishathalye/porcupine" "github.com/anishathalye/porcupine"
"github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp"
"golang.org/x/sync/errgroup"
"golang.org/x/time/rate" "golang.org/x/time/rate"
"go.etcd.io/etcd/tests/v3/framework/e2e" "go.etcd.io/etcd/tests/v3/framework/e2e"
@ -84,51 +85,52 @@ func TestLinearizability(t *testing.T) {
} }
for _, tc := range tcs { for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
failpoint := FailpointConfig{ ctx := context.Background()
clus, err := e2e.NewEtcdProcessCluster(ctx, t, e2e.WithConfig(&tc.config))
if err != nil {
t.Fatal(err)
}
defer clus.Close()
operations, events := testLinearizability(ctx, t, clus, FailpointConfig{
failpoint: tc.failpoint, failpoint: tc.failpoint,
count: 1, count: 1,
retries: 3, retries: 3,
waitBetweenTriggers: waitBetweenFailpointTriggers, waitBetweenTriggers: waitBetweenFailpointTriggers,
} }, trafficConfig{
traffic := trafficConfig{
minimalQPS: minimalQPS, minimalQPS: minimalQPS,
maximalQPS: maximalQPS, maximalQPS: maximalQPS,
clientCount: 8, clientCount: 8,
traffic: DefaultTraffic, traffic: DefaultTraffic,
} })
testLinearizability(context.Background(), t, tc.config, failpoint, traffic) validateEventsMatch(t, events)
checkOperationsAndPersistResults(t, operations, clus)
}) })
} }
} }
func testLinearizability(ctx context.Context, t *testing.T, config e2e.EtcdProcessClusterConfig, failpoint FailpointConfig, traffic trafficConfig) { func testLinearizability(ctx context.Context, t *testing.T, clus *e2e.EtcdProcessCluster, failpoint FailpointConfig, traffic trafficConfig) (operations []porcupine.Operation, events [][]watchEvent) {
clus, err := e2e.NewEtcdProcessCluster(ctx, t, e2e.WithConfig(&config)) // Run multiple test components (traffic, failpoints, etc) in parallel and use canceling context to propagate stop signal.
if err != nil { g := errgroup.Group{}
t.Fatal(err)
}
defer clus.Close()
trafficCtx, trafficCancel := context.WithCancel(ctx) trafficCtx, trafficCancel := context.WithCancel(ctx)
go func() { g.Go(func() error {
defer trafficCancel()
triggerFailpoints(ctx, t, clus, failpoint) triggerFailpoints(ctx, t, clus, failpoint)
// Wait second to collect traffic after triggering last failpoint.
time.Sleep(time.Second) time.Sleep(time.Second)
}() trafficCancel()
return nil
})
watchCtx, watchCancel := context.WithCancel(ctx) watchCtx, watchCancel := context.WithCancel(ctx)
var operations []porcupine.Operation g.Go(func() error {
go func() {
defer watchCancel()
operations = simulateTraffic(trafficCtx, t, clus, traffic) operations = simulateTraffic(trafficCtx, t, clus, traffic)
// Wait second to collect watch events after all traffic was sent.
time.Sleep(time.Second) time.Sleep(time.Second)
}() watchCancel()
events := collectClusterWatchEvents(watchCtx, t, clus) return nil
err = clus.Stop() })
if err != nil { g.Go(func() error {
t.Error(err) events = collectClusterWatchEvents(watchCtx, t, clus)
} return nil
validateEventsMatch(t, events) })
checkOperationsAndPersistResults(t, operations, clus) g.Wait()
return operations, events
} }
func triggerFailpoints(ctx context.Context, t *testing.T, clus *e2e.EtcdProcessCluster, config FailpointConfig) { func triggerFailpoints(ctx context.Context, t *testing.T, clus *e2e.EtcdProcessCluster, config FailpointConfig) {