forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_storage_service_test.go
79 lines (66 loc) · 2.09 KB
/
cache_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
72
73
74
75
76
77
78
79
// 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/offchainlabs/nitro/das/dastree"
)
func TestCacheStorageService(t *testing.T) {
ctx := context.Background()
baseStorageService := NewMemoryBackedStorageService(ctx)
cacheService := NewCacheStorageService(TestCacheConfig, baseStorageService)
val1 := []byte("The first value")
val1CorrectKey := dastree.Hash(val1)
val1IncorrectKey := dastree.Hash(append(val1, 0))
_, err := cacheService.GetByHash(ctx, val1CorrectKey)
if !errors.Is(err, ErrNotFound) {
t.Fatal(err)
}
err = cacheService.Put(ctx, val1, 1)
Require(t, err)
_, err = cacheService.GetByHash(ctx, val1IncorrectKey)
if !errors.Is(err, ErrNotFound) {
t.Fatal(err)
}
val, err := cacheService.GetByHash(ctx, val1CorrectKey)
Require(t, err)
if !bytes.Equal(val, val1) {
t.Fatal(val, val1)
}
// For Case where the value is present in the base storage but not present in the cache.
val2 := []byte("The Second value")
val2CorrectKey := dastree.Hash(val2)
val2IncorrectKey := dastree.Hash(append(val2, 0))
err = baseStorageService.Put(ctx, val2, 1)
Require(t, err)
_, err = cacheService.GetByHash(ctx, val2IncorrectKey)
if !errors.Is(err, ErrNotFound) {
t.Fatal(err)
}
val, err = cacheService.GetByHash(ctx, val2CorrectKey)
Require(t, err)
if !bytes.Equal(val, val2) {
t.Fatal(val, val2)
}
// For Case where the value is present in the cache storage but not present in the base.
emptyBaseStorageService := NewMemoryBackedStorageService(ctx)
cacheServiceWithEmptyBaseStorage := &CacheStorageService{
baseStorageService: emptyBaseStorageService,
cache: cacheService.cache,
}
val, err = cacheServiceWithEmptyBaseStorage.GetByHash(ctx, val1CorrectKey)
Require(t, err)
if !bytes.Equal(val, val1) {
t.Fatal(val, val1)
}
// Closes the base storage properly.
err = cacheService.Close(ctx)
Require(t, err)
_, err = baseStorageService.GetByHash(ctx, val1CorrectKey)
if !errors.Is(err, ErrClosed) {
t.Fatal(err)
}
}