@@ -35,6 +35,17 @@ func assertPinned(t *testing.T, p Pinner, c *cid.Cid, failmsg string) {
35
35
}
36
36
}
37
37
38
+ func assertUnpinned (t * testing.T , p Pinner , c * cid.Cid , failmsg string ) {
39
+ _ , pinned , err := p .IsPinned (c )
40
+ if err != nil {
41
+ t .Fatal (err )
42
+ }
43
+
44
+ if pinned {
45
+ t .Fatal (failmsg )
46
+ }
47
+ }
48
+
38
49
func TestPinnerBasic (t * testing.T ) {
39
50
ctx := context .Background ()
40
51
@@ -143,6 +154,122 @@ func TestPinnerBasic(t *testing.T) {
143
154
144
155
// Test recursively pinned
145
156
assertPinned (t , np , bk , "could not find recursively pinned node" )
157
+
158
+ }
159
+
160
+ func TestIsPinnedLookup (t * testing.T ) {
161
+ // We are going to test that lookups work in pins which share
162
+ // the same branches. For that we will construct this tree:
163
+ //
164
+ // A5->A4->A3->A2->A1->A0
165
+ // / /
166
+ // B------- /
167
+ // \ /
168
+ // C---------------
169
+ //
170
+ // We will ensure that IsPinned works for all objects both when they
171
+ // are pinned and once they have been unpinned.
172
+ aBranchLen := 6
173
+ if aBranchLen < 3 {
174
+ t .Fatal ("set aBranchLen to at least 3" )
175
+ }
176
+
177
+ ctx := context .Background ()
178
+ dstore := dssync .MutexWrap (ds .NewMapDatastore ())
179
+ bstore := blockstore .NewBlockstore (dstore )
180
+ bserv := bs .New (bstore , offline .Exchange (bstore ))
181
+
182
+ dserv := mdag .NewDAGService (bserv )
183
+
184
+ // TODO does pinner need to share datastore with blockservice?
185
+ p := NewPinner (dstore , dserv , dserv )
186
+
187
+ aNodes := make ([]* mdag.ProtoNode , aBranchLen , aBranchLen )
188
+ aKeys := make ([]* cid.Cid , aBranchLen , aBranchLen )
189
+ for i := 0 ; i < aBranchLen ; i ++ {
190
+ a , _ := randNode ()
191
+ if i >= 1 {
192
+ err := a .AddNodeLink ("child" , aNodes [i - 1 ])
193
+ if err != nil {
194
+ t .Fatal (err )
195
+ }
196
+ }
197
+
198
+ ak , err := dserv .Add (a )
199
+ if err != nil {
200
+ t .Fatal (err )
201
+ }
202
+ //t.Logf("a[%d] is %s", i, ak)
203
+ aNodes [i ] = a
204
+ aKeys [i ] = ak
205
+ }
206
+
207
+ // Pin A5 recursively
208
+ err := p .Pin (ctx , aNodes [aBranchLen - 1 ], true )
209
+ if err != nil {
210
+ t .Fatal (err )
211
+ }
212
+
213
+ // Create node B and add A3 as child
214
+ b , _ := randNode ()
215
+ err = b .AddNodeLink ("mychild" , aNodes [3 ])
216
+ if err != nil {
217
+ t .Fatal (err )
218
+ }
219
+
220
+ // Create C node
221
+ c , _ := randNode ()
222
+ // Add A0 as child of C
223
+ err = c .AddNodeLink ("child" , aNodes [0 ])
224
+ if err != nil {
225
+ t .Fatal (err )
226
+ }
227
+
228
+ // Add C
229
+ ck , err := dserv .Add (c )
230
+ if err != nil {
231
+ t .Fatal (err )
232
+ }
233
+ //t.Logf("C is %s", ck)
234
+
235
+ // Add C to B and Add B
236
+ err = b .AddNodeLink ("myotherchild" , c )
237
+ if err != nil {
238
+ t .Fatal (err )
239
+ }
240
+ bk , err := dserv .Add (b )
241
+ if err != nil {
242
+ t .Fatal (err )
243
+ }
244
+ //t.Logf("B is %s", bk)
245
+
246
+ // Pin C recursively
247
+ err = p .Pin (ctx , c , true )
248
+ if err != nil {
249
+ t .Fatal (err )
250
+ }
251
+
252
+ // Pin B recursively
253
+ err = p .Pin (ctx , b , true )
254
+ if err != nil {
255
+ t .Fatal (err )
256
+ }
257
+
258
+ assertPinned (t , p , aKeys [0 ], "A0 should be pinned" )
259
+ assertPinned (t , p , aKeys [1 ], "A1 should be pinned" )
260
+ assertPinned (t , p , ck , "C should be pinned" )
261
+ assertPinned (t , p , bk , "B should be pinned" )
262
+
263
+ // Unpin A5 recursively
264
+ err = p .Unpin (ctx , aKeys [5 ], true )
265
+ assertPinned (t , p , aKeys [0 ], "A0 should still be pinned through B" )
266
+ assertUnpinned (t , p , aKeys [4 ], "A4 should be unpinned" )
267
+
268
+ // Unpin B recursively
269
+ err = p .Unpin (ctx , bk , true )
270
+ assertUnpinned (t , p , bk , "B should be unpinned" )
271
+ assertUnpinned (t , p , aKeys [1 ], "A1 should be unpinned" )
272
+ assertPinned (t , p , aKeys [0 ], "A0 should still be pinned through C" )
146
273
}
147
274
148
275
func TestDuplicateSemantics (t * testing.T ) {
0 commit comments