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

Code before:
		wantRevision: 8,
	},
	{
		name:         "range [/foo/a, /foo/c)",
		key:          "/foo/a",
		opts:         []clientv3.OpOption{clientv3.WithSerializable(), clientv3.WithRange("/foo/c")},
		wantKVs:      []*mvccpb.KeyValue{Rev8PutFooA.Kv, Rev7TxnPutFooB.Kv},
		wantRevision: 8,
	},
	{
		name:         "fromKey /foo/b",
		key:          "/foo/b",
		opts:         []clientv3.OpOption{clientv3.WithSerializable(), clientv3.WithFromKey()},
		wantKVs:      []*mvccpb.KeyValue{Rev7TxnPutFooB.Kv, Rev4PutFooC.Kv},
		wantRevision: 8,
	},
}

func TestCacheInitializationScenarios(t *testing.T) {
	lastRev := TestGetEvents[len(TestGetEvents)-1].Kv.ModRevision

	tcs := []struct {
		name                string
		initialLoadRevision int64
	}{
		{"empty cache", 0},
		{"partial load", 4},
		{"complete load", lastRev},
	}

	for _, tc := range tcs {
		tc := tc
		t.Run(tc.name, func(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
			ctx := t.Context()

			t.Log("Classify and load first batch of events")
			var firstBatch, secondBatch []*clientv3.Event
			for _, ev := range TestGetEvents {
				if ev.Kv.ModRevision <= tc.initialLoadRevision {
					firstBatch = append(firstBatch, ev)
				} else {
					secondBatch = append(secondBatch, ev)
				}
			}
			if len(firstBatch) > 0 {
				applyEvents(ctx, t, kv, firstBatch)

Review comment (performance):
Wouldn't this case duplicate `TestCacheWithoutPrefixGet`? This test is more generic so I would drop `TestCacheWithoutPrefixGet` and rename `TestCacheInitializationScenarios` to `TestCacheWithoutPrefixGet`

Code after:
}

var getTestCases = []getTestCase{
	{
		name:         "single key /foo/a",
		key:          "/foo/a",
		opts:         []clientv3.OpOption{clientv3.WithSerializable()},
		wantKVs:      []*mvccpb.KeyValue{Rev8PutFooA.Kv},
		wantRevision: 8,
	},
	{
		name:         "non-existing key",
		key:          "/doesnotexist",
		opts:         []clientv3.OpOption{clientv3.WithSerializable()},
		wantKVs:      nil,
		wantRevision: 8,
	},
	{
		name:         "prefix /foo",
		key:          "/foo",
		opts:         []clientv3.OpOption{clientv3.WithSerializable(), clientv3.WithPrefix()},
		wantKVs:      []*mvccpb.KeyValue{Rev8PutFooA.Kv, Rev7TxnPutFooB.Kv, Rev4PutFooC.Kv},
		wantRevision: 8,
	},
	{
		name:         "range [/foo/a, /foo/c)",
		key:          "/foo/a",
		opts:         []clientv3.OpOption{clientv3.WithSerializable(), clientv3.WithRange("/foo/c")},
		wantKVs:      []*mvccpb.KeyValue{Rev8PutFooA.Kv, Rev7TxnPutFooB.Kv},
		wantRevision: 8,
	},
	{
		name:         "fromKey /foo/b",
		key:          "/foo/b",
		opts:         []clientv3.OpOption{clientv3.WithSerializable(), clientv3.WithFromKey()},
		wantKVs:      []*mvccpb.KeyValue{Rev7TxnPutFooB.Kv, Rev4PutFooC.Kv},
		wantRevision: 8,
	},
}

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

	ctx := t.Context()

	tests := []struct {
		name        string
		key         string