Skip to content

Commit

Permalink
stop using tcp:0 in most tests
Browse files Browse the repository at this point in the history
  • Loading branch information
warner committed Sep 14, 2015
1 parent c157a33 commit 33276cf
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 191 deletions.
20 changes: 19 additions & 1 deletion foolscap/test/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
from twisted.internet import defer, reactor, task
from foolscap import broker
from foolscap.api import Referenceable, RemoteInterface, \
eventually, fireEventually, flushEventualQueue
eventually, fireEventually, flushEventualQueue, Tub
from foolscap.remoteinterface import getRemoteInterface, RemoteMethodSchema, \
UnconstrainedMethod
from foolscap.schema import Any, SetOf, DictOf, ListOf, TupleOf, \
NumberConstraint, ByteStringConstraint, IntegerConstraint, \
UnicodeConstraint, ChoiceOf
from foolscap.referenceable import TubRef
from foolscap.util import allocate_tcp_port

from twisted.python import failure
from twisted.internet.main import CONNECTION_DONE
Expand Down Expand Up @@ -397,3 +398,20 @@ def done(res):
(which, expected_failure, res))
d.addBoth(done)
return d

class MakeTubsMixin:
def makeTubs(self, numTubs, mangleLocation=None):
self.services = []
self.tub_ports = []
for i in range(numTubs):
t = Tub()
t.startService()
self.services.append(t)
portnum = allocate_tcp_port()
self.tub_ports.append(portnum)
t.listenOn("tcp:%d:interface=127.0.0.1" % portnum)
location = "127.0.0.1:%d" % portnum
if mangleLocation:
location = mangleLocation(portnum)
t.setLocation(location)
return self.services
21 changes: 7 additions & 14 deletions foolscap/test/test_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
from foolscap.eventual import flushEventualQueue
from foolscap.test.common import HelperTarget, TargetMixin, ShouldFailMixin
from foolscap.test.common import RIMyTarget, Target, TargetWithoutInterfaces, \
BrokenTarget
from foolscap.api import RemoteException, Tub, DeadReferenceError
BrokenTarget, MakeTubsMixin
from foolscap.api import RemoteException, DeadReferenceError
from foolscap.call import CopiedFailure
from foolscap.logging import log as flog

Expand Down Expand Up @@ -751,15 +751,12 @@ def test_local_return_violation_default(self):
# TODO: test Tub.setOption("expose-remote-exception-types")
# TODO: A calls B. B calls C. C raises an exception. What does A get?

class TubFailures(ExamineFailuresMixin, ShouldFailMixin, unittest.TestCase):
class TubFailures(ExamineFailuresMixin, ShouldFailMixin, MakeTubsMixin,
unittest.TestCase):
def setUp(self):
self.s = service.MultiService()
self.s.startService()
self.target_tub = Tub()
self.target_tub, self.source_tub = self.makeTubs(2)
self.target_tub.setServiceParent(self.s)
l = self.target_tub.listenOn("tcp:0:interface=127.0.0.1")
self.target_tub.setLocation("127.0.0.1:%d" % l.getPortnum())
self.source_tub = Tub()
self.source_tub.setServiceParent(self.s)

def tearDown(self):
Expand Down Expand Up @@ -799,15 +796,11 @@ def test_raise_default(self):
d.addCallback(self._examine_raise, False)
return d

class ReferenceCounting(ShouldFailMixin, unittest.TestCase):
class ReferenceCounting(ShouldFailMixin, MakeTubsMixin, unittest.TestCase):
def setUp(self):
self.s = service.MultiService()
self.s.startService()
self.target_tub = Tub()
self.target_tub, self.source_tub = self.makeTubs(2)
self.target_tub.setServiceParent(self.s)
l = self.target_tub.listenOn("tcp:0:interface=127.0.0.1")
self.target_tub.setLocation("127.0.0.1:%d" % l.getPortnum())
self.source_tub = Tub()
self.source_tub.setServiceParent(self.s)

def tearDown(self):
Expand Down
18 changes: 10 additions & 8 deletions foolscap/test/test_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from foolscap import pb
from foolscap.api import RemoteInterface, Referenceable, Tub, flushEventualQueue
from foolscap.remoteinterface import RemoteMethodSchema
from foolscap.util import allocate_tcp_port

class RIMyCryptoTarget(RemoteInterface):
# method constraints can be declared directly:
Expand Down Expand Up @@ -59,8 +60,8 @@ class TestPersist(UsefulMixin, unittest.TestCase):
def testPersist(self):
t1 = Target()
s1,s2 = self.services
l1 = s1.listenOn("0")
port = l1.getPortnum()
port = allocate_tcp_port()
s1.listenOn("tcp:%d" % port)
s1.setLocation("127.0.0.1:%d" % port)
public_url = s1.registerReference(t1, "name")
self.failUnless(public_url.startswith("pb:"))
Expand All @@ -74,8 +75,8 @@ def _testPersist_1(self, res, s1, s2, t1, public_url, port):
s3.startService()
self.services.append(s3)
t2 = Target()
l3 = s3.listenOn("0")
newport = l3.getPortnum()
newport = allocate_tcp_port()
s3.listenOn("tcp:%d" % newport)
s3.setLocation("127.0.0.1:%d" % newport)
s3.registerReference(t2, "name")
# now patch the URL to replace the port number
Expand All @@ -95,28 +96,29 @@ class TestListeners(UsefulMixin, unittest.TestCase):

def testListenOn(self):
s1 = self.services[0]
l = s1.listenOn("0")
l = s1.listenOn("tcp:%d" % allocate_tcp_port())
self.failUnless(isinstance(l, pb.Listener))
self.failUnlessEqual(len(s1.getListeners()), 1)
s1.stopListeningOn(l)
self.failUnlessEqual(len(s1.getListeners()), 0)

def testGetPort1(self):
s1,s2,s3 = self.services
s1.listenOn("0")
s1.listenOn("tcp:%d" % allocate_tcp_port())
listeners = s1.getListeners()
self.failUnlessEqual(len(listeners), 1)
portnum = listeners[0].getPortnum()
self.failUnless(portnum) # not 0, not None, must be *something*

def testGetPort2(self):
s1,s2,s3 = self.services
s1.listenOn("0")
s1.listenOn("tcp:%d" % allocate_tcp_port())
listeners = s1.getListeners()
self.failUnlessEqual(len(listeners), 1)
portnum = listeners[0].getPortnum()
self.failUnless(portnum) # not 0, not None, must be *something*
s1.listenOn("0") # listen on a second port too
# listen on a second port too
s1.listenOn("tcp:%d" % allocate_tcp_port())
l2 = s1.getListeners()
self.failUnlessEqual(len(l2), 2)
self.failIfEqual(l2[0].getPortnum(), l2[1].getPortnum())
27 changes: 10 additions & 17 deletions foolscap/test/test_gifts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from twisted.internet import defer, protocol, reactor
from twisted.internet.error import ConnectionRefusedError
from foolscap.api import RemoteInterface, Referenceable, flushEventualQueue, \
BananaError, Tub
BananaError
from foolscap.referenceable import RemoteReference
from foolscap.furl import encode_furl, decode_furl
from foolscap.test.common import HelperTarget, RIHelper, ShouldFailMixin
from foolscap.test.common import HelperTarget, RIHelper, ShouldFailMixin, \
MakeTubsMixin
from foolscap.tokens import NegotiationError

class RIConstrainedHelper(RemoteInterface):
Expand All @@ -23,18 +24,12 @@ def __init__(self, name="unnamed"):
def remote_set(self, obj):
self.obj = obj

class Base(ShouldFailMixin):
class Base(ShouldFailMixin, MakeTubsMixin):

debug = False

def setUp(self):
self.services = [Tub() for i in range(4)]
self.tubA, self.tubB, self.tubC, self.tubD = self.services
for s in self.services:
s.startService()
l = s.listenOn("tcp:0:interface=127.0.0.1")
loc = "127.0.0.1:%d" % l.getPortnum()
s.setLocation(loc)
self.tubA, self.tubB, self.tubC, self.tubD = self.makeTubs(4)

def tearDown(self):
d = defer.DeferredList([s.stopService() for s in self.services])
Expand Down Expand Up @@ -510,14 +505,12 @@ def _introduce(res):
class LongFURL(Base, unittest.TestCase):
# make sure the old 200-byte limit on gift FURLs is gone
def setUp(self):
self.services = [Tub() for i in range(4)]
self.tubA, self.tubB, self.tubC, self.tubD = self.services
for s in self.services:
s.startService()
l = s.listenOn("tcp:0:interface=127.0.0.1")
loc = "127.0.0.1:%d" % l.getPortnum()
def mangleLocation(portnum):
loc = "127.0.0.1:%d" % portnum
loc = ",".join([loc]*15) # 239 bytes of location, 281 of FURL
s.setLocation(loc)
return loc
(self.tubA, self.tubB,
self.tubC, self.tubD) = self.makeTubs(4, mangleLocation)

def testGift(self):
self.createCharacters()
Expand Down
12 changes: 4 additions & 8 deletions foolscap/test/test_keepalive.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
from twisted.internet import reactor, defer
from twisted.python.failure import Failure

from foolscap.api import DeadReferenceError, flushEventualQueue, Tub
from foolscap.api import DeadReferenceError, flushEventualQueue
from foolscap.broker import Broker
from foolscap.test.common import TargetWithoutInterfaces
from foolscap.test.common import TargetWithoutInterfaces, MakeTubsMixin

from twisted.python import log

Expand All @@ -22,15 +22,11 @@ def sendPONG(self, number):
log.msg("PONG: %d" % number)
Broker.sendPONG(self, number)

class Keepalives(unittest.TestCase):
class Keepalives(MakeTubsMixin, unittest.TestCase):
def setUp(self):
s0, s1 = self.services = [Tub(), Tub()]
s0, s1 = self.makeTubs(2)
s0.brokerClass = PingCountingBroker
s1.brokerClass = PingCountingBroker
s0.startService()
s1.startService()
l = s0.listenOn("tcp:0:interface=127.0.0.1")
s0.setLocation("127.0.0.1:%d" % l.getPortnum())
self.target = TargetWithoutInterfaces()
public_url = s0.registerReference(self.target, "target")
self.public_url = public_url
Expand Down
Loading

0 comments on commit 33276cf

Please sign in to comment.