Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / mod@v0.3.0 / sumdb / storage / test.go
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.
4
5 package storage
6
7 import (
8         "context"
9         "fmt"
10         "io"
11         "testing"
12 )
13
14 // TestStorage tests a Storage implementation.
15 func TestStorage(t *testing.T, ctx context.Context, storage Storage) {
16         s := storage
17
18         // Insert records.
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         // Read the records back.
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         // 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
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         // All same values should still be there.
74         testRead()
75 }