MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_sync

Method test_sync

test/engine/test_pool.py:971–1026  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

969
970 @testing.requires.timing_intensive
971 def test_sync(self):
972 pool = self._queuepool_fixture(pool_size=3, max_overflow=0)
973
974 evt = Mock()
975
976 @event.listens_for(pool, "first_connect")
977 def slow_first_connect(dbapi_con, rec):
978 time.sleep(1)
979 evt.first_connect()
980
981 @event.listens_for(pool, "connect")
982 def on_connect(dbapi_con, rec):
983 evt.connect()
984
985 def checkout():
986 barrier.wait()
987 for j in range(2):
988 c1 = pool.connect()
989 time.sleep(0.02)
990 c1.close()
991 time.sleep(0.02)
992
993 threads = []
994
995 # what we're trying to do here is have concurrent use of
996 # all three pooled connections at once, and the thing we want
997 # to test is that first_connect() finishes completely before
998 # any of the connections get returned. so first_connect()
999 # sleeps for one second, then pings the mock. the threads should
1000 # not have made it to the "checkout() event for that one second.
1001 barrier = threading.Barrier(5)
1002 for i in range(5):
1003 th = threading.Thread(target=checkout)
1004 th.start()
1005 threads.append(th)
1006 for th in threads:
1007 th.join(join_timeout)
1008
1009 # there is a very unlikely condition observed in CI on windows
1010 # where even though we have five threads above all calling upon the
1011 # pool, we didn't get concurrent use of all three connections, two
1012 # connections were enough. so here we purposely just check out
1013 # all three at once just to get a consistent test result.
1014 make_sure_all_three_are_connected = [pool.connect() for i in range(3)]
1015 for conn in make_sure_all_three_are_connected:
1016 conn.close()
1017
1018 eq_(
1019 evt.mock_calls,
1020 [
1021 call.first_connect(),
1022 call.connect(),
1023 call.connect(),
1024 call.connect(),
1025 ],
1026 )
1027
1028

Callers

nothing calls this directly

Calls 8

eq_Function · 0.90
_queuepool_fixtureMethod · 0.80
startMethod · 0.45
appendMethod · 0.45
joinMethod · 0.45
connectMethod · 0.45
closeMethod · 0.45
first_connectMethod · 0.45

Tested by

no test coverage detected