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