@@ -2,13 +2,16 @@ package blockstore
2
2
3
3
import (
4
4
"github.com/ipfs/go-ipfs/blocks"
5
+ "github.com/ipfs/go-ipfs/blocks/key"
5
6
"testing"
6
7
7
8
ds "gx/ipfs/QmTxLSvdhwg68WJimdS6icLPhZi28aTp6b7uihC2Yb47Xk/go-datastore"
8
9
syncds "gx/ipfs/QmTxLSvdhwg68WJimdS6icLPhZi28aTp6b7uihC2Yb47Xk/go-datastore/sync"
9
10
context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
10
11
)
11
12
13
+ var exampleBlock = blocks .NewBlock ([]byte ("foo" ))
14
+
12
15
func testArcCached (bs GCBlockstore , ctx context.Context ) (* arccache , error ) {
13
16
if ctx == nil {
14
17
ctx = context .TODO ()
@@ -24,15 +27,29 @@ func testArcCached(bs GCBlockstore, ctx context.Context) (*arccache, error) {
24
27
}
25
28
}
26
29
27
- func TestRemoveCacheEntryOnDelete (t * testing.T ) {
28
- b := blocks .NewBlock ([]byte ("foo" ))
30
+ func createStores (t * testing.T ) (* arccache , * blockstore , * callbackDatastore ) {
29
31
cd := & callbackDatastore {f : func () {}, ds : ds .NewMapDatastore ()}
30
32
bs := NewBlockstore (syncds .MutexWrap (cd ))
31
- cachedbs , err := testArcCached (bs , nil )
33
+ arc , err := testArcCached (bs , nil )
32
34
if err != nil {
33
35
t .Fatal (err )
34
36
}
35
- cachedbs .Put (b )
37
+ return arc , bs , cd
38
+ }
39
+
40
+ func trap (message string , cd * callbackDatastore , t * testing.T ) {
41
+ cd .SetFunc (func () {
42
+ t .Fatal (message )
43
+ })
44
+ }
45
+ func untrap (cd * callbackDatastore ) {
46
+ cd .SetFunc (func () {})
47
+ }
48
+
49
+ func TestRemoveCacheEntryOnDelete (t * testing.T ) {
50
+ arc , _ , cd := createStores (t )
51
+
52
+ arc .Put (exampleBlock )
36
53
37
54
cd .Lock ()
38
55
writeHitTheDatastore := false
@@ -42,26 +59,119 @@ func TestRemoveCacheEntryOnDelete(t *testing.T) {
42
59
writeHitTheDatastore = true
43
60
})
44
61
45
- cachedbs .DeleteBlock (b .Key ())
46
- cachedbs .Put (b )
62
+ arc .DeleteBlock (exampleBlock .Key ())
63
+ arc .Put (exampleBlock )
47
64
if ! writeHitTheDatastore {
48
65
t .Fail ()
49
66
}
50
67
}
51
68
52
69
func TestElideDuplicateWrite (t * testing.T ) {
53
- cd := & callbackDatastore {f : func () {}, ds : ds .NewMapDatastore ()}
54
- bs := NewBlockstore (syncds .MutexWrap (cd ))
55
- cachedbs , err := testArcCached (bs , nil )
70
+ arc , _ , cd := createStores (t )
71
+
72
+ arc .Put (exampleBlock )
73
+ trap ("write hit datastore" , cd , t )
74
+ arc .Put (exampleBlock )
75
+ }
76
+
77
+ func TestHasRequestTriggersCache (t * testing.T ) {
78
+ arc , _ , cd := createStores (t )
79
+
80
+ arc .Has (exampleBlock .Key ())
81
+ trap ("has hit datastore" , cd , t )
82
+ if has , err := arc .Has (exampleBlock .Key ()); has || err != nil {
83
+ t .Fatal ("has was true but there is no such block" )
84
+ }
85
+
86
+ untrap (cd )
87
+ err := arc .Put (exampleBlock )
56
88
if err != nil {
57
89
t .Fatal (err )
58
90
}
59
91
60
- b1 := blocks . NewBlock ([] byte ( "foo" ) )
92
+ trap ( "has hit datastore" , cd , t )
61
93
62
- cachedbs .Put (b1 )
63
- cd .SetFunc (func () {
64
- t .Fatal ("write hit the datastore" )
65
- })
66
- cachedbs .Put (b1 )
94
+ if has , err := arc .Has (exampleBlock .Key ()); ! has || err != nil {
95
+ t .Fatal ("has returned invalid result" )
96
+ }
97
+ }
98
+
99
+ func TestGetFillsCache (t * testing.T ) {
100
+ arc , _ , cd := createStores (t )
101
+
102
+ if bl , err := arc .Get (exampleBlock .Key ()); bl != nil || err == nil {
103
+ t .Fatal ("block was found or there was no error" )
104
+ }
105
+
106
+ trap ("has hit datastore" , cd , t )
107
+
108
+ if has , err := arc .Has (exampleBlock .Key ()); has || err != nil {
109
+ t .Fatal ("has was true but there is no such block" )
110
+ }
111
+
112
+ untrap (cd )
113
+
114
+ if err := arc .Put (exampleBlock ); err != nil {
115
+ t .Fatal (err )
116
+ }
117
+
118
+ trap ("has hit datastore" , cd , t )
119
+
120
+ if has , err := arc .Has (exampleBlock .Key ()); ! has || err != nil {
121
+ t .Fatal ("has returned invalid result" )
122
+ }
123
+ }
124
+
125
+ func TestGetAndDeleteFalseShortCirciot (t * testing.T ) {
126
+ arc , _ , cd := createStores (t )
127
+
128
+ arc .Has (exampleBlock .Key ())
129
+
130
+ trap ("get hit datastore" , cd , t )
131
+
132
+ if bl , err := arc .Get (exampleBlock .Key ()); bl != nil || err != ErrNotFound {
133
+ t .Fatal ("get returned invalid result" )
134
+ }
135
+
136
+ if arc .DeleteBlock (exampleBlock .Key ()) != ErrNotFound {
137
+ t .Fatal ("expected ErrNotFound error" )
138
+ }
139
+ }
140
+
141
+ func TestArcCreationFailure (t * testing.T ) {
142
+ if arc , err := arcCached (nil , - 1 ); arc != nil || err == nil {
143
+ t .Fatal ("expected error and no cache" )
144
+ }
145
+ }
146
+
147
+ func TestInvalidKey (t * testing.T ) {
148
+ arc , _ , _ := createStores (t )
149
+
150
+ bl , err := arc .Get (key .Key ("" ))
151
+
152
+ if bl != nil {
153
+ t .Fatal ("blocks should be nil" )
154
+ }
155
+ if err == nil {
156
+ t .Fatal ("expected error" )
157
+ }
158
+ }
159
+
160
+ func TestHasAfterSucessfulGetIsCached (t * testing.T ) {
161
+ arc , bs , cd := createStores (t )
162
+
163
+ bs .Put (exampleBlock )
164
+
165
+ arc .Get (exampleBlock .Key ())
166
+
167
+ trap ("has hit datastore" , cd , t )
168
+ arc .Has (exampleBlock .Key ())
169
+ }
170
+
171
+ func TestPutManyCaches (t * testing.T ) {
172
+ arc , _ , cd := createStores (t )
173
+ arc .PutMany ([]blocks.Block {exampleBlock })
174
+
175
+ trap ("has hit datastore" , cd , t )
176
+ arc .Has (exampleBlock .Key ())
67
177
}
0 commit comments