Skip to content

Commit

Permalink
Dispatch channel disconnect & close events to protocol as exceptions
Browse files Browse the repository at this point in the history
• Added a utility test for server disconnects (ignored by default)
  • Loading branch information
kdubb committed Dec 3, 2013
1 parent 05168f2 commit 65f2922
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
<exclude>**/PerformanceTest.java</exclude>
<exclude>**/GiantBlobTest.java</exclude>
<exclude>**/SSLTest.java</exclude>
<exclude>**/ServerDisconnectTest.java</exclude>
</excludes>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
*/
package com.impossibl.postgres.protocol.v30;

import java.nio.channels.ClosedChannelException;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
Expand All @@ -45,6 +48,24 @@ public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Ex
protocol.dispatch(msg);
}

@Override
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {

ProtocolImpl protocol = (ProtocolImpl) ctx.getChannel().getAttachment();
if (protocol != null) {
protocol.dispatchException(new ClosedChannelException());
}
}

@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {

ProtocolImpl protocol = (ProtocolImpl) ctx.getChannel().getAttachment();
if (protocol != null) {
protocol.dispatchException(new ClosedChannelException());
}
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {

Expand Down
109 changes: 109 additions & 0 deletions src/test/java/com/impossibl/postgres/jdbc/ServerDisconnectTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* Copyright (c) 2013, impossibl.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of impossibl.com nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package com.impossibl.postgres.jdbc;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import static org.junit.Assert.assertTrue;



/**
* Checks for when the server disappears without closing the connection
*
* NOTE: this cannot be run during normal tests as it nukes the postgres
* process; which puts the sysetm in recovery mode. If we know how to nuke the
* process without recovery mode that would be helpful.
*
* @author kdubb
*
*/
@RunWith(JUnit4.class)
public class ServerDisconnectTest {

Connection conn;

@Before
public void setUp() throws Exception {
conn = TestUtil.openDB();
}

@After
public void tearDown() throws SQLException {
}

@Test(expected = SQLException.class)
public void testServerDisconnect() throws SQLException, IOException {

try (Statement stmt = conn.createStatement()) {

// Query connection pid
final int pid;
try (ResultSet rs = stmt.executeQuery("SELECT pg_backend_pid();")) {
assertTrue(rs.next());

pid = rs.getInt(1);
assertTrue(pid != 0);
}

// Kill the postgres process for this connection after 1 second...
Thread killThread = new Thread() {

@Override
public void run() {
try {
Thread.sleep(1000);
Runtime.getRuntime().exec("kill -KILL " + pid);
}
catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}

};
killThread.start();

stmt.executeQuery("SELECT pg_sleep(5);");

}

}


}

0 comments on commit 65f2922

Please sign in to comment.