@@ -62,6 +62,9 @@ class CfgIfaceTest : public ::testing::Test {
62
62
// / @param timeout Wait timeout in milliseconds.
63
63
void doWait (const long timeout);
64
64
65
+ // / @brief Interrupt the current wait (if one).
66
+ void stopWait ();
67
+
65
68
// / @brief Holds a fake configuration of the interfaces.
66
69
IfaceMgrTestConfig iface_mgr_test_config_;
67
70
@@ -90,8 +93,8 @@ CfgIfaceTest::TearDown() {
90
93
TimerMgr::instance ()->unregisterTimers ();
91
94
92
95
IfaceMgr::instance ().setTestMode (false );
93
- IfaceMgr::instance ().clearIfaces ();
94
96
IfaceMgr::instance ().closeSockets ();
97
+ IfaceMgr::instance ().clearIfaces ();
95
98
IfaceMgr::instance ().detectIfaces ();
96
99
97
100
// Reset global handlers
@@ -121,11 +124,20 @@ CfgIfaceTest::doWait(const long timeout) {
121
124
timer.setup ([this ]() {
122
125
io_service_->stop ();
123
126
}, timeout, asiolink::IntervalTimer::ONE_SHOT);
127
+
124
128
io_service_->run ();
125
129
io_service_->stop ();
126
130
io_service_->restart ();
127
131
}
128
132
133
+ void
134
+ CfgIfaceTest::stopWait () {
135
+ // Post it so we don't stop in the middle of a callback
136
+ io_service_->post ([this ]() {
137
+ io_service_->stop ();
138
+ });
139
+ }
140
+
129
141
// This test checks that the interface names can be explicitly selected
130
142
// by their names and IPv4 sockets are opened on these interfaces.
131
143
TEST_F (CfgIfaceTest, explicitNamesV4) {
@@ -626,7 +638,7 @@ TEST_F(CfgIfaceTest, retryOpenServiceSockets4) {
626
638
627
639
// Parameters
628
640
const uint16_t RETRIES = 5 ;
629
- const uint16_t WAIT_TIME = 10 ; // miliseconds
641
+ const uint16_t WAIT_TIME = 5 ; // miliseconds
630
642
// The number of sockets opened in a single retry attempt.
631
643
// iface: eth0 addr: 10.0.0.1 port: 67 rbcast: 0 sbcast: 0
632
644
// iface: eth1 addr: 192.0.2.3 port: 67 rbcast: 0 sbcast: 0
@@ -636,10 +648,13 @@ TEST_F(CfgIfaceTest, retryOpenServiceSockets4) {
636
648
cfg4.setServiceSocketsMaxRetries (RETRIES);
637
649
cfg4.setServiceSocketsRetryWaitTime (WAIT_TIME);
638
650
651
+ // For each interface perform 1 init open and a few retries.
652
+ size_t exp_calls = CALLS_PER_RETRY * (RETRIES + 1 );
653
+
639
654
// Set the callback to count calls and check wait time
640
655
size_t total_calls = 0 ;
641
656
auto last_call_time = std::chrono::system_clock::time_point::min ();
642
- auto open_callback = [&total_calls, &last_call_time, WAIT_TIME](uint16_t ) {
657
+ auto open_callback = [this , &total_calls, &last_call_time, WAIT_TIME, exp_calls ](uint16_t ) {
643
658
auto now = std::chrono::system_clock::now ();
644
659
645
660
// Check waiting time only for the first call in a retry attempt.
@@ -660,6 +675,10 @@ TEST_F(CfgIfaceTest, retryOpenServiceSockets4) {
660
675
661
676
total_calls++;
662
677
678
+ if (total_calls == exp_calls) {
679
+ stopWait ();
680
+ }
681
+
663
682
// Fail to open a socket
664
683
isc_throw (Unexpected, " CfgIfaceTest: cannot open a port" );
665
684
};
@@ -677,10 +696,9 @@ TEST_F(CfgIfaceTest, retryOpenServiceSockets4) {
677
696
ASSERT_NO_THROW (cfg4.openSockets (AF_INET, DHCP4_SERVER_PORT));
678
697
679
698
// Wait for a finish sockets binding (with a safe margin).
680
- doWait (RETRIES * WAIT_TIME * 2 );
699
+ doWait (RETRIES * WAIT_TIME * 10 );
681
700
682
- // For each interface perform 1 init open and a few retries.
683
- EXPECT_EQ (CALLS_PER_RETRY * (RETRIES + 1 ), total_calls);
701
+ EXPECT_EQ (exp_calls, total_calls);
684
702
}
685
703
686
704
// This test verifies that if any IPv4 socket fails to bind, the opening will
@@ -693,7 +711,7 @@ TEST_F(CfgIfaceTest, retryOpenServiceSockets4OmitBound) {
693
711
694
712
// Parameters
695
713
const uint16_t RETRIES = 5 ;
696
- const uint16_t WAIT_TIME = 10 ; // miliseconds
714
+ const uint16_t WAIT_TIME = 5 ; // miliseconds
697
715
698
716
// Require retry socket binding
699
717
cfg4.setServiceSocketsMaxRetries (RETRIES);
@@ -702,7 +720,12 @@ TEST_F(CfgIfaceTest, retryOpenServiceSockets4OmitBound) {
702
720
// Set the callback to count calls and check wait time
703
721
size_t total_calls = 0 ;
704
722
auto last_call_time = std::chrono::system_clock::time_point::min ();
705
- auto open_callback = [&total_calls, &last_call_time, WAIT_TIME](uint16_t ) {
723
+
724
+ // For eth0 interface perform 1 init open and a few retries,
725
+ // for eth1 interface perform only init open.
726
+ size_t exp_calls = (RETRIES + 1 ) + 1 ;
727
+
728
+ auto open_callback = [this , &total_calls, &last_call_time, WAIT_TIME, exp_calls](uint16_t ) {
706
729
auto now = std::chrono::system_clock::now ();
707
730
bool is_eth1 = total_calls == 1 ;
708
731
@@ -726,10 +749,15 @@ TEST_F(CfgIfaceTest, retryOpenServiceSockets4OmitBound) {
726
749
727
750
total_calls++;
728
751
752
+ if (total_calls == exp_calls) {
753
+ stopWait ();
754
+ }
755
+
729
756
// Fail to open a socket on eth0, success for eth1
730
757
if (!is_eth1) {
731
758
isc_throw (Unexpected, " CfgIfaceTest: cannot open a port" );
732
759
}
760
+
733
761
};
734
762
735
763
boost::shared_ptr<isc::dhcp::test::PktFilterTestStub> filter (
@@ -745,11 +773,9 @@ TEST_F(CfgIfaceTest, retryOpenServiceSockets4OmitBound) {
745
773
ASSERT_NO_THROW (cfg4.openSockets (AF_INET, DHCP4_SERVER_PORT));
746
774
747
775
// Wait for a finish sockets binding (with a safe margin).
748
- doWait (RETRIES * WAIT_TIME * 2 );
776
+ doWait (RETRIES * WAIT_TIME * 10 );
749
777
750
- // For eth0 interface perform 1 init open and a few retries,
751
- // for eth1 interface perform only init open.
752
- EXPECT_EQ ((RETRIES + 1 ) + 1 , total_calls);
778
+ EXPECT_EQ (exp_calls, total_calls);
753
779
}
754
780
755
781
// Test that only one reopen timer is active simultaneously. If a new opening
@@ -822,7 +848,7 @@ TEST_F(CfgIfaceTest, retryOpenServiceSockets6) {
822
848
823
849
// Parameters
824
850
const uint16_t RETRIES = 5 ;
825
- const uint16_t WAIT_TIME = 10 ; // miliseconds
851
+ const uint16_t WAIT_TIME = 5 ; // miliseconds
826
852
// The number of sockets opened in a single retry attempt.
827
853
// 1 unicast and 2 multicast sockets.
828
854
// iface: eth0 addr: 2001:db8:1::1 port: 547 multicast: 0
@@ -834,10 +860,13 @@ TEST_F(CfgIfaceTest, retryOpenServiceSockets6) {
834
860
cfg6.setServiceSocketsMaxRetries (RETRIES);
835
861
cfg6.setServiceSocketsRetryWaitTime (WAIT_TIME);
836
862
863
+ // For each interface perform 1 init open and a few retries.
864
+ size_t exp_calls = CALLS_PER_RETRY * (RETRIES + 1 );
865
+
837
866
// Set the callback to count calls and check wait time
838
867
size_t total_calls = 0 ;
839
868
auto last_call_time = std::chrono::system_clock::time_point::min ();
840
- auto open_callback = [&total_calls, &last_call_time, WAIT_TIME](uint16_t ) {
869
+ auto open_callback = [this , &total_calls, &last_call_time, WAIT_TIME, exp_calls ](uint16_t ) {
841
870
auto now = std::chrono::system_clock::now ();
842
871
843
872
// Check waiting time only for the first call in a retry attempt.
@@ -858,6 +887,10 @@ TEST_F(CfgIfaceTest, retryOpenServiceSockets6) {
858
887
859
888
total_calls++;
860
889
890
+ if (total_calls == exp_calls) {
891
+ stopWait ();
892
+ }
893
+
861
894
// Fail to open a socket
862
895
isc_throw (Unexpected, " CfgIfaceTest: cannot open a port" );
863
896
};
@@ -873,10 +906,10 @@ TEST_F(CfgIfaceTest, retryOpenServiceSockets6) {
873
906
ASSERT_NO_THROW (cfg6.openSockets (AF_INET6, DHCP6_SERVER_PORT));
874
907
875
908
// Wait for a finish sockets binding (with a safe margin).
876
- doWait (RETRIES * WAIT_TIME * 2 );
909
+ doWait (RETRIES * WAIT_TIME * 10 );
877
910
878
911
// For each interface perform 1 init open and a few retries.
879
- EXPECT_EQ (CALLS_PER_RETRY * (RETRIES + 1 ) , total_calls);
912
+ EXPECT_EQ (exp_calls , total_calls);
880
913
}
881
914
882
915
// This test verifies that if any IPv6 socket fails to bind, the opening will
@@ -889,7 +922,7 @@ TEST_F(CfgIfaceTest, retryOpenServiceSockets6OmitBound) {
889
922
890
923
// Parameters
891
924
const uint16_t RETRIES = 5 ;
892
- const uint16_t WAIT_TIME = 10 ; // miliseconds
925
+ const uint16_t WAIT_TIME = 5 ; // miliseconds
893
926
894
927
// Require retry socket binding
895
928
cfg6.setServiceSocketsMaxRetries (RETRIES);
@@ -901,10 +934,15 @@ TEST_F(CfgIfaceTest, retryOpenServiceSockets6OmitBound) {
901
934
const uint32_t opened_by_eth0 = 2 ;
902
935
#endif
903
936
937
+
938
+ // For eth0 interface perform only 3 (on Linux Systems or 2 otherwise) init open,
939
+ // for eth1 interface perform 1 init open and a few retries.
940
+ size_t exp_calls = RETRIES + 1 + opened_by_eth0;
941
+
904
942
// Set the callback to count calls and check wait time
905
943
size_t total_calls = 0 ;
906
944
auto last_call_time = std::chrono::system_clock::time_point::min ();
907
- auto open_callback = [&total_calls, &last_call_time, WAIT_TIME](uint16_t ) {
945
+ auto open_callback = [this , &total_calls, &last_call_time, WAIT_TIME, exp_calls ](uint16_t ) {
908
946
auto now = std::chrono::system_clock::now ();
909
947
bool is_eth0 = total_calls < opened_by_eth0;
910
948
@@ -931,6 +969,10 @@ TEST_F(CfgIfaceTest, retryOpenServiceSockets6OmitBound) {
931
969
932
970
total_calls++;
933
971
972
+ if (total_calls == exp_calls) {
973
+ stopWait ();
974
+ }
975
+
934
976
// Fail to open a socket on eth1, success for eth0
935
977
if (!is_eth0) {
936
978
isc_throw (Unexpected, " CfgIfaceTest: cannot open a port" );
@@ -950,11 +992,9 @@ TEST_F(CfgIfaceTest, retryOpenServiceSockets6OmitBound) {
950
992
ASSERT_NO_THROW (cfg6.openSockets (AF_INET6, DHCP6_SERVER_PORT));
951
993
952
994
// Wait for a finish sockets binding (with a safe margin).
953
- doWait (RETRIES * WAIT_TIME * 2 );
995
+ doWait (RETRIES * WAIT_TIME * 10 );
954
996
955
- // For eth0 interface perform only 3 (on Linux Systems or 2 otherwise) init open,
956
- // for eth1 interface perform 1 init open and a few retries.
957
- EXPECT_EQ ((RETRIES + 1 ) + opened_by_eth0, total_calls);
997
+ EXPECT_EQ (exp_calls, total_calls);
958
998
}
959
999
960
1000
// Test that only one reopen timer is active simultaneously. If a new opening
0 commit comments