Skip to content

Commit

Permalink
Fix sharing for IndexedMap and add testcase.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Duda committed Mar 17, 2017
1 parent 6bde3ab commit 0c59002
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ public void onWatcherCompleted(String name, int affected) {

public void onWatcherPaused(String name) {
log(event(LOG_STORAGE_WATCHER)
.put(ID_NAME, name)
.put(ID_MESSAGE, CoreStrings.WATCHER_PAUSED));
.put(ID_NAME, name)
.put(ID_MESSAGE, CoreStrings.WATCHER_PAUSED));
}

public void onWatcherResumed(String name) {
Expand Down Expand Up @@ -251,14 +251,11 @@ public StorageContext<Value> setDB(String DB) {
*/
public StorageContext<Value> setPlugin(String plugin) {
this.plugin = plugin;
updateIdentifier();
return this;
}

private void updateIdentifier() {
if (plugin != null) {
this.identifier = CoreStrings.getDBIdentifier(DB, collection);
}
this.identifier = CoreStrings.getDBIdentifier(DB, collection);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class IndexedMap<Value extends Storable> implements AsyncStorage<Value> {

@SuppressWarnings("unchecked")
public IndexedMap(Future<AsyncStorage<Value>> future, StorageContext<Value> context) {
if (maps.containsKey(context().identifier())) {
if (maps.containsKey(context.identifier())) {
future.complete((IndexedMap<Value>) maps.get(context.identifier()));
} else {
this.context = context;
Expand Down
36 changes: 30 additions & 6 deletions core/main/java/com/codingchili/core/testing/MapTestCases.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@
@Ignore("Extend this class to run the tests.")
@RunWith(VertxUnitRunner.class)
public class MapTestCases {
protected static final String DB = "spinach";
protected static final String COLLECTION = "leaves";
protected StorageContext<StorageObject> context;
protected AsyncStorage<StorageObject> store;
protected static Integer STARTUP_DELAY = 1;
private static final Long TEST_ITEM_COUNT = 200L;
private static final Long SNOWFLAKE_INTERVAL = 10L;
private static final Long SNOWFLAKE_COUNT = TEST_ITEM_COUNT / SNOWFLAKE_INTERVAL;
Expand All @@ -48,6 +43,12 @@ public class MapTestCases {
private static final StorageObject OBJECT_TWO = new StorageObject(TWO, 2);
private static final String SNOW_KEYWORD = "SNOW";
private static final int LEVEL_BUCKET_SIZE = 10;
private Class plugin;
protected static final String DB = "spinach";
protected static final String COLLECTION = "leaves";
protected StorageContext<StorageObject> context;
protected AsyncStorage<StorageObject> store;
protected static Integer STARTUP_DELAY = 1;

@Rule
public Timeout timeout = Timeout.seconds(10);
Expand All @@ -67,7 +68,8 @@ public void tearDown(TestContext test) {
}

public void setUp(Async async, Class plugin, Vertx vertx) {
context = new StorageContext<>(vertx);
this.context = new StorageContext<>(vertx);
this.plugin = plugin;

new StorageLoader<StorageObject>(context)
.withDB(DB, COLLECTION)
Expand Down Expand Up @@ -730,4 +732,26 @@ public void testPollStorage(TestContext test) {
}
}, () -> 50);
}

@Test
public void testStorageIsShared(TestContext test) {
Async async = test.async();
StorageContext context2 = new StorageContext<>(context.vertx());

// creates a new storage using another context with the same DB/colletion
new StorageLoader<StorageObject>(context2)
.withDB(DB, COLLECTION)
.withClass(StorageObject.class)
.withPlugin(plugin)
.build(result -> {
AsyncStorage<StorageObject> newStorage = result.result();
store.size(size -> {
newStorage.size(newSize -> {
test.assertEquals(size.result(), newSize.result());
test.assertEquals(newSize.result(), TEST_ITEM_COUNT);
async.complete();
});
});
});
}
}
4 changes: 4 additions & 0 deletions core/test/java/com/codingchili/core/storage/JsonMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ public void testMapSaved(TestContext test) {
}
}));
}

@Ignore("Not supported yet, saving the map breaks this.")
public void testStorageIsShared(TestContext test) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.vertx.ext.unit.junit.VertxUnitRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.runner.RunWith;

import com.codingchili.core.testing.MapTestCases;
Expand All @@ -26,4 +27,8 @@ public void setUp(TestContext test) {
public void tearDown(TestContext test) {
super.tearDown(test);
}

@Ignore("Map not shared.")
public void testStorageIsShared(TestContext test) {
}
}

0 comments on commit 0c59002

Please sign in to comment.