forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredundant_storage_test.go
66 lines (54 loc) · 1.4 KB
/
redundant_storage_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
// Copyright 2021-2022, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
package das
import (
"bytes"
"context"
"errors"
"testing"
"time"
"github.com/offchainlabs/nitro/das/dastree"
)
const NumServices = 3
func TestRedundantStorageService(t *testing.T) {
ctx := context.Background()
// #nosec G115
timeout := uint64(time.Now().Add(time.Hour).Unix())
services := []StorageService{}
for i := 0; i < NumServices; i++ {
services = append(services, NewMemoryBackedStorageService(ctx))
}
redundantService, err := NewRedundantStorageService(ctx, services)
Require(t, err)
val1 := []byte("The first value")
key1 := dastree.Hash(val1)
key2 := dastree.Hash(append(val1, 0))
_, err = redundantService.GetByHash(ctx, key1)
if !errors.Is(err, ErrNotFound) {
t.Fatal(err)
}
err = redundantService.Put(ctx, val1, timeout)
Require(t, err)
_, err = redundantService.GetByHash(ctx, key2)
if !errors.Is(err, ErrNotFound) {
t.Fatal(err)
}
val, err := redundantService.GetByHash(ctx, key1)
Require(t, err)
if !bytes.Equal(val, val1) {
t.Fatal(val, val1)
}
for _, serv := range services {
val, err = serv.GetByHash(ctx, key1)
Require(t, err)
if !bytes.Equal(val, val1) {
t.Fatal(val, val1)
}
}
err = redundantService.Close(ctx)
Require(t, err)
_, err = redundantService.GetByHash(ctx, key1)
if !errors.Is(err, ErrClosed) {
t.Fatal(err)
}
}