From 0e56cb0c9a9deea867fc058b1e2604c452ca3cd0 Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Wed, 11 Sep 2024 18:32:15 -0500 Subject: [PATCH] test: make timeout test faster and shorten query --- test/test_http_timeout.py | 36 ++++++++++++++++-------------------- test/test_worker_error.py | 7 +++++-- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/test/test_http_timeout.py b/test/test_http_timeout.py index 5a6251a..49f7907 100644 --- a/test/test_http_timeout.py +++ b/test/test_http_timeout.py @@ -56,33 +56,29 @@ def test_many_slow_mixed_with_fast(sess): """many fast responses finish despite being mixed with slow responses, the fast responses will wait the timeout duration""" sess.execute(text( - """ - select - net.http_get(url := 'http://localhost:8080/pathological?status=200') - , net.http_get(url := 'http://localhost:8080/pathological?status=200&delay=10') - , net.http_get(url := 'http://localhost:8080/pathological?status=200') - , net.http_get(url := 'http://localhost:8080/pathological?status=200&delay=10') - from generate_series(1,25) _; + """ + select + net.http_get(url := 'http://localhost:8080/pathological?status=200') + , net.http_get(url := 'http://localhost:8080/pathological?status=200&delay=2', timeout_milliseconds := 1000) + , net.http_get(url := 'http://localhost:8080/pathological?status=200') + , net.http_get(url := 'http://localhost:8080/pathological?status=200&delay=2', timeout_milliseconds := 1000) + from generate_series(1,25) _; """ )) sess.commit() - # wait for timeout - time.sleep(6) + # wait for timeouts + time.sleep(3) - (status_code,count) = sess.execute(text( + (request_successes, request_timeouts) = sess.execute(text( """ - select status_code, count(*) from net._http_response where status_code = 200 group by status_code; + select + count(*) filter (where error_msg is null and status_code = 200) as request_successes, + count(*) filter (where error_msg is not null and error_msg like 'Timeout was reached') as request_timeouts + from net._http_response; """ )).fetchone() - assert status_code == 200 - assert count == 50 - - (timed_out_count,) = sess.execute(text( - """ - select count(*) from net._http_response where error_msg ilike '%Timeout%'; - """ - )).fetchone() - assert timed_out_count == 50 + assert request_successes == 50 + assert request_timeouts == 50 diff --git a/test/test_worker_error.py b/test/test_worker_error.py index 64be7f7..5791e1f 100644 --- a/test/test_worker_error.py +++ b/test/test_worker_error.py @@ -11,12 +11,15 @@ def test_success_when_worker_is_up(sess): assert result is not None assert result == '' + def test_http_get_error_when_worker_down(sess): """net.http_get returns an error when pg background worker is down""" - sess.execute(text(""" + (restarted,) = sess.execute(text(""" select net.worker_restart(); - """)) + """)).fetchone() + assert restarted is not None + assert restarted == True time.sleep(0.1)