Changeset 817355d in trunk


Ignore:
Timestamp:
2020-07-27T17:06:41Z (5 years ago)
Author:
Itamar Turner-Trauring <itamar@…>
Branches:
master
Children:
361e758
Parents:
f9bda5b
Message:

Minimal testing for listenOnUnused.

Location:
src/allmydata
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified src/allmydata/test/test_iputil.py

    rf9bda5b r817355d  
    1414    from builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, int, list, object, range, str, max, min  # noqa: F401
    1515
    16 import re, errno, subprocess, os
     16import re, errno, subprocess, os, socket
    1717
    1818from twisted.trial import unittest
     19
     20from foolscap.api import Tub
    1921
    2022from allmydata.util import iputil
     
    184186        self.patch(iputil, 'platform', "cygwin")
    185187        return self._test_list_async_mock(None, None, CYGWIN_TEST_ADDRESSES)
     188
     189
     190class ListenOnUsed(unittest.TestCase):
     191    """Tests for listenOnUnused."""
     192
     193    def create_tub(self, basedir):
     194        os.makedirs(basedir)
     195        tubfile = os.path.join(basedir, "tub.pem")
     196        tub = Tub(certFile=tubfile)
     197        tub.setOption("expose-remote-exception-types", False)
     198        tub.startService()
     199        self.addCleanup(tub.stopService)
     200        return tub
     201
     202    def test_random_port(self):
     203        """A random port is selected if none is given."""
     204        tub = self.create_tub("utils/ListenOnUsed/test_randomport")
     205        self.assertEqual(len(tub.getListeners()), 0)
     206        portnum = iputil.listenOnUnused(tub)
     207        # We can connect to this port:
     208        s = socket.socket()
     209        s.connect(("127.0.0.1", portnum))
     210        s.close()
     211        self.assertEqual(len(tub.getListeners()), 1)
     212
     213        # Listen on another port:
     214        tub2 = self.create_tub("utils/ListenOnUsed/test_randomport_2")
     215        portnum2 = iputil.listenOnUnused(tub2)
     216        self.assertNotEqual(portnum, portnum2)
  • TabularUnified src/allmydata/util/iputil.py

    rf9bda5b r817355d  
    380380           "get_local_addresses_async",
    381381           "get_local_ip_for",
     382           "listenOnUnused",
    382383           ]
Note: See TracChangeset for help on using the changeset viewer.