use of com.google.cloud.spanner.pgadapter.wireprotocol.ParseMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testParseMessageWithNonMatchingParameterTypeCount.
@Test
public void testParseMessageWithNonMatchingParameterTypeCount() throws Exception {
byte[] messageMetadata = { 'P' };
String statementName = "some statement\0";
String payload = "SELECT * FROM users WHERE name = $1 /*This is a comment*/ --this is another comment\0";
byte[] length = intToBytes(4 + statementName.length() + payload.length() + 1);
byte[] value = Bytes.concat(messageMetadata, length, statementName.getBytes(), payload.getBytes(), intToBytes(0));
int[] expectedParameterDataTypes = new int[0];
String expectedSQL = "SELECT * FROM users WHERE name = $1";
String expectedMessageName = "some statement";
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
ByteArrayOutputStream result = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(result);
when(connectionHandler.getServer()).thenReturn(server);
when(server.getOptions()).thenReturn(options);
when(connectionHandler.getSpannerConnection()).thenReturn(connection);
when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
when(connectionMetadata.getInputStream()).thenReturn(inputStream);
when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
WireMessage message = ControlMessage.create(connectionHandler);
assertEquals(ParseMessage.class, message.getClass());
assertEquals(expectedMessageName, ((ParseMessage) message).getName());
assertEquals(expectedSQL, ((ParseMessage) message).getStatement().getSql());
assertArrayEquals(expectedParameterDataTypes, ((ParseMessage) message).getStatement().getParameterDataTypes());
when(connectionHandler.hasStatement(anyString())).thenReturn(false);
message.send();
verify(connectionHandler).registerStatement(expectedMessageName, ((ParseMessage) message).getStatement());
// ParseCompleteResponse
DataInputStream outputResult = inputStreamFromOutputStream(result);
assertEquals('1', outputResult.readByte());
assertEquals(4, outputResult.readInt());
}
use of com.google.cloud.spanner.pgadapter.wireprotocol.ParseMessage in project pgadapter by GoogleCloudPlatform.
the class JdbcMockServerTest method testCursorFailsHalfway.
@Test
public void testCursorFailsHalfway() throws SQLException {
mockSpanner.setExecuteStreamingSqlExecutionTime(SimulatedExecutionTime.ofStreamException(Status.DATA_LOSS.asRuntimeException(), 2));
try (Connection connection = DriverManager.getConnection(createUrl())) {
connection.setAutoCommit(false);
try (PreparedStatement statement = connection.prepareStatement(SELECT_FIVE_ROWS.getSql())) {
// Fetch one row at a time from the PG server.
statement.setFetchSize(1);
try (ResultSet resultSet = statement.executeQuery()) {
// The first row should succeed.
assertTrue(resultSet.next());
// The second row should fail.
assertThrows(SQLException.class, resultSet::next);
}
}
connection.rollback();
}
// The ExecuteSql request should only be sent once to Cloud Spanner.
assertEquals(1, mockSpanner.countRequestsOfType(ExecuteSqlRequest.class));
ExecuteSqlRequest executeRequest = mockSpanner.getRequestsOfType(ExecuteSqlRequest.class).get(0);
assertEquals(QueryMode.NORMAL, executeRequest.getQueryMode());
assertEquals(SELECT_FIVE_ROWS.getSql(), executeRequest.getSql());
// The JDBC driver does not send a ROLLBACK
if (pgServer != null) {
List<DescribeMessage> describeMessages = getWireMessagesOfType(DescribeMessage.class);
assertEquals(1, describeMessages.size());
DescribeMessage describeMessage = describeMessages.get(0);
assertEquals(PreparedType.Portal, describeMessage.getType());
List<ExecuteMessage> executeMessages = getWireMessagesOfType(ExecuteMessage.class);
assertEquals(4, executeMessages.size());
assertEquals("", executeMessages.get(0).getName());
for (ExecuteMessage executeMessage : executeMessages.subList(1, executeMessages.size() - 1)) {
assertEquals(describeMessage.getName(), executeMessage.getName());
assertEquals(1, executeMessage.getMaxRows());
}
assertEquals("", executeMessages.get(executeMessages.size() - 1).getName());
List<ParseMessage> parseMessages = getWireMessagesOfType(ParseMessage.class);
assertEquals(3, parseMessages.size());
assertEquals("BEGIN", parseMessages.get(0).getStatement().getSql());
assertEquals(SELECT_FIVE_ROWS.getSql(), parseMessages.get(1).getStatement().getSql());
assertEquals("ROLLBACK", parseMessages.get(2).getStatement().getSql());
}
}
use of com.google.cloud.spanner.pgadapter.wireprotocol.ParseMessage in project pgadapter by GoogleCloudPlatform.
the class ProtocolTest method testParseMessage.
@Test
public void testParseMessage() throws Exception {
byte[] messageMetadata = { 'P' };
String statementName = "some statement\0";
String payload = "SELECT * FROM users WHERE name = $1 /*This is a comment*/ --this is another comment\0";
byte[] parameterCount = { 0, 1 };
byte[] parameters = intToBytes(1002);
byte[] length = intToBytes(4 + statementName.length() + payload.length() + parameterCount.length + parameters.length);
byte[] value = Bytes.concat(messageMetadata, length, statementName.getBytes(), payload.getBytes(), parameterCount, parameters);
int[] expectedParameterDataTypes = new int[] { 1002 };
String expectedSQL = "SELECT * FROM users WHERE name = $1";
String expectedMessageName = "some statement";
DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(value));
ByteArrayOutputStream result = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(result);
when(connectionHandler.getServer()).thenReturn(server);
when(server.getOptions()).thenReturn(options);
when(connectionHandler.getSpannerConnection()).thenReturn(connection);
when(connectionHandler.getConnectionMetadata()).thenReturn(connectionMetadata);
when(connectionMetadata.getInputStream()).thenReturn(inputStream);
when(connectionMetadata.getOutputStream()).thenReturn(outputStream);
WireMessage message = ControlMessage.create(connectionHandler);
assertEquals(ParseMessage.class, message.getClass());
assertEquals(expectedMessageName, ((ParseMessage) message).getName());
assertEquals(expectedSQL, ((ParseMessage) message).getStatement().getSql());
assertArrayEquals(expectedParameterDataTypes, ((ParseMessage) message).getStatement().getParameterDataTypes());
when(connectionHandler.hasStatement(anyString())).thenReturn(false);
message.send();
verify(connectionHandler).registerStatement(expectedMessageName, ((ParseMessage) message).getStatement());
// ParseCompleteResponse
DataInputStream outputResult = inputStreamFromOutputStream(result);
assertEquals('1', outputResult.readByte());
assertEquals(4, outputResult.readInt());
}
Aggregations