Fix issue with request flags leak between tests

Reset request flags of RTNLHandler on Stop().  Make Stop() private, but
call it in teardown of all unit tests that call Start().

BUG=chromium-os:18833
TEST=Rerun unit tests

Change-Id: I83d32abc561e80c9dc72239825907cb5d5155bca
Reviewed-on: http://gerrit.chromium.org/gerrit/5461
Reviewed-by: Darin Petkov <petkov@chromium.org>
Tested-by: Paul Stewart <pstew@chromium.org>
diff --git a/device_info_unittest.cc b/device_info_unittest.cc
index 5f3fa76..e3dfd47 100644
--- a/device_info_unittest.cc
+++ b/device_info_unittest.cc
@@ -54,6 +54,10 @@
 
   void AddLink();
 
+  virtual void TearDown() {
+    RTNLHandler::GetInstance()->Stop();
+  }
+
  protected:
   static const int kTestSocket;
   static const int kTestDeviceIndex;
@@ -139,12 +143,9 @@
 }
 
 TEST_F(DeviceInfoTest, DeviceEnumerationReverse) {
-  // Start our own private device_info _after_ RTNLHandler has been started
-  // TODO(pstew): Find out why this EXPECT_CALL needed to be moved above
-  //              StartRTNLHandler()
+  StartRTNLHandler();
   EXPECT_CALL(sockets_, SendTo(kTestSocket, _, _, 0, _, sizeof(sockaddr_nl)))
       .WillOnce(Return(0));
-  StartRTNLHandler();
   device_info_.Start();
 
   AddLink();
diff --git a/routing_table_unittest.cc b/routing_table_unittest.cc
index 333b81c..39e7d1b 100644
--- a/routing_table_unittest.cc
+++ b/routing_table_unittest.cc
@@ -45,6 +45,10 @@
                     uint32 interface_index,
                     const RoutingTableEntry &entry);
 
+  virtual void TearDown() {
+    RTNLHandler::GetInstance()->Stop();
+  }
+
  protected:
   static const int kTestSocket;
   static const uint32 kTestDeviceIndex0;
diff --git a/rtnl_handler.cc b/rtnl_handler.cc
index cd4d9f8..8fd4167 100644
--- a/rtnl_handler.cc
+++ b/rtnl_handler.cc
@@ -92,6 +92,7 @@
   sockets_->Close(rtnl_socket_);
   in_request_ = false;
   sockets_ = NULL;
+  request_flags_ = 0;
   VLOG(2) << "RTNLHandler stopped";
 }
 
diff --git a/rtnl_handler.h b/rtnl_handler.h
index 4a5f118..ead1981 100644
--- a/rtnl_handler.h
+++ b/rtnl_handler.h
@@ -54,9 +54,6 @@
   // event loop.
   void Start(EventDispatcher *dispatcher, Sockets *sockets);
 
-  // This stops the event-monitoring function of the RTNL handler
-  void Stop();
-
   // Add an RTNL event listener to the list of entities that will
   // be notified of RTNL events.
   void AddListener(RTNLListener *to_add);
@@ -104,6 +101,11 @@
   RTNLHandler();
   virtual ~RTNLHandler();
 
+  // This stops the event-monitoring function of the RTNL handler -- it is
+  // private since it will never happen in normal running, but is useful for
+  // tests.
+  void Stop();
+
   // Dispatches an rtnl message to all listeners
   void DispatchEvent(int type, struct nlmsghdr *hdr);
   // Send the next table-dump request to the kernel