File: test.go

package info (click to toggle)
golang-golang-x-exp 0.0~git20230522.2e198f4-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 6,404 kB
  • sloc: ansic: 1,900; objc: 276; sh: 272; asm: 48; makefile: 26
file content (77 lines) | stat: -rw-r--r-- 1,842 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package tkvtest contains a test harness for testing tkv.Storage implementations.
package tkvtest

import (
	"context"
	"fmt"
	"io"
	"testing"

	"golang.org/x/exp/sumdb/internal/tkv"
)

func TestStorage(t *testing.T, ctx context.Context, storage tkv.Storage) {
	s := storage

	// Insert records.
	err := s.ReadWrite(ctx, func(ctx context.Context, tx tkv.Transaction) error {
		for i := 0; i < 10; i++ {
			err := tx.BufferWrites([]tkv.Write{
				{Key: fmt.Sprint(i), Value: fmt.Sprint(-i)},
				{Key: fmt.Sprint(1000 + i), Value: fmt.Sprint(-1000 - i)},
			})
			if err != nil {
				t.Fatal(err)
			}
		}
		return nil
	})
	if err != nil {
		t.Fatal(err)
	}

	// Read the records back.
	testRead := func() {
		err := s.ReadOnly(ctx, func(ctx context.Context, tx tkv.Transaction) error {
			for i := int64(0); i < 1010; i++ {
				if i == 10 {
					i = 1000
				}
				val, err := tx.ReadValue(ctx, fmt.Sprint(i))
				if err != nil {
					t.Fatalf("reading %v: %v", i, err)
				}
				if val != fmt.Sprint(-i) {
					t.Fatalf("ReadValue %v = %q, want %v", i, val, fmt.Sprint(-i))
				}
			}
			return nil
		})
		if err != nil {
			t.Fatal(err)
		}
	}
	testRead()

	// Buffered writes in failed transaction should not be applied.
	err = s.ReadWrite(ctx, func(ctx context.Context, tx tkv.Transaction) error {
		tx.BufferWrites([]tkv.Write{
			{Key: fmt.Sprint(0), Value: ""},          // delete
			{Key: fmt.Sprint(1), Value: "overwrite"}, // overwrite
		})
		if err != nil {
			t.Fatal(err)
		}
		return io.ErrUnexpectedEOF
	})
	if err != io.ErrUnexpectedEOF {
		t.Fatalf("ReadWrite returned %v, want ErrUnexpectedEOF", err)
	}

	// All same values should still be there.
	testRead()
}