| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package tkvtest |
| 7 |
|
| 8 |
import ( |
| 9 |
"context" |
| 10 |
"fmt" |
| 11 |
"io" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"golang.org/x/exp/sumdb/internal/tkv" |
| 15 |
) |
| 16 |
|
| 17 |
func TestStorage(t *testing.T, ctx context.Context, storage tkv.Storage) { |
| 18 |
s := storage |
| 19 |
|
| 20 |
|
| 21 |
err := s.ReadWrite(ctx, func(ctx context.Context, tx tkv.Transaction) error { |
| 22 |
for i := 0; i < 10; i++ { |
| 23 |
err := tx.BufferWrites([]tkv.Write{ |
| 24 |
{Key: fmt.Sprint(i), Value: fmt.Sprint(-i)}, |
| 25 |
{Key: fmt.Sprint(1000 + i), Value: fmt.Sprint(-1000 - i)}, |
| 26 |
}) |
| 27 |
if err != nil { |
| 28 |
t.Fatal(err) |
| 29 |
} |
| 30 |
} |
| 31 |
return nil |
| 32 |
}) |
| 33 |
if err != nil { |
| 34 |
t.Fatal(err) |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
testRead := func() { |
| 39 |
err := s.ReadOnly(ctx, func(ctx context.Context, tx tkv.Transaction) error { |
| 40 |
for i := int64(0); i < 1010; i++ { |
| 41 |
if i == 10 { |
| 42 |
i = 1000 |
| 43 |
} |
| 44 |
val, err := tx.ReadValue(ctx, fmt.Sprint(i)) |
| 45 |
if err != nil { |
| 46 |
t.Fatalf("reading %v: %v", i, err) |
| 47 |
} |
| 48 |
if val != fmt.Sprint(-i) { |
| 49 |
t.Fatalf("ReadValue %v = %q, want %v", i, val, fmt.Sprint(-i)) |
| 50 |
} |
| 51 |
} |
| 52 |
return nil |
| 53 |
}) |
| 54 |
if err != nil { |
| 55 |
t.Fatal(err) |
| 56 |
} |
| 57 |
} |
| 58 |
testRead() |
| 59 |
|
| 60 |
|
| 61 |
err = s.ReadWrite(ctx, func(ctx context.Context, tx tkv.Transaction) error { |
| 62 |
tx.BufferWrites([]tkv.Write{ |
| 63 |
{Key: fmt.Sprint(0), Value: ""}, |
| 64 |
{Key: fmt.Sprint(1), Value: "overwrite"}, |
| 65 |
}) |
| 66 |
if err != nil { |
| 67 |
t.Fatal(err) |
| 68 |
} |
| 69 |
return io.ErrUnexpectedEOF |
| 70 |
}) |
| 71 |
if err != io.ErrUnexpectedEOF { |
| 72 |
t.Fatalf("ReadWrite returned %v, want ErrUnexpectedEOF", err) |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
testRead() |
| 77 |
} |