1818
1919import static org .junit .jupiter .api .Assertions .assertEquals ;
2020import static org .junit .jupiter .api .Assertions .assertNull ;
21+ import static org .junit .jupiter .api .Assertions .assertTrue ;
2122
23+ import java .util .concurrent .CountDownLatch ;
24+ import java .util .concurrent .TimeUnit ;
25+ import java .util .concurrent .atomic .AtomicReference ;
2226import org .junit .jupiter .api .AfterEach ;
2327import org .junit .jupiter .api .BeforeEach ;
2428import org .junit .jupiter .api .Test ;
@@ -43,27 +47,106 @@ public void tearDown() {
4347 @ Test
4448 public void testRegisterAndRetrieveConnectionId () {
4549 BigQueryJdbcMdc .registerInstance (mockConnection1 , "123" );
46-
47- assertEquals ("JdbcConnection-123" , BigQueryJdbcMdc .getConnectionId (mockConnection1 ));
4850 assertEquals ("JdbcConnection-123" , BigQueryJdbcMdc .getConnectionId ());
4951 }
5052
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+
5167 @ Test
5268 public void testClearContext () {
5369 BigQueryJdbcMdc .registerInstance (mockConnection1 , "456" );
54- assertEquals ("JdbcConnection-456" , BigQueryJdbcMdc .getConnectionId (mockConnection1 ));
70+ assertEquals ("JdbcConnection-456" , BigQueryJdbcMdc .getConnectionId ());
5571
5672 BigQueryJdbcMdc .clear ();
57-
5873 assertNull (BigQueryJdbcMdc .getConnectionId ());
5974 }
6075
6176 @ Test
62- public void testMultipleConnectionsSameThread () {
63- BigQueryJdbcMdc .registerInstance (mockConnection1 , "111" );
64- BigQueryJdbcMdc .registerInstance (mockConnection2 , "222" );
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 ));
65146
66- assertEquals ("JdbcConnection-111" , BigQueryJdbcMdc .getConnectionId (mockConnection1 ));
67- assertEquals ("JdbcConnection-222" , BigQueryJdbcMdc .getConnectionId (mockConnection2 ));
147+ assertEquals ("JdbcConnection-A" , threadAIdBeforeB .get ());
148+ assertNull (threadBIdBeforeRegister .get ());
149+ assertEquals ("JdbcConnection-B" , threadBIdAfterRegister .get ());
150+ assertEquals ("JdbcConnection-A" , threadAIdAfterB .get ());
68151 }
69152}
0 commit comments