This repository has been archived by the owner on Dec 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemoTest.java
93 lines (81 loc) · 2.01 KB
/
DemoTest.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
package com.example.demo;
import static org.junit.jupiter.api.Assertions.fail;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.jupiter.api.Test;
public class DemoTest {
private static final String NEXT_SYNC_EVENT_QUERY = """
DELETE FROM queue
WHERE
id IN (
SELECT id
FROM queue
ORDER BY id
LIMIT 1
FOR UPDATE
SKIP LOCKED
)
RETURNING *
""";
@Test
void loadTestQueue() throws Exception {
try (Connection connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:55612/test",
"test",
"test"
)) {
recreateTable(connection);
insertAndRemoveMessages(connection);
}
}
private void recreateTable(Connection connection) throws SQLException {
try (Statement statement = connection.createStatement()) {
statement.executeUpdate("""
DROP TABLE IF EXISTS "queue"
""");
statement.executeUpdate("""
CREATE TABLE "queue" (
"id" BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
"action" TEXT NOT NULL
);
""");
}
}
private void insertAndRemoveMessages(Connection connection)
throws SQLException {
int total = 256_000;
int step = 3;
for (int i = 0; i < total; i += step) {
try (Statement insertStatement = connection.createStatement()) {
for (int j = 0; j < step; j++) {
insertStatement.execute(
"INSERT INTO queue (action) VALUES ('action')"
);
}
}
try (Statement statement = connection.createStatement()) {
for (int j = 0; j < step; j++) {
try (ResultSet resultSet = statement
.executeQuery(NEXT_SYNC_EVENT_QUERY)) {
int count = 0;
while (resultSet.next()) {
count++;
}
if (count > 1) {
System.out.println(
"Got " + count
+ " results instead of 1 after approx. "
+ i + " entries"
);
return;
}
}
}
}
}
fail("Never got too many query results");
}
}