MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / test_timeout_race

Method test_timeout_race

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

Source from the content-addressed store, hash-verified

1131 @testing.requires.threading_with_mock
1132 @testing.requires.timing_intensive
1133 def test_timeout_race(self):
1134 # test a race condition where the initial connecting threads all race
1135 # to queue.Empty, then block on the mutex. each thread consumes a
1136 # connection as they go in. when the limit is reached, the remaining
1137 # threads go in, and get TimeoutError; even though they never got to
1138 # wait for the timeout on queue.get(). the fix involves checking the
1139 # timeout again within the mutex, and if so, unlocking and throwing
1140 # them back to the start of do_get()
1141 dbapi = MockDBAPI()
1142 p = pool.QueuePool(
1143 creator=lambda: dbapi.connect(delay=0.05),
1144 pool_size=2,
1145 max_overflow=1,
1146 timeout=3,
1147 )
1148 timeouts = []
1149
1150 def checkout():
1151 barrier.wait()
1152 for x in range(1):
1153 now = time.time()
1154 try:
1155 c1 = p.connect()
1156 except tsa.exc.TimeoutError:
1157 timeouts.append(time.time() - now)
1158 continue
1159 time.sleep(4)
1160 c1.close()
1161
1162 barrier = threading.Barrier(10)
1163 threads = []
1164 for i in range(10):
1165 th = threading.Thread(target=checkout)
1166 th.start()
1167 threads.append(th)
1168 for th in threads:
1169 th.join(join_timeout)
1170
1171 assert len(timeouts) > 0
1172 for t in timeouts:
1173 assert t >= 3, "Not all timeouts were >= 3 seconds %r" % timeouts
1174 # normally, the timeout should under 4 seconds,
1175 # but on a loaded down buildbot it can go up.
1176 assert t < 14, "Not all timeouts were < 14 seconds %r" % timeouts
1177
1178 def _test_overflow(self, thread_count, max_overflow):
1179 reaper = testing.engines.ConnectionKiller()

Callers

nothing calls this directly

Calls 5

MockDBAPIFunction · 0.70
connectMethod · 0.45
startMethod · 0.45
appendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected