'Clean up all the event sources left behind by either directly by
test methods or indirectly via some distrib API.'

def tearDown(self):

    dl = [defer.Deferred(), defer.Deferred()]
    if ((self.f1 is not None) and (self.f1.proto is not None)):
        self.f1.proto.notifyOnDisconnect((lambda : dl[0].callback(None)))
    else:
        dl[0].callback(None)
    if ((self.sub is not None) and (self.sub.publisher is not None)):
        self.sub.publisher.broker.notifyOnDisconnect((lambda : dl[1].callback(None)))
        self.sub.publisher.broker.transport.loseConnection()
    else:
        dl[1].callback(None)
    if (self.port1 is not None):
        dl.append(self.port1.stopListening())
    if (self.port2 is not None):
        dl.append(self.port2.stopListening())
    return defer.gatherResults(dl)
