-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathBlockingRawTest.java
100 lines (86 loc) · 3.62 KB
/
BlockingRawTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.example.grpc.hibernate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import com.example.test.TestOuterClass;
import com.example.test.TestRaw;
import io.quarkus.grpc.GrpcClient;
import io.quarkus.test.junit.QuarkusTest;
import io.smallrye.mutiny.Multi;
@QuarkusTest
public class BlockingRawTest {
public static final int NO_OF_ELTS = 100;
public static final int TIMEOUT = 60;
public static final TestOuterClass.Empty EMPTY = TestOuterClass.Empty.getDefaultInstance();
@GrpcClient
TestRaw client;
@BeforeEach
void clear() {
client.clear(EMPTY).onFailure().invoke(e -> {
throw new RuntimeException("Failed to clear items", e);
}).await().atMost(Duration.ofSeconds(20));
}
@Test
@Timeout(TIMEOUT)
void shouldAdd() {
List<String> expected = new ArrayList<>();
for (int i = 0; i < NO_OF_ELTS; i++) {
String text = "text " + i;
expected.add(text);
final int attempt = i;
client.add(TestOuterClass.Item.newBuilder().setText(text).build())
.onFailure().invoke(e -> {
throw new RuntimeException("Failed to add on attempt " + attempt, e);
})
.await().atMost(Duration.ofSeconds(5));
}
List<String> actual = new CopyOnWriteArrayList<>();
Multi<TestOuterClass.Item> all = client.getAll(EMPTY)
.onFailure().invoke(th -> {
System.out.println("Failed to read");
th.printStackTrace();
});
all.subscribe().with(item -> actual.add(item.getText()));
await().atMost(Duration.ofSeconds(TIMEOUT / 2))
.until(() -> actual.size() == NO_OF_ELTS);
assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
}
@Test
@Timeout(TIMEOUT)
void shouldAddViaBidi() {
List<String> expected = new ArrayList<>();
List<String> echoed = new CopyOnWriteArrayList<>();
List<String> actual = new CopyOnWriteArrayList<>();
Multi<TestOuterClass.Item> request = Multi.createFrom().emitter(
m -> {
for (int i = 0; i < NO_OF_ELTS; i++) {
String text = "text " + i;
expected.add(text);
m.emit(TestOuterClass.Item.newBuilder().setText(text).build());
}
m.complete();
});
client.bidi(request).subscribe().with(item -> echoed.add(item.getText()));
await().atMost(Duration.ofSeconds(TIMEOUT / 2))
.until(() -> echoed.size() == NO_OF_ELTS);
assertThat(echoed).containsExactlyInAnyOrderElementsOf(expected);
Multi<TestOuterClass.Item> all = client.getAll(EMPTY)
.onFailure().invoke(th -> {
System.out.println("Failed to read");
th.printStackTrace();
});
all.subscribe().with(item -> actual.add(item.getText()));
await().atMost(Duration.ofSeconds(TIMEOUT / 2))
.until(() -> {
System.out.println("no of elements: " + actual.size());
return actual.size() == NO_OF_ELTS;
});
assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
}
}