MCPcopy Index your code
hub / github.com/PyMySQL/PyMySQL / test_execution_time_limit

Method test_execution_time_limit

pymysql/tests/test_cursor.py:168–230  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

166 cursor.execute("DROP TABLE IF EXISTS percent_test")
167
168 def test_execution_time_limit(self):
169 # this method is similarly implemented in test_SScursor
170
171 conn = self.test_connection
172 db_type = self.get_mysql_vendor(conn)
173
174 with conn.cursor(pymysql.cursors.Cursor) as cur:
175 # MySQL MAX_EXECUTION_TIME takes ms
176 # MariaDB max_statement_time takes seconds as int/float, introduced in 10.1
177
178 # this will sleep 0.01 seconds per row
179 if db_type == "mysql":
180 sql = (
181 "SELECT /*+ MAX_EXECUTION_TIME(2000) */ data, sleep(0.01) FROM test"
182 )
183 else:
184 sql = "SET STATEMENT max_statement_time=2 FOR SELECT data, sleep(0.01) FROM test"
185
186 cur.execute(sql)
187 # unlike SSCursor, Cursor returns a tuple of tuples here
188 self.assertEqual(
189 cur.fetchall(),
190 (
191 ("row1", 0),
192 ("row2", 0),
193 ("row3", 0),
194 ("row4", 0),
195 ("row5", 0),
196 ),
197 )
198
199 if db_type == "mysql":
200 sql = (
201 "SELECT /*+ MAX_EXECUTION_TIME(2000) */ data, sleep(0.01) FROM test"
202 )
203 else:
204 sql = "SET STATEMENT max_statement_time=2 FOR SELECT data, sleep(0.01) FROM test"
205 cur.execute(sql)
206 self.assertEqual(cur.fetchone(), ("row1", 0))
207
208 # this discards the previous unfinished query
209 cur.execute("SELECT 1")
210 self.assertEqual(cur.fetchone(), (1,))
211
212 if db_type == "mysql":
213 sql = "SELECT /*+ MAX_EXECUTION_TIME(1) */ data, sleep(1) FROM test"
214 else:
215 sql = "SET STATEMENT max_statement_time=0.001 FOR SELECT data, sleep(1) FROM test"
216 with pytest.raises(pymysql.err.OperationalError) as cm:
217 # in a buffered cursor this should reliably raise an
218 # OperationalError
219 cur.execute(sql)
220
221 if db_type == "mysql":
222 # this constant was only introduced in MySQL 5.7, not sure
223 # what was returned before, may have been ER_QUERY_INTERRUPTED
224 self.assertEqual(cm.value.args[0], ER.QUERY_TIMEOUT)
225 else:

Callers

nothing calls this directly

Calls 5

get_mysql_vendorMethod · 0.80
cursorMethod · 0.80
executeMethod · 0.80
fetchallMethod · 0.45
fetchoneMethod · 0.45

Tested by

no test coverage detected