Skip to content

Commit 560017e

Browse files
author
Philip Z
committed
Exercise Business ID allocation on live PostgreSQL and MySQL
1 parent 2816e6a commit 560017e

4 files changed

Lines changed: 128 additions & 5 deletions

File tree

‎.github/workflows/live-dialects.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ jobs:
4949
cache: maven
5050
- name: Verify both live dialects (skips are forbidden)
5151
run: >-
52-
mvn -B -ntp -pl teaql-postgres,teaql-mysql -am
53-
-Dtest=PostgresIntegrationTest,MysqlIntegrationTest
52+
mvn -B -ntp -pl teaql-postgres,teaql-mysql,examples/business-id-runtime -am
53+
-Dtest=PostgresIntegrationTest,MysqlIntegrationTest,JdbcBusinessIdLiveDialectIT
5454
-Dsurefire.failIfNoSpecifiedTests=false test

‎examples/business-id-runtime/pom.xml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@
2727
<version>3.53.2.1</version>
2828
<scope>test</scope>
2929
</dependency>
30+
<dependency>
31+
<groupId>org.postgresql</groupId>
32+
<artifactId>postgresql</artifactId>
33+
<version>42.7.3</version>
34+
<scope>test</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>com.mysql</groupId>
38+
<artifactId>mysql-connector-j</artifactId>
39+
<scope>test</scope>
40+
</dependency>
3041
<dependency>
3142
<groupId>junit</groupId>
3243
<artifactId>junit</artifactId>

‎examples/business-id-runtime/src/test/java/io/teaql/examples/businessid/BusinessIdRuntimeExampleTest.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ private static UserContext context(LocalDate date) {
298298
return context(date, null);
299299
}
300300

301-
private static UserContext context(
301+
static UserContext context(
302302
LocalDate date, JdbcBusinessIdAllocator businessIdInfrastructure) {
303303
TeaQLRuntime.Builder builder = TeaQLRuntime.builder()
304304
.metadata(new EntityMetaFactory() {
@@ -336,7 +336,7 @@ private static Connection open(Path path) throws SQLException {
336336
return connection;
337337
}
338338

339-
private static SchemaExecutor noOpSchemaExecutor() {
339+
static SchemaExecutor noOpSchemaExecutor() {
340340
return (SchemaExecutor) java.lang.reflect.Proxy.newProxyInstance(
341341
SchemaExecutor.class.getClassLoader(),
342342
new Class<?>[] {SchemaExecutor.class},
@@ -350,7 +350,7 @@ private static SchemaExecutor noOpSchemaExecutor() {
350350
});
351351
}
352352

353-
private static TeaQLDatabase database(Connection connection) {
353+
static TeaQLDatabase database(Connection connection) {
354354
return new TeaQLDatabase() {
355355
@Override
356356
public List<Map<String, Object>> query(String sql, Object[] args) {
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package io.teaql.examples.businessid;
2+
3+
import io.teaql.businessid.jdbc.JdbcBusinessIdAllocator;
4+
import io.teaql.core.SchemaExecutor;
5+
import io.teaql.core.UserContext;
6+
import io.teaql.core.businessid.BusinessIdDefinition;
7+
import io.teaql.core.businessid.BusinessIdGenerationRequest;
8+
import io.teaql.core.businessid.BusinessIdPlan;
9+
import io.teaql.runtime.businessid.DailySequenceBusinessIdProfile;
10+
import java.sql.Connection;
11+
import java.sql.DriverManager;
12+
import java.time.LocalDate;
13+
import java.util.ArrayList;
14+
import java.util.HashSet;
15+
import java.util.List;
16+
import java.util.Set;
17+
import java.util.UUID;
18+
import java.util.concurrent.CountDownLatch;
19+
import java.util.concurrent.ExecutorService;
20+
import java.util.concurrent.Executors;
21+
import java.util.concurrent.Future;
22+
import java.util.concurrent.TimeUnit;
23+
import org.junit.Assert;
24+
import org.junit.Assume;
25+
import org.junit.Test;
26+
27+
/** Live, independent-connection Business ID allocation across PostgreSQL and MySQL. */
28+
public class JdbcBusinessIdLiveDialectIT {
29+
private static final LocalDate BUSINESS_DATE = LocalDate.of(2026, 9, 20);
30+
private static final int ALLOCATIONS_PER_INSTANCE = 40;
31+
32+
@Test
33+
public void postgresAllocatesAcrossInstancesAndRestart() throws Exception {
34+
verify("POSTGRES");
35+
}
36+
37+
@Test
38+
public void mysqlAllocatesAcrossInstancesAndRestart() throws Exception {
39+
verify("MYSQL");
40+
}
41+
42+
private void verify(String dialect) throws Exception {
43+
String prefix = "TEAQL_TEST_" + dialect + "_";
44+
String url = System.getenv(prefix + "URL");
45+
String user = System.getenv(prefix + "USER");
46+
String password = System.getenv(prefix + "PASSWORD");
47+
if (url == null || user == null || password == null) {
48+
if (Boolean.parseBoolean(System.getenv("TEAQL_REQUIRE_LIVE_DB"))) {
49+
Assert.fail(dialect + " Business ID live gate requires URL, USER, and PASSWORD");
50+
}
51+
Assume.assumeTrue(dialect + " live database is not configured", false);
52+
}
53+
Assert.assertTrue("Use a dedicated teaql_live_* database for " + dialect,
54+
url.contains("teaql_live_"));
55+
56+
BusinessIdDefinition definition = BusinessIdDefinition.dailySequence(
57+
"order_number", "CO", "commerce_order");
58+
BusinessIdPlan plan = new DailySequenceBusinessIdProfile().plan(
59+
new BusinessIdGenerationRequest(
60+
definition, "tenant-" + UUID.randomUUID(),
61+
"commerce_order", BUSINESS_DATE));
62+
63+
try (Connection schemaConnection = DriverManager.getConnection(url, user, password)) {
64+
JdbcBusinessIdAllocator allocator = new JdbcBusinessIdAllocator(
65+
BusinessIdRuntimeExampleTest.database(schemaConnection));
66+
UserContext context = BusinessIdRuntimeExampleTest.context(BUSINESS_DATE, allocator);
67+
context.putAttribute(SchemaExecutor.class.getName(),
68+
BusinessIdRuntimeExampleTest.noOpSchemaExecutor());
69+
context.ensureSchema();
70+
}
71+
72+
CountDownLatch start = new CountDownLatch(1);
73+
ExecutorService workers = Executors.newFixedThreadPool(2);
74+
try {
75+
List<Future<List<Long>>> futures = new ArrayList<>();
76+
for (int worker = 0; worker < 2; worker++) {
77+
futures.add(workers.submit(() -> {
78+
try (Connection connection = DriverManager.getConnection(url, user, password)) {
79+
JdbcBusinessIdAllocator allocator = new JdbcBusinessIdAllocator(
80+
BusinessIdRuntimeExampleTest.database(connection));
81+
Assert.assertTrue("Concurrent start timed out",
82+
start.await(15, TimeUnit.SECONDS));
83+
List<Long> sequences = new ArrayList<>();
84+
for (int index = 0; index < ALLOCATIONS_PER_INSTANCE; index++) {
85+
sequences.add(allocator.allocate(plan).sequence());
86+
}
87+
return sequences;
88+
}
89+
}));
90+
}
91+
start.countDown();
92+
Set<Long> allocated = new HashSet<>();
93+
for (Future<List<Long>> future : futures) {
94+
allocated.addAll(future.get(45, TimeUnit.SECONDS));
95+
}
96+
int total = 2 * ALLOCATIONS_PER_INSTANCE;
97+
Assert.assertEquals(total, allocated.size());
98+
for (long sequence = 1; sequence <= total; sequence++) {
99+
Assert.assertTrue("Missing sequence " + sequence, allocated.contains(sequence));
100+
}
101+
} finally {
102+
workers.shutdownNow();
103+
}
104+
105+
try (Connection restarted = DriverManager.getConnection(url, user, password)) {
106+
JdbcBusinessIdAllocator allocator = new JdbcBusinessIdAllocator(
107+
BusinessIdRuntimeExampleTest.database(restarted));
108+
Assert.assertEquals(2L * ALLOCATIONS_PER_INSTANCE + 1,
109+
allocator.allocate(plan).sequence());
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)