use of org.apache.calcite.avatica.Meta.ConnectionHandle in project calcite-avatica by apache.
the class JdbcMetaTest method testRacingConnectionOpens.
@Test
public void testRacingConnectionOpens() throws Exception {
final Map<String, String> properties = Collections.emptyMap();
final Connection conn1 = Mockito.mock(Connection.class);
final Connection conn2 = Mockito.mock(Connection.class);
final ConnectionHandle ch1 = new ConnectionHandle("id1");
// Override JdbcMeta to shim in a fake Connection object. Irrelevant for the test
JdbcMeta meta = new JdbcMeta("jdbc:url") {
@Override
protected Connection createConnection(String url, Properties info) {
// Hack to mimic the race condition of a connection being cached by another thread.
getConnectionCache().put(ch1.id, conn1);
// Return our "newly created" connectino
return conn2;
}
};
try {
meta.openConnection(ch1, properties);
fail("Should see an exception when the cache already contained our connection after" + " creating it");
} catch (RuntimeException e) {
// pass
}
// Our opened connection should get closed when this race condition happens
Mockito.verify(conn2).close();
}
use of org.apache.calcite.avatica.Meta.ConnectionHandle in project calcite-avatica by apache.
the class JdbcMetaTest method testPrepareSetsMaxRows.
@Test
public void testPrepareSetsMaxRows() throws Exception {
final String id = UUID.randomUUID().toString();
final String sql = "SELECT * FROM FOO";
final int maxRows = 500;
final ConnectionHandle ch = new ConnectionHandle(id);
final AtomicInteger statementIdGenerator = new AtomicInteger(0);
JdbcMeta meta = Mockito.mock(JdbcMeta.class);
Connection connection = Mockito.mock(Connection.class);
PreparedStatement statement = Mockito.mock(PreparedStatement.class);
ResultSetMetaData resultSetMetaData = Mockito.mock(ResultSetMetaData.class);
ParameterMetaData parameterMetaData = Mockito.mock(ParameterMetaData.class);
@SuppressWarnings("unchecked") Cache<Integer, StatementInfo> statementCache = (Cache<Integer, StatementInfo>) Mockito.mock(Cache.class);
Mockito.when(meta.getStatementIdGenerator()).thenReturn(statementIdGenerator);
Mockito.when(meta.getStatementCache()).thenReturn(statementCache);
Mockito.when(meta.getConnection(id)).thenReturn(connection);
Mockito.when(connection.prepareStatement(sql)).thenReturn(statement);
Mockito.when(statement.isWrapperFor(AvaticaPreparedStatement.class)).thenReturn(false);
Mockito.when(statement.getMetaData()).thenReturn(resultSetMetaData);
Mockito.when(statement.getParameterMetaData()).thenReturn(parameterMetaData);
// Call the real methods
Mockito.doCallRealMethod().when(meta).setMaxRows(statement, maxRows);
Mockito.doCallRealMethod().when(meta).prepare(ch, sql, maxRows);
meta.prepare(ch, sql, maxRows);
Mockito.verify(statement).setMaxRows(maxRows);
}
use of org.apache.calcite.avatica.Meta.ConnectionHandle in project calcite-avatica by apache.
the class JdbcMetaTest method testConcurrentConnectionOpening.
@Test
public void testConcurrentConnectionOpening() throws Exception {
final Map<String, String> properties = Collections.emptyMap();
final Connection conn = Mockito.mock(Connection.class);
// Override JdbcMeta to shim in a fake Connection object. Irrelevant for the test
JdbcMeta meta = new JdbcMeta("jdbc:url") {
@Override
protected Connection createConnection(String url, Properties info) {
return conn;
}
};
ConnectionHandle ch1 = new ConnectionHandle("id1");
meta.openConnection(ch1, properties);
assertEquals(conn, meta.getConnectionCache().getIfPresent(ch1.id));
try {
meta.openConnection(ch1, properties);
fail("Should not be allowed to open two connections with the same ID");
} catch (RuntimeException e) {
// pass
}
// Cached object should not change
assertEquals(conn, meta.getConnectionCache().getIfPresent(ch1.id));
}
Aggregations