|
| 1 | +/* |
| 2 | + * Copyright 2026 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.bigquery.jdbc; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 22 | + |
| 23 | +import java.util.concurrent.CountDownLatch; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | +import java.util.concurrent.atomic.AtomicReference; |
| 26 | +import org.junit.jupiter.api.AfterEach; |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.mockito.Mockito; |
| 30 | + |
| 31 | +public class BigQueryJdbcMdcTest { |
| 32 | + |
| 33 | + private BigQueryConnection mockConnection1; |
| 34 | + private BigQueryConnection mockConnection2; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + public void setUp() { |
| 38 | + mockConnection1 = Mockito.mock(BigQueryConnection.class); |
| 39 | + mockConnection2 = Mockito.mock(BigQueryConnection.class); |
| 40 | + } |
| 41 | + |
| 42 | + @AfterEach |
| 43 | + public void tearDown() { |
| 44 | + BigQueryJdbcMdc.clear(); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testRegisterAndRetrieveConnectionId() { |
| 49 | + BigQueryJdbcMdc.registerInstance(mockConnection1, "123"); |
| 50 | + assertEquals("JdbcConnection-123", BigQueryJdbcMdc.getConnectionId()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testRemoveInstance() { |
| 55 | + BigQueryJdbcMdc.registerInstance(mockConnection1, "1"); |
| 56 | + assertEquals("JdbcConnection-1", BigQueryJdbcMdc.getConnectionId()); |
| 57 | + |
| 58 | + BigQueryJdbcMdc.removeInstance(mockConnection1); |
| 59 | + // Note: removeInstance does not clear currentConnectionId on the current thread |
| 60 | + // based on current implementation. |
| 61 | + assertEquals("JdbcConnection-1", BigQueryJdbcMdc.getConnectionId()); |
| 62 | + |
| 63 | + BigQueryJdbcMdc.clear(); |
| 64 | + assertNull(BigQueryJdbcMdc.getConnectionId()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testClearContext() { |
| 69 | + BigQueryJdbcMdc.registerInstance(mockConnection1, "456"); |
| 70 | + assertEquals("JdbcConnection-456", BigQueryJdbcMdc.getConnectionId()); |
| 71 | + |
| 72 | + BigQueryJdbcMdc.clear(); |
| 73 | + assertNull(BigQueryJdbcMdc.getConnectionId()); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testThreadInheritance() throws InterruptedException { |
| 78 | + BigQueryJdbcMdc.registerInstance(mockConnection1, "parent"); |
| 79 | + assertEquals("JdbcConnection-parent", BigQueryJdbcMdc.getConnectionId()); |
| 80 | + |
| 81 | + AtomicReference<String> childConnectionId = new AtomicReference<>(); |
| 82 | + CountDownLatch latch = new CountDownLatch(1); |
| 83 | + |
| 84 | + Thread childThread = |
| 85 | + new Thread( |
| 86 | + () -> { |
| 87 | + childConnectionId.set(BigQueryJdbcMdc.getConnectionId()); |
| 88 | + latch.countDown(); |
| 89 | + }); |
| 90 | + childThread.start(); |
| 91 | + assertTrue(latch.await(5, TimeUnit.SECONDS)); |
| 92 | + |
| 93 | + assertEquals("JdbcConnection-parent", childConnectionId.get()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testThreadIsolation() throws InterruptedException { |
| 98 | + CountDownLatch threadARegistered = new CountDownLatch(1); |
| 99 | + CountDownLatch threadBChecked = new CountDownLatch(1); |
| 100 | + CountDownLatch threadBRegistered = new CountDownLatch(1); |
| 101 | + CountDownLatch testFinished = new CountDownLatch(2); |
| 102 | + |
| 103 | + AtomicReference<String> threadAIdBeforeB = new AtomicReference<>(); |
| 104 | + AtomicReference<String> threadAIdAfterB = new AtomicReference<>(); |
| 105 | + AtomicReference<String> threadBIdBeforeRegister = new AtomicReference<>(); |
| 106 | + AtomicReference<String> threadBIdAfterRegister = new AtomicReference<>(); |
| 107 | + |
| 108 | + Thread threadA = |
| 109 | + new Thread( |
| 110 | + () -> { |
| 111 | + try { |
| 112 | + BigQueryJdbcMdc.registerInstance(mockConnection1, "A"); |
| 113 | + threadAIdBeforeB.set(BigQueryJdbcMdc.getConnectionId()); |
| 114 | + threadARegistered.countDown(); |
| 115 | + |
| 116 | + threadBRegistered.await(); |
| 117 | + threadAIdAfterB.set(BigQueryJdbcMdc.getConnectionId()); |
| 118 | + } catch (InterruptedException e) { |
| 119 | + Thread.currentThread().interrupt(); |
| 120 | + } finally { |
| 121 | + testFinished.countDown(); |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + Thread threadB = |
| 126 | + new Thread( |
| 127 | + () -> { |
| 128 | + try { |
| 129 | + threadARegistered.await(); |
| 130 | + threadBIdBeforeRegister.set(BigQueryJdbcMdc.getConnectionId()); |
| 131 | + |
| 132 | + BigQueryJdbcMdc.registerInstance(mockConnection2, "B"); |
| 133 | + threadBIdAfterRegister.set(BigQueryJdbcMdc.getConnectionId()); |
| 134 | + threadBRegistered.countDown(); |
| 135 | + } catch (InterruptedException e) { |
| 136 | + Thread.currentThread().interrupt(); |
| 137 | + } finally { |
| 138 | + testFinished.countDown(); |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 142 | + threadA.start(); |
| 143 | + threadB.start(); |
| 144 | + |
| 145 | + assertTrue(testFinished.await(5, TimeUnit.SECONDS)); |
| 146 | + |
| 147 | + assertEquals("JdbcConnection-A", threadAIdBeforeB.get()); |
| 148 | + assertNull(threadBIdBeforeRegister.get()); |
| 149 | + assertEquals("JdbcConnection-B", threadBIdAfterRegister.get()); |
| 150 | + assertEquals("JdbcConnection-A", threadAIdAfterB.get()); |
| 151 | + } |
| 152 | +} |
0 commit comments