Skip to content

Commit 5df3b03

Browse files
fix unit tests failed (#137)
* fix unit tests failed to create socket * update getReady method * add await() method Co-authored-by: Huynh, Tu | RASIA <[email protected]>
1 parent 5ab0215 commit 5df3b03

File tree

6 files changed

+19
-13
lines changed

6 files changed

+19
-13
lines changed

app/src/test/java/dev/keva/app/ApplicationTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package dev.keva.app;
22

3-
import dev.keva.app.Application;
43
import lombok.val;
54
import org.junit.jupiter.api.Test;
65
import redis.clients.jedis.Jedis;

core/src/main/java/dev/keva/core/server/KevaServer.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
import lombok.extern.slf4j.Slf4j;
2121

2222
import java.lang.reflect.InvocationTargetException;
23+
import java.util.concurrent.CompletableFuture;
2324
import java.util.concurrent.TimeUnit;
2425

2526
@Slf4j
2627
@Component
2728
@ComponentScan("dev.keva.core")
2829
public class KevaServer implements Server {
30+
private final CompletableFuture<Void> ready = new CompletableFuture<>();
31+
2932
private static final String KEVA_BANNER = "\n" +
3033
" _ __ ___ __ __ _ \n" +
3134
" | |/ / | __| \\ \\ / / /_\\ \n" +
@@ -115,18 +118,27 @@ public void run() {
115118
stopwatch.elapsed(TimeUnit.MILLISECONDS));
116119
log.info("Ready to accept connections");
117120

121+
ready.complete(null);
122+
118123
channel = sync.channel();
119124
channel.closeFuture().sync();
120125
} catch (InterruptedException e) {
121126
log.error("Failed to start server: ", e);
122127
Thread.currentThread().interrupt();
123128
} catch (Exception e) {
124129
log.error("Failed to start server: ", e);
130+
// Release err future
131+
ready.completeExceptionally(e);
125132
} finally {
126133
stopwatch.stop();
127134
}
128135
}
129136

137+
@Override
138+
public void await() {
139+
this.ready.join();
140+
}
141+
130142
@Override
131143
public void clear() {
132144
database.flush();

core/src/main/java/dev/keva/core/server/Server.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
public interface Server extends Runnable {
44
void run();
55

6+
void await();
7+
68
void shutdown();
79

810
void clear();

core/src/test/java/dev/keva/core/command/BaseCommandTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import org.junit.jupiter.api.BeforeEach;
1212
import redis.clients.jedis.Jedis;
1313

14-
import java.util.concurrent.TimeUnit;
15-
1614
@Slf4j
1715
public class BaseCommandTest {
1816
protected static final String host = "localhost";
@@ -21,7 +19,7 @@ public class BaseCommandTest {
2119
protected static Server server;
2220

2321
@BeforeAll
24-
static void startServer() throws Exception {
22+
static void startServer() {
2523
val config = KevaConfig.builder()
2624
.persistence(false)
2725
.aof(false)
@@ -41,7 +39,7 @@ static void startServer() throws Exception {
4139
}).start();
4240

4341
// Wait for server to start
44-
TimeUnit.SECONDS.sleep(2);
42+
server.await();
4543

4644
jedis = new Jedis(host, port);
4745
jedis.auth("keva-auth");

core/src/test/java/dev/keva/core/server/AOFTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ Server startServer(int port) throws Exception {
4141
}
4242
}).start();
4343

44-
// Wait for server to start
45-
TimeUnit.SECONDS.sleep(2);
44+
server.await();
4645
return server;
4746
}
4847

@@ -74,8 +73,7 @@ void sync(int port) throws InterruptedException {
7473
fail(e);
7574
}
7675
jedis.disconnect();
77-
// Wait for the interval to run
78-
TimeUnit.SECONDS.sleep(4);
76+
7977
try {
8078
stop(server);
8179
} catch (Exception e) {

core/src/test/java/dev/keva/core/server/PersistenceTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
import org.junit.jupiter.api.Timeout;
88
import redis.clients.jedis.Jedis;
99

10-
import java.util.concurrent.TimeUnit;
11-
1210
import static dev.keva.core.utils.PortUtil.getAvailablePort;
1311
import static org.junit.jupiter.api.Assertions.assertEquals;
1412
import static org.junit.jupiter.api.Assertions.fail;
@@ -35,8 +33,7 @@ Server startServer(int port) throws Exception {
3533
}
3634
}).start();
3735

38-
// Wait for server to start
39-
TimeUnit.SECONDS.sleep(2);
36+
server.await();
4037
return server;
4138
}
4239

0 commit comments

Comments
 (0)