Skip to content

Commit 1b860ab

Browse files
committed
test(websocket): fix sporadic comparison failure in MockConnectionTest
1 parent 4386150 commit 1b860ab

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

org.eclipse.lsp4j.websocket/src/test/java/org/eclipse/lsp4j/websocket/test/MockConnectionTest.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.Random;
1616
import java.util.concurrent.CompletableFuture;
1717
import java.util.concurrent.TimeUnit;
18+
import java.util.concurrent.atomic.AtomicReference;
1819
import java.util.function.Supplier;
1920

2021
import org.eclipse.lsp4j.jsonrpc.Launcher;
@@ -54,18 +55,18 @@ public void testClientRequest() throws Exception {
5455
@Test
5556
public void testNotifications() throws Exception {
5657
server.client.notify("12");
57-
await(() -> client.result.length() == 2);
58+
await(() -> client.result.get().length() == 2);
5859
client.server.notify("foo");
59-
await(() -> server.result.length() == 3);
60+
await(() -> server.result.get().length() == 3);
6061
server.client.notify("34");
61-
await(() -> client.result.length() == 4);
62+
await(() -> client.result.get().length() == 4);
6263
client.server.notify("bar");
63-
await(() -> server.result.length() == 6);
64+
await(() -> server.result.get().length() == 6);
6465
server.client.notify("56");
65-
await(() -> client.result.length() == 6);
66-
67-
Assert.assertEquals("foobar", server.result);
68-
Assert.assertEquals("123456", client.result);
66+
await(() -> client.result.get().length() == 6);
67+
68+
Assert.assertEquals("foobar", server.result.get());
69+
Assert.assertEquals("123456", client.result.get());
6970
}
7071

7172
@Test
@@ -78,11 +79,11 @@ public void testManyConcurrentNotifications() throws Exception {
7879
}
7980
int expectedResultLenght = expectedResult.length();
8081
try {
81-
await(() -> server.result.length() == expectedResultLenght);
82+
await(() -> server.result.get().length() == expectedResultLenght);
8283
} catch (Error e) {
8384
// discard this error so that the nice error displays in the assertEquals
8485
}
85-
Assert.assertEquals(expectedResult, server.result);
86+
Assert.assertEquals(expectedResult, server.result.get());
8687
}
8788

8889
@Test
@@ -95,9 +96,9 @@ public void testChunkedNotification() throws Exception {
9596
String message = messageBuilder.toString();
9697

9798
server.client.notify(message);
98-
await(() -> client.result.length() == message.length());
99-
100-
Assert.assertEquals(message, client.result);
99+
await(() -> client.result.get().length() == message.length());
100+
101+
Assert.assertEquals(message, client.result.get());
101102
}
102103

103104
private void await(Supplier<Boolean> condition) throws InterruptedException {
@@ -119,11 +120,11 @@ private interface ClientInterface {
119120

120121
private static class Client implements ClientInterface {
121122
ServerInterface server;
122-
String result = "";
123+
AtomicReference<String> result = new AtomicReference<>("");
123124

124125
@Override
125126
public void notify(String arg) {
126-
this.result += arg;
127+
this.result.getAndUpdate(s -> s + arg);
127128
}
128129
}
129130

@@ -139,16 +140,16 @@ private interface ServerInterface {
139140

140141
private static class Server implements ServerInterface {
141142
ClientInterface client;
142-
String result = "";
143-
143+
AtomicReference<String> result = new AtomicReference<>("");
144+
144145
@Override
145146
public CompletableFuture<String> request(String arg) {
146147
return CompletableFuture.supplyAsync(() -> arg + "bar");
147148
}
148149

149150
@Override
150151
public void notify(String arg) {
151-
this.result += arg;
152+
this.result.getAndUpdate(s -> s + arg);
152153
}
153154
}
154155

0 commit comments

Comments
 (0)