tests: UnicastUdpTransport test suite improvements

Fixed m_hasBeenUsedRecently uninitialized in DatagramTransport

refs #3307

Change-Id: I786a84009d9cf01e0bb211c872d4375f46c747fd
diff --git a/tests/daemon/face/stream-transport.t.cpp b/tests/daemon/face/stream-transport.t.cpp
index 24b8b43..89470de 100644
--- a/tests/daemon/face/stream-transport.t.cpp
+++ b/tests/daemon/face/stream-transport.t.cpp
@@ -26,6 +26,8 @@
 #include "tcp-transport-fixture.hpp"
 #include "unix-stream-transport-fixture.hpp"
 
+#include <boost/mpl/vector.hpp>
+
 namespace nfd {
 namespace face {
 namespace tests {
@@ -122,27 +124,67 @@
 {
   this->initialize();
 
-  std::vector<uint8_t> bytes(ndn::MAX_NDN_PACKET_SIZE + 1, 0);
-  Block pkt = ndn::encoding::makeBinaryBlock(300, bytes.data(), bytes.size());
-  ndn::Buffer buf(pkt.begin(), pkt.end());
+  std::vector<uint8_t> bytes(ndn::MAX_NDN_PACKET_SIZE, 0);
+  Block pkt1 = ndn::encoding::makeBinaryBlock(300, bytes.data(), bytes.size() - 6);
+  ndn::Buffer buf1(pkt1.begin(), pkt1.end());
+  BOOST_REQUIRE_EQUAL(buf1.size(), ndn::MAX_NDN_PACKET_SIZE);
 
-  this->remoteWrite(buf, false);
+  Block pkt2 = ndn::encoding::makeBinaryBlock(301, bytes.data(), bytes.size());
+  ndn::Buffer buf2(pkt2.begin(), pkt2.end());
+  BOOST_REQUIRE_GT(buf2.size(), ndn::MAX_NDN_PACKET_SIZE);
 
-  BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 0);
-  BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, 0);
-  BOOST_CHECK_EQUAL(this->receivedPackets->size(), 0);
-  BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::CLOSED);
+  this->remoteWrite(buf1); // this should succeed
+
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf1.size());
+  BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
+  BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::UP);
+
+  int nStateChanges = 0;
+  this->transport->afterStateChange.connect(
+    [this, &nStateChanges] (TransportState oldState, TransportState newState) {
+      switch (nStateChanges) {
+      case 0:
+        BOOST_CHECK_EQUAL(oldState, TransportState::UP);
+        BOOST_CHECK_EQUAL(newState, TransportState::FAILED);
+        break;
+      case 1:
+        BOOST_CHECK_EQUAL(oldState, TransportState::FAILED);
+        BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
+        break;
+      default:
+        BOOST_CHECK(false);
+      }
+      nStateChanges++;
+    });
+
+  this->remoteWrite(buf2, false); // this should fail
+
+  BOOST_CHECK_EQUAL(nStateChanges, 2);
+
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nInPackets, 1);
+  BOOST_CHECK_EQUAL(this->transport->getCounters().nInBytes, buf1.size());
+  BOOST_CHECK_EQUAL(this->receivedPackets->size(), 1);
 }
 
 BOOST_FIXTURE_TEST_CASE_TEMPLATE(Close, T, StreamTransportFixtures, T)
 {
   this->initialize();
 
-  this->transport->close();
-  BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::CLOSING);
+  this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
+    BOOST_CHECK_EQUAL(oldState, TransportState::UP);
+    BOOST_CHECK_EQUAL(newState, TransportState::CLOSING);
+  });
 
-  this->g_io.poll();
-  BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::CLOSED);
+  this->transport->close();
+
+  this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
+    BOOST_CHECK_EQUAL(oldState, TransportState::CLOSING);
+    BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
+    this->limitedIo.afterOp();
+  });
+
+  BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
 }
 
 BOOST_FIXTURE_TEST_CASE_TEMPLATE(RemoteClose, T, StreamTransportFixtures, T)
@@ -158,8 +200,13 @@
   this->remoteSocket.close();
   BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
 
-  this->g_io.poll();
-  BOOST_CHECK_EQUAL(this->transport->getState(), TransportState::CLOSED);
+  this->transport->afterStateChange.connectSingleShot([this] (TransportState oldState, TransportState newState) {
+    BOOST_CHECK_EQUAL(oldState, TransportState::FAILED);
+    BOOST_CHECK_EQUAL(newState, TransportState::CLOSED);
+    this->limitedIo.afterOp();
+  });
+
+  BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, time::seconds(1)), LimitedIo::EXCEED_OPS);
 }
 
 BOOST_AUTO_TEST_SUITE_END() // TestStreamTransport
@@ -167,4 +214,4 @@
 
 } // namespace tests
 } // namespace face
-} // namespace nfd
\ No newline at end of file
+} // namespace nfd