From 61a0bcaaf67a98b3daa6479efe7bbcadbea3265f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl>
Date: Mon, 9 May 2022 23:20:02 +0200
Subject: [PATCH] rc3 - fixed tests

---
 satella/__init__.py                    |  2 +-
 satella/coding/resources/cp_manager.py | 17 ++++++++++++-----
 tests/test_coding/test_resources.py    |  1 -
 3 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/satella/__init__.py b/satella/__init__.py
index 41fe7571..1e2c5e41 100644
--- a/satella/__init__.py
+++ b/satella/__init__.py
@@ -1 +1 @@
-__version__ = '2.19.0rc2'
+__version__ = '2.19.0rc3'
diff --git a/satella/coding/resources/cp_manager.py b/satella/coding/resources/cp_manager.py
index 72303fa3..24b75e24 100644
--- a/satella/coding/resources/cp_manager.py
+++ b/satella/coding/resources/cp_manager.py
@@ -34,8 +34,8 @@ class CPManager(Monitor, Closeable, tp.Generic[T], metaclass=abc.ABCMeta):
     """
 
     def __init__(self, max_number: int, max_cycle_no: int):
-        super().__init__()
         Closeable.__init__(self)
+        Monitor.__init__(self)
         if sys.implementation.name != 'cpython':
             warnings.warn(f'This may run bad on {sys.implementation.name}', RuntimeWarning)
         self.connections = queue.Queue(max_number)
@@ -43,29 +43,36 @@ class CPManager(Monitor, Closeable, tp.Generic[T], metaclass=abc.ABCMeta):
         self.max_number = max_number
         self.max_cycle_no = max_cycle_no
         self.id_to_times = {}       # type: tp.Dict[int, int]
+        self.terminating = False
 
-    def close(self) -> bool:
+    def close(self) -> None:
         if super().close():
-            while self.spawned_connections:
+            self.terminating = True
+            while self.spawned_connections > 0:
                 self.teardown_connection(self.connections.get())
                 self.spawned_connections -= 1
 
     def acquire_connection(self) -> T:
         """
         Either acquire a new connection, or just establish it in the background
+
+        :return: a new connection:
+        :raises RuntimeError: CPManager is terminating!
         """
+        if self.terminating:
+            raise RuntimeError('CPManager is terminating')
         try:
             conn = self.connections.get(False)
         except queue.Empty:
             while True:
                 with silence_excs(queue.Empty), Monitor.acquire(self):
-                    if self.spawned_connections == self.max_number:
+                    if self.spawned_connections >= self.max_number:
                         conn = self.connections.get(False, 5)
                         break
                     elif self.spawned_connections < self.max_number:
                         conn = self.create_connection()
-                        self.connections.put(conn)
                         self.spawned_connections += 1
+                        self.connections.put(conn)
                         break
         obj_id = id(conn)
         try:
diff --git a/tests/test_coding/test_resources.py b/tests/test_coding/test_resources.py
index 73008b3f..24182f90 100644
--- a/tests/test_coding/test_resources.py
+++ b/tests/test_coding/test_resources.py
@@ -37,5 +37,4 @@ class TestResources(unittest.TestCase):
 
         while conns:
             cp.release_connection(conns.pop())
-
         del cp
-- 
GitLab