tests: Refactor getting longest history

Signed-off-by: Marek Siarkowicz <siarkowicz@google.com>
This commit is contained in:
Marek Siarkowicz 2023-02-15 09:55:03 +01:00
parent c60e5954b3
commit 2afaddd5b7

View File

@ -19,7 +19,6 @@ import (
"encoding/json" "encoding/json"
"os" "os"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
"sync" "sync"
"testing" "testing"
@ -179,10 +178,10 @@ func TestLinearizability(t *testing.T) {
forcestopCluster(clus) forcestopCluster(clus)
watchProgressNotifyEnabled := clus.Cfg.WatchProcessNotifyInterval != 0 watchProgressNotifyEnabled := clus.Cfg.WatchProcessNotifyInterval != 0
validateWatchResponses(t, watchResponses, watchProgressNotifyEnabled) validateWatchResponses(t, watchResponses, watchProgressNotifyEnabled)
longestHistory, remainingEvents := watchEventHistory(watchResponses) watchEvents := watchEvents(watchResponses)
validateEventsMatch(t, longestHistory, remainingEvents) validateEventsMatch(t, watchEvents)
operations = patchOperationBasedOnWatchEvents(operations, longestHistory) patchedOperations := patchOperationBasedOnWatchEvents(operations, longestHistory(watchEvents))
checkOperationsAndPersistResults(t, lg, operations, clus) checkOperationsAndPersistResults(t, lg, patchedOperations, clus)
}) })
} }
} }
@ -383,28 +382,35 @@ type trafficConfig struct {
traffic Traffic traffic Traffic
} }
func watchEventHistory(responses [][]watchResponse) (longest []watchEvent, rest [][]watchEvent) { func watchEvents(responses [][]watchResponse) [][]watchEvent {
ops := make([][]watchEvent, len(responses)) ops := make([][]watchEvent, len(responses))
for i, resps := range responses { for i, resps := range responses {
ops[i] = toWatchEvents(resps) ops[i] = toWatchEvents(resps)
} }
return ops
sort.Slice(ops, func(i, j int) bool {
return len(ops[i]) > len(ops[j])
})
return ops[0], ops[1:]
} }
func validateEventsMatch(t *testing.T, longestHistory []watchEvent, other [][]watchEvent) { func validateEventsMatch(t *testing.T, histories [][]watchEvent) {
for i := 0; i < len(other); i++ { longestHistory := longestHistory(histories)
length := len(other[i]) for i := 0; i < len(histories); i++ {
length := len(histories[i])
// We compare prefix of watch events, as we are not guaranteed to collect all events from each node. // We compare prefix of watch events, as we are not guaranteed to collect all events from each node.
if diff := cmp.Diff(longestHistory[:length], other[i][:length], cmpopts.IgnoreFields(watchEvent{}, "Time")); diff != "" { if diff := cmp.Diff(longestHistory[:length], histories[i][:length], cmpopts.IgnoreFields(watchEvent{}, "Time")); diff != "" {
t.Errorf("Events in watches do not match, %s", diff) t.Errorf("Events in watches do not match, %s", diff)
} }
} }
} }
func longestHistory(histories [][]watchEvent) []watchEvent {
longestIndex := 0
for i, history := range histories {
if len(history) > len(histories[longestIndex]) {
longestIndex = i
}
}
return histories[longestIndex]
}
func checkOperationsAndPersistResults(t *testing.T, lg *zap.Logger, operations []porcupine.Operation, clus *e2e.EtcdProcessCluster) { func checkOperationsAndPersistResults(t *testing.T, lg *zap.Logger, operations []porcupine.Operation, clus *e2e.EtcdProcessCluster) {
path, err := testResultsDirectory(t) path, err := testResultsDirectory(t)
if err != nil { if err != nil {