/
MySQLSelectForUpdateLockBasedTransactionTest.java
81 lines (70 loc) · 3.21 KB
/
MySQLSelectForUpdateLockBasedTransactionTest.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
package io.github.bucket4j.mysql;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import io.github.bucket4j.distributed.jdbc.BucketTableSettings;
import io.github.bucket4j.distributed.jdbc.SQLProxyConfiguration;
import io.github.bucket4j.distributed.proxy.ClientSideConfig;
import io.github.bucket4j.distributed.proxy.ProxyManager;
import io.github.bucket4j.tck.AbstractDistributedBucketTest;
import io.github.bucket4j.tck.ProxyManagerSpec;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.testcontainers.containers.MySQLContainer;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
public class MySQLSelectForUpdateLockBasedTransactionTest extends AbstractDistributedBucketTest {
private static MySQLContainer container;
private static DataSource dataSource;
private static MySQLSelectForUpdateBasedProxyManager<Long> proxyManager;
@BeforeAll
public static void initializeInstance() throws SQLException {
container = startMySQLContainer();
dataSource = createJdbcDataSource(container);
BucketTableSettings tableSettings = BucketTableSettings.customSettings("test.bucket", "id", "state");
final String INIT_TABLE_SCRIPT = "CREATE TABLE IF NOT EXISTS {0}({1} BIGINT PRIMARY KEY, {2} BLOB)";
try (Connection connection = dataSource.getConnection()) {
try (Statement statement = connection.createStatement()) {
String query = MessageFormat.format(INIT_TABLE_SCRIPT, tableSettings.getTableName(), tableSettings.getIdName(), tableSettings.getStateName());
statement.execute(query);
}
}
specs = Arrays.asList(
new ProxyManagerSpec<>(
"MySQLSelectForUpdateBasedProxyManager",
() -> ThreadLocalRandom.current().nextLong(1_000_000_000),
clientConfig -> {
SQLProxyConfiguration<Long> configuration = SQLProxyConfiguration.builder()
.withTableSettings(tableSettings)
.withClientSideConfig(clientConfig)
.build(dataSource);
return new MySQLSelectForUpdateBasedProxyManager<>(configuration);
}
)
);
}
@AfterAll
public static void shutdown() {
if (container != null) {
container.stop();
}
}
private static DataSource createJdbcDataSource(MySQLContainer container) {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(container.getJdbcUrl());
hikariConfig.setUsername(container.getUsername());
hikariConfig.setPassword(container.getPassword());
hikariConfig.setDriverClassName(container.getDriverClassName());
hikariConfig.setMaximumPoolSize(100);
return new HikariDataSource(hikariConfig);
}
private static MySQLContainer startMySQLContainer() {
MySQLContainer container = new MySQLContainer();
container.start();
return container;
}
}