|
16 | 16 | package com.github.shyiko.mysql.binlog;
|
17 | 17 |
|
18 | 18 | import com.github.shyiko.mysql.binlog.jmx.BinaryLogClientStatistics;
|
| 19 | +import com.github.shyiko.mysql.binlog.network.SocketFactory; |
19 | 20 | import org.testng.annotations.Test;
|
20 | 21 |
|
| 22 | +import java.io.EOFException; |
| 23 | +import java.io.FilterInputStream; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStream; |
| 26 | +import java.net.InetSocketAddress; |
| 27 | +import java.net.ServerSocket; |
| 28 | +import java.net.Socket; |
| 29 | +import java.net.SocketException; |
| 30 | +import java.util.concurrent.CountDownLatch; |
| 31 | +import java.util.concurrent.TimeUnit; |
21 | 32 | import java.util.concurrent.TimeoutException;
|
22 | 33 |
|
23 | 34 | import static org.testng.Assert.assertEquals;
|
@@ -69,4 +80,69 @@ public void testNullEventDeserializerIsNotAllowed() throws Exception {
|
69 | 80 | new BinaryLogClient("localhost", 3306, "root", "mysql").setEventDeserializer(null);
|
70 | 81 | }
|
71 | 82 |
|
| 83 | + @Test(timeOut = 15000) |
| 84 | + public void testDisconnectWhileBlockedByFBRead() throws Exception { |
| 85 | + final BinaryLogClient binaryLogClient = new BinaryLogClient("localhost", 33060, "root", "mysql"); |
| 86 | + final CountDownLatch readAttempted = new CountDownLatch(1); |
| 87 | + binaryLogClient.setSocketFactory(new SocketFactory() { |
| 88 | + @Override |
| 89 | + public Socket createSocket() throws SocketException { |
| 90 | + return new Socket() { |
| 91 | + |
| 92 | + @Override |
| 93 | + public InputStream getInputStream() throws IOException { |
| 94 | + return new FilterInputStream(super.getInputStream()) { |
| 95 | + |
| 96 | + @Override |
| 97 | + public int read(byte[] b, int off, int len) throws IOException { |
| 98 | + readAttempted.countDown(); |
| 99 | + return super.read(b, off, len); |
| 100 | + } |
| 101 | + }; |
| 102 | + } |
| 103 | + }; |
| 104 | + } |
| 105 | + }); |
| 106 | + binaryLogClient.setKeepAlive(false); |
| 107 | + final CountDownLatch socketBound = new CountDownLatch(1); |
| 108 | + new Thread(new Runnable() { |
| 109 | + @Override |
| 110 | + public void run() { |
| 111 | + try { |
| 112 | + final ServerSocket serverSocket = new ServerSocket(); |
| 113 | + try { |
| 114 | + serverSocket.bind(new InetSocketAddress("localhost", 33060)); |
| 115 | + socketBound.countDown(); |
| 116 | + serverSocket.accept(); // accept socket but do NOT send anything |
| 117 | + assertTrue(readAttempted.await(3000, TimeUnit.MILLISECONDS)); |
| 118 | + Thread thread = new Thread(new Runnable() { |
| 119 | + @Override |
| 120 | + public void run() { |
| 121 | + try { |
| 122 | + Thread.yield(); |
| 123 | + binaryLogClient.disconnect(); |
| 124 | + } catch (IOException e) { |
| 125 | + throw new RuntimeException(e); |
| 126 | + } |
| 127 | + } |
| 128 | + }); |
| 129 | + thread.start(); |
| 130 | + thread.join(); |
| 131 | + } finally { |
| 132 | + serverSocket.close(); |
| 133 | + } |
| 134 | + } catch (Exception e) { |
| 135 | + throw new RuntimeException(e); |
| 136 | + } |
| 137 | + } |
| 138 | + }).start(); |
| 139 | + assertTrue(socketBound.await(3000, TimeUnit.MILLISECONDS)); |
| 140 | + try { |
| 141 | + binaryLogClient.connect(); |
| 142 | + } catch (IOException e) { |
| 143 | + assertEquals(readAttempted.getCount(), 0); |
| 144 | + assertTrue(e.getCause() instanceof EOFException); |
| 145 | + } |
| 146 | + } |
| 147 | + |
72 | 148 | }
|
0 commit comments