Skip to content

Commit 9b41e25

Browse files
committed
Added test to ensure that client can be disconnected even if no data can be read from channel (shyiko#154)
1 parent 52c9e41 commit 9b41e25

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

src/test/java/com/github/shyiko/mysql/binlog/BinaryLogClientTest.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,19 @@
1616
package com.github.shyiko.mysql.binlog;
1717

1818
import com.github.shyiko.mysql.binlog.jmx.BinaryLogClientStatistics;
19+
import com.github.shyiko.mysql.binlog.network.SocketFactory;
1920
import org.testng.annotations.Test;
2021

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;
2132
import java.util.concurrent.TimeoutException;
2233

2334
import static org.testng.Assert.assertEquals;
@@ -69,4 +80,69 @@ public void testNullEventDeserializerIsNotAllowed() throws Exception {
6980
new BinaryLogClient("localhost", 3306, "root", "mysql").setEventDeserializer(null);
7081
}
7182

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+
72148
}

0 commit comments

Comments
 (0)