-
Notifications
You must be signed in to change notification settings - Fork 12
/
SlickIT.java
116 lines (95 loc) · 3.95 KB
/
SlickIT.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package alpakka.slick;
import eu.rekawek.toxiproxy.Proxy;
import eu.rekawek.toxiproxy.ToxiproxyClient;
import eu.rekawek.toxiproxy.model.ToxicDirection;
import org.junit.ClassRule;
import org.junit.jupiter.api.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.ToxiproxyContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import scala.jdk.javaapi.FutureConverters;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Use JUnit as test runner because of "type trouble"
* when using PostgreSQLContainer with Scala
* <br>
* Use ToxiProxy (= a TCP proxy running in a docker container) to test non-happy-path
* Doc:
* https://www.testcontainers.org/modules/databases/postgres
* https://www.testcontainers.org/modules/toxiproxy/
*/
@Testcontainers
public class SlickIT {
private static final Logger LOGGER = LoggerFactory.getLogger(SlickIT.class);
private static SlickRunner SLICK_RUNNER;
private static String URL_WITH_MAPPED_PORT;
@ClassRule
public static Network network = Network.newNetwork();
@Container
public static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(DockerImageName.parse("postgres:latest"))
.withDatabaseName("test")
.withUsername("test")
.withPassword("test")
.withNetwork(network)
.withNetworkAliases("postgres");
@Container
public static ToxiproxyContainer toxiproxy = new ToxiproxyContainer("ghcr.io/shopify/toxiproxy:2.5.0")
.withNetwork(network);
@BeforeAll
public static void setup() throws IOException {
String dbName = postgres.getDatabaseName();
final ToxiproxyClient toxiproxyClient = new ToxiproxyClient(toxiproxy.getHost(), toxiproxy.getControlPort());
// ToxiProxyContainer reserves 31 ports, starting at 8666
final Proxy proxy = toxiproxyClient.createProxy("postgres", "0.0.0.0:8666", "postgres:5432");
// TODO Add more toxics
proxy.toxics()
.latency("latency", ToxicDirection.DOWNSTREAM, 3)
.setJitter(2);
// Wire via ToxiProxy
URL_WITH_MAPPED_PORT = postgres.getJdbcUrl().replace(postgres.getMappedPort(5432).toString(), toxiproxy.getMappedPort(8666).toString());
String username = postgres.getUsername();
String pw = postgres.getPassword();
LOGGER.info("DB: {} created, reached via ToxiProxy at URL: {}, username: {}, password: {}", dbName, URL_WITH_MAPPED_PORT, username, pw);
}
@AfterAll
public static void teardown() {
SLICK_RUNNER.terminate();
}
@BeforeEach
public void before() {
SLICK_RUNNER = SlickRunner.apply(URL_WITH_MAPPED_PORT);
SLICK_RUNNER.createTableOnSession();
}
@AfterEach
public void after() {
SLICK_RUNNER.dropTableOnSession();
SLICK_RUNNER.session().close();
}
@Test
public void populateAndReadUsers() {
int noOfUsers = 100;
SLICK_RUNNER.populateSync(noOfUsers);
assertThat(SLICK_RUNNER.readUsersSync().size()).isEqualTo(noOfUsers);
}
@Test
public void populateAndReadUsersPaged() {
int noOfUsers = 10000;
SLICK_RUNNER.populateSync(noOfUsers);
// Done via counter to avoid Scala->Java result collection conversion "type trouble"
assertThat(FutureConverters.asJava(SLICK_RUNNER.processUsersPaged())).succeedsWithin(5, TimeUnit.SECONDS);
assertThat(SLICK_RUNNER.counter().get()).isEqualTo(noOfUsers);
}
@Test
public void populateAndCountUsers() {
int noOfUsers = 100;
SLICK_RUNNER.populateSync(noOfUsers);
assertThat(SLICK_RUNNER.getTotal()).isEqualTo(noOfUsers);
}
}