|
15 | 15 | */
|
16 | 16 | package com.github.shyiko.mysql.binlog;
|
17 | 17 |
|
18 |
| -import com.github.shyiko.mysql.binlog.event.QueryEventData; |
19 |
| -import com.github.shyiko.mysql.binlog.event.XidEventData; |
| 18 | +import com.github.shyiko.mysql.binlog.event.*; |
20 | 19 | import com.github.shyiko.mysql.binlog.event.deserialization.EventDeserializer;
|
21 | 20 | import org.testng.SkipException;
|
22 |
| -import org.testng.annotations.AfterClass; |
23 |
| -import org.testng.annotations.BeforeClass; |
24 | 21 | import org.testng.annotations.Test;
|
25 | 22 |
|
26 | 23 | import java.sql.ResultSet;
|
27 | 24 | import java.sql.SQLException;
|
28 | 25 | import java.sql.Statement;
|
29 | 26 | import java.util.concurrent.TimeUnit;
|
30 | 27 |
|
| 28 | +import static org.testng.Assert.assertEquals; |
31 | 29 | import static org.testng.Assert.assertNotEquals;
|
32 | 30 | import static org.testng.AssertJUnit.assertNotNull;
|
33 | 31 |
|
@@ -126,4 +124,63 @@ public void execute(Statement statement) throws SQLException {
|
126 | 124 | client.connect(DEFAULT_TIMEOUT);
|
127 | 125 | }
|
128 | 126 | }
|
| 127 | + |
| 128 | + |
| 129 | + @Test |
| 130 | + public void testGtidServerId() throws Exception { |
| 131 | + master.execute("CREATE TABLE if not exists foo (i int)"); |
| 132 | + |
| 133 | + final String[] expectedServerId = new String[1]; |
| 134 | + master.query("select @@server_uuid", new Callback<ResultSet>() { |
| 135 | + @Override |
| 136 | + public void execute(ResultSet rs) throws SQLException { |
| 137 | + rs.next(); |
| 138 | + expectedServerId[0] = rs.getString(1); |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 142 | + final String[] actualServerId = new String[1]; |
| 143 | + |
| 144 | + EventDeserializer eventDeserializer = new EventDeserializer(); |
| 145 | + try { |
| 146 | + client.disconnect(); |
| 147 | + final BinaryLogClient clientWithKeepAlive = new BinaryLogClient(slave.hostname(), slave.port(), |
| 148 | + slave.username(), slave.password()); |
| 149 | + |
| 150 | + clientWithKeepAlive.setGtidSet(""); |
| 151 | + clientWithKeepAlive.registerEventListener(eventListener); |
| 152 | + |
| 153 | + |
| 154 | + clientWithKeepAlive.registerEventListener(new BinaryLogClient.EventListener() { |
| 155 | + @Override |
| 156 | + public void onEvent(Event event) { |
| 157 | + if (event.getHeader().getEventType() == EventType.GTID) { |
| 158 | + actualServerId[0] = ((GtidEventData) event.getData()).getMySqlGtid().getServerId().toString(); |
| 159 | + } |
| 160 | + } |
| 161 | + }); |
| 162 | + clientWithKeepAlive.setEventDeserializer(eventDeserializer); |
| 163 | + try { |
| 164 | + eventListener.reset(); |
| 165 | + clientWithKeepAlive.connect(DEFAULT_TIMEOUT); |
| 166 | + |
| 167 | + master.execute(new Callback<Statement>() { |
| 168 | + @Override |
| 169 | + public void execute(Statement statement) throws SQLException { |
| 170 | + statement.execute("INSERT INTO foo set i = 2"); |
| 171 | + } |
| 172 | + }); |
| 173 | + |
| 174 | + eventListener.waitFor(XidEventData.class, 1, TimeUnit.SECONDS.toMillis(4)); |
| 175 | + assertEquals(actualServerId[0], expectedServerId[0]); |
| 176 | + |
| 177 | + |
| 178 | + } finally { |
| 179 | + clientWithKeepAlive.disconnect(); |
| 180 | + } |
| 181 | + } finally { |
| 182 | + client.connect(DEFAULT_TIMEOUT); |
| 183 | + } |
| 184 | + } |
| 185 | + |
129 | 186 | }
|
0 commit comments