-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
BoltSchedulerShouldReportFailureWhenBusyIT.java
163 lines (142 loc) · 7.04 KB
/
BoltSchedulerShouldReportFailureWhenBusyIT.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
/*
* 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.runtime.integration;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Map;
import java.util.concurrent.RejectedExecutionException;
import java.util.function.Consumer;
import org.neo4j.bolt.AbstractBoltTransportsTest;
import org.neo4j.bolt.runtime.BoltConnection;
import org.neo4j.bolt.v1.transport.integration.Neo4jWithSocket;
import org.neo4j.bolt.v1.transport.socket.client.TransportConnection;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.kernel.api.exceptions.Status;
import org.neo4j.kernel.configuration.BoltConnector;
import org.neo4j.logging.AssertableLogProvider;
import org.neo4j.test.TestGraphDatabaseFactory;
import org.neo4j.test.rule.fs.EphemeralFileSystemRule;
import static java.util.Collections.emptyMap;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.isA;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.neo4j.bolt.v1.messaging.message.DiscardAllMessage.discardAll;
import static org.neo4j.bolt.v1.messaging.message.InitMessage.init;
import static org.neo4j.bolt.v1.messaging.message.PullAllMessage.pullAll;
import static org.neo4j.bolt.v1.messaging.message.RunMessage.run;
import static org.neo4j.bolt.v1.messaging.util.MessageMatchers.msgFailure;
import static org.neo4j.bolt.v1.messaging.util.MessageMatchers.msgSuccess;
import static org.neo4j.bolt.v1.transport.integration.Neo4jWithSocket.DEFAULT_CONNECTOR_KEY;
import static org.neo4j.bolt.v1.transport.integration.TransportTestUtil.eventuallyReceives;
@RunWith( Parameterized.class )
public class BoltSchedulerShouldReportFailureWhenBusyIT extends AbstractBoltTransportsTest
{
private AssertableLogProvider internalLogProvider = new AssertableLogProvider();
private AssertableLogProvider userLogProvider = new AssertableLogProvider();
private EphemeralFileSystemRule fsRule = new EphemeralFileSystemRule();
private Neo4jWithSocket server = new Neo4jWithSocket( getClass(), getTestGraphDatabaseFactory(), fsRule::get, getSettingsFunction() );
private TransportConnection connection1;
private TransportConnection connection2;
private TransportConnection connection3;
@Rule
public RuleChain ruleChain = RuleChain.outerRule( fsRule ).around( server );
protected TestGraphDatabaseFactory getTestGraphDatabaseFactory()
{
TestGraphDatabaseFactory factory = new TestGraphDatabaseFactory();
factory.setInternalLogProvider( internalLogProvider );
factory.setUserLogProvider( userLogProvider );
return factory;
}
protected Consumer<Map<String,String>> getSettingsFunction()
{
return settings ->
{
settings.put( GraphDatabaseSettings.auth_enabled.name(), "false" );
settings.put( new BoltConnector( DEFAULT_CONNECTOR_KEY ).enabled.name(), "TRUE" );
settings.put( new BoltConnector( DEFAULT_CONNECTOR_KEY ).listen_address.name(), "localhost:0" );
settings.put( new BoltConnector( DEFAULT_CONNECTOR_KEY ).type.name(), BoltConnector.ConnectorType.BOLT.name() );
settings.put( new BoltConnector( DEFAULT_CONNECTOR_KEY ).thread_pool_core_size.name(), "0" );
settings.put( new BoltConnector( DEFAULT_CONNECTOR_KEY ).thread_pool_max_size.name(), "2" );
};
}
@Before
public void setup() throws Exception
{
address = server.lookupDefaultConnector();
connection1 = performHandshake( newConnection() );
connection2 = performHandshake( newConnection() );
connection3 = performHandshake( newConnection() );
}
@After
public void cleanup() throws Exception
{
connection1.disconnect();
connection2.disconnect();
connection3.disconnect();
}
@Test
public void shouldReportFailureWhenAllThreadsInThreadPoolAreBusy() throws Exception
{
// it's enough to get the bolt state machine into streaming mode to have
// the thread sticked to the connection, causing all the available threads
// to be busy (logically)
enterStreaming( connection1 );
enterStreaming( connection2 );
try
{
connection3.send( util.chunk( run( "RETURN 1" ), pullAll() ) );
assertThat( connection3, util.eventuallyReceives(
msgFailure( Status.Request.NoThreadsAvailable, "There are no available threads to serve this request at the moment" ),
msgFailure( Status.Request.NoThreadsAvailable, "There are no available threads to serve this request at the moment" ) ) );
userLogProvider.assertContainsMessageContaining( "since there are no available threads to serve it at the moment. You can retry at a later time" );
internalLogProvider.assertAtLeastOnce( AssertableLogProvider.inLog( startsWith( BoltConnection.class.getPackage().getName() ) ).error(
containsString( "since there are no available threads to serve it at the moment. You can retry at a later time" ),
isA( RejectedExecutionException.class ) ) );
}
finally
{
exitStreaming( connection1 );
exitStreaming( connection2 );
}
}
private TransportConnection performHandshake( TransportConnection connection ) throws Exception
{
connection.connect( address ).send( util.acceptedVersions( 1, 0, 0, 0 ) ).send( util.chunk( init( "TestClient/1.1", emptyMap() ) ) );
assertThat( connection, eventuallyReceives( new byte[]{0, 0, 0, 1} ) );
assertThat( connection, util.eventuallyReceives( msgSuccess() ) );
return connection;
}
private void enterStreaming( TransportConnection connection ) throws Exception
{
connection.send( util.chunk( run( "UNWIND RANGE (1, 100) AS x RETURN x" ) ) );
assertThat( connection, util.eventuallyReceives( msgSuccess() ) );
}
private void exitStreaming( TransportConnection connection ) throws Exception
{
connection.send( util.chunk( discardAll() ) );
assertThat( connection, util.eventuallyReceives( msgSuccess() ) );
}
}