Made tests more stable, albeit slightly slower

This commit is contained in:
Christian Basler 2016-05-28 11:04:47 +02:00
parent b8f88b02d1
commit c1fa642b4e
2 changed files with 34 additions and 44 deletions

View File

@ -24,9 +24,7 @@ import ch.dissem.bitmessage.entity.valueobject.NetworkAddress;
import ch.dissem.bitmessage.exception.NodeException; import ch.dissem.bitmessage.exception.NodeException;
import ch.dissem.bitmessage.ports.*; import ch.dissem.bitmessage.ports.*;
import ch.dissem.bitmessage.utils.Property; import ch.dissem.bitmessage.utils.Property;
import org.junit.AfterClass; import org.junit.*;
import org.junit.BeforeClass;
import org.junit.Test;
import java.net.InetAddress; import java.net.InetAddress;
import java.util.concurrent.Future; import java.util.concurrent.Future;
@ -44,15 +42,15 @@ import static org.mockito.Mockito.mock;
public class NetworkHandlerTest { public class NetworkHandlerTest {
private static NetworkAddress localhost = new NetworkAddress.Builder().ipv4(127, 0, 0, 1).port(6001).build(); private static NetworkAddress localhost = new NetworkAddress.Builder().ipv4(127, 0, 0, 1).port(6001).build();
private static TestInventory peerInventory; private TestInventory peerInventory;
private static TestInventory nodeInventory; private TestInventory nodeInventory;
private static BitmessageContext peer; private BitmessageContext peer;
private static BitmessageContext node; private BitmessageContext node;
private static NetworkHandler networkHandler; private NetworkHandler networkHandler;
@BeforeClass @Before
public static void setUp() { public void setUp() {
peerInventory = new TestInventory(); peerInventory = new TestInventory();
peer = new BitmessageContext.Builder() peer = new BitmessageContext.Builder()
.addressRepo(mock(AddressRepository.class)) .addressRepo(mock(AddressRepository.class))
@ -99,34 +97,33 @@ public class NetworkHandlerTest {
.build(); .build();
} }
@AfterClass @After
public static void cleanUp() { public void cleanUp() {
shutdown(peer); shutdown(peer);
shutdown(node);
} }
private static void shutdown(BitmessageContext node) { private static void shutdown(BitmessageContext ctx) {
node.shutdown(); if (!ctx.isRunning()) return;
ctx.shutdown();
do { do {
try { try {
Thread.sleep(100); Thread.sleep(100);
} catch (InterruptedException ignore) { } catch (InterruptedException ignore) {
} }
} while (node.isRunning()); } while (ctx.isRunning());
} }
@Test(timeout = 5_000) @Test(timeout = 5_000)
public void ensureNodesAreConnecting() { public void ensureNodesAreConnecting() {
try { node.startup();
node.startup(); Property status;
Property status; do {
do { Thread.yield();
Thread.yield(); status = node.status().getProperty("network", "connections", "stream 0");
status = node.status().getProperty("network", "connections", "stream 0"); } while (status == null);
} while (status == null); assertEquals(1, status.getProperty("outgoing").getValue());
assertEquals(1, status.getProperty("outgoing").getValue());
} finally {
shutdown(node);
}
} }
@Test(timeout = 5_000) @Test(timeout = 5_000)
@ -141,26 +138,20 @@ public class NetworkHandlerTest {
assertThat(response, notNullValue()); assertThat(response, notNullValue());
assertThat(response.getCustomCommand(), is("test response")); assertThat(response.getCustomCommand(), is("test response"));
assertThat(response.getData(), is(data)); assertThat(response.getData(), is(data));
shutdown(node);
} }
@Test(timeout = 5_000, expected = NodeException.class) @Test(timeout = 5_000, expected = NodeException.class)
public void ensureCustomMessageWithoutResponsYieldsException() throws Exception { public void ensureCustomMessageWithoutResponsYieldsException() throws Exception {
try { byte[] data = cryptography().randomBytes(8);
byte[] data = cryptography().randomBytes(8); data[0] = (byte) 0;
data[0] = (byte) 0; CustomMessage request = new CustomMessage("test request", data);
CustomMessage request = new CustomMessage("test request", data); node.startup();
node.startup();
CustomMessage response = networkHandler.send(InetAddress.getLocalHost(), 6002, request); CustomMessage response = networkHandler.send(InetAddress.getLocalHost(), 6002, request);
assertThat(response, notNullValue()); assertThat(response, notNullValue());
assertThat(response.getCustomCommand(), is("test response")); assertThat(response.getCustomCommand(), is("test response"));
assertThat(response.getData(), is(request.getData())); assertThat(response.getData(), is(request.getData()));
} finally {
shutdown(node);
}
} }
@Test(timeout = 5_000) @Test(timeout = 5_000)

View File

@ -27,7 +27,6 @@ import ch.dissem.bitmessage.entity.valueobject.PrivateKey;
import ch.dissem.bitmessage.ports.AddressRepository; import ch.dissem.bitmessage.ports.AddressRepository;
import ch.dissem.bitmessage.ports.MessageRepository; import ch.dissem.bitmessage.ports.MessageRepository;
import ch.dissem.bitmessage.utils.UnixTime; import ch.dissem.bitmessage.utils.UnixTime;
import org.hamcrest.Matchers;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -212,14 +211,14 @@ public class JdbcMessageRepositoryTest extends TestBase {
.to(contactA) .to(contactA)
.message("Subject", "Message") .message("Subject", "Message")
.status(Plaintext.Status.SENT) .status(Plaintext.Status.SENT)
.ttl(1) .ttl(2)
.build(); .build();
message.updateNextTry(); message.updateNextTry();
assertThat(message.getRetries(), is(1)); assertThat(message.getRetries(), is(1));
assertThat(message.getNextTry(), greaterThan(UnixTime.now())); assertThat(message.getNextTry(), greaterThan(UnixTime.now()));
assertThat(message.getNextTry(), lessThanOrEqualTo(UnixTime.now(+1))); assertThat(message.getNextTry(), lessThanOrEqualTo(UnixTime.now(+2)));
repo.save(message); repo.save(message);
Thread.sleep(2100); Thread.sleep(4100);
List<Plaintext> messagesToResend = repo.findMessagesToResend(); List<Plaintext> messagesToResend = repo.findMessagesToResend();
assertThat(messagesToResend, hasSize(1)); assertThat(messagesToResend, hasSize(1));