github.com/blong14/gache@v0.0.0-20240124023949-89416fd8bbfa/internal/proxy/table_test.go (about) 1 package proxy_test 2 3 import ( 4 "bytes" 5 "context" 6 "testing" 7 8 gdb "github.com/blong14/gache/internal/db" 9 gproxy "github.com/blong14/gache/internal/proxy" 10 ) 11 12 func assertMatch(t *testing.T, want []byte, got []byte) { 13 if !bytes.Equal(want, got) { 14 t.Errorf("want %s got %s", want, got) 15 } 16 } 17 18 func testGetHit(ctx context.Context, v *gproxy.Table, expected *gdb.QueryResponse) func(t *testing.T) { 19 return func(t *testing.T) { 20 t.Parallel() 21 query, outbox := gdb.NewSetValueQuery(ctx, 22 []byte("default"), expected.Key, expected.Value) 23 go v.Execute(query.Context(), query) 24 select { 25 case <-ctx.Done(): 26 t.Error(ctx.Err()) 27 case actual, ok := <-outbox: 28 if !ok || !actual.Success { 29 t.Errorf("not ok %v", query) 30 } 31 } 32 33 query, outbox = gdb.NewGetValueQuery(ctx, []byte("default"), expected.Key) 34 query.KeyRange.Start = expected.Key 35 go v.Execute(query.Context(), query) 36 select { 37 case <-ctx.Done(): 38 t.Error(ctx.Err()) 39 case actual, ok := <-outbox: 40 if !ok || !actual.Success { 41 t.Errorf("not ok %v", query) 42 } 43 for _, r := range actual.RangeValues { 44 assertMatch(t, expected.Value, r[1]) 45 } 46 } 47 } 48 } 49 50 func testScanHit(ctx context.Context, v *gproxy.Table, expected *gdb.QueryResponse) func(t *testing.T) { 51 return func(t *testing.T) { 52 t.Parallel() 53 query, outbox := gdb.NewSetValueQuery(ctx, 54 []byte("default"), expected.Key, expected.Value) 55 go v.Execute(query.Context(), query) 56 select { 57 case <-ctx.Done(): 58 t.Error(ctx.Err()) 59 case actual, ok := <-outbox: 60 if !ok || !actual.Success { 61 t.Errorf("not ok %v", query) 62 } 63 } 64 65 query, outbox = gdb.NewGetValueQuery(ctx, []byte("default"), expected.Key) 66 query.Header.Inst = gdb.GetRange 67 query.KeyRange.Start = expected.Key 68 go v.Execute(query.Context(), query) 69 select { 70 case <-ctx.Done(): 71 t.Error(ctx.Err()) 72 case actual, ok := <-outbox: 73 if !ok || !actual.Success { 74 t.Errorf("not ok %v", query) 75 } 76 for _, r := range actual.RangeValues { 77 assertMatch(t, expected.Value, r[1]) 78 } 79 } 80 81 query, outbox = gdb.NewGetValueQuery(ctx, []byte("default"), expected.Key) 82 query.Header.Inst = gdb.GetRange 83 query.KeyRange.Start = expected.Key 84 go v.Execute(query.Context(), query) 85 select { 86 case <-ctx.Done(): 87 t.Error(ctx.Err()) 88 case actual, ok := <-outbox: 89 if !ok || !actual.Success { 90 t.Errorf("not ok %v", query) 91 } 92 for _, r := range actual.RangeValues { 93 assertMatch(t, expected.Value, r[1]) 94 } 95 } 96 } 97 } 98 99 func TestTable_Get(t *testing.T) { 100 t.Parallel() 101 // given 102 ctx, cancel := context.WithCancel(context.Background()) 103 t.Cleanup(cancel) 104 opts := &gdb.TableOpts{ 105 TableName: []byte("default"), 106 InMemory: true, 107 } 108 v := gproxy.NewTable(opts) 109 hit := &gdb.QueryResponse{ 110 Key: []byte("key"), 111 Value: []byte("value"), 112 } 113 t.Run("hit", testGetHit(ctx, v, hit)) 114 t.Run("hit", testScanHit(ctx, v, hit)) 115 }