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