Skip to content

Commit

Permalink
Assign oldObject in the JoinMapLaneRelayUpdate.beginPhase method when…
Browse files Browse the repository at this point in the history
… phase is 2

Fix for issue #66
  • Loading branch information
ajay-gov committed Oct 12, 2021
1 parent bcbb1fd commit 15dddeb
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,13 @@ public void didClose() {
.keyClass(String.class)
.valueClass(String.class)
.hostUri("warp://localhost:53556/")
.nodeUri("/join/map/all")
.nodeUri("/join/mapA/all")
.laneUri("join")
.observe(new JoinMapLinkController())
.open();

joinDidReceive.await(1, TimeUnit.SECONDS);
joinDidUpdate.await(1, TimeUnit.SECONDS);
joinDidReceive.await(3, TimeUnit.SECONDS);
joinDidUpdate.await(3, TimeUnit.SECONDS);
assertEquals(joinDidReceive.getCount(), 0);
assertEquals(joinDidUpdate.getCount(), 0);
assertEquals(join.size(), 4);
Expand All @@ -179,6 +179,63 @@ public void didClose() {
}
}

private static CountDownLatch laneWillDownlink = new CountDownLatch(2);
private static CountDownLatch laneDidDownlink = new CountDownLatch(2);
private static CountDownLatch laneWillUpdate = new CountDownLatch(4);
private static CountDownLatch laneDidUpdate0 = new CountDownLatch(2);
private static CountDownLatch laneDidUpdate1 = new CountDownLatch(2);
private static CountDownLatch laneWillRemove = new CountDownLatch(1);
private static CountDownLatch laneDidRemove = new CountDownLatch(1);

@Test
public void testJoinMapLaneCallback() throws InterruptedException {
final Kernel kernel = ServerLoader.loadServerStack();
final TestJoinMapPlane plane = kernel.openSpace(ActorSpaceDef.fromName("test"))
.openPlane("test", TestJoinMapPlane.class);

try {
kernel.openService(WebServiceDef.standard().port(53556).spaceName("test"));
kernel.start();
final MapDownlink<String, String> xs = plane.downlinkMap()
.keyClass(String.class)
.valueClass(String.class)
.hostUri("warp://localhost:53556/")
.nodeUri("/map/xs")
.laneUri("map")
.open();
xs.didSync(() -> {
xs.put("x0", "a");
xs.put("x1", "b");
});

final MapDownlink<String, String> join = plane.downlinkMap()
.keyClass(String.class)
.valueClass(String.class)
.hostUri("warp://localhost:53556/")
.nodeUri("/join/mapB/all")
.laneUri("join")
.open();

laneDidUpdate0.await(3, TimeUnit.SECONDS);
assertEquals(laneWillDownlink.getCount(), 0);
assertEquals(laneDidDownlink.getCount(), 0);
assertEquals(laneWillUpdate.getCount(), 2);
assertEquals(laneDidUpdate0.getCount(), 0);

xs.put("x0", "aa");
xs.put("x1", "bb");

laneDidUpdate1.await(3, TimeUnit.SECONDS);
assertEquals(laneWillDownlink.getCount(), 0);
assertEquals(laneDidDownlink.getCount(), 0);
assertEquals(laneWillUpdate.getCount(), 0);
assertEquals(laneDidUpdate1.getCount(), 0);

} finally {
kernel.stop();
}
}

static class TestMapLaneAgent extends AbstractAgent {

@SwimLane("map")
Expand Down Expand Up @@ -213,7 +270,20 @@ public void didRemove(String key, String oldValue) {

}

static class TestJoinMapLaneAgent extends AbstractAgent {
static class TestJoinMapLaneAgentA extends AbstractAgent {

@SwimLane("join")
JoinMapLane<String, String, String> testJoinMap = this.<String, String, String>joinMapLane();

@Override
public void didStart() {
this.testJoinMap.downlink("xs").hostUri("warp://localhost:53556").nodeUri("/map/xs").laneUri("map").open();
this.testJoinMap.downlink("ys").hostUri("warp://localhost:53556").nodeUri("/map/ys").laneUri("map").open();
}

}

static class TestJoinMapLaneAgentB extends AbstractAgent {

@SwimLane("join")
JoinMapLane<String, String, String> testJoinMap = this.<String, String, String>joinMapLane()
Expand All @@ -232,33 +302,51 @@ class TestJoinMapLaneController implements WillDownlinkMap<String>, DidDownlinkM
@Override
public MapDownlink<?, ?> willDownlink(String key, MapDownlink<?, ?> downlink) {
System.out.println(nodeUri() + " willDownlink key: " + Format.debug(key) + "; downlink: " + downlink);
laneWillDownlink.countDown();
return downlink;
}

@Override
public void didDownlink(String key, MapDownlink<?, ?> downlink) {
System.out.println(nodeUri() + " didDownlink key: " + Format.debug(key) + "; downlink: " + downlink);
laneDidDownlink.countDown();
}

@Override
public String willUpdate(String key, String newValue) {
System.out.println(nodeUri() + " willUpdate key: " + Format.debug(key) + "; newValue: " + Format.debug(newValue));
laneWillUpdate.countDown();
return newValue;
}

@Override
public void didUpdate(String key, String newValue, String oldValue) {
System.out.println(nodeUri() + " didUpdate key: " + Format.debug(key) + "; newValue: " + Format.debug(newValue) + "; oldValue: " + Format.debug(oldValue));
if (key.equals("x0") && newValue.equals("a")) {
//assertEquals(oldValue, "");
laneDidUpdate0.countDown();
} else if (key.equals("x0") && newValue.equals("aa")) {
assertEquals(oldValue, "a");
laneDidUpdate1.countDown();
} else if (key.equals("x1") && newValue.equals("b")) {
assertEquals(oldValue, "");
laneDidUpdate0.countDown();
} else if (key.equals("x1") && newValue.equals("bb")) {
//assertEquals(oldValue, "b");
laneDidUpdate1.countDown();
}
}

@Override
public void willRemove(String key) {
System.out.println(nodeUri() + " willRemove key: " + Format.debug(key));
laneWillRemove.countDown();
}

@Override
public void didRemove(String key, String oldValue) {
System.out.println(nodeUri() + " didRemove key: " + Format.debug(key) + "; oldValue: " + Format.debug(oldValue));
laneDidRemove.countDown();
}

}
Expand All @@ -270,8 +358,11 @@ static class TestJoinMapPlane extends AbstractPlane {
@SwimRoute("/map/:name")
AgentRoute<TestMapLaneAgent> mapRoute;

@SwimRoute("/join/map/:name")
AgentRoute<TestJoinMapLaneAgent> joinMapRoute;
@SwimRoute("/join/mapA/:name")
AgentRoute<TestJoinMapLaneAgentA> joinMapA;

@SwimRoute("/join/mapB/:name")
AgentRoute<TestJoinMapLaneAgentB> joinMapB;

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,14 @@ protected void beginPhase(int phase) {
if (this.oldValue == null) {
this.oldValue = Value.absent();
}

if (this.valueForm != null) {
this.oldObject = this.valueForm.cast(this.oldValue);
if (this.oldObject == null) {
this.oldObject = this.valueForm.unit();
}
}

}
}

Expand Down

0 comments on commit 15dddeb

Please sign in to comment.