forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreezer_test.go
100 lines (86 loc) · 2.46 KB
/
freezer_test.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package freezer_test
import (
"os"
"path"
"strconv"
"sync/atomic"
"testing"
"github.com/ledgerwatch/erigon/cl/freezer"
"github.com/stretchr/testify/assert"
)
func runBlobStoreTest(t *testing.T, b *freezer.BlobStore) {
var err error
// put bad item into obj
err = b.Put(nil, "../../../test", "a", "b")
assert.ErrorIs(t, err, os.ErrInvalid)
// get bad item
_, err = b.Get("../../../test", "a", "b")
assert.ErrorIs(t, err, os.ErrInvalid)
// put item into obj
orig := []byte{1, 2, 3, 4}
err = b.Put(orig, "test", "a", "b")
assert.NoError(t, err)
// get item from obj
ans, err := b.Get("test", "a", "b")
assert.NoError(t, err)
assert.EqualValues(t, orig, ans)
ans, err = b.Get("test", "b", "a")
assert.ErrorIs(t, err, os.ErrNotExist)
assert.Nil(t, ans)
}
func runSidecarBlobStoreTest(t *testing.T, b *freezer.SidecarBlobStore) {
var err error
// put bad item into obj
err = b.Put(nil, nil, "../../../test", "a", "b")
assert.ErrorIs(t, err, os.ErrInvalid)
// get bad item
_, _, err = b.Get("../../../test", "a", "b")
assert.ErrorIs(t, err, os.ErrInvalid)
// put item into obj
orig := []byte{1, 2, 3, 4}
orig2 := []byte{5, 6, 7, 8}
err = b.Put(orig, orig2, "test", "a", "b")
assert.NoError(t, err)
// get item from obj
ans, sidecar, err := b.Get("test", "a", "b")
assert.NoError(t, err)
assert.EqualValues(t, orig, ans)
assert.EqualValues(t, orig2, sidecar)
ans, sidecar, err = b.Get("test", "b", "a")
assert.ErrorIs(t, err, os.ErrNotExist)
assert.Nil(t, ans)
assert.Nil(t, sidecar)
// put item without sidecar
err = b.Put(orig2, nil, "test", "a", "c")
assert.NoError(t, err)
// get item from obj
ans, sidecar, err = b.Get("test", "a", "c")
assert.NoError(t, err)
assert.EqualValues(t, orig2, ans)
assert.Nil(t, sidecar)
}
func testFreezer(t *testing.T, fn func() (freezer.Freezer, func())) {
t.Run("BlobStore", func(t *testing.T) {
f, cn := fn()
defer cn()
runBlobStoreTest(t, freezer.NewBlobStore(f))
})
t.Run("SidecarBlobStore", func(t *testing.T) {
f, cn := fn()
defer cn()
runSidecarBlobStoreTest(t, freezer.NewSidecarBlobStore(f))
})
}
func TestMemoryStore(t *testing.T) {
testFreezer(t, func() (freezer.Freezer, func()) {
return &freezer.InMemory{}, func() {}
})
}
func TestRootPathStore(t *testing.T) {
cnt := atomic.Uint64{}
c := func() (freezer.Freezer, func()) {
base := path.Join("test_output", strconv.Itoa(int(cnt.Load())))
return &freezer.RootPathOsFs{base}, func() { os.RemoveAll(base) }
}
testFreezer(t, c)
}