Skip to content

Commit

Permalink
test: an explicit test of N5CacheTest
Browse files Browse the repository at this point in the history
  • Loading branch information
bogovicj committed Apr 13, 2023
1 parent 6b36c8c commit e7cdd33
Showing 1 changed file with 121 additions and 0 deletions.
121 changes: 121 additions & 0 deletions src/test/java/org/janelia/saalfeldlab/n5/cache/N5CacheTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package org.janelia.saalfeldlab.n5.cache;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.google.gson.JsonElement;

public class N5CacheTest {

@Test
public void cacheBackingTest() {

final DummyBackingStorage backingStorage = new DummyBackingStorage();
final N5JsonCache cache = new N5JsonCache(backingStorage::backingAttrs, backingStorage::exists,
backingStorage::isGroup, backingStorage::isDataset, backingStorage::list);

// check existance, ensure backing storage is only called once
assertEquals(0, backingStorage.existsCallCount);
cache.exists("a");
assertEquals(1, backingStorage.existsCallCount);
cache.exists("a");
assertEquals(1, backingStorage.existsCallCount);

// check existance of new group, ensure backing storage is only called one more time
cache.exists("b");
assertEquals(2, backingStorage.existsCallCount);
cache.exists("b");
assertEquals(2, backingStorage.existsCallCount);

// check isDataset, ensure backing storage is only called when expected
// isDataset is called by exists, so should have been called twice here
assertEquals(2, backingStorage.isDatasetCallCount);
cache.isDataset("a");
assertEquals(2, backingStorage.isDatasetCallCount);

assertEquals(2, backingStorage.isDatasetCallCount);
cache.isDataset("b");
assertEquals(2, backingStorage.isDatasetCallCount);

// check isGroup, ensure backing storage is only called when expected
// isGroup is called by exists, so should have been called twice here
assertEquals(2, backingStorage.isGroupCallCount);
cache.isDataset("a");
assertEquals(2, backingStorage.isGroupCallCount);

assertEquals(2, backingStorage.isGroupCallCount);
cache.isDataset("b");
assertEquals(2, backingStorage.isGroupCallCount);

// similarly check list, ensure backing storage is only called when expected
// list is called by exists, so should have been called twice here
assertEquals(2, backingStorage.listCallCount);
cache.list("a");
assertEquals(2, backingStorage.listCallCount);

assertEquals(2, backingStorage.listCallCount);
cache.list("b");
assertEquals(2, backingStorage.listCallCount);

// finally check getAttributes
// it is not called by exists (since it needs the cache key)
assertEquals(0, backingStorage.attrCallCount);
cache.getAttributes("a", "foo");
assertEquals(1, backingStorage.attrCallCount);
cache.getAttributes("a", "foo");
assertEquals(1, backingStorage.attrCallCount);
cache.getAttributes("a", "bar");
assertEquals(2, backingStorage.attrCallCount);
cache.getAttributes("a", "bar");
assertEquals(2, backingStorage.attrCallCount);

cache.getAttributes("b", "foo");
assertEquals(3, backingStorage.attrCallCount);
cache.getAttributes("b", "foo");
assertEquals(3, backingStorage.attrCallCount);
cache.getAttributes("b", "bar");
assertEquals(4, backingStorage.attrCallCount);
cache.getAttributes("b", "bar");
assertEquals(4, backingStorage.attrCallCount);

}

protected static class DummyBackingStorage {

int attrCallCount = 0;
int existsCallCount = 0;
int isGroupCallCount = 0;
int isDatasetCallCount = 0;
int listCallCount = 0;

public DummyBackingStorage() {
}

public JsonElement backingAttrs(final String key, final String cacheKey) {
attrCallCount++;
return null;
}

public boolean exists(final String key) {
existsCallCount++;
return true;
}

public boolean isGroup(final String key) {
isGroupCallCount++;
return true;
}

public boolean isDataset(final String key) {
isDatasetCallCount++;
return true;
}

public String[] list(final String key) {
listCallCount++;
return new String[] { "list" };
}
}

}

0 comments on commit e7cdd33

Please sign in to comment.