From 0cbf2cca0896639c3fcadd376875124e930dbca4 Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@allmydata.com>
Date: Wed, 19 Sep 2007 11:50:13 -0700
Subject: [PATCH] introducer.py: add test coverage of _disconnected()

---
 src/allmydata/introducer.py           |  1 +
 src/allmydata/test/test_introducer.py | 41 ++++++++++++++++++++-------
 2 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/src/allmydata/introducer.py b/src/allmydata/introducer.py
index caf72969..2afe893a 100644
--- a/src/allmydata/introducer.py
+++ b/src/allmydata/introducer.py
@@ -117,6 +117,7 @@ class IntroducerClient(service.Service, Referenceable):
         introducer.notifyOnDisconnect(self._disconnected)
 
     def _disconnected(self):
+        self.log("bummer, we've lost our connection to the introducer")
         self._connected = False
 
     def notify_on_new_connection(self, cb):
diff --git a/src/allmydata/test/test_introducer.py b/src/allmydata/test/test_introducer.py
index b63b79ca..11a9a038 100644
--- a/src/allmydata/test/test_introducer.py
+++ b/src/allmydata/test/test_introducer.py
@@ -15,7 +15,7 @@ class MyNode(Referenceable):
 
 class LoggingMultiService(service.MultiService):
     def log(self, msg):
-        pass
+        log.msg(msg)
 
 class TestIntroducer(unittest.TestCase, testutil.PollMixin):
     def setUp(self):
@@ -83,10 +83,13 @@ class TestIntroducer(unittest.TestCase, testutil.PollMixin):
 
         # d will fire once everybody is connected
 
-        def _check(res):
-            log.msg("doing _check")
+        def _check1(res):
+            log.msg("doing _check1")
             for c in clients:
                 self.failUnlessEqual(len(c.connections), NUMCLIENTS)
+                self.failUnless(c._connected) # to the introducer
+        d.addCallback(_check1)
+        def _disconnect_somebody_else(res):
             # now disconnect somebody's connection to someone else
             self.waiting_for_connections = 2
             d2 = self._done_counting = defer.Deferred()
@@ -100,11 +103,13 @@ class TestIntroducer(unittest.TestCase, testutil.PollMixin):
             victim.tracker.broker.transport.loseConnection()
             log.msg(" did disconnect")
             return d2
-        d.addCallback(_check)
-        def _check_again(res):
-            log.msg("doing _check_again")
+        d.addCallback(_disconnect_somebody_else)
+        def _check2(res):
+            log.msg("doing _check2")
             for c in clients:
                 self.failUnlessEqual(len(c.connections), NUMCLIENTS)
+        d.addCallback(_check2)
+        def _disconnect_yourself(res):
             # now disconnect somebody's connection to themselves. This will
             # only result in one new connection, since it is a loopback.
             self.waiting_for_connections = 1
@@ -119,13 +124,27 @@ class TestIntroducer(unittest.TestCase, testutil.PollMixin):
             victim.tracker.broker.transport.loseConnection()
             log.msg(" did disconnect")
             return d2
-        d.addCallback(_check_again)
-        def _check_again2(res):
-            log.msg("doing _check_again2")
+        d.addCallback(_disconnect_yourself)
+        def _check3(res):
+            log.msg("doing _check3")
+            for c in clients:
+                self.failUnlessEqual(len(c.connections), NUMCLIENTS)
+        d.addCallback(_check3)
+        def _shutdown_introducer(res):
+            # now shut down the introducer. We do this by shutting down the
+            # tub it's using. Nobody's connections (to each other) should go
+            # down. All clients should notice the loss, and no other errors
+            # should occur.
+            log.msg("shutting down the introducer")
+            return self.central_tub.disownServiceParent()
+        d.addCallback(_shutdown_introducer)
+        d.addCallback(self.stall, 2)
+        def _check4(res):
+            log.msg("doing _check4")
             for c in clients:
                 self.failUnlessEqual(len(c.connections), NUMCLIENTS)
-            # now disconnect somebody's connection to themselves
-        d.addCallback(_check_again2)
+                self.failIf(c._connected)
+        d.addCallback(_check4)
         return d
     test_system.timeout = 2400
 
-- 
2.45.2