Commit 569fb284 authored by Wez's avatar Wez Committed by Commit Bot

[fuchsia] De-flake NetworkChangeNotifierFuchsiaTests.

Switch the tests that expect a call ot GetRouteTable() to explicitly
wait for that to happen, rather than attempting to flush all pending
events from both Thread and main loop.

Bug: 973242
Change-Id: I0e19bb46aa7bf119fa8d6721e6f2f2a42e93a49b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1820205
Auto-Submit: Wez <wez@chromium.org>
Reviewed-by: default avatarKevin Marshall <kmarshall@chromium.org>
Commit-Queue: Wez <wez@chromium.org>
Cr-Commit-Position: refs/heads/master@{#699910}
parent 2e040b41
...@@ -108,41 +108,33 @@ class FakeNetstack : public fuchsia::netstack::testing::Netstack_TestBase { ...@@ -108,41 +108,33 @@ class FakeNetstack : public fuchsia::netstack::testing::Netstack_TestBase {
~FakeNetstack() override = default; ~FakeNetstack() override = default;
// Adds |interface| to the interface query response list. // Adds |interface| to the interface query response list.
void PushInterface(fuchsia::netstack::NetInterface&& interface) { void PushInterface(fuchsia::netstack::NetInterface interface) {
interfaces_.push_back(std::move(interface)); interfaces_.push_back(std::move(interface));
} }
void PushRouteTableEntry(fuchsia::netstack::RouteTableEntry&& interface) {
route_table_.push_back(std::move(interface));
}
// Sends the accumulated |interfaces_| to the OnInterfacesChanged event. // Sends the accumulated |interfaces_| to the OnInterfacesChanged event.
void NotifyInterfaces() { void NotifyInterfaces() {
did_work_ = true;
binding_.events().OnInterfacesChanged(std::move(interfaces_)); binding_.events().OnInterfacesChanged(std::move(interfaces_));
interfaces_.clear(); interfaces_.clear();
} }
// Sets |*did_work_out| to |true| if any FIDL API was called since the void SetOnGetRouteTableCallback(base::OnceClosure callback) {
// last DidDoWork() call. This is used by the FakeNetstackAsync::Synchronize() on_get_route_table_ = std::move(callback);
// call to determine when to stop pumping the message loops.
void DidDoWork(base::OnceClosure done, bool* did_work_out) {
*did_work_out = std::exchange(did_work_, false);
std::move(done).Run();
} }
private: private:
void GetInterfaces(GetInterfacesCallback callback) override { void GetInterfaces(GetInterfacesCallback callback) override {
did_work_ = true;
callback(std::move(interfaces_)); callback(std::move(interfaces_));
} }
void GetRouteTable(GetRouteTableCallback callback) override { void GetRouteTable(GetRouteTableCallback callback) override {
did_work_ = true;
std::vector<fuchsia::netstack::RouteTableEntry> table(2); std::vector<fuchsia::netstack::RouteTableEntry> table(2);
table[0] = CreateRouteTableEntry(kDefaultNic, true); table[0] = CreateRouteTableEntry(kDefaultNic, true);
table[1] = CreateRouteTableEntry(kSecondaryNic, false); table[1] = CreateRouteTableEntry(kSecondaryNic, false);
callback(std::move(table)); callback(std::move(table));
if (on_get_route_table_)
std::move(on_get_route_table_).Run();
} }
void NotImplemented_(const std::string& name) override { void NotImplemented_(const std::string& name) override {
...@@ -150,12 +142,10 @@ class FakeNetstack : public fuchsia::netstack::testing::Netstack_TestBase { ...@@ -150,12 +142,10 @@ class FakeNetstack : public fuchsia::netstack::testing::Netstack_TestBase {
} }
std::vector<fuchsia::netstack::NetInterface> interfaces_; std::vector<fuchsia::netstack::NetInterface> interfaces_;
std::vector<fuchsia::netstack::RouteTableEntry> route_table_;
fidl::Binding<fuchsia::netstack::Netstack> binding_; fidl::Binding<fuchsia::netstack::Netstack> binding_;
// |true| if any FIDL API was called since the last DidDoWork(). base::OnceClosure on_get_route_table_;
bool did_work_ = false;
DISALLOW_COPY_AND_ASSIGN(FakeNetstack); DISALLOW_COPY_AND_ASSIGN(FakeNetstack);
}; };
...@@ -173,32 +163,23 @@ class FakeNetstackAsync { ...@@ -173,32 +163,23 @@ class FakeNetstackAsync {
~FakeNetstackAsync() = default; ~FakeNetstackAsync() = default;
// Asynchronously update the state of the netstack. // Asynchronously update the state of the netstack.
void PushInterface(fuchsia::netstack::NetInterface&& interface) { void PushInterface(fuchsia::netstack::NetInterface interface) {
netstack_.Post(FROM_HERE, &FakeNetstack::PushInterface, netstack_.Post(FROM_HERE, &FakeNetstack::PushInterface,
std::move(interface)); std::move(interface));
} }
void PushRouteTableEntry(fuchsia::netstack::RouteTableEntry&& route) {
netstack_.Post(FROM_HERE, &FakeNetstack::PushRouteTableEntry,
std::move(route));
}
void NotifyInterfaces() { void NotifyInterfaces() {
netstack_.Post(FROM_HERE, &FakeNetstack::NotifyInterfaces); netstack_.Post(FROM_HERE, &FakeNetstack::NotifyInterfaces);
} }
// Pump the test main and Netstack loops until things stabilize. void SetOnGetRouteTableCallback(base::OnceClosure callback) {
void Synchronize() { netstack_.Post(FROM_HERE, &FakeNetstack::SetOnGetRouteTableCallback,
std::move(callback));
}
// Ensure that any PushInterface() or NotifyInterfaces() have been processed.
void FlushNetstackThread() {
// Ensure that pending Push*() and Notify*() calls were processed. // Ensure that pending Push*() and Notify*() calls were processed.
thread_.FlushForTesting(); thread_.FlushForTesting();
// Spin the Netstack until it stops receiving FIDL calls.
bool did_work = false;
do {
base::RunLoop loop;
did_work = false;
netstack_.Post(FROM_HERE, &FakeNetstack::DidDoWork,
loop.QuitWhenIdleClosure(), &did_work);
loop.Run();
} while (did_work);
} }
private: private:
...@@ -234,7 +215,7 @@ class NetworkChangeNotifierFuchsiaTest : public testing::Test { ...@@ -234,7 +215,7 @@ class NetworkChangeNotifierFuchsiaTest : public testing::Test {
void CreateNotifier(uint32_t required_features = 0) { void CreateNotifier(uint32_t required_features = 0) {
// Ensure that the Netstack internal state is up-to-date before the // Ensure that the Netstack internal state is up-to-date before the
// notifier queries it. // notifier queries it.
netstack_.Synchronize(); netstack_.FlushNetstackThread();
// Use a noop DNS notifier. // Use a noop DNS notifier.
dns_config_notifier_ = std::make_unique<SystemDnsConfigChangeNotifier>( dns_config_notifier_ = std::make_unique<SystemDnsConfigChangeNotifier>(
...@@ -249,7 +230,8 @@ class NetworkChangeNotifierFuchsiaTest : public testing::Test { ...@@ -249,7 +230,8 @@ class NetworkChangeNotifierFuchsiaTest : public testing::Test {
void TearDown() override { void TearDown() override {
// Spin the loops to catch any unintended notifications. // Spin the loops to catch any unintended notifications.
netstack_.Synchronize(); netstack_.FlushNetstackThread();
base::RunLoop().RunUntilIdle();
if (notifier_) { if (notifier_) {
NetworkChangeNotifier::RemoveConnectionTypeObserver(&observer_); NetworkChangeNotifier::RemoveConnectionTypeObserver(&observer_);
...@@ -257,6 +239,15 @@ class NetworkChangeNotifierFuchsiaTest : public testing::Test { ...@@ -257,6 +239,15 @@ class NetworkChangeNotifierFuchsiaTest : public testing::Test {
} }
} }
// Causes FakeNetstack to emit NotifyInterfaces(), and then runs the loop
// until the GetRouteTable() is called.
void NetstackNotifyInterfacesAndWaitForGetRouteTable() {
base::RunLoop loop;
netstack_.SetOnGetRouteTableCallback(loop.QuitClosure());
netstack_.NotifyInterfaces();
loop.Run();
}
protected: protected:
base::test::SingleThreadTaskEnvironment task_environment_{ base::test::SingleThreadTaskEnvironment task_environment_{
base::test::SingleThreadTaskEnvironment::MainThreadType::IO}; base::test::SingleThreadTaskEnvironment::MainThreadType::IO};
...@@ -287,7 +278,6 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, NoChange) { ...@@ -287,7 +278,6 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, NoChange) {
CreateNetInterface(kDefaultNic, fuchsia::netstack::NetInterfaceFlagUp, 0, CreateNetInterface(kDefaultNic, fuchsia::netstack::NetInterfaceFlagUp, 0,
CreateIPv4Address(169, 254, 0, 1), CreateIPv4Address(169, 254, 0, 1),
CreateIPv4Address(255, 255, 255, 0), {})); CreateIPv4Address(255, 255, 255, 0), {}));
netstack_.PushRouteTableEntry(CreateRouteTableEntry(kDefaultNic, true));
CreateNotifier(); CreateNotifier();
EXPECT_EQ(NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, EXPECT_EQ(NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN,
...@@ -297,7 +287,6 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, NoChange) { ...@@ -297,7 +287,6 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, NoChange) {
CreateNetInterface(kDefaultNic, fuchsia::netstack::NetInterfaceFlagUp, 0, CreateNetInterface(kDefaultNic, fuchsia::netstack::NetInterfaceFlagUp, 0,
CreateIPv4Address(169, 254, 0, 1), CreateIPv4Address(169, 254, 0, 1),
CreateIPv4Address(255, 255, 255, 0), {})); CreateIPv4Address(255, 255, 255, 0), {}));
netstack_.PushRouteTableEntry(CreateRouteTableEntry(kDefaultNic, true));
netstack_.NotifyInterfaces(); netstack_.NotifyInterfaces();
} }
...@@ -370,7 +359,8 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, IpChange) { ...@@ -370,7 +359,8 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, IpChange) {
netstack_.PushInterface(CreateNetInterface( netstack_.PushInterface(CreateNetInterface(
kDefaultNic, fuchsia::netstack::NetInterfaceFlagUp, 0, kDefaultNic, fuchsia::netstack::NetInterfaceFlagUp, 0,
CreateIPv4Address(10, 0, 0, 1), CreateIPv4Address(255, 255, 0, 0), {})); CreateIPv4Address(10, 0, 0, 1), CreateIPv4Address(255, 255, 0, 0), {}));
netstack_.NotifyInterfaces();
NetstackNotifyInterfacesAndWaitForGetRouteTable();
} }
TEST_F(NetworkChangeNotifierFuchsiaTest, IpChangeV6) { TEST_F(NetworkChangeNotifierFuchsiaTest, IpChangeV6) {
...@@ -388,7 +378,8 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, IpChangeV6) { ...@@ -388,7 +378,8 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, IpChangeV6) {
CreateNetInterface(kDefaultNic, fuchsia::netstack::NetInterfaceFlagUp, 0, CreateNetInterface(kDefaultNic, fuchsia::netstack::NetInterfaceFlagUp, 0,
CreateIPv6Address({0xfe, 0x80, 0x02}), CreateIPv6Address({0xfe, 0x80, 0x02}),
CreateIPv6Address({0xfe, 0x80}), {})); CreateIPv6Address({0xfe, 0x80}), {}));
netstack_.NotifyInterfaces();
NetstackNotifyInterfacesAndWaitForGetRouteTable();
} }
TEST_F(NetworkChangeNotifierFuchsiaTest, MultiV6IPChanged) { TEST_F(NetworkChangeNotifierFuchsiaTest, MultiV6IPChanged) {
...@@ -410,7 +401,8 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, MultiV6IPChanged) { ...@@ -410,7 +401,8 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, MultiV6IPChanged) {
kDefaultNic, fuchsia::netstack::NetInterfaceFlagUp, 0, kDefaultNic, fuchsia::netstack::NetInterfaceFlagUp, 0,
CreateIPv4Address(10, 0, 0, 1), CreateIPv4Address(255, 255, 0, 0), CreateIPv4Address(10, 0, 0, 1), CreateIPv4Address(255, 255, 0, 0),
std::move(addresses))); std::move(addresses)));
netstack_.NotifyInterfaces();
NetstackNotifyInterfacesAndWaitForGetRouteTable();
} }
TEST_F(NetworkChangeNotifierFuchsiaTest, Ipv6AdditionalIpChange) { TEST_F(NetworkChangeNotifierFuchsiaTest, Ipv6AdditionalIpChange) {
...@@ -431,7 +423,8 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, Ipv6AdditionalIpChange) { ...@@ -431,7 +423,8 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, Ipv6AdditionalIpChange) {
kDefaultNic, fuchsia::netstack::NetInterfaceFlagUp, 0, kDefaultNic, fuchsia::netstack::NetInterfaceFlagUp, 0,
CreateIPv4Address(169, 254, 0, 1), CreateIPv4Address(255, 255, 255, 0), CreateIPv4Address(169, 254, 0, 1), CreateIPv4Address(255, 255, 255, 0),
std::move(addresses))); std::move(addresses)));
netstack_.NotifyInterfaces();
NetstackNotifyInterfacesAndWaitForGetRouteTable();
} }
TEST_F(NetworkChangeNotifierFuchsiaTest, InterfaceDown) { TEST_F(NetworkChangeNotifierFuchsiaTest, InterfaceDown) {
...@@ -451,7 +444,8 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, InterfaceDown) { ...@@ -451,7 +444,8 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, InterfaceDown) {
netstack_.PushInterface( netstack_.PushInterface(
CreateNetInterface(1, 0, 0, CreateIPv4Address(169, 254, 0, 1), CreateNetInterface(1, 0, 0, CreateIPv4Address(169, 254, 0, 1),
CreateIPv4Address(255, 255, 0, 0), {})); CreateIPv4Address(255, 255, 0, 0), {}));
netstack_.NotifyInterfaces();
NetstackNotifyInterfacesAndWaitForGetRouteTable();
} }
TEST_F(NetworkChangeNotifierFuchsiaTest, InterfaceUp) { TEST_F(NetworkChangeNotifierFuchsiaTest, InterfaceUp) {
...@@ -471,7 +465,8 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, InterfaceUp) { ...@@ -471,7 +465,8 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, InterfaceUp) {
CreateNetInterface(kDefaultNic, fuchsia::netstack::NetInterfaceFlagUp, 0, CreateNetInterface(kDefaultNic, fuchsia::netstack::NetInterfaceFlagUp, 0,
CreateIPv4Address(169, 254, 0, 1), CreateIPv4Address(169, 254, 0, 1),
CreateIPv4Address(255, 255, 0, 0), {})); CreateIPv4Address(255, 255, 0, 0), {}));
netstack_.NotifyInterfaces();
NetstackNotifyInterfacesAndWaitForGetRouteTable();
} }
TEST_F(NetworkChangeNotifierFuchsiaTest, InterfaceDeleted) { TEST_F(NetworkChangeNotifierFuchsiaTest, InterfaceDeleted) {
...@@ -488,7 +483,7 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, InterfaceDeleted) { ...@@ -488,7 +483,7 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, InterfaceDeleted) {
notifier_->GetCurrentConnectionType()); notifier_->GetCurrentConnectionType());
// NotifyInterfaces() with no new PushInterfaces() means removing everything. // NotifyInterfaces() with no new PushInterfaces() means removing everything.
netstack_.NotifyInterfaces(); NetstackNotifyInterfacesAndWaitForGetRouteTable();
} }
TEST_F(NetworkChangeNotifierFuchsiaTest, InterfaceAdded) { TEST_F(NetworkChangeNotifierFuchsiaTest, InterfaceAdded) {
...@@ -507,7 +502,8 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, InterfaceAdded) { ...@@ -507,7 +502,8 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, InterfaceAdded) {
fuchsia::hardware::ethernet::INFO_FEATURE_WLAN, fuchsia::hardware::ethernet::INFO_FEATURE_WLAN,
CreateIPv4Address(169, 254, 0, 1), CreateIPv4Address(169, 254, 0, 1),
CreateIPv4Address(255, 255, 255, 0), {})); CreateIPv4Address(255, 255, 255, 0), {}));
netstack_.NotifyInterfaces();
NetstackNotifyInterfacesAndWaitForGetRouteTable();
} }
TEST_F(NetworkChangeNotifierFuchsiaTest, SecondaryInterfaceAddedNoop) { TEST_F(NetworkChangeNotifierFuchsiaTest, SecondaryInterfaceAddedNoop) {
...@@ -525,6 +521,7 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, SecondaryInterfaceAddedNoop) { ...@@ -525,6 +521,7 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, SecondaryInterfaceAddedNoop) {
CreateNetInterface(kDefaultNic, fuchsia::netstack::NetInterfaceFlagUp, 0, CreateNetInterface(kDefaultNic, fuchsia::netstack::NetInterfaceFlagUp, 0,
CreateIPv4Address(169, 254, 0, 1), CreateIPv4Address(169, 254, 0, 1),
CreateIPv4Address(255, 255, 255, 0), {})); CreateIPv4Address(255, 255, 255, 0), {}));
netstack_.NotifyInterfaces(); netstack_.NotifyInterfaces();
} }
...@@ -543,6 +540,7 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, SecondaryInterfaceDeletedNoop) { ...@@ -543,6 +540,7 @@ TEST_F(NetworkChangeNotifierFuchsiaTest, SecondaryInterfaceDeletedNoop) {
CreateNetInterface(kDefaultNic, fuchsia::netstack::NetInterfaceFlagUp, 0, CreateNetInterface(kDefaultNic, fuchsia::netstack::NetInterfaceFlagUp, 0,
CreateIPv4Address(169, 254, 0, 1), CreateIPv4Address(169, 254, 0, 1),
CreateIPv4Address(255, 255, 255, 0), {})); CreateIPv4Address(255, 255, 255, 0), {}));
netstack_.NotifyInterfaces(); netstack_.NotifyInterfaces();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment