Skip to content

Commit 042130d

Browse files
Artem LabazinArtem Labazin
authored andcommitted
Fix tests
1 parent 4150b6e commit 042130d

5 files changed

Lines changed: 40 additions & 22 deletions

File tree

client/src/main/java/io/appulse/epmd/java/client/Connection.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,32 @@ class Connection implements Closeable {
6767

6868
Socket socket = new Socket();
6969

70-
@SneakyThrows
71-
public void send (@NonNull Object request) {
70+
public void send (@NonNull Object request) throws EpmdConnectionException {
7271
log.debug("Sending: {}", request);
7372

7473
val bytes = SERIALIZER.serialize(request);
7574

7675
connect();
7776

78-
socket.getOutputStream().write(bytes);
79-
socket.getOutputStream().flush();
77+
try {
78+
socket.getOutputStream().write(bytes);
79+
socket.getOutputStream().flush();
80+
} catch (IOException ex) {
81+
throw new EpmdConnectionException(ex);
82+
}
8083

8184
log.debug("Message {} was sent", request);
8285
}
8386

84-
public <T> T send (@NonNull Object request, @NonNull Class<T> responseType) throws IOException {
87+
public <T> T send (@NonNull Object request, @NonNull Class<T> responseType) throws EpmdConnectionException {
8588
send(request);
8689

87-
val messageBytes = read(responseType);
90+
byte[] messageBytes;
91+
try {
92+
messageBytes = read(responseType);
93+
} catch (IOException ex) {
94+
throw new EpmdConnectionException(ex);
95+
}
8896

8997
log.debug("Received bytes:\n{}", messageBytes);
9098
val result = DESERIALIZER.deserialize(messageBytes, responseType);

client/src/main/java/io/appulse/epmd/java/client/EpmdClient.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static lombok.AccessLevel.PRIVATE;
2121

2222
import java.io.Closeable;
23-
import java.io.IOException;
2423
import java.net.InetAddress;
2524
import java.util.List;
2625

@@ -100,7 +99,6 @@ public int register (String name, int nodePort, NodeType type, Protocol protocol
10099
return register(request);
101100
}
102101

103-
@SneakyThrows
104102
public int register (@NonNull Registration request) {
105103
log.debug("Registering: {}", request.getName());
106104
val connection = getLocalConnection();
@@ -122,7 +120,6 @@ public int register (@NonNull Registration request) {
122120
return response.getCreation();
123121
}
124122

125-
@SneakyThrows
126123
public List<EpmdDump.NodeDump> dumpAll () {
127124
try (val connection = new Connection(address, port)) {
128125
val dump = connection.send(new GetEpmdDump(), EpmdDump.class);
@@ -139,7 +136,7 @@ public boolean kill () {
139136
try (val connection = new Connection(address, port)) {
140137
val result = connection.send(new Kill(), KillResult.class);
141138
return result == OK;
142-
} catch (IOException ex) {
139+
} catch (RuntimeException ex) {
143140
return false;
144141
} finally {
145142
close();
@@ -162,6 +159,10 @@ public void close () {
162159
}
163160
}
164161

162+
public void clearCaches () {
163+
lookupService.clearCache();
164+
}
165+
165166
@Override
166167
protected void finalize () throws Throwable {
167168
close();

client/src/main/java/io/appulse/epmd/java/client/LookupService.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static java.util.Optional.ofNullable;
2020
import static lombok.AccessLevel.PRIVATE;
2121

22-
import java.io.IOException;
2322
import java.net.InetAddress;
2423
import java.util.Map;
2524
import java.util.Optional;
@@ -62,8 +61,6 @@ class LookupService {
6261
val request = new GetNodeInfo(key.getNode());
6362
try (val connection = new Connection(key.getAddress(), key.getPort())) {
6463
return connection.send(request, NodeInfo.class);
65-
} catch (IOException ex) {
66-
return null;
6764
}
6865
};
6966
}
@@ -96,13 +93,17 @@ public Optional<NodeInfo> lookup (@NonNull String node, @NonNull InetAddress add
9693
val tokens = node.split("@", 2);
9794
val shortName = tokens[0];
9895

99-
log.debug("Looking up node {} at {}", shortName, address);
96+
log.debug("Looking up node '{}' at '{}'", shortName, address);
10097

10198
val descriptor = new NodeDescriptor(shortName, address, port);
10299
val nodeInfo = CACHE.compute(descriptor, COMPUTE);
103100
return ofNullable(nodeInfo);
104101
}
105102

103+
void clearCache () {
104+
CACHE.clear();
105+
}
106+
106107
@Value
107108
private static class NodeDescriptor {
108109

client/src/test/java/io/appulse/epmd/java/client/LocalEpmdClientTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static io.appulse.epmd.java.core.model.response.EpmdDump.NodeDump.Status.ACTIVE;
2323
import static lombok.AccessLevel.PRIVATE;
2424
import static org.assertj.core.api.Assertions.assertThat;
25+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2526

2627
import io.appulse.epmd.java.client.exception.EpmdConnectionException;
2728
import io.appulse.epmd.java.client.util.CheckLocalEpmdExists;
@@ -63,6 +64,7 @@ public void before () {
6364
@After
6465
public void after () {
6566
if (client != null) {
67+
client.clearCaches();
6668
client.close();
6769
client = null;
6870
}
@@ -101,14 +103,18 @@ public void register () throws Exception {
101103
});
102104
}
103105

104-
@Test(expected = EpmdConnectionException.class)
105-
public void registerBroken () {
106+
@Test
107+
public void connectionBroken () {
106108
val creation = client.register("register", 8971, R3_ERLANG, TCP, R6, R6);
107109
assertThat(creation).isNotEqualTo(0);
108110

109111
LocalEpmdHelper.kill();
110112

111-
client.lookup("register");
113+
assertThat(LocalEpmdHelper.isRunning())
114+
.isFalse();
115+
116+
assertThatExceptionOfType(EpmdConnectionException.class)
117+
.isThrownBy(() -> client.lookup("register"));
112118
}
113119

114120
@Test

client/src/test/java/io/appulse/epmd/java/client/util/LocalEpmdHelper.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525

2626
import lombok.SneakyThrows;
2727
import lombok.val;
28+
import lombok.extern.slf4j.Slf4j;
2829

2930
/**
3031
*
3132
* @author Artem Labazin
3233
* @since 0.2.1
3334
*/
35+
@Slf4j
3436
public final class LocalEpmdHelper {
3537

3638
@SneakyThrows
@@ -80,15 +82,15 @@ public static void kill () {
8082
process.destroy();
8183
throw new RuntimeException("Killing local EPMD is too long, pid: " + pids);
8284
}
83-
// if (process.exitValue() != 0) {
84-
// process.destroy();
85-
// throw new RuntimeException("Couldn't kill local EPMD with PID " + pids);
86-
// }
85+
if (process.exitValue() != 0) {
86+
process.destroy();
87+
throw new RuntimeException("Couldn't kill local EPMD with PID " + pids);
88+
}
8789
}
8890

8991
@SneakyThrows
9092
public static boolean isRunning () {
91-
val builder = new ProcessBuilder("pgrep", "-f", "epmd");
93+
val builder = new ProcessBuilder("epmd", "-names");
9294
val process = builder.start();
9395
if (!process.waitFor(1, MINUTES)) {
9496
process.destroy();

0 commit comments

Comments
 (0)