From bc9a3fa4edb2df758f0225cad223f65c74a2e3e0 Mon Sep 17 00:00:00 2001 From: Clement Date: Wed, 25 Sep 2024 18:51:19 +0800 Subject: [PATCH] apply suggestions: at most N occurrences Signed-off-by: Clement --- tests/integration/raft_log_test.go | 71 +++++++++++++++++++----------- 1 file changed, 45 insertions(+), 26 deletions(-) diff --git a/tests/integration/raft_log_test.go b/tests/integration/raft_log_test.go index 680f30497..54e2080b0 100644 --- a/tests/integration/raft_log_test.go +++ b/tests/integration/raft_log_test.go @@ -20,6 +20,8 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + pb "go.etcd.io/etcd/api/v3/etcdserverpb" "go.etcd.io/etcd/tests/v3/framework/integration" ) @@ -36,60 +38,77 @@ func TestRaftLogCompaction(t *testing.T) { defer clus.Terminate(t) mem := clus.Members[0] + + // Get applied index of raft log + endpoint := mem.Client.Endpoints()[0] + assert.NotEmpty(t, endpoint) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + status, _ := mem.Client.Status(ctx, endpoint) + appliedi := status.RaftAppliedIndex + // Assume applied index is less than 10, should be fine at this stage + assert.Less(t, appliedi, uint64(10)) + kvc := integration.ToGRPC(mem.Client).KV - // When starting a new cluster with 1 member, the member will have an index of 4. - // TODO: Can someone explain this? - // Currently, if `ep.appliedi-ep.snapi > s.Cfg.SnapshotCount`, - // a raft log snapshot is created, and raft log entries are compacted. - // In this case, it triggers when the index is a multiple of 11. - appliedi := 4 - for ; appliedi <= 10; appliedi++ { + // When applied index is a multiple of 11 (SnapshotCount+1), + // a snapshot is created, and entries are compacted. + // + // increase applied index to 10 + for ; appliedi < 10; appliedi++ { _, err := kvc.Put(context.TODO(), &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}) if err != nil { t.Errorf("#%d: couldn't put key (%v)", appliedi, err) } } // The first snapshot and compaction shouldn't happen because the index is less than 11 - expectMemberLogTimeout(t, mem, 5*time.Second, "saved snapshot", 1) - expectMemberLogTimeout(t, mem, time.Second, "compacted Raft logs", 1) + logOccurredAtMostNTimes(t, mem, 5*time.Second, "saved snapshot", 0) + logOccurredAtMostNTimes(t, mem, time.Second, "compacted Raft logs", 0) - for ; appliedi <= 11; appliedi++ { + // increase applied index to 11 + for ; appliedi < 11; appliedi++ { _, err := kvc.Put(context.TODO(), &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}) if err != nil { t.Errorf("#%d: couldn't put key (%v)", appliedi, err) } } // The first snapshot and compaction should happen because the index is 11 - expectMemberLog(t, mem, 5*time.Second, "saved snapshot", 1) - expectMemberLog(t, mem, time.Second, "compacted Raft logs", 1) + logOccurredAtMostNTimes(t, mem, 5*time.Second, "saved snapshot", 1) + logOccurredAtMostNTimes(t, mem, time.Second, "compacted Raft logs", 1) expectMemberLog(t, mem, time.Second, "\"compact-index\": 6", 1) - for ; appliedi <= 1100; appliedi++ { + // increase applied index to 1100 + for ; appliedi < 1100; appliedi++ { _, err := kvc.Put(context.TODO(), &pb.PutRequest{Key: []byte("foo"), Value: []byte("bar")}) if err != nil { t.Errorf("#%d: couldn't put key (%v)", appliedi, err) } } - // With the index at 1100, snapshot and compaction should happen 100 times. - expectMemberLog(t, mem, 5*time.Second, "saved snapshot", 100) - expectMemberLog(t, mem, time.Second, "compacted Raft logs", 100) + // With applied index at 1100, snapshot and compaction should happen 100 times. + logOccurredAtMostNTimes(t, mem, 5*time.Second, "saved snapshot", 100) + logOccurredAtMostNTimes(t, mem, time.Second, "compacted Raft logs", 100) expectMemberLog(t, mem, time.Second, "\"compact-index\": 1095", 1) - - // No more snapshot and compaction should happen. - expectMemberLogTimeout(t, mem, 5*time.Second, "saved snapshot", 101) - expectMemberLogTimeout(t, mem, time.Second, "compacted Raft logs", 101) } -// expectMemberLogTimeout ensures that the log has fewer than `count` occurrences of `s` before timing out -func expectMemberLogTimeout(t *testing.T, m *integration.Member, timeout time.Duration, s string, count int) { +// logOccurredAtMostNTimes ensures that the log has exactly `count` occurrences of `s` before timing out, no more, no less. +func logOccurredAtMostNTimes(t *testing.T, m *integration.Member, timeout time.Duration, s string, count int) { ctx, cancel := context.WithTimeout(context.TODO(), timeout) defer cancel() + // The log must have `count` occurrences before timeout _, err := m.LogObserver.Expect(ctx, s, count) - if !errors.Is(err, context.DeadlineExceeded) { - if err != nil { - t.Fatalf("failed to expect (log:%s, count:%v): %v", s, count, err) + if err != nil { + t.Fatalf("failed to expect(log:%s, count:%d): %v", s, count, err) + } + + // The log mustn't have `count+1` occurrences before timeout + lines, err := m.LogObserver.Expect(ctx, s, count+1) + if err != nil { + if errors.Is(err, context.DeadlineExceeded) { + return + } else { + t.Fatalf("failed to expect(log:%s, count:%d): %v", s, count+1, err) } } -} + t.Fatalf("failed: too many occurrences of %s, expect %d, got %d", s, count, len(lines)) +} \ No newline at end of file