From 7fb837645fcb7c4df79252415db54cd0f5fd5794 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Fri, 25 Sep 2015 23:35:31 +0200 Subject: [PATCH] Synchronisation now shouldn't fail if the trusted host has no new messages - fixed tests for Gradle builds - fixed SerializationTest --- domain/build.gradle | 13 +++++++++++++ .../bitmessage/entity/SerializationTest.java | 16 +++++++++++----- networking/build.gradle | 1 + .../dissem/bitmessage/networking/Connection.java | 13 ++++++++++--- .../networking/NetworkHandlerTest.java | 2 +- 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/domain/build.gradle b/domain/build.gradle index 6bcbe6d..1880c6b 100644 --- a/domain/build.gradle +++ b/domain/build.gradle @@ -10,6 +10,19 @@ uploadArchives { } } +configurations { + testArtifacts.extendsFrom testRuntime +} + +task testJar(type: Jar) { + classifier = 'test' + from sourceSets.test.output +} + +artifacts { + testArtifacts testJar +} + dependencies { compile 'org.slf4j:slf4j-api:1.7.12' testCompile 'junit:junit:4.11' diff --git a/domain/src/test/java/ch/dissem/bitmessage/entity/SerializationTest.java b/domain/src/test/java/ch/dissem/bitmessage/entity/SerializationTest.java index de0c807..03b7bc5 100644 --- a/domain/src/test/java/ch/dissem/bitmessage/entity/SerializationTest.java +++ b/domain/src/test/java/ch/dissem/bitmessage/entity/SerializationTest.java @@ -18,18 +18,17 @@ package ch.dissem.bitmessage.entity; import ch.dissem.bitmessage.entity.payload.*; import ch.dissem.bitmessage.entity.valueobject.Label; -import ch.dissem.bitmessage.exception.DecryptionFailedException; import ch.dissem.bitmessage.factory.Factory; import ch.dissem.bitmessage.utils.TestBase; import ch.dissem.bitmessage.utils.TestUtils; import org.junit.Test; import java.io.*; -import java.util.Arrays; +import java.lang.reflect.Field; +import java.util.Collections; import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; public class SerializationTest extends TestBase { @Test @@ -87,6 +86,12 @@ public class SerializationTest extends TestBase { p1.write(out); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); Plaintext p2 = Plaintext.read(MSG, in); + + // Received is automatically set on deserialization, so we'll need to set it to 0 + Field received = Plaintext.class.getDeclaredField("received"); + received.setAccessible(true); + received.set(p2, 0L); + assertEquals(p1, p2); } @@ -95,6 +100,7 @@ public class SerializationTest extends TestBase { InputStream in = new ByteArrayInputStream(data); ObjectMessage object = Factory.getObjectMessage(version, in, data.length); ByteArrayOutputStream out = new ByteArrayOutputStream(); + assertNotNull(object); object.write(out); assertArrayEquals(data, out.toByteArray()); assertEquals(expectedPayloadType.getCanonicalName(), object.getPayload().getClass().getCanonicalName()); @@ -105,7 +111,7 @@ public class SerializationTest extends TestBase { Plaintext plaintext = new Plaintext.Builder(MSG) .from(TestUtils.loadContact()) .to(TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8")) - .labels(Arrays.asList(new Label("Test", Label.Type.INBOX, 0))) + .labels(Collections.singletonList(new Label("Test", Label.Type.INBOX, 0))) .message("Test", "Test Test.\nTest") .build(); ByteArrayOutputStream out = new ByteArrayOutputStream(); diff --git a/networking/build.gradle b/networking/build.gradle index 6e9366c..06bd5d0 100644 --- a/networking/build.gradle +++ b/networking/build.gradle @@ -15,5 +15,6 @@ dependencies { testCompile 'junit:junit:4.11' testCompile 'org.slf4j:slf4j-simple:1.7.12' testCompile 'org.mockito:mockito-core:1.10.19' + testCompile project(path: ':domain', configuration: 'testArtifacts') testCompile project(':security-bc') } \ No newline at end of file diff --git a/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java b/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java index bb8e62c..400bada 100644 --- a/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java +++ b/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java @@ -25,7 +25,6 @@ import ch.dissem.bitmessage.exception.InsufficientProofOfWorkException; import ch.dissem.bitmessage.exception.NodeException; import ch.dissem.bitmessage.factory.Factory; import ch.dissem.bitmessage.ports.NetworkHandler.MessageListener; -import ch.dissem.bitmessage.utils.DebugUtils; import ch.dissem.bitmessage.utils.UnixTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -70,6 +69,7 @@ public class Connection implements Runnable { private OutputStream out; private int version; private long[] streams; + private int readTimeoutCounter; public Connection(InternalContext context, Mode mode, Socket socket, MessageListener listener, ConcurrentMap requestedObjectsMap) throws IOException { @@ -185,6 +185,7 @@ public class Connection implements Runnable { } catch (SocketTimeoutException ignore) { if (state == ACTIVE) { sendQueue(); + if (syncFinished(null)) disconnect(); } } } @@ -205,7 +206,13 @@ public class Connection implements Runnable { if (syncTimeout < UnixTime.now()) { return true; } - if (!(msg.getPayload() instanceof Addr) && requestedObjects.isEmpty() && sendingQueue.isEmpty()) { + if (msg == null) { + readTimeoutCounter++; + return readTimeoutCounter > 1; + } + readTimeoutCounter = 0; + if (!(msg.getPayload() instanceof Addr) && !(msg.getPayload() instanceof GetData) + && requestedObjects.isEmpty() && sendingQueue.isEmpty()) { return true; } return false; @@ -311,9 +318,9 @@ public class Connection implements Runnable { ctx.getNetworkHandler().offer(objectMessage.getInventoryVector()); } catch (InsufficientProofOfWorkException e) { LOG.warn(e.getMessage()); + // DebugUtils.saveToFile(objectMessage); // this line must not be committed active } catch (IOException e) { LOG.error("Stream " + objectMessage.getStream() + ", object type " + objectMessage.getType() + ": " + e.getMessage(), e); - DebugUtils.saveToFile(objectMessage); } finally { requestedObjects.remove(objectMessage.getInventoryVector()); } diff --git a/networking/src/test/java/ch/dissem/bitmessage/networking/NetworkHandlerTest.java b/networking/src/test/java/ch/dissem/bitmessage/networking/NetworkHandlerTest.java index cd8a743..7beca24 100644 --- a/networking/src/test/java/ch/dissem/bitmessage/networking/NetworkHandlerTest.java +++ b/networking/src/test/java/ch/dissem/bitmessage/networking/NetworkHandlerTest.java @@ -122,7 +122,7 @@ public class NetworkHandlerTest { assertEquals(2, peerInventory.getInventory().size()); } - @Test(timeout = 5_000) + @Test(timeout = 10_000) public void ensureObjectsAreSynchronizedIfOnlyNodeHasObjects() throws Exception { peerInventory.init();