Fix for bug ONOS-959: BgpSessionManagerTest unit test failure
Previously, when checking the winning BGP routes, we expect-and-wait until the size of the table with the winning routes has certain size, and then we verify whether it contains a particular value. This is error-prone and time-sensitive. For example, if we are testing whether a particular route from one BGP peer is replaced with the same route from another BGP peer, the test might fail because the table size doesn't change. Modified the unit test to expect-and-wait until the expected route is received. Also, fixed some of the paragraph tags in some of the Javadocs. Change-Id: Ia96dc7c412e78bbc9279dd935dec6919096adeb3
Showing
1 changed file
with
50 additions
and
14 deletions
... | @@ -299,9 +299,10 @@ public class BgpSessionManagerTest { | ... | @@ -299,9 +299,10 @@ public class BgpSessionManagerTest { |
299 | 299 | ||
300 | /** | 300 | /** |
301 | * Gets BGP RIB-IN routes by waiting until they are received. | 301 | * Gets BGP RIB-IN routes by waiting until they are received. |
302 | - * <p/> | 302 | + * <p> |
303 | * NOTE: We keep checking once every 10ms the number of received routes, | 303 | * NOTE: We keep checking once every 10ms the number of received routes, |
304 | * up to 5 seconds. | 304 | * up to 5 seconds. |
305 | + * </p> | ||
305 | * | 306 | * |
306 | * @param bgpSession the BGP session that is expected to receive the | 307 | * @param bgpSession the BGP session that is expected to receive the |
307 | * routes | 308 | * routes |
... | @@ -328,9 +329,10 @@ public class BgpSessionManagerTest { | ... | @@ -328,9 +329,10 @@ public class BgpSessionManagerTest { |
328 | 329 | ||
329 | /** | 330 | /** |
330 | * Gets BGP merged routes by waiting until they are received. | 331 | * Gets BGP merged routes by waiting until they are received. |
331 | - * <p/> | 332 | + * <p> |
332 | * NOTE: We keep checking once every 10ms the number of received routes, | 333 | * NOTE: We keep checking once every 10ms the number of received routes, |
333 | * up to 5 seconds. | 334 | * up to 5 seconds. |
335 | + * </p> | ||
334 | * | 336 | * |
335 | * @param expectedRoutes the expected number of routes | 337 | * @param expectedRoutes the expected number of routes |
336 | * @return the BGP Session Manager routes as received within the expected | 338 | * @return the BGP Session Manager routes as received within the expected |
... | @@ -354,12 +356,45 @@ public class BgpSessionManagerTest { | ... | @@ -354,12 +356,45 @@ public class BgpSessionManagerTest { |
354 | } | 356 | } |
355 | 357 | ||
356 | /** | 358 | /** |
359 | + * Gets a merged BGP route by waiting until it is received. | ||
360 | + * <p> | ||
361 | + * NOTE: We keep checking once every 10ms whether the route is received, | ||
362 | + * up to 5 seconds. | ||
363 | + * </p> | ||
364 | + * | ||
365 | + * @param expectedRoute the expected route | ||
366 | + * @return the merged BGP route if received within the expected time | ||
367 | + * interval, otherwise null | ||
368 | + */ | ||
369 | + private BgpRouteEntry waitForBgpRoute(BgpRouteEntry expectedRoute) | ||
370 | + throws InterruptedException { | ||
371 | + Collection<BgpRouteEntry> bgpRoutes = | ||
372 | + bgpSessionManager.getBgpRoutes4(); | ||
373 | + | ||
374 | + final int maxChecks = 500; // Max wait of 5 seconds | ||
375 | + for (int i = 0; i < maxChecks; i++) { | ||
376 | + for (BgpRouteEntry bgpRouteEntry : bgpRoutes) { | ||
377 | + if (bgpRouteEntry.equals(expectedRoute) && | ||
378 | + bgpRouteEntry.getBgpSession() == | ||
379 | + expectedRoute.getBgpSession()) { | ||
380 | + return bgpRouteEntry; | ||
381 | + } | ||
382 | + } | ||
383 | + Thread.sleep(10); | ||
384 | + bgpRoutes = bgpSessionManager.getBgpRoutes4(); | ||
385 | + } | ||
386 | + | ||
387 | + return null; | ||
388 | + } | ||
389 | + | ||
390 | + /** | ||
357 | * Tests that the BGP OPEN messages have been exchanged, followed by | 391 | * Tests that the BGP OPEN messages have been exchanged, followed by |
358 | * KEEPALIVE. | 392 | * KEEPALIVE. |
359 | * <p> | 393 | * <p> |
360 | * The BGP Peer opens the sessions and transmits OPEN Message, eventually | 394 | * The BGP Peer opens the sessions and transmits OPEN Message, eventually |
361 | * followed by KEEPALIVE. The tested BGP listener should respond by | 395 | * followed by KEEPALIVE. The tested BGP listener should respond by |
362 | * OPEN Message, followed by KEEPALIVE. | 396 | * OPEN Message, followed by KEEPALIVE. |
397 | + * </p> | ||
363 | * | 398 | * |
364 | * @throws TestUtilsException TestUtils error | 399 | * @throws TestUtilsException TestUtils error |
365 | */ | 400 | */ |
... | @@ -406,6 +441,7 @@ public class BgpSessionManagerTest { | ... | @@ -406,6 +441,7 @@ public class BgpSessionManagerTest { |
406 | * The BGP Peer opens the sessions and transmits OPEN Message, eventually | 441 | * The BGP Peer opens the sessions and transmits OPEN Message, eventually |
407 | * followed by KEEPALIVE. The tested BGP listener should respond by | 442 | * followed by KEEPALIVE. The tested BGP listener should respond by |
408 | * OPEN Message, followed by KEEPALIVE. | 443 | * OPEN Message, followed by KEEPALIVE. |
444 | + * </p> | ||
409 | * | 445 | * |
410 | * @throws TestUtilsException TestUtils error | 446 | * @throws TestUtilsException TestUtils error |
411 | */ | 447 | */ |
... | @@ -523,7 +559,7 @@ public class BgpSessionManagerTest { | ... | @@ -523,7 +559,7 @@ public class BgpSessionManagerTest { |
523 | DEFAULT_LOCAL_PREF); | 559 | DEFAULT_LOCAL_PREF); |
524 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); | 560 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
525 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); | 561 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); |
526 | - assertThat(bgpRoutes, hasBgpRouteEntry(bgpRouteEntry)); | 562 | + assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
527 | // | 563 | // |
528 | bgpRouteEntry = | 564 | bgpRouteEntry = |
529 | new BgpRouteEntry(bgpSession1, | 565 | new BgpRouteEntry(bgpSession1, |
... | @@ -534,7 +570,7 @@ public class BgpSessionManagerTest { | ... | @@ -534,7 +570,7 @@ public class BgpSessionManagerTest { |
534 | DEFAULT_LOCAL_PREF); | 570 | DEFAULT_LOCAL_PREF); |
535 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); | 571 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
536 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); | 572 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); |
537 | - assertThat(bgpRoutes, hasBgpRouteEntry(bgpRouteEntry)); | 573 | + assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
538 | // | 574 | // |
539 | bgpRouteEntry = | 575 | bgpRouteEntry = |
540 | new BgpRouteEntry(bgpSession1, | 576 | new BgpRouteEntry(bgpSession1, |
... | @@ -545,7 +581,7 @@ public class BgpSessionManagerTest { | ... | @@ -545,7 +581,7 @@ public class BgpSessionManagerTest { |
545 | DEFAULT_LOCAL_PREF); | 581 | DEFAULT_LOCAL_PREF); |
546 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); | 582 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
547 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); | 583 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); |
548 | - assertThat(bgpRoutes, hasBgpRouteEntry(bgpRouteEntry)); | 584 | + assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
549 | // | 585 | // |
550 | bgpRouteEntry = | 586 | bgpRouteEntry = |
551 | new BgpRouteEntry(bgpSession1, | 587 | new BgpRouteEntry(bgpSession1, |
... | @@ -556,7 +592,7 @@ public class BgpSessionManagerTest { | ... | @@ -556,7 +592,7 @@ public class BgpSessionManagerTest { |
556 | DEFAULT_LOCAL_PREF); | 592 | DEFAULT_LOCAL_PREF); |
557 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); | 593 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
558 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); | 594 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); |
559 | - assertThat(bgpRoutes, hasBgpRouteEntry(bgpRouteEntry)); | 595 | + assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
560 | // | 596 | // |
561 | bgpRouteEntry = | 597 | bgpRouteEntry = |
562 | new BgpRouteEntry(bgpSession1, | 598 | new BgpRouteEntry(bgpSession1, |
... | @@ -567,7 +603,7 @@ public class BgpSessionManagerTest { | ... | @@ -567,7 +603,7 @@ public class BgpSessionManagerTest { |
567 | DEFAULT_LOCAL_PREF); | 603 | DEFAULT_LOCAL_PREF); |
568 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); | 604 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
569 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); | 605 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); |
570 | - assertThat(bgpRoutes, hasBgpRouteEntry(bgpRouteEntry)); | 606 | + assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
571 | 607 | ||
572 | // | 608 | // |
573 | // Delete some routes | 609 | // Delete some routes |
... | @@ -602,7 +638,7 @@ public class BgpSessionManagerTest { | ... | @@ -602,7 +638,7 @@ public class BgpSessionManagerTest { |
602 | DEFAULT_LOCAL_PREF); | 638 | DEFAULT_LOCAL_PREF); |
603 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); | 639 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
604 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); | 640 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); |
605 | - assertThat(bgpRoutes, hasBgpRouteEntry(bgpRouteEntry)); | 641 | + assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
606 | // | 642 | // |
607 | bgpRouteEntry = | 643 | bgpRouteEntry = |
608 | new BgpRouteEntry(bgpSession1, | 644 | new BgpRouteEntry(bgpSession1, |
... | @@ -613,7 +649,7 @@ public class BgpSessionManagerTest { | ... | @@ -613,7 +649,7 @@ public class BgpSessionManagerTest { |
613 | DEFAULT_LOCAL_PREF); | 649 | DEFAULT_LOCAL_PREF); |
614 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); | 650 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
615 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); | 651 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); |
616 | - assertThat(bgpRoutes, hasBgpRouteEntry(bgpRouteEntry)); | 652 | + assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
617 | // | 653 | // |
618 | bgpRouteEntry = | 654 | bgpRouteEntry = |
619 | new BgpRouteEntry(bgpSession1, | 655 | new BgpRouteEntry(bgpSession1, |
... | @@ -624,7 +660,7 @@ public class BgpSessionManagerTest { | ... | @@ -624,7 +660,7 @@ public class BgpSessionManagerTest { |
624 | DEFAULT_LOCAL_PREF); | 660 | DEFAULT_LOCAL_PREF); |
625 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); | 661 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
626 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); | 662 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); |
627 | - assertThat(bgpRoutes, hasBgpRouteEntry(bgpRouteEntry)); | 663 | + assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
628 | 664 | ||
629 | 665 | ||
630 | // Close the channels and test there are no routes | 666 | // Close the channels and test there are no routes |
... | @@ -703,7 +739,7 @@ public class BgpSessionManagerTest { | ... | @@ -703,7 +739,7 @@ public class BgpSessionManagerTest { |
703 | BETTER_LOCAL_PREF); | 739 | BETTER_LOCAL_PREF); |
704 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); | 740 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
705 | assertThat(bgpRibIn2, hasBgpRouteEntry(bgpRouteEntry)); | 741 | assertThat(bgpRibIn2, hasBgpRouteEntry(bgpRouteEntry)); |
706 | - assertThat(bgpRoutes, hasBgpRouteEntry(bgpRouteEntry)); | 742 | + assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
707 | 743 | ||
708 | // | 744 | // |
709 | // Add a route entry to Peer3 with a shorter AS path | 745 | // Add a route entry to Peer3 with a shorter AS path |
... | @@ -737,7 +773,7 @@ public class BgpSessionManagerTest { | ... | @@ -737,7 +773,7 @@ public class BgpSessionManagerTest { |
737 | BETTER_LOCAL_PREF); | 773 | BETTER_LOCAL_PREF); |
738 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); | 774 | bgpRouteEntry.setMultiExitDisc(DEFAULT_MULTI_EXIT_DISC); |
739 | assertThat(bgpRibIn3, hasBgpRouteEntry(bgpRouteEntry)); | 775 | assertThat(bgpRibIn3, hasBgpRouteEntry(bgpRouteEntry)); |
740 | - assertThat(bgpRoutes, hasBgpRouteEntry(bgpRouteEntry)); | 776 | + assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
741 | 777 | ||
742 | // | 778 | // |
743 | // Cleanup in preparation for next test: delete old route entry from | 779 | // Cleanup in preparation for next test: delete old route entry from |
... | @@ -793,7 +829,7 @@ public class BgpSessionManagerTest { | ... | @@ -793,7 +829,7 @@ public class BgpSessionManagerTest { |
793 | BETTER_LOCAL_PREF); | 829 | BETTER_LOCAL_PREF); |
794 | bgpRouteEntry.setMultiExitDisc(BETTER_MULTI_EXIT_DISC); | 830 | bgpRouteEntry.setMultiExitDisc(BETTER_MULTI_EXIT_DISC); |
795 | assertThat(bgpRibIn2, hasBgpRouteEntry(bgpRouteEntry)); | 831 | assertThat(bgpRibIn2, hasBgpRouteEntry(bgpRouteEntry)); |
796 | - assertThat(bgpRoutes, hasBgpRouteEntry(bgpRouteEntry)); | 832 | + assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
797 | 833 | ||
798 | // | 834 | // |
799 | // Add a route entry to Peer1 with a better (lower) BGP ID | 835 | // Add a route entry to Peer1 with a better (lower) BGP ID |
... | @@ -828,7 +864,7 @@ public class BgpSessionManagerTest { | ... | @@ -828,7 +864,7 @@ public class BgpSessionManagerTest { |
828 | BETTER_LOCAL_PREF); | 864 | BETTER_LOCAL_PREF); |
829 | bgpRouteEntry.setMultiExitDisc(BETTER_MULTI_EXIT_DISC); | 865 | bgpRouteEntry.setMultiExitDisc(BETTER_MULTI_EXIT_DISC); |
830 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); | 866 | assertThat(bgpRibIn1, hasBgpRouteEntry(bgpRouteEntry)); |
831 | - assertThat(bgpRoutes, hasBgpRouteEntry(bgpRouteEntry)); | 867 | + assertThat(waitForBgpRoute(bgpRouteEntry), notNullValue()); |
832 | 868 | ||
833 | 869 | ||
834 | // Close the channels and test there are no routes | 870 | // Close the channels and test there are no routes | ... | ... |
-
Please register or login to post a comment