Skip to content

Commit 99111bf

Browse files
authored
Proxysql setup (#1377)
* Revert "Setup ProxySQL before each test (#1375)" This reverts commit 3432c20. * Make ProxySQLRule wait on startup before connectivity is checked Also, log ProxySQL output Signed-off-by: Thomas Segismont <[email protected]> * Add MySQL Logs Signed-off-by: Thomas Segismont <[email protected]> * Prefix MySQL network alias Signed-off-by: Thomas Segismont <[email protected]> --------- Signed-off-by: Thomas Segismont <[email protected]>
1 parent 2ceb9f0 commit 99111bf

File tree

6 files changed

+37
-24
lines changed

6 files changed

+37
-24
lines changed

vertx-mysql-client/src/test/java/io/vertx/mysqlclient/ProxySQLBatchInsertExceptionTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313

1414
import io.vertx.ext.unit.junit.VertxUnitRunner;
1515
import io.vertx.mysqlclient.junit.ProxySQLRule;
16-
import org.junit.Rule;
16+
import org.junit.ClassRule;
1717
import org.junit.runner.RunWith;
1818

1919
@RunWith(VertxUnitRunner.class)
2020
public class ProxySQLBatchInsertExceptionTest extends MySQLBatchInsertExceptionTestBase {
2121

22-
@Rule
23-
public ProxySQLRule proxySql = new ProxySQLRule(rule);
22+
@ClassRule
23+
public static ProxySQLRule proxySql = new ProxySQLRule(rule);
2424

2525
@Override
2626
protected MySQLConnectOptions createOptions() {

vertx-mysql-client/src/test/java/io/vertx/mysqlclient/junit/MySQLRule.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ private void initServer() throws IOException {
7777
.withEnv("MYSQL_PASSWORD", "password")
7878
.withEnv("MYSQL_ROOT_PASSWORD", "password")
7979
.withEnv("MYSQL_DATABASE", "testschema")
80+
.withLogConsumer(of -> System.out.print("[" + databaseServerInfo.databaseType + "] " + of.getUtf8String()))
8081
.withCreateContainerCmdModifier(createContainerCmd -> {
8182
createContainerCmd.getHostConfig().withUlimits(new Ulimit[]{new Ulimit("nofile", 262144L, 262144L)});
8283
})
@@ -107,12 +108,12 @@ private void initServer() throws IOException {
107108
}
108109
}
109110

110-
public String network() {
111+
String network() {
111112
return network != null ? network.getId() : "mysql_default";
112113
}
113114

114-
public String networkAlias() {
115-
return network != null ? Integer.toHexString(System.identityHashCode(this)) : "test-mysql";
115+
String networkAlias() {
116+
return network != null ? "test-mysql-" + Integer.toHexString(System.identityHashCode(this)) : "test-mysql";
116117
}
117118

118119
private static DatabaseType parseDatabaseTypeString(String databaseInfo) throws IllegalArgumentException {

vertx-mysql-client/src/test/java/io/vertx/mysqlclient/junit/ProxySQLRule.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
import io.vertx.mysqlclient.MySQLConnectOptions;
44
import org.junit.rules.ExternalResource;
5-
import org.testcontainers.containers.Container;
5+
import org.testcontainers.containers.Container.ExecResult;
66
import org.testcontainers.containers.GenericContainer;
77
import org.testcontainers.containers.wait.strategy.Wait;
88

9-
import java.util.concurrent.TimeUnit;
10-
11-
import static java.lang.String.*;
9+
import static java.lang.String.format;
10+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
1211

1312
public class ProxySQLRule extends ExternalResource {
1413

@@ -25,6 +24,7 @@ public ProxySQLRule(MySQLRule mySQLRule) {
2524
@Override
2625
protected void before() throws Throwable {
2726
proxySql = new GenericContainer<>("proxysql/proxysql")
27+
.withLogConsumer(of -> System.out.print("[ProxySQL] " + of.getUtf8String()))
2828
.withCreateContainerCmdModifier(createContainerCmd -> {
2929
createContainerCmd.getHostConfig().withNetworkMode(mySQLRule.network());
3030
})
@@ -38,6 +38,7 @@ protected void before() throws Throwable {
3838

3939
execStatement(format("UPDATE global_variables SET variable_value='%s' WHERE variable_name='mysql-monitor_username'", MONITORING_USER));
4040
execStatement(format("UPDATE global_variables SET variable_value='%s' WHERE variable_name='mysql-monitor_password'", MONITORING_USER_PASSWORD));
41+
execStatement("UPDATE global_variables SET variable_value='2000' WHERE variable_name IN ('mysql-monitor_connect_interval','mysql-monitor_ping_interval','mysql-monitor_read_only_interval')");
4142

4243
execStatement("LOAD MYSQL VARIABLES TO RUNTIME");
4344

@@ -46,24 +47,35 @@ protected void before() throws Throwable {
4647

4748
execStatement(format("INSERT INTO mysql_users (username,password) VALUES ('%s','%s')", mySQLRule.options().getUser(), mySQLRule.options().getPassword()));
4849
execStatement("LOAD MYSQL USERS TO RUNTIME");
50+
51+
for (long start = System.currentTimeMillis(); ; ) {
52+
ExecResult res = execStatement("SELECT connect_error FROM monitor.mysql_server_connect_log ORDER BY time_start_us LIMIT 1");
53+
if (res.getStdout().startsWith("NULL")) {
54+
break;
55+
}
56+
if (System.currentTimeMillis() - start > 30000) {
57+
throw new IllegalStateException(String.format("ProxySQL could not connect to backend: %s%n%s", res.getStdout(), res.getStderr()));
58+
}
59+
MILLISECONDS.sleep(500);
60+
}
4961
}
5062

51-
private void execStatement(String statement) throws Exception {
52-
execStatement(statement, 0);
63+
private ExecResult execStatement(String statement) throws Exception {
64+
return execStatement(statement, 0);
5365
}
5466

55-
private void execStatement(String statement, int retry) throws Exception {
67+
private ExecResult execStatement(String statement, int retry) throws Exception {
5668
RuntimeException failure;
5769
for (int i = 0; ; i++) {
58-
Container.ExecResult result = proxySql.execInContainer("mysql", "-u", "admin", "-p" + "admin", "-h", "127.0.0.1", "-P", "6032", "-e", statement);
70+
ExecResult result = proxySql.execInContainer("mysql", "-u", "admin", "-padmin", "-h", "127.0.0.1", "-P", "6032", "-sN", "-e", statement);
5971
if (result.getExitCode() == 0) {
60-
return;
72+
return result;
6173
}
6274
if (i >= retry && !result.getStderr().contains("ERROR 2002")) {
6375
failure = new RuntimeException("Failed to execute statement: " + statement + "\n" + result.getStderr());
6476
break;
6577
}
66-
TimeUnit.MILLISECONDS.sleep(200);
78+
MILLISECONDS.sleep(200);
6779
}
6880
throw failure;
6981
}

vertx-mysql-client/src/test/java/io/vertx/mysqlclient/tck/ProxySQLPreparedBatchTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
import io.vertx.ext.unit.junit.VertxUnitRunner;
1515
import io.vertx.mysqlclient.MySQLConnectOptions;
1616
import io.vertx.mysqlclient.junit.ProxySQLRule;
17-
import org.junit.Rule;
17+
import org.junit.ClassRule;
1818
import org.junit.runner.RunWith;
1919

2020
@RunWith(VertxUnitRunner.class)
2121
public class ProxySQLPreparedBatchTest extends MySQLPreparedBatchTest {
2222

23-
@Rule
24-
public ProxySQLRule proxySql = new ProxySQLRule(rule);
23+
@ClassRule
24+
public static ProxySQLRule proxySql = new ProxySQLRule(rule);
2525

2626
@Override
2727
protected void initConnector() {

vertx-mysql-client/src/test/java/io/vertx/mysqlclient/tck/ProxySQLPreparedQueryTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
import io.vertx.ext.unit.TestContext;
1515
import io.vertx.ext.unit.junit.VertxUnitRunner;
1616
import io.vertx.mysqlclient.junit.ProxySQLRule;
17+
import org.junit.ClassRule;
1718
import org.junit.Ignore;
18-
import org.junit.Rule;
1919
import org.junit.Test;
2020
import org.junit.runner.RunWith;
2121

2222
@RunWith(VertxUnitRunner.class)
2323
public class ProxySQLPreparedQueryTest extends MySQLPreparedQueryTest {
2424

25-
@Rule
26-
public ProxySQLRule proxySql = new ProxySQLRule(rule);
25+
@ClassRule
26+
public static ProxySQLRule proxySql = new ProxySQLRule(rule);
2727

2828
@Override
2929
protected void initConnector() {

vertx-mysql-client/src/test/resources/init.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,6 @@ CREATE TABLE duplicate_test
260260
INSERT INTO duplicate_test
261261
VALUES (1);
262262

263-
# testing ProxySQL connectivity
263+
#Testing ProxySQL connectivity
264264
CREATE USER 'proxysql'@'%' IDENTIFIED BY 'proxysql1234#';
265-
GRANT ALL ON *.* TO 'proxysql'@'%';
265+
GRANT USAGE, REPLICATION CLIENT ON *.* TO 'proxysql'@'%';

0 commit comments

Comments
 (0)