Skip to content

Commit

Permalink
IGNITE-5234: JDBC Thin Driver: implemented Connection.setNetworkTimeo…
Browse files Browse the repository at this point in the history
…ut method. This closes apache#5819.

(cherry picked from commit 35882c5)
  • Loading branch information
sanpwc authored and mcherkasov committed May 21, 2019
1 parent 3e07e53 commit 40f5554
Show file tree
Hide file tree
Showing 5 changed files with 349 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.apache.ignite.jdbc.thin.JdbcThinConnectionMvccEnabledSelfTest;
import org.apache.ignite.jdbc.thin.JdbcThinConnectionSSLTest;
import org.apache.ignite.jdbc.thin.JdbcThinConnectionSelfTest;
import org.apache.ignite.jdbc.thin.JdbcThinConnectionTimeoutSelfTest;
import org.apache.ignite.jdbc.thin.JdbcThinDataSourceSelfTest;
import org.apache.ignite.jdbc.thin.JdbcThinDeleteStatementSelfTest;
import org.apache.ignite.jdbc.thin.JdbcThinDynamicIndexAtomicPartitionedNearSelfTest;
Expand Down Expand Up @@ -175,6 +176,7 @@ public static TestSuite suite() {
suite.addTest(new JUnit4TestAdapter(JdbcThinMetadataPrimaryKeysSelfTest.class));
suite.addTest(new JUnit4TestAdapter(JdbcThinErrorsSelfTest.class));
suite.addTest(new JUnit4TestAdapter(JdbcThinStatementCancelSelfTest.class));
suite.addTest(new JUnit4TestAdapter(JdbcThinConnectionTimeoutSelfTest.class));

suite.addTest(new JUnit4TestAdapter(JdbcThinInsertStatementSelfTest.class));
suite.addTest(new JUnit4TestAdapter(JdbcThinUpdateStatementSelfTest.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1916,19 +1916,6 @@ public void testGetSetNetworkTimeout() throws Exception {

final int timeout = 1000;

//Invalid executor
GridTestUtils.assertThrows(log,
new Callable<Object>() {
@Override public Object call() throws Exception {
conn.setNetworkTimeout(null, timeout);

return null;
}
},
SQLException.class,
"Executor cannot be null"
);

//Invalid timeout
GridTestUtils.assertThrows(log,
new Callable<Object>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
/*
* Copyright 2019 GridGain Systems, Inc. and Contributors.
*
* Licensed under the GridGain Community Edition License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.ignite.jdbc.thin;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLTimeoutException;
import java.sql.Statement;
import java.util.concurrent.Executor;
import org.apache.ignite.cache.query.annotations.QuerySqlFunction;
import org.apache.ignite.configuration.CacheConfiguration;
import org.apache.ignite.configuration.ClientConnectorConfiguration;
import org.apache.ignite.configuration.IgniteConfiguration;
import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
import org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinder;
import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
import org.apache.ignite.testframework.GridTestUtils;
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.apache.ignite.cache.CacheMode.PARTITIONED;
import static org.apache.ignite.cache.CacheWriteSynchronizationMode.FULL_SYNC;

/**
* Jdbc Thin Connection timeout tests.
*/
@RunWith(JUnit4.class)
@SuppressWarnings("ThrowableNotThrown")
public class JdbcThinConnectionTimeoutSelfTest extends JdbcThinAbstractSelfTest {

/** IP finder. */
private static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true);

/** URL. */
private static final String URL = "jdbc:ignite:thin://127.0.0.1/";

/** Server thread pull size. */
private static final int SERVER_THREAD_POOL_SIZE = 4;

/** Nodes count. */
private static final byte NODES_COUNT = 3;

/** Max table rows. */
private static final int MAX_ROWS = 10000;

/** Executor stub */
private static final Executor EXECUTOR_STUB = (Runnable command) -> {};

/** Connection. */
private Connection conn;

/** Statement. */
private Statement stmt;

/** {@inheritDoc} */
@Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);

CacheConfiguration<?, ?> cache = defaultCacheConfiguration();

cache.setCacheMode(PARTITIONED);
cache.setBackups(1);
cache.setWriteSynchronizationMode(FULL_SYNC);
cache.setSqlFunctionClasses(JdbcThinConnectionTimeoutSelfTest.class);
cache.setIndexedTypes(Integer.class, Integer.class);

cfg.setCacheConfiguration(cache);

TcpDiscoverySpi disco = new TcpDiscoverySpi();

disco.setIpFinder(IP_FINDER);

cfg.setDiscoverySpi(disco);

cfg.setClientConnectorConfiguration(new ClientConnectorConfiguration().setThreadPoolSize(SERVER_THREAD_POOL_SIZE));

return cfg;
}

/** {@inheritDoc} */
@Override protected void beforeTestsStarted() throws Exception {
super.beforeTestsStarted();

startGridsMultiThreaded(NODES_COUNT);

for (int i = 0; i < MAX_ROWS; ++i)
grid(0).cache(DEFAULT_CACHE_NAME).put(i, i);
}

/**
* Called before execution of every test method in class.
*
* @throws Exception If failed.
*/
@Before
public void before() throws Exception {
conn = DriverManager.getConnection(URL);

conn.setSchema('"' + DEFAULT_CACHE_NAME + '"');

stmt = conn.createStatement();

assert stmt != null;
assert !stmt.isClosed();
}

/**
* Called after execution of every test method in class.
*
* @throws Exception If failed.
*/
@After
public void after() throws Exception {
if (stmt != null && !stmt.isClosed()) {
stmt.close();

assert stmt.isClosed();
}

conn.close();

assert stmt.isClosed();
assert conn.isClosed();
}

/**
*
*/
@Test
public void testSettingNegativeConnectionTimeout() {

GridTestUtils.assertThrows(log,
() -> {
conn.setNetworkTimeout(EXECUTOR_STUB, -1);
return null;
},
SQLException.class, "Network timeout cannot be negative.");
}

/**
* @throws Exception If failed.
*/
@Test
public void testConnectionTimeoutRetrieval() throws Exception {
conn.setNetworkTimeout(EXECUTOR_STUB, 2000);
assertEquals(2000, conn.getNetworkTimeout());
}

/**
* @throws Exception If failed.
*/
@Test
public void testConnectionTimeout() throws Exception {

conn.setNetworkTimeout(EXECUTOR_STUB, 1000);

GridTestUtils.assertThrows(log,
() -> {
stmt.execute("select sleep_func(2000)");
return null;
},
SQLException.class, "Connection timed out.");

GridTestUtils.assertThrows(log,
() -> {
stmt.execute("select 1");
return null;
},
SQLException.class, "Statement is closed.");
}

/**
* @throws Exception If failed.
*/
@Test
public void testQueryTimeoutOccursBeforeConnectionTimeout() throws Exception {
conn.setNetworkTimeout(EXECUTOR_STUB, 10_000);

stmt.setQueryTimeout(1);

GridTestUtils.assertThrows(log, () -> {
stmt.executeQuery("select sleep_func(10) from Integer;");

return null;
}, SQLTimeoutException.class, "The query was cancelled while executing.");

stmt.execute("select 1");
}

/**
* @throws Exception If failed.
*/
@Test
public void testConnectionTimeoutUpdate() throws Exception {
conn.setNetworkTimeout(EXECUTOR_STUB, 5000);

stmt.execute("select sleep_func(1000)");

conn.setNetworkTimeout(EXECUTOR_STUB, 500);

GridTestUtils.assertThrows(log, () -> {
stmt.execute("select sleep_func(1000)");
return null;
}, SQLException.class, "Connection timed out.");
}

/**
* @throws Exception If failed.
*/
@Test
public void testCancelingTimedOutStatement() throws Exception {
conn.setNetworkTimeout(EXECUTOR_STUB, 1);

GridTestUtils.runAsync(
() -> {
try {
Thread.sleep(1000);

GridTestUtils.assertThrows(log,
() -> {
stmt.cancel();
return null;
},
SQLException.class, "Statement is closed.");
}
catch (Exception e) {
log.error("Unexpected exception.", e);

fail("Unexpected exception");
}
});

GridTestUtils.runAsync(() -> {
try {
GridTestUtils.assertThrows(log,
() -> {
stmt.execute("select sleep_func(1000)");
return null;
},
SQLException.class, "Connection timed out.");
}
catch (Exception e) {
log.error("Unexpected exception.", e);

fail("Unexpected exception");
}
});
}

/**
* @param v amount of milliseconds to sleep
* @return amount of milliseconds to sleep
*/
@SuppressWarnings("unused")
@QuerySqlFunction
public static int sleep_func(int v) {
try {
Thread.sleep(v);
}
catch (InterruptedException ignored) {
// No-op
}
return v;
}
}

0 comments on commit 40f5554

Please sign in to comment.