You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuweni.apache.org by to...@apache.org on 2019/05/31 23:56:44 UTC
[incubator-tuweni] 02/03: Delegate writing to file to a specific
thread. Write the message bytes as a hex string
This is an automated email from the ASF dual-hosted git repository.
toulmean pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git
commit d806b1aca890ee8be99733815eeef6e671102bde
Author: Antoine Toulme <an...@lunar-ocean.com>
AuthorDate: Fri May 31 16:53:21 2019 -0700
Delegate writing to file to a specific thread. Write the message bytes as a hex string
---
gossip/src/main/java/org/apache/tuweni/gossip/GossipApp.java | 9 ++++++---
.../java/org/apache/tuweni/gossip/GossipIntegrationTest.java | 11 +++++------
2 files changed, 11 insertions(+), 9 deletions(-)
diff --git a/gossip/src/main/java/org/apache/tuweni/gossip/GossipApp.java b/gossip/src/main/java/org/apache/tuweni/gossip/GossipApp.java
index f92481b..8866eee 100644
--- a/gossip/src/main/java/org/apache/tuweni/gossip/GossipApp.java
+++ b/gossip/src/main/java/org/apache/tuweni/gossip/GossipApp.java
@@ -84,6 +84,7 @@ public final class GossipApp {
private final PrintStream outStream;
private final VertxGossipServer server;
private final HttpServer rpcServer;
+ private final ExecutorService fileWriter = Executors.newSingleThreadExecutor();
GossipApp(
Vertx vertx,
@@ -209,14 +210,16 @@ public final class GossipApp {
errStream.println("RPC server could not stop: " + e.getMessage());
terminateFunction.run();
}
+
+ fileWriter.shutdown();
}
private void readMessage(String messageLog, PrintStream err, Bytes bytes) {
- synchronized (this) {
+ fileWriter.submit(() -> {
ObjectMapper mapper = new ObjectMapper();
ObjectNode node = mapper.createObjectNode();
node.put("timestamp", Instant.now().toString());
- node.put("value", new String(bytes.toArrayUnsafe(), StandardCharsets.UTF_8));
+ node.put("value", bytes.toHexString());
try {
Path path = Paths.get(messageLog);
Files.write(
@@ -227,7 +230,7 @@ public final class GossipApp {
} catch (IOException e) {
err.println(e.getMessage());
}
- }
+ });
}
public void publish(Bytes message) {
diff --git a/gossip/src/test/java/org/apache/tuweni/gossip/GossipIntegrationTest.java b/gossip/src/test/java/org/apache/tuweni/gossip/GossipIntegrationTest.java
index e0418a9..89a9dcb 100644
--- a/gossip/src/test/java/org/apache/tuweni/gossip/GossipIntegrationTest.java
+++ b/gossip/src/test/java/org/apache/tuweni/gossip/GossipIntegrationTest.java
@@ -19,7 +19,6 @@ import org.apache.tuweni.bytes.Bytes;
import org.apache.tuweni.bytes.Bytes32;
import org.apache.tuweni.junit.*;
-import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
@@ -114,15 +113,15 @@ class GossipIntegrationTest {
List<String> sent = new ArrayList<>();
for (int i = 0; i < 20; i++) {
- String message = Bytes32.rightPad(Bytes.ofUnsignedInt(i)).toHexString();
- sent.add(message);
+ Bytes message = Bytes32.rightPad(Bytes.ofUnsignedInt(i));
+ sent.add(message.toHexString());
Thread.sleep(100);
client.request(HttpMethod.POST, 10000, "127.0.0.1", "/publish").exceptionHandler(thr -> {
throw new RuntimeException(thr);
}).handler(resp -> {
- }).end(Buffer.buffer(message.getBytes(StandardCharsets.UTF_8)));
+ }).end(Buffer.buffer(message.toArrayUnsafe()));
}
List<String> receiver1 = Collections.emptyList();
@@ -162,7 +161,7 @@ class GossipIntegrationTest {
receiver2Expected.remove(value);
}
- assertTrue(receiver1Expected.isEmpty());
- assertTrue(receiver2Expected.isEmpty());
+ assertTrue(receiver1Expected.isEmpty(), "Elements left:" + receiver1Expected);
+ assertTrue(receiver2Expected.isEmpty(), "Elements left:" + receiver2Expected);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@tuweni.apache.org
For additional commands, e-mail: commits-help@tuweni.apache.org