|
17 | 17 | package com.google.cloud.spanner;
|
18 | 18 |
|
19 | 19 | import static com.google.common.truth.Truth.assertThat;
|
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | +import static org.junit.Assert.assertNull; |
| 22 | +import static org.junit.Assert.assertTrue; |
20 | 23 | import static org.mockito.ArgumentMatchers.any;
|
21 | 24 | import static org.mockito.Mockito.doNothing;
|
22 | 25 | import static org.mockito.Mockito.mock;
|
|
29 | 32 | import com.google.cloud.spanner.SessionClient.SessionConsumer;
|
30 | 33 | import com.google.cloud.spanner.spi.v1.SpannerRpc;
|
31 | 34 | import com.google.cloud.spanner.spi.v1.SpannerRpc.Option;
|
| 35 | +import com.google.common.collect.ImmutableMap; |
32 | 36 | import io.opencensus.trace.Tracing;
|
33 | 37 | import io.opentelemetry.api.OpenTelemetry;
|
34 | 38 | import java.util.ArrayList;
|
@@ -151,6 +155,81 @@ public void createAndCloseSession() {
|
151 | 155 | }
|
152 | 156 | }
|
153 | 157 |
|
| 158 | + @Test |
| 159 | + public void createAndCloseMultiplexedSession() { |
| 160 | + DatabaseId db = DatabaseId.of(dbName); |
| 161 | + String sessionName = dbName + "/sessions/s1"; |
| 162 | + Map<String, String> labels = ImmutableMap.of("env", "dev"); |
| 163 | + String databaseRole = "role"; |
| 164 | + when(spannerOptions.getSessionLabels()).thenReturn(labels); |
| 165 | + when(spannerOptions.getDatabaseRole()).thenReturn(databaseRole); |
| 166 | + com.google.spanner.v1.Session sessionProto = |
| 167 | + com.google.spanner.v1.Session.newBuilder() |
| 168 | + .setName(sessionName) |
| 169 | + .setMultiplexed(true) |
| 170 | + .putAllLabels(labels) |
| 171 | + .build(); |
| 172 | + when(rpc.createSession( |
| 173 | + Mockito.eq(dbName), |
| 174 | + Mockito.eq(databaseRole), |
| 175 | + Mockito.eq(labels), |
| 176 | + options.capture(), |
| 177 | + Mockito.eq(true))) |
| 178 | + .thenReturn(sessionProto); |
| 179 | + final AtomicInteger returnedSessionCount = new AtomicInteger(); |
| 180 | + final SessionConsumer consumer = |
| 181 | + new SessionConsumer() { |
| 182 | + @Override |
| 183 | + public void onSessionReady(SessionImpl session) { |
| 184 | + assertEquals(sessionName, session.getName()); |
| 185 | + returnedSessionCount.incrementAndGet(); |
| 186 | + |
| 187 | + session.close(); |
| 188 | + Mockito.verify(rpc).deleteSession(sessionName, options.getValue()); |
| 189 | + } |
| 190 | + |
| 191 | + @Override |
| 192 | + public void onSessionCreateFailure(Throwable t, int createFailureForSessionCount) {} |
| 193 | + }; |
| 194 | + try (SessionClient client = new SessionClient(spanner, db, new TestExecutorFactory())) { |
| 195 | + client.createMultiplexedSession(consumer); |
| 196 | + } |
| 197 | + // for multiplexed session there is no channel hint pass in the RPC options |
| 198 | + assertNull(options.getValue()); |
| 199 | + assertEquals(1, returnedSessionCount.get()); |
| 200 | + } |
| 201 | + |
| 202 | + @Test |
| 203 | + public void createAndCloseMultiplexedSession_whenRPCThrowsException_thenAssertException() { |
| 204 | + DatabaseId db = DatabaseId.of(dbName); |
| 205 | + Map<String, String> labels = ImmutableMap.of("env", "dev"); |
| 206 | + String databaseRole = "role"; |
| 207 | + when(spannerOptions.getSessionLabels()).thenReturn(labels); |
| 208 | + when(spannerOptions.getDatabaseRole()).thenReturn(databaseRole); |
| 209 | + when(rpc.createSession( |
| 210 | + Mockito.eq(dbName), |
| 211 | + Mockito.eq(databaseRole), |
| 212 | + Mockito.eq(labels), |
| 213 | + options.capture(), |
| 214 | + Mockito.eq(true))) |
| 215 | + .thenThrow(RuntimeException.class); |
| 216 | + final SessionConsumer consumer = |
| 217 | + new SessionConsumer() { |
| 218 | + @Override |
| 219 | + public void onSessionReady(SessionImpl session) {} |
| 220 | + |
| 221 | + @Override |
| 222 | + public void onSessionCreateFailure(Throwable t, int createFailureForSessionCount) { |
| 223 | + assertTrue(t instanceof RuntimeException); |
| 224 | + } |
| 225 | + }; |
| 226 | + try (SessionClient client = new SessionClient(spanner, db, new TestExecutorFactory())) { |
| 227 | + client.createMultiplexedSession(consumer); |
| 228 | + } |
| 229 | + // for multiplexed session there is no channel hint pass in the RPC options |
| 230 | + assertNull(options.getValue()); |
| 231 | + } |
| 232 | + |
154 | 233 | @SuppressWarnings("unchecked")
|
155 | 234 | @Test
|
156 | 235 | public void batchCreateAndCloseSessions() {
|
|
0 commit comments