|
| 1 | +/* |
| 2 | + * Copyright 2023 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.spanner.jdbc; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertThrows; |
| 21 | +import static org.junit.Assert.assertTrue; |
| 22 | + |
| 23 | +import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult; |
| 24 | +import com.google.cloud.spanner.SessionPoolOptions; |
| 25 | +import com.google.cloud.spanner.connection.AbstractMockServerTest; |
| 26 | +import com.google.cloud.spanner.connection.ConnectionOptions; |
| 27 | +import com.google.protobuf.ListValue; |
| 28 | +import com.google.protobuf.Value; |
| 29 | +import com.google.rpc.Code; |
| 30 | +import com.google.spanner.v1.ResultSetMetadata; |
| 31 | +import com.google.spanner.v1.StructType; |
| 32 | +import com.google.spanner.v1.StructType.Field; |
| 33 | +import com.google.spanner.v1.Type; |
| 34 | +import com.google.spanner.v1.TypeCode; |
| 35 | +import java.sql.Connection; |
| 36 | +import java.sql.SQLException; |
| 37 | +import org.junit.Before; |
| 38 | +import org.junit.Test; |
| 39 | +import org.junit.runner.RunWith; |
| 40 | +import org.junit.runners.JUnit4; |
| 41 | + |
| 42 | +/** |
| 43 | + * Test class for verifying that the methods execute, executeQuery, and executeUpdate work as |
| 44 | + * intended. |
| 45 | + */ |
| 46 | +@RunWith(JUnit4.class) |
| 47 | +public class ExecuteMockServerTest extends AbstractMockServerTest { |
| 48 | + private static final String QUERY = "select * from my_table"; |
| 49 | + |
| 50 | + @Before |
| 51 | + public void setupResults() { |
| 52 | + super.setupResults(); |
| 53 | + com.google.spanner.v1.ResultSet resultSet = |
| 54 | + com.google.spanner.v1.ResultSet.newBuilder() |
| 55 | + .setMetadata( |
| 56 | + ResultSetMetadata.newBuilder() |
| 57 | + .setRowType( |
| 58 | + StructType.newBuilder() |
| 59 | + .addFields( |
| 60 | + Field.newBuilder() |
| 61 | + .setType(Type.newBuilder().setCode(TypeCode.INT64).build()) |
| 62 | + .setName("id") |
| 63 | + .build()) |
| 64 | + .addFields( |
| 65 | + Field.newBuilder() |
| 66 | + .setType(Type.newBuilder().setCode(TypeCode.STRING).build()) |
| 67 | + .setName("value") |
| 68 | + .build()) |
| 69 | + .build()) |
| 70 | + .build()) |
| 71 | + .addRows( |
| 72 | + ListValue.newBuilder() |
| 73 | + .addValues(Value.newBuilder().setStringValue("1").build()) |
| 74 | + .addValues(Value.newBuilder().setStringValue("One").build()) |
| 75 | + .build()) |
| 76 | + .build(); |
| 77 | + mockSpanner.putStatementResult( |
| 78 | + StatementResult.query(com.google.cloud.spanner.Statement.of(QUERY), resultSet)); |
| 79 | + } |
| 80 | + |
| 81 | + private String createUrl() { |
| 82 | + return String.format( |
| 83 | + "jdbc:cloudspanner://localhost:%d/projects/%s/instances/%s/databases/%s?usePlainText=true", |
| 84 | + getPort(), "proj", "inst", "db"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testInvalidExecuteUpdate_shouldNotLeakSession() throws SQLException { |
| 89 | + int maxSessions = 1; |
| 90 | + try (Connection connection = |
| 91 | + new JdbcConnection( |
| 92 | + createUrl(), |
| 93 | + ConnectionOptions.newBuilder() |
| 94 | + .setUri(createUrl().substring("jdbc:".length())) |
| 95 | + .setSessionPoolOptions( |
| 96 | + SessionPoolOptions.newBuilder() |
| 97 | + .setMinSessions(1) |
| 98 | + .setMaxSessions(maxSessions) |
| 99 | + .setFailIfPoolExhausted() |
| 100 | + .build()) |
| 101 | + .build())) { |
| 102 | + |
| 103 | + for (int i = 0; i < (maxSessions + 1); i++) { |
| 104 | + SQLException exception = |
| 105 | + assertThrows( |
| 106 | + SQLException.class, () -> connection.createStatement().executeUpdate(QUERY)); |
| 107 | + assertTrue(exception instanceof JdbcSqlException); |
| 108 | + JdbcSqlException jdbcSqlException = (JdbcSqlException) exception; |
| 109 | + // This would be RESOURCE_EXHAUSTED if the query leaked a session. |
| 110 | + assertEquals(Code.INVALID_ARGUMENT, jdbcSqlException.getCode()); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments