|
| 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