|
50 | 50 | import org.mockito.junit.MockitoJUnitRunner; |
51 | 51 |
|
52 | 52 | import java.util.List; |
| 53 | +import java.util.Map; |
| 54 | +import java.util.concurrent.ScheduledExecutorService; |
53 | 55 | import java.util.concurrent.atomic.AtomicBoolean; |
| 56 | +import java.util.concurrent.atomic.AtomicInteger; |
54 | 57 | import java.util.concurrent.atomic.AtomicReference; |
55 | 58 |
|
56 | 59 | import static org.junit.Assert.assertEquals; |
|
62 | 65 | import static org.mockito.Mockito.mock; |
63 | 66 | import static org.mockito.Mockito.doNothing; |
64 | 67 | import static org.mockito.Mockito.never; |
| 68 | +import static org.mockito.Mockito.times; |
65 | 69 | import static org.mockito.Mockito.verify; |
66 | 70 | import static org.mockito.Mockito.when; |
67 | 71 |
|
@@ -233,6 +237,84 @@ protected void transitionVpnConnectionState(Site2SiteVpnConnectionVO connection, |
233 | 237 | assertEquals(Site2SiteVpnConnection.State.Connected, transitionedState.get()); |
234 | 238 | } |
235 | 239 |
|
| 240 | + @Test |
| 241 | + public void testPollVpnConnectionStatusTransitionsDown() { |
| 242 | + Site2SiteVpnConnectionVO connection = mock(Site2SiteVpnConnectionVO.class); |
| 243 | + VpcVO vpc = mock(VpcVO.class); |
| 244 | + when(connection.getState()).thenReturn(Site2SiteVpnConnection.State.Connected); |
| 245 | + AtomicReference<Site2SiteVpnConnection.State> transitionedState = new AtomicReference<>(); |
| 246 | + |
| 247 | + NsxServiceImpl service = new NsxServiceImpl() { |
| 248 | + @Override |
| 249 | + public String getVpnConnectionStatus(Vpc vpc, String connectionUuid) { |
| 250 | + return VPN_SESSION_STATUS_DOWN; |
| 251 | + } |
| 252 | + |
| 253 | + @Override |
| 254 | + protected void transitionVpnConnectionState(Site2SiteVpnConnectionVO connection, VpcVO vpc, |
| 255 | + Site2SiteVpnConnection.State observedState, |
| 256 | + Site2SiteVpnConnection.State newState) { |
| 257 | + transitionedState.set(newState); |
| 258 | + } |
| 259 | + }; |
| 260 | + |
| 261 | + service.pollVpnConnectionStatus(connection, vpc); |
| 262 | + |
| 263 | + assertEquals(Site2SiteVpnConnection.State.Disconnected, transitionedState.get()); |
| 264 | + } |
| 265 | + |
| 266 | + @Test |
| 267 | + public void testPollVpnConnectionStatusKeepsPendingConnectionWhenSessionIsNotFound() { |
| 268 | + Site2SiteVpnConnectionVO connection = mock(Site2SiteVpnConnectionVO.class); |
| 269 | + VpcVO vpc = mock(VpcVO.class); |
| 270 | + when(connection.getState()).thenReturn(Site2SiteVpnConnection.State.Pending); |
| 271 | + AtomicBoolean transitioned = new AtomicBoolean(); |
| 272 | + |
| 273 | + NsxServiceImpl service = new NsxServiceImpl() { |
| 274 | + @Override |
| 275 | + public String getVpnConnectionStatus(Vpc vpc, String connectionUuid) { |
| 276 | + return VPN_SESSION_STATUS_NOT_FOUND; |
| 277 | + } |
| 278 | + |
| 279 | + @Override |
| 280 | + protected void transitionVpnConnectionState(Site2SiteVpnConnectionVO connection, VpcVO vpc, |
| 281 | + Site2SiteVpnConnection.State observedState, |
| 282 | + Site2SiteVpnConnection.State newState) { |
| 283 | + transitioned.set(true); |
| 284 | + } |
| 285 | + }; |
| 286 | + |
| 287 | + service.pollVpnConnectionStatus(connection, vpc); |
| 288 | + |
| 289 | + assertFalse(transitioned.get()); |
| 290 | + } |
| 291 | + |
| 292 | + @Test |
| 293 | + public void testPollVpnConnectionStatusMarksMissingConnectedSessionAsError() { |
| 294 | + Site2SiteVpnConnectionVO connection = mock(Site2SiteVpnConnectionVO.class); |
| 295 | + VpcVO vpc = mock(VpcVO.class); |
| 296 | + when(connection.getState()).thenReturn(Site2SiteVpnConnection.State.Connected); |
| 297 | + AtomicReference<Site2SiteVpnConnection.State> transitionedState = new AtomicReference<>(); |
| 298 | + |
| 299 | + NsxServiceImpl service = new NsxServiceImpl() { |
| 300 | + @Override |
| 301 | + public String getVpnConnectionStatus(Vpc vpc, String connectionUuid) { |
| 302 | + return VPN_SESSION_STATUS_NOT_FOUND; |
| 303 | + } |
| 304 | + |
| 305 | + @Override |
| 306 | + protected void transitionVpnConnectionState(Site2SiteVpnConnectionVO connection, VpcVO vpc, |
| 307 | + Site2SiteVpnConnection.State observedState, |
| 308 | + Site2SiteVpnConnection.State newState) { |
| 309 | + transitionedState.set(newState); |
| 310 | + } |
| 311 | + }; |
| 312 | + |
| 313 | + service.pollVpnConnectionStatus(connection, vpc); |
| 314 | + |
| 315 | + assertEquals(Site2SiteVpnConnection.State.Error, transitionedState.get()); |
| 316 | + } |
| 317 | + |
236 | 318 | @Test |
237 | 319 | public void testPollVpnConnectionStatusDoesNotTransitionOnQueryFailure() { |
238 | 320 | Site2SiteVpnConnectionVO connection = mock(Site2SiteVpnConnectionVO.class); |
@@ -308,12 +390,56 @@ public void testVpnStatusPollerUsesPersistedOwnershipAfterOfferingChanges() { |
308 | 390 | verify(service).pollVpnConnectionStatus(connection, vpc); |
309 | 391 | } |
310 | 392 |
|
| 393 | + @Test |
| 394 | + public void testVpnStatusPollerQueriesOnlyPollableStates() { |
| 395 | + nsxService.new VpnStatusPollTask().runInContext(); |
| 396 | + |
| 397 | + verify(site2SiteVpnConnectionDao).listByStates( |
| 398 | + Site2SiteVpnConnection.State.Pending, |
| 399 | + Site2SiteVpnConnection.State.Connecting, |
| 400 | + Site2SiteVpnConnection.State.Connected, |
| 401 | + Site2SiteVpnConnection.State.Disconnected); |
| 402 | + verify(site2SiteVpnConnectionDao, never()).listAll(); |
| 403 | + } |
| 404 | + |
| 405 | + @Test |
| 406 | + public void testVpnStatusPollerCanRestartInSameJvm() throws Exception { |
| 407 | + ScheduledExecutorService firstExecutor = mock(ScheduledExecutorService.class); |
| 408 | + ScheduledExecutorService secondExecutor = mock(ScheduledExecutorService.class); |
| 409 | + AtomicInteger executorIndex = new AtomicInteger(); |
| 410 | + NsxServiceImpl service = new NsxServiceImpl() { |
| 411 | + @Override |
| 412 | + protected ScheduledExecutorService createVpnStatusPollExecutor() { |
| 413 | + return executorIndex.getAndIncrement() == 0 ? firstExecutor : secondExecutor; |
| 414 | + } |
| 415 | + }; |
| 416 | + service.configure("NsxService", Map.of()); |
| 417 | + try { |
| 418 | + assertTrue(service.start()); |
| 419 | + verify(firstExecutor).scheduleWithFixedDelay(any(Runnable.class), eq(60L), eq(60L), eq(java.util.concurrent.TimeUnit.SECONDS)); |
| 420 | + |
| 421 | + assertTrue(service.stop()); |
| 422 | + verify(firstExecutor).shutdownNow(); |
| 423 | + |
| 424 | + assertTrue(service.start()); |
| 425 | + verify(secondExecutor).scheduleWithFixedDelay(any(Runnable.class), eq(60L), eq(60L), eq(java.util.concurrent.TimeUnit.SECONDS)); |
| 426 | + assertEquals(2, executorIndex.get()); |
| 427 | + } finally { |
| 428 | + service.stop(); |
| 429 | + } |
| 430 | + verify(secondExecutor).shutdownNow(); |
| 431 | + verify(firstExecutor, times(1)).shutdownNow(); |
| 432 | + } |
| 433 | + |
311 | 434 | private Site2SiteVpnConnectionVO mockPollableVpnConnection() { |
312 | 435 | Site2SiteVpnConnectionVO connection = mock(Site2SiteVpnConnectionVO.class); |
313 | 436 | when(connection.getId()).thenReturn(11L); |
314 | 437 | when(connection.getVpnGatewayId()).thenReturn(7L); |
315 | | - when(connection.getState()).thenReturn(Site2SiteVpnConnection.State.Connected); |
316 | | - when(site2SiteVpnConnectionDao.listAll()).thenReturn(List.of(connection)); |
| 438 | + when(site2SiteVpnConnectionDao.listByStates( |
| 439 | + Site2SiteVpnConnection.State.Pending, |
| 440 | + Site2SiteVpnConnection.State.Connecting, |
| 441 | + Site2SiteVpnConnection.State.Connected, |
| 442 | + Site2SiteVpnConnection.State.Disconnected)).thenReturn(List.of(connection)); |
317 | 443 | return connection; |
318 | 444 | } |
319 | 445 |
|
|
0 commit comments