Skip to content

Commit f1c1c83

Browse files
committed
Test in use count
1 parent c660463 commit f1c1c83

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

neo4j/bolt/connection.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -419,18 +419,15 @@ def release(self, connection):
419419
connection.in_use = False
420420

421421
def in_use_connection_count(self, address):
422+
""" Count the number of connections currently in use to a given
423+
address.
424+
"""
422425
try:
423426
connections = self.connections[address]
424427
except KeyError:
425428
return 0
426429
else:
427-
in_use_count = 0
428-
429-
for connection in list(connections):
430-
if connection.in_use:
431-
in_use_count += 1
432-
433-
return in_use_count
430+
return sum(1 if connection.in_use else 0 for connection in connections)
434431

435432
def remove(self, address):
436433
""" Remove an address from the connection pool, if present, closing

test/integration/test_connection.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,11 @@ def test_cannot_acquire_after_close(self):
108108
pool.close()
109109
with self.assertRaises(ServiceUnavailable):
110110
_ = pool.acquire_direct("X")
111+
112+
def test_in_use_count(self):
113+
address = ("127.0.0.1", 7687)
114+
self.assertEqual(self.pool.in_use_connection_count(address), 0)
115+
connection = self.pool.acquire_direct(address)
116+
self.assertEqual(self.pool.in_use_connection_count(address), 1)
117+
self.pool.release(connection)
118+
self.assertEqual(self.pool.in_use_connection_count(address), 0)

0 commit comments

Comments
 (0)