forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfallback_storage_service_test.go
71 lines (56 loc) · 1.63 KB
/
fallback_storage_service_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
// Copyright 2021-2022, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
package das
import (
"bytes"
"context"
"errors"
"testing"
"github.com/ethereum/go-ethereum/common/math"
"github.com/offchainlabs/nitro/das/dastree"
)
func TestFallbackStorageService(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
val1 := []byte("First value")
hash1 := dastree.Hash(val1)
val2 := []byte("Second value")
hash2 := dastree.Hash(val2)
primary := NewMemoryBackedStorageService(ctx)
err := primary.Put(ctx, val1, math.MaxUint64)
Require(t, err)
fallback := NewMemoryBackedStorageService(ctx)
err = fallback.Put(ctx, val2, math.MaxUint64)
Require(t, err)
fss := NewFallbackStorageService(primary, fallback, fallback, 60*60, true, true)
res1, err := fss.GetByHash(ctx, hash1)
Require(t, err)
if !bytes.Equal(res1, val1) {
t.Fatal()
}
res2, err := fss.GetByHash(ctx, hash2)
Require(t, err)
if !bytes.Equal(res2, val2) {
t.Fatal()
}
res2, err = primary.GetByHash(ctx, hash2)
Require(t, err)
if !bytes.Equal(res2, val2) {
t.Fatal()
}
}
func TestFallbackStorageServiceRecursive(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
val1 := []byte("First value")
hash1 := dastree.Hash(val1)
ss := NewMemoryBackedStorageService(ctx)
fss := NewFallbackStorageService(ss, ss, ss, 60*60, true, true)
// artificially make fss recursive
fss.backup = fss
// try a recursive read of a non-existent item -- should give ErrNotFound
_, err := fss.GetByHash(ctx, hash1)
if !errors.Is(err, ErrNotFound) {
t.Fatal(err)
}
}