|
20 | 20 |
|
21 | 21 | import io.netty.channel.ChannelPromise;
|
22 | 22 | import io.netty.channel.embedded.EmbeddedChannel;
|
| 23 | +import io.netty.handler.codec.DecoderException; |
23 | 24 | import org.junit.After;
|
24 | 25 | import org.junit.Before;
|
25 | 26 | import org.junit.Test;
|
26 | 27 |
|
| 28 | +import java.io.IOException; |
| 29 | +import javax.net.ssl.SSLHandshakeException; |
| 30 | + |
27 | 31 | import org.neo4j.driver.internal.async.inbound.ChunkDecoder;
|
28 | 32 | import org.neo4j.driver.internal.async.inbound.InboundMessageDispatcher;
|
29 | 33 | import org.neo4j.driver.internal.async.inbound.InboundMessageHandler;
|
30 | 34 | import org.neo4j.driver.internal.async.inbound.MessageDecoder;
|
31 | 35 | import org.neo4j.driver.internal.async.outbound.OutboundMessageHandler;
|
| 36 | +import org.neo4j.driver.internal.util.ErrorUtil; |
32 | 37 | import org.neo4j.driver.v1.exceptions.ClientException;
|
| 38 | +import org.neo4j.driver.v1.exceptions.SecurityException; |
| 39 | +import org.neo4j.driver.v1.exceptions.ServiceUnavailableException; |
33 | 40 |
|
34 | 41 | import static io.netty.buffer.Unpooled.copyInt;
|
35 | 42 | import static org.hamcrest.Matchers.instanceOf;
|
@@ -87,6 +94,93 @@ public void shouldFailGivenPromiseWhenExceptionCaught()
|
87 | 94 | assertNull( await( channel.closeFuture() ) );
|
88 | 95 | }
|
89 | 96 |
|
| 97 | + @Test |
| 98 | + public void shouldFailGivenPromiseWhenMultipleExceptionsCaught() |
| 99 | + { |
| 100 | + ChannelPromise handshakeCompletedPromise = channel.newPromise(); |
| 101 | + HandshakeHandler handler = newHandler( handshakeCompletedPromise ); |
| 102 | + channel.pipeline().addLast( handler ); |
| 103 | + |
| 104 | + RuntimeException error1 = new RuntimeException( "Error 1" ); |
| 105 | + RuntimeException error2 = new RuntimeException( "Error 2" ); |
| 106 | + channel.pipeline().fireExceptionCaught( error1 ); |
| 107 | + channel.pipeline().fireExceptionCaught( error2 ); |
| 108 | + |
| 109 | + try |
| 110 | + { |
| 111 | + // promise should fail |
| 112 | + await( handshakeCompletedPromise ); |
| 113 | + fail( "Exception expected" ); |
| 114 | + } |
| 115 | + catch ( RuntimeException e ) |
| 116 | + { |
| 117 | + assertEquals( error1, e ); |
| 118 | + } |
| 119 | + |
| 120 | + // channel should be closed |
| 121 | + assertNull( await( channel.closeFuture() ) ); |
| 122 | + |
| 123 | + try |
| 124 | + { |
| 125 | + channel.checkException(); |
| 126 | + fail( "Exception expected" ); |
| 127 | + } |
| 128 | + catch ( RuntimeException e ) |
| 129 | + { |
| 130 | + assertEquals( error2, e ); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void shouldUnwrapDecoderException() |
| 136 | + { |
| 137 | + ChannelPromise handshakeCompletedPromise = channel.newPromise(); |
| 138 | + HandshakeHandler handler = newHandler( handshakeCompletedPromise ); |
| 139 | + channel.pipeline().addLast( handler ); |
| 140 | + |
| 141 | + IOException cause = new IOException( "Error!" ); |
| 142 | + channel.pipeline().fireExceptionCaught( new DecoderException( cause ) ); |
| 143 | + |
| 144 | + try |
| 145 | + { |
| 146 | + // promise should fail |
| 147 | + await( handshakeCompletedPromise ); |
| 148 | + fail( "Exception expected" ); |
| 149 | + } |
| 150 | + catch ( Exception e ) |
| 151 | + { |
| 152 | + assertEquals( cause, e ); |
| 153 | + } |
| 154 | + |
| 155 | + // channel should be closed |
| 156 | + assertNull( await( channel.closeFuture() ) ); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void shouldTranslateSSLHandshakeException() |
| 161 | + { |
| 162 | + ChannelPromise handshakeCompletedPromise = channel.newPromise(); |
| 163 | + HandshakeHandler handler = newHandler( handshakeCompletedPromise ); |
| 164 | + channel.pipeline().addLast( handler ); |
| 165 | + |
| 166 | + SSLHandshakeException error = new SSLHandshakeException( "Invalid certificate" ); |
| 167 | + channel.pipeline().fireExceptionCaught( error ); |
| 168 | + |
| 169 | + try |
| 170 | + { |
| 171 | + // promise should fail |
| 172 | + await( handshakeCompletedPromise ); |
| 173 | + fail( "Exception expected" ); |
| 174 | + } |
| 175 | + catch ( SecurityException e ) |
| 176 | + { |
| 177 | + assertEquals( error, e.getCause() ); |
| 178 | + } |
| 179 | + |
| 180 | + // channel should be closed |
| 181 | + assertNull( await( channel.closeFuture() ) ); |
| 182 | + } |
| 183 | + |
90 | 184 | @Test
|
91 | 185 | public void shouldSelectProtocolV1WhenServerSuggests()
|
92 | 186 | {
|
@@ -129,6 +223,30 @@ public void shouldFailGivenPromiseWhenServerSuggestsUnknownProtocol()
|
129 | 223 | testFailure( 42, "Protocol error" );
|
130 | 224 | }
|
131 | 225 |
|
| 226 | + @Test |
| 227 | + public void shouldFailGivenPromiseWhenChannelInactive() |
| 228 | + { |
| 229 | + ChannelPromise handshakeCompletedPromise = channel.newPromise(); |
| 230 | + HandshakeHandler handler = newHandler( handshakeCompletedPromise ); |
| 231 | + channel.pipeline().addLast( handler ); |
| 232 | + |
| 233 | + channel.pipeline().fireChannelInactive(); |
| 234 | + |
| 235 | + try |
| 236 | + { |
| 237 | + // promise should fail |
| 238 | + await( handshakeCompletedPromise ); |
| 239 | + fail( "Exception expected" ); |
| 240 | + } |
| 241 | + catch ( ServiceUnavailableException e ) |
| 242 | + { |
| 243 | + assertEquals( ErrorUtil.newConnectionTerminatedError().getMessage(), e.getMessage() ); |
| 244 | + } |
| 245 | + |
| 246 | + // channel should be closed |
| 247 | + assertNull( await( channel.closeFuture() ) ); |
| 248 | + } |
| 249 | + |
132 | 250 | private void testFailure( int serverSuggestedVersion, String expectedMessagePrefix )
|
133 | 251 | {
|
134 | 252 | ChannelPromise handshakeCompletedPromise = channel.newPromise();
|
|
0 commit comments