-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SocketTransportHandlerTest.java
225 lines (184 loc) · 8.13 KB
/
SocketTransportHandlerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.bolt.v1.transport.socket;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import org.junit.Test;
import org.neo4j.bolt.logging.BoltMessageLogging;
import org.neo4j.bolt.transport.BoltHandshakeProtocolHandler;
import org.neo4j.bolt.transport.BoltMessagingProtocolHandler;
import org.neo4j.bolt.transport.BoltProtocolHandlerFactory;
import org.neo4j.bolt.transport.SocketTransportHandler;
import org.neo4j.bolt.transport.TransportThrottleGroup;
import org.neo4j.bolt.v1.messaging.Neo4jPackV1;
import org.neo4j.bolt.v1.runtime.BoltStateMachine;
import org.neo4j.bolt.v1.runtime.SynchronousBoltWorker;
import org.neo4j.bolt.v1.transport.BoltMessagingProtocolV1Handler;
import org.neo4j.kernel.impl.logging.NullLogService;
import org.neo4j.logging.AssertableLogProvider;
import org.neo4j.logging.LogProvider;
import org.neo4j.logging.NullLogProvider;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.neo4j.logging.AssertableLogProvider.inLog;
public class SocketTransportHandlerTest
{
private static final LogProvider LOG_PROVIDER = NullLogProvider.getInstance();
private static final BoltMessageLogging BOLT_LOGGING = BoltMessageLogging.none();
@Test
public void shouldCloseProtocolOnChannelInactive() throws Throwable
{
// Given
BoltStateMachine machine = mock( BoltStateMachine.class );
ChannelHandlerContext ctx = channelHandlerContextMock();
SocketTransportHandler handler = newSocketTransportHandler( newHandshakeHandler( machine ) );
// And Given a session has been established
handler.channelRead( ctx, handshake() );
// When
handler.channelInactive( ctx );
// Then
verify( machine ).close();
}
@Test
public void shouldCloseContextWhenProtocolNotInitializedOnChannelInactive()
{
// Given
ChannelHandlerContext context = mock( ChannelHandlerContext.class );
SocketTransportHandler handler = newSocketTransportHandler( mock( BoltHandshakeProtocolHandler.class ) );
// When
handler.channelInactive( context );
// Then
verify( context ).close();
}
@Test
public void shouldCloseProtocolOnHandlerRemoved() throws Throwable
{
// Given
BoltStateMachine machine = mock( BoltStateMachine.class );
ChannelHandlerContext ctx = channelHandlerContextMock();
SocketTransportHandler handler = newSocketTransportHandler( newHandshakeHandler( machine ) );
// And Given a session has been established
handler.channelRead( ctx, handshake() );
// When
handler.handlerRemoved( ctx );
// Then
verify( machine ).close();
}
@Test
public void shouldCloseContextWhenProtocolNotInitializedOnHandlerRemoved()
{
// Given
ChannelHandlerContext context = mock( ChannelHandlerContext.class );
SocketTransportHandler handler = newSocketTransportHandler( mock( BoltHandshakeProtocolHandler.class ) );
// When
handler.handlerRemoved( context );
// Then
verify( context ).close();
}
@Test
public void logsAndClosesProtocolOnUnexpectedExceptions() throws Throwable
{
// Given
BoltStateMachine machine = mock( BoltStateMachine.class );
ChannelHandlerContext ctx = channelHandlerContextMock();
AssertableLogProvider logging = new AssertableLogProvider();
BoltHandshakeProtocolHandler handshakeHandler = newHandshakeHandler( machine );
SocketTransportHandler handler = new SocketTransportHandler( handshakeHandler, logging, BOLT_LOGGING );
// And Given a session has been established
handler.channelRead( ctx, handshake() );
// When
Throwable cause = new Throwable( "Oh no!" );
handler.exceptionCaught( ctx, cause );
// Then
verify( machine ).close();
logging.assertExactly( inLog( SocketTransportHandler.class )
.error( equalTo( "Fatal error occurred when handling a client connection: Oh no!" ), is( cause ) ) );
}
@Test
public void logsAndClosesContextWhenProtocolNotInitializedOnUnexpectedExceptions()
{
// Given
ChannelHandlerContext context = mock( ChannelHandlerContext.class );
AssertableLogProvider logging = new AssertableLogProvider();
SocketTransportHandler handler = new SocketTransportHandler( mock( BoltHandshakeProtocolHandler.class ),
logging, BOLT_LOGGING );
// When
Throwable cause = new Throwable( "Oh no!" );
handler.exceptionCaught( context, cause );
// Then
verify( context ).close();
logging.assertExactly( inLog( SocketTransportHandler.class )
.error( equalTo( "Fatal error occurred when handling a client connection: Oh no!" ),
is( cause ) ) );
}
@Test
public void shouldInitializeProtocolOnFirstMessage() throws Exception
{
BoltStateMachine machine = mock( BoltStateMachine.class );
BoltHandshakeProtocolHandler handshakeHandler = newHandshakeHandler( machine );
ChannelHandlerContext context = channelHandlerContextMock();
SocketTransportHandler handler = new SocketTransportHandler( handshakeHandler, LOG_PROVIDER, BOLT_LOGGING );
handler.channelRead( context, handshake() );
BoltMessagingProtocolHandler protocol1 = handshakeHandler.chosenProtocol();
handler.channelRead( context, handshake() );
BoltMessagingProtocolHandler protocol2 = handshakeHandler.chosenProtocol();
assertSame( protocol1, protocol2 );
}
private static SocketTransportHandler newSocketTransportHandler( BoltHandshakeProtocolHandler handler )
{
return new SocketTransportHandler( handler, LOG_PROVIDER, BOLT_LOGGING );
}
private static ChannelHandlerContext channelHandlerContextMock()
{
Channel channel = mock( Channel.class );
ChannelHandlerContext context = mock( ChannelHandlerContext.class );
when( context.channel() ).thenReturn( channel );
when( channel.alloc() ).thenReturn( UnpooledByteBufAllocator.DEFAULT );
when( context.alloc() ).thenReturn( UnpooledByteBufAllocator.DEFAULT );
return context;
}
private static BoltHandshakeProtocolHandler newHandshakeHandler( BoltStateMachine machine )
{
BoltProtocolHandlerFactory handlerFactory = ( version, channel ) ->
{
assertEquals( 1, version );
return new BoltMessagingProtocolV1Handler( channel, new Neo4jPackV1(), new SynchronousBoltWorker( machine ),
TransportThrottleGroup.NO_THROTTLE, NullLogService.getInstance() );
};
return new BoltHandshakeProtocolHandler( handlerFactory, false, true );
}
private ByteBuf handshake()
{
ByteBuf buf = UnpooledByteBufAllocator.DEFAULT.buffer();
buf.writeInt( 0x6060B017 );
buf.writeInt( 1 );
buf.writeInt( 0 );
buf.writeInt( 0 );
buf.writeInt( 0 );
return buf;
}
}