Skip to content

Commit

Permalink
Removed Duplicate Method
Browse files Browse the repository at this point in the history
  • Loading branch information
super3 committed Jan 25, 2016
1 parent ecc44c8 commit 5d581c1
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 28 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ ntplib >= 0.3.3
twisted >= 15.4.0
ipaddress >= 1.0.14
requests >= 2.8.1
pyp2p >= 0.7.3
pyp2p >= 0.7.5
jsonschema >= 2.5.1
apigen >= 1.4.1
18 changes: 1 addition & 17 deletions storjnode/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from pycoin.encoding import a2b_hashed_base58, b2a_hashed_base58
from collections import OrderedDict
import storjnode
from pyp2p.lib import get_unused_port


_log = storjnode.log.getLogger(__name__)
Expand Down Expand Up @@ -267,23 +268,6 @@ def ensure_path_exists(path):
raise Exception(msg.format(path)) # pragma: no cover


def get_unused_port(port=None):
"""Checks if port is already in use."""
if port is None:
port = random.randint(1024, 65535)
assert(1024 <= port <= 65535)
while True:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind(('', port)) # Try to open port
except socket.error as e:
if e.errno in (98, 10048): # 98, 10048 means address already bound
return get_unused_port(None)
raise e
s.close()
return port


def validate_positive_integer(i):
if i < 0:
msg = "Value must be greater then 0!"
Expand Down
11 changes: 1 addition & 10 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,7 @@ def test_edge_cases(self):
# XXX assumes ports are not in use
self.assertEqual(storjnode.util.get_unused_port(1024), 1024)
self.assertEqual(storjnode.util.get_unused_port(65535), 65535)

def test_value_out_of_range(self):

def callback():
storjnode.util.get_unused_port(1023) # below lower limit
self.assertRaises(AssertionError, callback)

def callback():
storjnode.util.get_unused_port(65536) # above upper limit
self.assertRaises(AssertionError, callback)
storjnode.util.get_unused_port()

def test_already_used(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Expand Down

0 comments on commit 5d581c1

Please sign in to comment.