Skip to content

Commit 91d4a26

Browse files
committed
ändrat latch await till 1sek för att hanterna timeout i test.
1 parent d5c2cd2 commit 91d4a26

2 files changed

Lines changed: 19 additions & 15 deletions

File tree

src/main/java/com/example/HelloModel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private void receiveMessage() {
7070
}
7171

7272
if (System.getProperty("java.awt.headless", "false").equals("true")) {
73-
messages.add(message); // lägg direkt i listan
73+
messages.add(message);
7474
return;
7575
}
7676

src/test/java/com/example/HelloModelTest.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ static void setupJavaFX() {
2929
}
3030
}
3131

32-
//successful send tests
32+
// successful send tests
3333
@Test
3434
void shouldSendMessageThroughConnection() throws InterruptedException {
3535
NtfyConnectionSpy connectionSpy = new NtfyConnectionSpy();
@@ -39,7 +39,7 @@ void shouldSendMessageThroughConnection() throws InterruptedException {
3939
CountDownLatch latch = new CountDownLatch(1);
4040
model.sendMessageAsync(success -> latch.countDown());
4141

42-
assertThat(latch.await(500, TimeUnit.MILLISECONDS)).isTrue();
42+
assertThat(latch.await(1, TimeUnit.SECONDS)).isTrue();
4343
assertThat(connectionSpy.message).isEqualTo("Hello World");
4444
}
4545

@@ -69,7 +69,7 @@ void shouldHandleMultipleConsecutiveSends() throws InterruptedException {
6969
assertThat(connectionSpy.message).isEqualTo("Second");
7070
}
7171

72-
//invalid send tests
72+
// invalid send tests
7373
@Test
7474
void shouldRejectBlankMessages() throws InterruptedException {
7575
NtfyConnectionSpy connectionSpy = new NtfyConnectionSpy();
@@ -87,7 +87,7 @@ void shouldRejectBlankMessages() throws InterruptedException {
8787
latch.countDown();
8888
});
8989

90-
latch.await(500, TimeUnit.MILLISECONDS);
90+
latch.await(1, TimeUnit.SECONDS);
9191
assertThat(wasSuccessful[0]).isFalse();
9292
assertThat(connectionSpy.message).isNull();
9393
}
@@ -107,7 +107,7 @@ void shouldFailWhenSendingEmptyText() throws InterruptedException {
107107
latch.countDown();
108108
});
109109

110-
latch.await(500, TimeUnit.MILLISECONDS);
110+
latch.await(1, TimeUnit.SECONDS);
111111
assertThat(wasSuccessful[0]).isFalse();
112112
assertThat(connectionSpy.message).isNull();
113113
}
@@ -126,12 +126,12 @@ void shouldFailWhenSendingNullMessage() throws InterruptedException {
126126
latch.countDown();
127127
});
128128

129-
latch.await(500, TimeUnit.MILLISECONDS);
129+
latch.await(1, TimeUnit.SECONDS);
130130
assertThat(wasSuccessful[0]).isFalse();
131131
assertThat(connectionSpy.message).isNull();
132132
}
133133

134-
//error handling tests
134+
// error handling tests
135135
@Test
136136
void shouldReturnFailureWhenConnectionFails() throws InterruptedException {
137137
NtfyConnection failingConn = new NtfyConnection() {
@@ -153,7 +153,7 @@ public void receive(Consumer<NtfyMessageDto> messageHandler) { }
153153
latch.countDown();
154154
});
155155

156-
latch.await(500, TimeUnit.MILLISECONDS);
156+
latch.await(1, TimeUnit.SECONDS);
157157
assertThat(wasSuccessful[0]).isFalse();
158158
assertThat(model.getMessageToSend()).isEqualTo("Fail this message");
159159
}
@@ -184,11 +184,11 @@ public void receive(Consumer<NtfyMessageDto> messageHandler) { }
184184
latch.countDown();
185185
}
186186

187-
latch.await(500, TimeUnit.MILLISECONDS);
187+
latch.await(1, TimeUnit.SECONDS);
188188
assertThat(wasSuccessful[0]).isFalse();
189189
}
190190

191-
//receiving messages tests
191+
// receiving messages tests
192192
@Test
193193
void shouldAddIncomingMessageToList() throws InterruptedException {
194194
NtfyConnectionSpy connectionSpy = new NtfyConnectionSpy();
@@ -209,6 +209,7 @@ void shouldAddIncomingMessageToList() throws InterruptedException {
209209
assertThat(latch.await(1, TimeUnit.SECONDS)).isTrue();
210210
assertThat(model.getMessages()).contains(incomingMsg);
211211
}
212+
212213
@Test
213214
void shouldDiscardNullIncomingMessage() throws InterruptedException {
214215
NtfyConnectionSpy connectionSpy = new NtfyConnectionSpy();
@@ -223,10 +224,11 @@ void shouldDiscardNullIncomingMessage() throws InterruptedException {
223224

224225
connectionSpy.simulateIncoming(null);
225226

226-
boolean messageAdded = latch.await(500, TimeUnit.MILLISECONDS);
227+
boolean messageAdded = latch.await(1, TimeUnit.SECONDS);
227228
assertThat(messageAdded).isFalse();
228229
assertThat(model.getMessages()).isEmpty();
229230
}
231+
230232
@Test
231233
void shouldIgnoreMessagesWithBlankContent() throws InterruptedException {
232234
NtfyConnectionSpy connectionSpy = new NtfyConnectionSpy();
@@ -245,10 +247,11 @@ void shouldIgnoreMessagesWithBlankContent() throws InterruptedException {
245247
connectionSpy.simulateIncoming(whitespaceMsg);
246248
connectionSpy.simulateIncoming(emptyMsg);
247249

248-
boolean messageAdded = latch.await(500, TimeUnit.MILLISECONDS);
250+
boolean messageAdded = latch.await(1, TimeUnit.SECONDS);
249251
assertThat(messageAdded).isFalse();
250252
assertThat(model.getMessages()).isEmpty();
251253
}
254+
252255
@Test
253256
void shouldRejectAllInvalidIncomingMessages() throws InterruptedException {
254257
NtfyConnectionSpy connectionSpy = new NtfyConnectionSpy();
@@ -265,11 +268,12 @@ void shouldRejectAllInvalidIncomingMessages() throws InterruptedException {
265268
connectionSpy.simulateIncoming(new NtfyMessageDto("id2", 2, "message", "room", " "));
266269
connectionSpy.simulateIncoming(null);
267270

268-
boolean messageAdded = latch.await(500, TimeUnit.MILLISECONDS);
271+
boolean messageAdded = latch.await(1, TimeUnit.SECONDS);
269272
assertThat(messageAdded).isFalse();
270273
assertThat(model.getMessages()).isEmpty();
271274
}
272-
//integration test
275+
276+
// integration test
273277
@Test
274278
void shouldCommunicateWithMockedServer(WireMockRuntimeInfo wmInfo) throws InterruptedException {
275279
NtfyConnectionImpl connection = new NtfyConnectionImpl("http://localhost:" + wmInfo.getHttpPort());

0 commit comments

Comments
 (0)