1818# See the License for the specific language governing permissions and
1919# limitations under the License.
2020
21+ from socket import create_connection
2122
2223from neo4j .v1 .bolt import ConnectionPool
2324
@@ -40,65 +41,60 @@ def close(self):
4041 self .socket .close ()
4142
4243
43- def connector (address ):
44- from socket import create_connection
45- return QuickConnection (create_connection (address ))
46-
44+ class ConnectionPoolTestCase (ServerTestCase ):
4745
48- def assert_pool_size (pool , address , expected_active , expected_inactive ):
49- try :
50- connections = pool ._connections [address ]
51- except KeyError :
52- assert 0 == expected_active
53- assert 0 == expected_inactive
54- else :
55- assert len ([c for c in connections if c .in_use ]) == expected_active
56- assert len ([c for c in connections if not c .in_use ]) == expected_inactive
46+ def setUp (self ):
47+ self .pool = ConnectionPool (lambda a : QuickConnection (create_connection (a )))
5748
49+ def tearDown (self ):
50+ self .pool .close ()
5851
59- class ConnectionPoolTestCase (ServerTestCase ):
52+ def assert_pool_size (self , address , expected_active , expected_inactive ):
53+ try :
54+ connections = self .pool .connections [address ]
55+ except KeyError :
56+ assert 0 == expected_active
57+ assert 0 == expected_inactive
58+ else :
59+ assert len ([c for c in connections if c .in_use ]) == expected_active
60+ assert len ([c for c in connections if not c .in_use ]) == expected_inactive
6061
6162 def test_can_acquire (self ):
62- with ConnectionPool (connector ) as pool :
63- address = ("127.0.0.1" , 7687 )
64- connection = pool .acquire (address )
65- assert connection .address == address
66- assert_pool_size (pool , address , 1 , 0 )
63+ address = ("127.0.0.1" , 7687 )
64+ connection = self .pool .acquire (address )
65+ assert connection .address == address
66+ self .assert_pool_size (address , 1 , 0 )
6767
6868 def test_can_acquire_twice (self ):
69- with ConnectionPool (connector ) as pool :
70- address = ("127.0.0.1" , 7687 )
71- connection_1 = pool .acquire (address )
72- connection_2 = pool .acquire (address )
73- assert connection_1 .address == address
74- assert connection_2 .address == address
75- assert connection_1 is not connection_2
76- assert_pool_size (pool , address , 2 , 0 )
69+ address = ("127.0.0.1" , 7687 )
70+ connection_1 = self .pool .acquire (address )
71+ connection_2 = self .pool .acquire (address )
72+ assert connection_1 .address == address
73+ assert connection_2 .address == address
74+ assert connection_1 is not connection_2
75+ self .assert_pool_size (address , 2 , 0 )
7776
7877 def test_can_acquire_two_addresses (self ):
79- with ConnectionPool (connector ) as pool :
80- address_1 = ("127.0.0.1" , 7687 )
81- address_2 = ("127.0.0.1" , 7474 )
82- connection_1 = pool .acquire (address_1 )
83- connection_2 = pool .acquire (address_2 )
84- assert connection_1 .address == address_1
85- assert connection_2 .address == address_2
86- assert_pool_size (pool , address_1 , 1 , 0 )
87- assert_pool_size (pool , address_2 , 1 , 0 )
78+ address_1 = ("127.0.0.1" , 7687 )
79+ address_2 = ("127.0.0.1" , 7474 )
80+ connection_1 = self .pool .acquire (address_1 )
81+ connection_2 = self .pool .acquire (address_2 )
82+ assert connection_1 .address == address_1
83+ assert connection_2 .address == address_2
84+ self .assert_pool_size (address_1 , 1 , 0 )
85+ self .assert_pool_size (address_2 , 1 , 0 )
8886
8987 def test_can_acquire_and_release (self ):
90- with ConnectionPool (connector ) as pool :
91- address = ("127.0.0.1" , 7687 )
92- connection = pool .acquire (address )
93- assert_pool_size (pool , address , 1 , 0 )
94- pool .release (connection )
95- assert_pool_size (pool , address , 0 , 1 )
88+ address = ("127.0.0.1" , 7687 )
89+ connection = self .pool .acquire (address )
90+ self .assert_pool_size (address , 1 , 0 )
91+ self .pool .release (connection )
92+ self .assert_pool_size (address , 0 , 1 )
9693
9794 def test_releasing_twice (self ):
98- with ConnectionPool (connector ) as pool :
99- address = ("127.0.0.1" , 7687 )
100- connection = pool .acquire (address )
101- pool .release (connection )
102- assert_pool_size (pool , address , 0 , 1 )
103- pool .release (connection )
104- assert_pool_size (pool , address , 0 , 1 )
95+ address = ("127.0.0.1" , 7687 )
96+ connection = self .pool .acquire (address )
97+ self .pool .release (connection )
98+ self .assert_pool_size (address , 0 , 1 )
99+ self .pool .release (connection )
100+ self .assert_pool_size (address , 0 , 1 )
0 commit comments