Skip to content

Commit

Permalink
Attempt to stabilize transport listener IT's and EntryWatcherTest.
Browse files Browse the repository at this point in the history
  • Loading branch information
chilimannen committed Apr 1, 2017
1 parent 2ee0236 commit fb600a5
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 51 deletions.
45 changes: 20 additions & 25 deletions core/test/java/com/codingchili/core/storage/EntryWatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@
@RunWith(VertxUnitRunner.class)
public class EntryWatcherTest {
private static final String TEST_NAME = "TEST_NAME";
private static final String DB = "db";
private static final String COLLECTION = "collection";
private static final String DB = "entrywatcher";
private static final String COLLECTION = "test";
private static final String LEVEL = "level";
private static final int WAIT_MS = 500;
private static final int REMOVE_INTERVAL = 50;
private static final int LEVEL_PERSIST = 50;
private static final int LEVEL_REMOVE = 0;
private AsyncStorage<StorageObject> storage;
private StorageObject object = new StorageObject(TEST_NAME, 5);
private StorageContext context;

@Rule
public Timeout timeout = new Timeout(60, TimeUnit.SECONDS);
public Timeout timeout = new Timeout(10, TimeUnit.SECONDS);

@Before
public void setUp(TestContext test) {
Expand All @@ -44,7 +47,7 @@ public void setUp(TestContext test) {

getQuery().poll(entry -> entry.forEach(item -> {
storage.remove(item.id(), removed -> {});
}), this::getInterval);
}), () -> REMOVE_INTERVAL);

storage.put(object, put -> {
test.assertTrue(put.succeeded());
Expand All @@ -56,12 +59,8 @@ public void setUp(TestContext test) {
});
}

private int getInterval() {
return 150;
}

private QueryBuilder<StorageObject> getQuery() {
return storage.query(LEVEL).between(Long.MIN_VALUE, 0L)
return storage.query(LEVEL).between(Long.MIN_VALUE, 0L)
.or(LEVEL).between(100L, Long.MAX_VALUE);
}

Expand All @@ -84,13 +83,13 @@ public void tearDown(TestContext test) {
}

private void setPersist() {
object.setLevel(50);
object.setLevel(LEVEL_PERSIST);
storage.update(object, result -> {
});
}

private void setRemove() {
object.setLevel(0);
object.setLevel(LEVEL_REMOVE);
storage.update(object, result -> {
});
}
Expand All @@ -100,26 +99,22 @@ public void testItemRemovedOnHandler(TestContext test) {
Async async = test.async();
setRemove();

context.timer(WAIT_MS, handler -> {
storage.get(TEST_NAME, event -> {
test.assertFalse(event.succeeded());
test.assertNull(event.result());
async.complete();
});
});
context.timer(WAIT_MS, handler -> storage.get(TEST_NAME, event -> {
test.assertFalse(event.succeeded());
test.assertNull(event.result());
async.complete();
}));
}

@Test
public void testRealmNotRemovedWhenNotStale(TestContext test) {
Async async = test.async();
setPersist();

context.timer(WAIT_MS, handler -> {
storage.get(TEST_NAME, get -> {
test.assertTrue(get.succeeded());
test.assertNotNull(get.result());
async.complete();
});
});
context.timer(WAIT_MS, handler -> storage.get(TEST_NAME, get -> {
test.assertTrue(get.succeeded());
test.assertNotNull(get.result());
async.complete();
}));
}
}
21 changes: 15 additions & 6 deletions services/router/main/java/com/codingchili/router/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@
import com.codingchili.router.controller.transport.*;
import io.vertx.core.*;

import java.util.ArrayList;
import java.util.List;

import com.codingchili.core.protocol.ClusterNode;

import static io.vertx.core.CompositeFuture.all;

/**
* @author Robin Duda
* root game server, deploys realmName servers.
*/
public class Service extends ClusterNode {
private RouterContext context;

public Service() {}
public Service() {
}

public Service(RouterContext context) {
this.context = context;
Expand All @@ -32,33 +38,36 @@ public void init(Vertx vertx, Context context) {

@Override
public void start(Future<Void> start) {
List<Future> deployments = new ArrayList<>();

for (ListenerSettings listener : context.transports()) {
RouterHandler<RouterContext> handler = new RouterHandler<>(context);

for (int i = 0; i < settings.getHandlers(); i++) {
boolean singleHandlerOnly = false;
Future<String> future = Future.future();
deployments.add(future);
boolean singleHandlerOnly = false;

switch (listener.getType()) {
case UDP:
context.deploy(new UdpListener(handler), future);
singleHandlerOnly = true;
break;
case TCP:
context.deploy(new TcpListener(handler));
context.deploy(new TcpListener(handler), future);
break;
case WEBSOCKET:
context.deploy(new WebsocketListener(handler));
context.deploy(new WebsocketListener(handler), future);
break;
case REST:
context.deploy(new RestListener(handler));
context.deploy(new RestListener(handler), future);
break;
}
if (singleHandlerOnly) {
break;
}
}
}
start.complete();
all(deployments).setHandler(done -> start.complete());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.vertx.core.http.HttpServerOptions;

import java.util.HashMap;
import java.util.Map;
import java.util.*;

/**
* @author Robin Duda
Expand All @@ -17,6 +16,7 @@ public class ListenerSettings {
private HttpServerOptions httpOptions = new HttpServerOptions();
private Map<String, Endpoint> api = new HashMap<>();
private WireType type = WireType.REST;
private Set<Integer> actualPorts = new HashSet<>();
private int port = 8080;
private int timeout = 3000;
private int maxRequestBytes = 64;
Expand Down Expand Up @@ -87,4 +87,22 @@ public ListenerSettings addMapping(String route, Endpoint endpoint) {
api.put(route, endpoint);
return this;
}

/**
* @param port adds a port that the server is listening to. useful if the
* port is set to 0.
*/
public void addListenPort(int port) {
actualPorts.add(port);
}

/**
* @return a list of ports the listener is listening to. this list contains
* all ports that are being listened to for the configuration, which may
* differ from the requested listening port.
*/
@JsonIgnore
public Set<Integer> getListenPorts() {
return actualPorts;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void start(Future<Void> start) {
.requestHandler(router::accept)
.listen(listener().getPort(), getBindAddress(), listen -> {
if (listen.succeeded()) {
listener().addListenPort(listen.result().actualPort());
handler.start(start);
} else {
start.fail(listen.cause());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void start(Future<Void> start) {
handler.handler(data -> packet(handler, data));
}).listen(listener().getPort(), getBindAddress(), listen -> {
if (listen.succeeded()) {
listener().addListenPort(listen.result().actualPort());
handler.start(start);
} else {
start.fail(listen.cause());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public UdpListener(RouterHandler<RouterContext> handler) {
public void start(Future<Void> start) {
vertx.createDatagramSocket().listen(listener().getPort(), getBindAddress(), listen -> {
if (listen.succeeded()) {
listener().addListenPort(listen.result().localAddress().port());
listen.result().handler(this::handle);
handler.start(start);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void start(Future<Void> start) {
socket.handler(data -> handle(socket, data));
}).listen(listener().getPort(), getBindAddress(), listen -> {
if (listen.succeeded()) {
listener().addListenPort(listen.result().actualPort());
handler.start(start);
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.codingchili.core.protocol.ResponseStatus;

import static com.codingchili.common.Strings.*;
import static com.codingchili.core.protocol.ResponseStatus.ACCEPTED;

/**
* @author Robin Duda
Expand All @@ -32,7 +33,7 @@ public void testMappedApiRoutes(TestContext context) {
mockNode(NODE_PATCHING);

sendRequest((result, status) -> {
context.assertEquals(ResponseStatus.ACCEPTED, status);
context.assertEquals(ACCEPTED, status);
context.assertEquals(NODE_PATCHING, result.getString(PROTOCOL_TARGET));
async.complete();
}, new JsonObject().put(PROTOCOL_ROUTE, PATCHING_ROOT));
Expand All @@ -45,7 +46,7 @@ public void testUnmappedApiRoutesWebserver(TestContext context) {
mockNode(NODE_WEBSERVER);

sendRequest((result, status) -> {
context.assertEquals(ResponseStatus.ACCEPTED, status);
context.assertEquals(ACCEPTED, status);
context.assertEquals(NODE_WEBSERVER, result.getString(PROTOCOL_TARGET));
async.complete();
}, new JsonObject().put(PROTOCOL_ROUTE, SAMPLE_URL));
Expand All @@ -55,9 +56,9 @@ public void testUnmappedApiRoutesWebserver(TestContext context) {
public void testRouterSupportsGet(TestContext context) {
Async async = context.async();

sendGetRequest("/?" + PROTOCOL_ROUTE + "=" + ID_PING + "&" + PROTOCOL_TARGET + "=" + NODE_ROUTING,
sendGetRequest(String.format("/?%s=%s&%s=%s", PROTOCOL_ROUTE, ID_PING, PROTOCOL_TARGET, NODE_ROUTING),
(result, status) -> {
context.assertEquals(ResponseStatus.ACCEPTED, status);
context.assertEquals(ACCEPTED, status);
async.complete();
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.codingchili.router.controller.transport;

import com.codingchili.common.Strings;
import com.codingchili.router.Service;
import com.codingchili.router.configuration.ListenerSettings;
import com.codingchili.router.configuration.RouterSettings;
Expand All @@ -18,7 +17,6 @@
import org.junit.runner.RunWith;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import com.codingchili.core.protocol.ResponseStatus;
import com.codingchili.core.security.RemoteIdentity;
Expand All @@ -35,33 +33,35 @@
public abstract class TransportTestCases {
static final String PATCHING_ROOT = "/patching";
static final String HOST = getLoopbackAddress();
private static final AtomicInteger PORT = new AtomicInteger(9896);
private static final int MAX_REQUEST_BYTES = 256;
private static final String ONE_CHAR = "x";
private static final String DATA = "data";
private ContextMock context;
private WireType wireType;
int port;
Vertx vertx;
protected int port;
protected static Vertx vertx;

TransportTestCases(WireType wireType) {
this.wireType = wireType;
}

@Rule
public Timeout timeout = new Timeout(8, TimeUnit.SECONDS);
public Timeout timeout = new Timeout(30, TimeUnit.SECONDS);

@BeforeClass
public static void setUpClass() {
vertx = Vertx.vertx();
}

@Before
public void setUp(TestContext test) {
Async async = test.async();

vertx = Vertx.vertx();
port = PORT.getAndIncrement();
context = new ContextMock(vertx);

ListenerSettings listener = new ListenerSettings()
.setMaxRequestBytes(MAX_REQUEST_BYTES)
.setPort(port)
.setPort(0)
.setType(wireType)
.setTimeout(7000)
.setHttpOptions(new HttpServerOptions().setCompressionSupported(false))
Expand All @@ -71,19 +71,18 @@ public void setUp(TestContext test) {
.addHidden(NODE_LOGGING)
.addTransport(listener);

settings.addHidden(Strings.NODE_LOGGING);
settings.addTransport(listener);
context.setSettings(settings);

vertx.deployVerticle(new Service(context), deploy -> {
this.port = listener.getListenPorts().iterator().next();
test.assertTrue(deploy.succeeded());
async.complete();
});
}

@After
public void tearDown(TestContext context) {
vertx.close(context.asyncAssertSuccess());
@AfterClass
public static void tearDown(TestContext test) {
vertx.close(test.asyncAssertSuccess());
}

@Test
Expand Down

0 comments on commit fb600a5

Please sign in to comment.