shill: do not report partial results of IcmpSession in Stop()

Do not call |result_callback_| with partial results of the IcmpSession
if Stop() is called (either by the user or on destruction of the
object). This allows IcmpSession objects to be cleaned up easily
without worrying about handling callback invocations.

BUG=None.
TEST=Compile shill and run unit tests.

Change-Id: I5d797f4001f1e932b1d34318d8ebcb056fa154c1
Reviewed-on: https://chromium-review.googlesource.com/285672
Tested-by: Samuel Tan <samueltan@chromium.org>
Reviewed-by: mukesh agrawal <quiche@chromium.org>
Commit-Queue: Samuel Tan <samueltan@chromium.org>
diff --git a/icmp_session_unittest.cc b/icmp_session_unittest.cc
index cd24cd2..fb8780e 100644
--- a/icmp_session_unittest.cc
+++ b/icmp_session_unittest.cc
@@ -112,6 +112,10 @@
     icmp_session_.TransmitEchoRequestTask(destination);
   }
 
+  void ReportResultAndStopSession() {
+    icmp_session_.ReportResultAndStopSession();
+  }
+
   void VerifyIcmpSessionStopped() {
     EXPECT_TRUE(icmp_session_.timeout_callback_.IsCancelled());
     EXPECT_TRUE(icmp_session_.result_callback_.is_null());
@@ -383,14 +387,25 @@
   EXPECT_EQ(now, GetSeqNumToSentRecvTime()->at(kIcmpEchoReply3_SeqNum).first);
   EXPECT_EQ(kIcmpEchoReply3_SeqNum + 1, GetCurrentSequenceNumber());
 
-  // Timeout triggered or session manually stopped, so we report partial
-  // results.
+  // Timeout triggered, so report partial results.
   EXPECT_CALL(*this, ResultCallback(expected_partial_result));
   EXPECT_CALL(*icmp_, Stop());
-  Stop();
+  ReportResultAndStopSession();
   EXPECT_EQ(2, GetSeqNumToSentRecvTime()->size());
   EXPECT_EQ(1, GetReceivedEchoReplySeqNumbers()->size());
+  VerifyIcmpSessionStopped();
+}
 
+TEST_F(IcmpSessionTest, DoNotReportResultsOnStop) {
+  // Initiate session.
+  IPAddress ipv4_destination(IPAddress::kFamilyIPv4);
+  EXPECT_TRUE(ipv4_destination.SetAddressFromString(kIPAddress));
+  StartAndVerify(ipv4_destination);
+
+  // Session interrupted manually by calling Stop(), so do not report results.
+  EXPECT_CALL(*this, ResultCallback(_)).Times(0);
+  EXPECT_CALL(*icmp_, Stop());
+  Stop();
   VerifyIcmpSessionStopped();
 }