Skip to content

Commit 67a2ea1

Browse files
committed
test: flesh out WriteShardTests
1 parent 423f6ad commit 67a2ea1

1 file changed

Lines changed: 37 additions & 9 deletions

File tree

src/test/java/org/janelia/saalfeldlab/n5/shard/WriteShardTest.java

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
import static org.junit.Assert.assertTrue;
3434

3535
import java.util.Arrays;
36+
import java.util.stream.Collectors;
37+
38+
import org.apache.commons.lang3.stream.Streams;
3639
import org.janelia.saalfeldlab.n5.DataBlock;
3740
import org.janelia.saalfeldlab.n5.DataType;
3841
import org.janelia.saalfeldlab.n5.DatasetAttributes;
@@ -185,11 +188,16 @@ public void testWriteNullBlockRemovesShard() throws Exception {
185188
final PositionValueAccess store = new TestPositionValueAccess();
186189
final long[] shardKey = {1};
187190

191+
/**
192+
* ONE BLOCK, ONE SHARD
193+
*/
194+
188195
assertFalse("Shard should not exist at the start of the test", store.exists(shardKey));
189196

190197
// Write a single block at grid position [3]
191-
// This block is in shard [1] (shard 0 contains blocks 0-1, shard 1
192-
// contains blocks 2-3)
198+
// This block is in shard [1]
199+
// ( shard 0 contains blocks 0-1,
200+
// shard 1 contains blocks 2-3 )
193201
final long[] blockGridPosition = {3};
194202
final DataBlock<int[]> block = createDataBlock(datablockSize, blockGridPosition, 100);
195203

@@ -214,17 +222,30 @@ public void testWriteNullBlockRemovesShard() throws Exception {
214222

215223
// Verify the shard has been removed
216224
assertFalse("Shard should be removed after writing null block", store.exists(shardKey));
217-
218-
// Write two blocks into the same shard
225+
226+
/**
227+
* THREE BLOCKS, TWO SHARDS
228+
*/
229+
// Write two blocks into the same shard, and one block into a second shard
219230
// Shard [1] contains blocks [2] and [3]
231+
// Shard [2] contains block [4]
232+
final long[] shardKey1 = {1};
233+
final long[] shardKey2 = {2};
234+
220235
final DataBlock<int[]> block1 = createDataBlock(datablockSize, new long[]{2}, 100);
221236
final DataBlock<int[]> block2 = createDataBlock(datablockSize, new long[]{3}, 200);
237+
final DataBlock<int[]> block3 = createDataBlock(datablockSize, new long[]{4}, 300);
222238

223-
datasetAccess.writeBlock(store, block1);
224-
datasetAccess.writeBlock(store, block2);
239+
240+
assertFalse("Shard should not exist at the start of the test", store.exists(shardKey1));
241+
assertFalse("Shard should not exist at the start of the test", store.exists(shardKey2));
242+
243+
// write blocks
244+
datasetAccess.writeBlocks(store, Streams.of(block1, block2, block3).collect(Collectors.toList()));
225245

226246
// Verify the shard exists
227-
assertTrue("Shard should exist after writing blocks", store.exists(shardKey));
247+
assertTrue("Shard should exist after writing blocks", store.exists(shardKey1));
248+
assertTrue("Shard should exist after writing blocks", store.exists(shardKey2));
228249

229250
// Write a null block at block [3]'s location
230251
datasetAccess.writeRegion(
@@ -235,7 +256,8 @@ public void testWriteNullBlockRemovesShard() throws Exception {
235256
false);
236257

237258
// Verify the shard still exists (because block [2] is still there)
238-
assertTrue("Shard should still exist because it contains another block", store.exists(shardKey));
259+
assertTrue("Shard should still exist because it contains another block", store.exists(shardKey1));
260+
assertTrue("Shard should still exist because was not affected", store.exists(shardKey2));
239261

240262
// Verify we can still read block [2]
241263
final DataBlock<int[]> readBlock = datasetAccess.readBlock(store, new long[]{2});
@@ -246,6 +268,11 @@ public void testWriteNullBlockRemovesShard() throws Exception {
246268
final DataBlock<int[]> readBlock2 = datasetAccess.readBlock(store, new long[]{3});
247269
assertNull("Block [3] should be null after removal", readBlock2);
248270

271+
// Verify block [4] exists
272+
final DataBlock<int[]> readBlock3 = datasetAccess.readBlock(store, new long[]{4});
273+
assertTrue("Block [4] should still be readable", readBlock3 != null);
274+
assertTrue("Block [4] data should match", Arrays.equals(block3.getData(), readBlock3.getData()));
275+
249276
// Write a null block at block [2]'s location
250277
final long[] regionMin2 = {6}; // pixel position of block [3]
251278
final long[] regionSize2 = {3};
@@ -257,7 +284,8 @@ public void testWriteNullBlockRemovesShard() throws Exception {
257284
(gridPosition, existingBlock) -> null,
258285
false);
259286

260-
assertFalse("Shard should not exist after deleting all blocks", store.exists(shardKey));
287+
assertFalse("Shard should not exist after deleting all blocks", store.exists(shardKey1));
288+
assertTrue("Shard should still exist because was not affected", store.exists(shardKey2));
261289
}
262290

263291
private static DataBlock<int[]> createDataBlock(int[] size, long[] gridPosition, int startValue) {

0 commit comments

Comments
 (0)