Skip to content

Commit 8dc0764

Browse files
Alejo Sanchezfruch
authored andcommitted
Handle port passed as string to Cluster
If port number is passed as string the driver works but it starts failing later on address search. Check port passed as string is valid number and convert it to int. Signed-off-by: Alejo Sanchez <[email protected]>
1 parent 181be7b commit 8dc0764

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

cassandra/cluster.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,13 @@ def __init__(self,
11441144
11451145
Any of the mutable Cluster attributes may be set as keyword arguments to the constructor.
11461146
"""
1147+
1148+
# Handle port passed as string
1149+
if isinstance(port, str):
1150+
if not port.isdigit():
1151+
raise ValueError("Only numeric values are supported for port (%s)" % port)
1152+
port = int(port)
1153+
11471154
if connection_class is not None:
11481155
self.connection_class = connection_class
11491156

tests/unit/test_cluster.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,16 @@ def test_requests_in_flight_threshold(self):
121121
for n in (0, mn, 128):
122122
self.assertRaises(ValueError, c.set_max_requests_per_connection, d, n)
123123

124+
def test_port_str(self):
125+
"""Check port passed as tring is converted and checked properly"""
126+
cluster = Cluster(contact_points=['127.0.0.1'], port='1111')
127+
for cp in cluster.endpoints_resolved:
128+
if cp.address in ('::1', '127.0.0.1'):
129+
self.assertEqual(cp.port, 1111)
130+
131+
with self.assertRaises(ValueError):
132+
cluster = Cluster(contact_points=['127.0.0.1'], port='string')
133+
124134

125135
class SchedulerTest(unittest.TestCase):
126136
# TODO: this suite could be expanded; for now just adding a test covering a ticket

0 commit comments

Comments
 (0)