use of com.swiftmq.impl.routing.single.smqpr.v400.CommitRequest in project pgadapter by GoogleCloudPlatform.
the class JdbcSimpleModeMockServerTest method testTransactionStatementsInBatch.
@Test
public void testTransactionStatementsInBatch() throws SQLException {
String sql = "BEGIN TRANSACTION; INSERT INTO FOO VALUES (1); UPDATE FOO SET BAR=1 WHERE BAZ=2; COMMIT;";
try (Connection connection = DriverManager.getConnection(createUrl())) {
try (Statement statement = connection.createStatement()) {
// Send a complete transaction as a single batch.
assertFalse(statement.execute(sql));
// The BEGIN statement should not return anything.
assertEquals(0, statement.getUpdateCount());
// INSERT
assertFalse(statement.getMoreResults());
assertEquals(1, statement.getUpdateCount());
// UPDATE
assertFalse(statement.getMoreResults());
assertEquals(2, statement.getUpdateCount());
// COMMIT
assertFalse(statement.getMoreResults());
assertEquals(0, statement.getUpdateCount());
assertFalse(statement.getMoreResults());
assertEquals(-1, statement.getUpdateCount());
}
}
// As the two DML statements are adjacent in the batch, they can be combined into a BatchDML
// request. This is the first statement in the transaction and will include the BeginTransaction
// option.
List<ExecuteBatchDmlRequest> batchDmlRequests = mockSpanner.getRequestsOfType(ExecuteBatchDmlRequest.class);
assertEquals(1, batchDmlRequests.size());
ExecuteBatchDmlRequest batchDmlRequest = batchDmlRequests.get(0);
assertTrue(batchDmlRequest.getTransaction().hasBegin());
assertEquals(INSERT_STATEMENT.getSql(), batchDmlRequest.getStatements(0).getSql());
assertEquals(UPDATE_STATEMENT.getSql(), batchDmlRequest.getStatements(1).getSql());
List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
assertEquals(1, commitRequests.size());
}
use of com.swiftmq.impl.routing.single.smqpr.v400.CommitRequest in project pgadapter by GoogleCloudPlatform.
the class JdbcSimpleModeMockServerTest method testErrorHandlingOfDmlBatchAfterCommit.
@Test
public void testErrorHandlingOfDmlBatchAfterCommit() throws SQLException {
String sql = String.format("%s; %s; %s; %s; %s;", INSERT_STATEMENT, "COMMIT", UPDATE_STATEMENT, INVALID_DML, SELECT1);
try (Connection connection = DriverManager.getConnection(createUrl())) {
try (java.sql.Statement statement = connection.createStatement()) {
SQLException exception = assertThrows(SQLException.class, () -> statement.execute(sql));
assertThat(exception.getMessage(), containsString("INVALID_ARGUMENT: Statement is invalid."));
}
}
List<ExecuteSqlRequest> requests = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class);
assertEquals(1, requests.size());
assertEquals(INSERT_STATEMENT.getSql(), requests.get(0).getSql());
List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
assertEquals(1, commitRequests.size());
List<ExecuteBatchDmlRequest> batchDmlRequests = mockSpanner.getRequestsOfType(ExecuteBatchDmlRequest.class);
assertEquals(1, batchDmlRequests.size());
ExecuteBatchDmlRequest request = batchDmlRequests.get(0);
assertEquals(2, request.getStatementsCount());
assertEquals(UPDATE_STATEMENT.getSql(), request.getStatements(0).getSql());
assertEquals(INVALID_DML.getSql(), request.getStatements(1).getSql());
}
use of com.swiftmq.impl.routing.single.smqpr.v400.CommitRequest in project pgadapter by GoogleCloudPlatform.
the class CopyInMockServerTest method testCopyInError_BatchedNonAtomic.
@Test
public void testCopyInError_BatchedNonAtomic() throws SQLException {
setupCopyInformationSchemaResults();
mockSpanner.setCommitExecutionTime(SimulatedExecutionTime.ofException(Status.INVALID_ARGUMENT.asRuntimeException()));
try (Connection connection = DriverManager.getConnection(createUrl())) {
System.setProperty("copy_in_commit_limit", "10");
connection.createStatement().execute("set spanner.autocommit_dml_mode='partitioned_non_atomic'");
CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class));
SQLException exception = assertThrows(SQLException.class, () -> copyManager.copyIn("COPY users FROM STDIN;", new StringReader("5\t5\t5\n6\t6\t6\n7\t7\t7\n")));
assertTrue(exception.getMessage().contains("io.grpc.StatusRuntimeException: INVALID_ARGUMENT"));
} finally {
System.getProperties().remove("copy_in_commit_limit");
}
// The server should receive between 1 and 3 commit requests. We don't know exactly how many as
// the commits can be executed in parallel.
List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
assertTrue("Number of commits should be between 1 and 3", commitRequests.size() >= 1 && commitRequests.size() <= 3);
}
use of com.swiftmq.impl.routing.single.smqpr.v400.CommitRequest in project pgadapter by GoogleCloudPlatform.
the class CopyInMockServerTest method testCopyIn_QueryDuringCopy.
@Test
public void testCopyIn_QueryDuringCopy() throws SQLException, NoSuchFieldException, IllegalAccessException, IOException {
setupCopyInformationSchemaResults();
byte[] payload = "5\t5\t5\n".getBytes(StandardCharsets.UTF_8);
try (Connection connection = DriverManager.getConnection(createUrl())) {
CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class));
// Start a copy operation and then try to execute a query during the copy.
CopyIn copyOperation = copyManager.copyIn("COPY users FROM STDIN;");
copyOperation.writeToCopy(payload, 0, payload.length);
copyOperation.flushCopy();
// Use reflection to get hold of the underlying stream, so we can send an invalid message.
java.lang.reflect.Field queryExecutorField = CopyOperationImpl.class.getDeclaredField("queryExecutor");
queryExecutorField.setAccessible(true);
QueryExecutorImpl queryExecutor = (QueryExecutorImpl) queryExecutorField.get(copyOperation);
java.lang.reflect.Field pgStreamField = QueryExecutorBase.class.getDeclaredField("pgStream");
pgStreamField.setAccessible(true);
PGStream stream = (PGStream) pgStreamField.get(queryExecutor);
stream.sendChar('Q');
// Length = 4 + 8 + 1 = 13
// (msg length 4 bytes, 8 bytes for SELECT 1, 1 byte for \0)
stream.sendInteger4(13);
stream.send("SELECT 1".getBytes(StandardCharsets.UTF_8));
stream.sendChar(0);
stream.flush();
// PGAdapter drops the connection if an invalid message is received during a COPY. This is a
// safety measure as there is no other error handling in the COPY protocol, and the server
// could otherwise have been completely flushed with garbage if it continued to receive
// messages after receiving an invalid message.
SQLException exception = assertThrows(SQLException.class, copyOperation::endCopy);
assertEquals("Database connection failed when ending copy", exception.getMessage());
}
List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
assertTrue(commitRequests.isEmpty());
}
use of com.swiftmq.impl.routing.single.smqpr.v400.CommitRequest in project pgadapter by GoogleCloudPlatform.
the class CopyInMockServerTest method testCopyInExceedsCommitSizeLimit_BatchesInNonAtomicMode.
@Test
public void testCopyInExceedsCommitSizeLimit_BatchesInNonAtomicMode() throws SQLException, IOException {
setupCopyInformationSchemaResults();
try (Connection connection = DriverManager.getConnection(createUrl())) {
System.setProperty("copy_in_commit_limit", "10");
connection.createStatement().execute("set spanner.autocommit_dml_mode='partitioned_non_atomic'");
CopyManager copyManager = new CopyManager(connection.unwrap(BaseConnection.class));
copyManager.copyIn("COPY users FROM STDIN;", new StringReader("5\t5\t5\n6\t6\t6\n7\t7\t7\n"));
} finally {
System.getProperties().remove("copy_in_commit_limit");
}
List<CommitRequest> commitRequests = mockSpanner.getRequestsOfType(CommitRequest.class);
assertEquals(3, commitRequests.size());
for (CommitRequest request : commitRequests) {
assertEquals(1, request.getMutationsCount());
Mutation mutation = request.getMutations(0);
assertEquals(OperationCase.INSERT, mutation.getOperationCase());
assertEquals(1, mutation.getInsert().getValuesCount());
}
}
Aggregations