1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 // TestStorage tests a Storage implementation.
15 func TestStorage(t *testing.T, ctx context.Context, storage Storage) {
19 err := s.ReadWrite(ctx, func(ctx context.Context, tx Transaction) error {
20 for i := 0; i < 10; i++ {
21 err := tx.BufferWrites([]Write{
22 {Key: fmt.Sprint(i), Value: fmt.Sprint(-i)},
23 {Key: fmt.Sprint(1000 + i), Value: fmt.Sprint(-1000 - i)},
35 // Read the records back.
37 err := s.ReadOnly(ctx, func(ctx context.Context, tx Transaction) error {
38 for i := int64(0); i < 1010; i++ {
42 val, err := tx.ReadValue(ctx, fmt.Sprint(i))
44 t.Fatalf("reading %v: %v", i, err)
46 if want := fmt.Sprint(-i); val != want {
47 t.Fatalf("ReadValue %v = %q, want %v", i, val, want)
58 // Buffered writes in failed transaction should not be applied.
59 err = s.ReadWrite(ctx, func(ctx context.Context, tx Transaction) error {
60 tx.BufferWrites([]Write{
61 {Key: fmt.Sprint(0), Value: ""}, // delete
62 {Key: fmt.Sprint(1), Value: "overwrite"}, // overwrite
67 return io.ErrUnexpectedEOF
69 if err != io.ErrUnexpectedEOF {
70 t.Fatalf("ReadWrite returned %v, want ErrUnexpectedEOF", err)
73 // All same values should still be there.