File tree Expand file tree Collapse file tree 2 files changed +12
-7
lines changed Expand file tree Collapse file tree 2 files changed +12
-7
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments