|
36 | 36 | import org.apache.arrow.flight.FlightStatusCode; |
37 | 37 | import org.junit.jupiter.api.Assertions; |
38 | 38 | import org.junit.jupiter.api.Test; |
| 39 | +import org.junit.jupiter.api.Timeout; |
39 | 40 | import org.mockito.InOrder; |
40 | 41 | import org.mockito.MockedStatic; |
41 | 42 | import org.mockito.Mockito; |
|
44 | 45 | import org.xnio.XnioWorker; |
45 | 46 | import org.xnio.conduits.ConduitStreamSourceChannel; |
46 | 47 |
|
| 48 | +import java.util.ArrayList; |
| 49 | +import java.util.Collections; |
| 50 | +import java.util.List; |
| 51 | +import java.util.concurrent.CyclicBarrier; |
47 | 52 | import java.util.concurrent.RejectedExecutionException; |
48 | 53 |
|
49 | 54 | public class ConnectionExceedTest { |
@@ -190,4 +195,63 @@ public void testFlightSessionConnectionExceed() throws Exception { |
190 | 195 | Assertions.assertEquals(2, scheduler.getConnectPoolMgr().getFlightConnectionNum()); |
191 | 196 | } |
192 | 197 | } |
| 198 | + |
| 199 | + // Two first requests carrying the same bearer token race session creation. getConnectContext |
| 200 | + // serializes creation and re-checks the peer-identity index, so exactly one ConnectContext is |
| 201 | + // published for the token and every caller gets it. Without that (both build and register one, or |
| 202 | + // the loser hits the created-session guard) the pool would hold a second, orphaned session, or a |
| 203 | + // caller would fail. Each worker installs its own static mocks because Mockito's mockStatic is |
| 204 | + // thread-local; the ConnectScheduler is the one shared instance they publish into. |
| 205 | + @Test |
| 206 | + @Timeout(60) |
| 207 | + public void testConcurrentFirstRequestsPublishExactlyOneSessionPerToken() throws Exception { |
| 208 | + ConnectScheduler scheduler = new ConnectScheduler(1000, 100); |
| 209 | + Mockito.when(mockEnv.getInternalCatalog()).thenReturn(mockCatalog); |
| 210 | + Mockito.when(mockCatalog.getName()).thenReturn("internal"); |
| 211 | + Mockito.when(mockAuth.getMaxConn(Mockito.anyString())).thenReturn(100L); |
| 212 | + Mockito.when(mockEnv.getAuth()).thenReturn(mockAuth); |
| 213 | + Mockito.when(mockExecuteEnv.getScheduler()).thenReturn(scheduler); |
| 214 | + |
| 215 | + UserIdentity userIdentity = UserIdentity.createAnalyzedUserIdentWithIp("test_user", "%"); |
| 216 | + FlightTokenDetails tokenDetails = new FlightTokenDetails( |
| 217 | + "shared_token", "test_user", |
| 218 | + System.currentTimeMillis(), System.currentTimeMillis() + 3600000, |
| 219 | + userIdentity, "127.0.0.1"); |
| 220 | + Mockito.when(mockTokenManager.validateToken("shared_token")).thenReturn(tokenDetails); |
| 221 | + |
| 222 | + FlightSessionsWithTokenManager manager = new FlightSessionsWithTokenManager(mockTokenManager); |
| 223 | + |
| 224 | + int threadCount = 6; |
| 225 | + CyclicBarrier barrier = new CyclicBarrier(threadCount); |
| 226 | + List<ConnectContext> results = Collections.synchronizedList(new ArrayList<>()); |
| 227 | + List<Throwable> errors = Collections.synchronizedList(new ArrayList<>()); |
| 228 | + List<Thread> threads = new ArrayList<>(); |
| 229 | + for (int i = 0; i < threadCount; i++) { |
| 230 | + Thread t = new Thread(() -> { |
| 231 | + try (MockedStatic<ExecuteEnv> execEnv = Mockito.mockStatic(ExecuteEnv.class); |
| 232 | + MockedStatic<Env> env = Mockito.mockStatic(Env.class)) { |
| 233 | + execEnv.when(ExecuteEnv::getInstance).thenReturn(mockExecuteEnv); |
| 234 | + env.when(Env::getCurrentEnv).thenReturn(mockEnv); |
| 235 | + barrier.await(); |
| 236 | + results.add(manager.getConnectContext("shared_token")); |
| 237 | + } catch (Throwable e) { |
| 238 | + errors.add(e); |
| 239 | + } |
| 240 | + }); |
| 241 | + threads.add(t); |
| 242 | + t.start(); |
| 243 | + } |
| 244 | + for (Thread t : threads) { |
| 245 | + t.join(); |
| 246 | + } |
| 247 | + |
| 248 | + Assertions.assertTrue(errors.isEmpty(), "getConnectContext threw: " + errors); |
| 249 | + Assertions.assertEquals(1, scheduler.getConnectionNum(), |
| 250 | + "concurrent first requests on one token must publish exactly one session"); |
| 251 | + ConnectContext published = scheduler.getContextWithPeerIdentity("shared_token"); |
| 252 | + Assertions.assertNotNull(published); |
| 253 | + for (ConnectContext c : results) { |
| 254 | + Assertions.assertSame(published, c, "every caller must get the one published session"); |
| 255 | + } |
| 256 | + } |
193 | 257 | } |
0 commit comments