Repository: etcd-io/etcd
File: tests/integration/cache_test.go (Go)

Code before:
				}
				t.Cleanup(c.Close)
				if err := c.WaitReady(t.Context()); err != nil {
					t.Fatalf("cache not ready: %v", err)
				}
				return c
			}, tc.initialEvents, tc.followupEvents)
		})
	}
}

func TestGet(t *testing.T) {
	integration.BeforeTest(t)
	clus := integration.NewCluster(t, &integration.ClusterConfig{Size: 1})
	t.Cleanup(func() { clus.Terminate(t) })

	client := clus.Client(0)
	kv := client.KV

	testGet(t, kv, func() Getter { return kv }, TestGetEvents, nil)
}

func testGet(t *testing.T, kv clientv3.KV, getReader func() Getter, initialEvents, followupEvents []*clientv3.Event) {
	ctx := t.Context()
	t.Log("Setup")
	setupCompleteRev := applyEvents(ctx, t, kv, initialEvents)

	reader := getReader()
	if c, ok := reader.(*cache.Cache); ok {
		if err := c.WaitForRevision(ctx, setupCompleteRev); err != nil {
			t.Fatalf("cache never caught up to rev %d: %v", setupCompleteRev, err)
		}
	}

	allEventsCompleteRev := applyEvents(ctx, t, kv, followupEvents)
	if c, ok := reader.(*cache.Cache); ok {
		if err := c.WaitForRevision(ctx, allEventsCompleteRev); err != nil {
			t.Fatalf("cache never caught up to rev %d: %v", allEventsCompleteRev, err)
		}
	}

	t.Log("Validate")
	for _, tc := range getTestCases {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			op := clientv3.OpGet(tc.key, tc.opts...)
			requestedRev := op.Rev()
			resp, err := reader.Get(ctx, tc.key, tc.opts...)
			if tc.expectErr {
				if !errors.Is(err, rpctypes.ErrFutureRev) {
					t.Fatalf("expected ErrFutureRev for Get %v; got %v", tc.expectErr, err)

Review comment (suggestion):
The "Setup" step is from `t.Log("Setup")` on line 534 to `t.Log("Validate")` line 551. The name here totally doesn't make sense.

Best would be to separate `initialEvents` with `initialRev` and `followupEvents` with `followupRV`, or something that implies 2 stages, first and second one.

Code after:
				}
				t.Cleanup(c.Close)
				if err := c.WaitReady(t.Context()); err != nil {
					t.Fatalf("cache not ready: %v", err)
				}
				return c
			}, tc.initialEvents, tc.followupEvents)
		})
	}
}

func TestGet(t *testing.T) {
	integration.BeforeTest(t)
	clus := integration.NewCluster(t, &integration.ClusterConfig{Size: 1})
	t.Cleanup(func() { clus.Terminate(t) })

	client := clus.Client(0)
	kv := client.KV

	testGet(t, kv, func() Getter { return kv }, TestGetEvents, nil)
}

func testGet(t *testing.T, kv clientv3.KV, getReader func() Getter, initialEvents, followupEvents []*clientv3.Event) {
	ctx := t.Context()
	t.Log("Setup")
	initialRev := applyEvents(ctx, t, kv, initialEvents)

	reader := getReader()
	if c, ok := reader.(*cache.Cache); ok {
		if err := c.WaitForRevision(ctx, initialRev); err != nil {
			t.Fatalf("cache never caught up to rev %d: %v", initialRev, err)
		}
	}

	followupRev := applyEvents(ctx, t, kv, followupEvents)
	if c, ok := reader.(*cache.Cache); ok {
		if err := c.WaitForRevision(ctx, followupRev); err != nil {
			t.Fatalf("cache never caught up to rev %d: %v", followupRev, err)
		}
	}

	t.Log("Validate")
	for _, tc := range getTestCases {
		tc := tc
		t.Run(tc.name, func(t *testing.T) {
			op := clientv3.OpGet(tc.key, tc.opts...)
			requestedRev := op.Rev()
			resp, err := reader.Get(ctx, tc.key, tc.opts...)
			if err != nil {
				if _, ok := reader.(*cache.Cache); ok && requestedRev > 0 && requestedRev < initialRev && errors.Is(err, rpctypes.ErrCompacted) {
					t.Logf("expected ErrCompacted: requestedRev=%d < initialCompleteRev=%d", requestedRev, initialRev)