Skip to content

Commit a78be08

Browse files
committed
test: remove unnecessary sleeps causing flakiness
Instead we just wait for the last response in several places.
1 parent 25a0ab8 commit a78be08

File tree

3 files changed

+74
-24
lines changed

3 files changed

+74
-24
lines changed

test/test_http_errors.py

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,24 @@ def test_bad_utils(sess):
7777
def test_it_keeps_working_after_many_connection_refused(sess):
7878
"""the worker doesn't crash on many failed responses with connection refused"""
7979

80-
res = sess.execute(text(
80+
(request_id,) = sess.execute(text(
8181
f"""
82-
select net.http_get('http://localhost:{wrong_port}') from generate_series(1,10);
82+
select net.http_get('http://localhost:{wrong_port}') from generate_series(1,10) offset 9;
8383
"""
84-
))
84+
)).fetchone()
8585
sess.commit()
8686

87-
time.sleep(2)
87+
# Collect the last response, waiting as needed
88+
response = sess.execute(
89+
text(
90+
"""
91+
select * from net._http_collect_response(:request_id, async:=false);
92+
"""
93+
),
94+
{"request_id": request_id},
95+
).fetchone()
96+
assert response is not None
97+
assert response[0] == "ERROR"
8898

8999
(error_msg,count) = sess.execute(text(
90100
"""
@@ -121,14 +131,24 @@ def test_it_keeps_working_after_many_connection_refused(sess):
121131
def test_it_keeps_working_after_server_returns_nothing(sess):
122132
"""the worker doesn't crash on many failed responses with server returned nothing"""
123133

124-
sess.execute(text(
134+
(request_id,) = sess.execute(text(
125135
"""
126-
select net.http_get('http://localhost:8080/pathological?disconnect=true') from generate_series(1,10);
136+
select net.http_get('http://localhost:8080/pathological?disconnect=true') from generate_series(1,10) offset 9;
127137
"""
128-
))
138+
)).fetchone()
129139
sess.commit()
130140

131-
time.sleep(1.5)
141+
# Collect the last response, waiting as needed
142+
response = sess.execute(
143+
text(
144+
"""
145+
select * from net._http_collect_response(:request_id, async:=false);
146+
"""
147+
),
148+
{"request_id": request_id},
149+
).fetchone()
150+
assert response is not None
151+
assert response[0] == "ERROR"
132152

133153
(error_msg,count) = sess.execute(text(
134154
"""
@@ -139,15 +159,25 @@ def test_it_keeps_working_after_server_returns_nothing(sess):
139159
assert error_msg == "Server returned nothing (no headers, no data)"
140160
assert count == 10
141161

142-
sess.execute(text(
162+
(request_id,) = sess.execute(text(
143163
"""
144-
select net.http_get('http://localhost:8080/pathological?status=200') from generate_series(1,10);
164+
select net.http_get('http://localhost:8080/pathological?status=200') from generate_series(1,10) offset 9;
145165
"""
146166
)).fetchone()
147167

148168
sess.commit()
149169

150-
time.sleep(1.5)
170+
# Collect the last response, waiting as needed
171+
response = sess.execute(
172+
text(
173+
"""
174+
select * from net._http_collect_response(:request_id, async:=false);
175+
"""
176+
),
177+
{"request_id": request_id},
178+
).fetchone()
179+
assert response is not None
180+
assert response[0] == "SUCCESS"
151181

152182
(status_code,count) = sess.execute(text(
153183
"""

test/test_http_requests_deleted_after_ttl.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
def test_http_responses_deleted_after_ttl(sess, autocommit_sess):
77
"""Check that http responses will be deleted when they reach their ttl, not immediately but when the worker wakes again"""
88

9-
autocommit_sess.execute(text("alter system set pg_net.ttl to '4 seconds'"))
9+
autocommit_sess.execute(text("alter system set pg_net.ttl to '1 second'"))
1010
autocommit_sess.execute(text("select net.worker_restart()"))
1111
autocommit_sess.execute(text("select net.wait_until_running()"))
1212

@@ -34,13 +34,15 @@ def test_http_responses_deleted_after_ttl(sess, autocommit_sess):
3434
assert response[0] == "SUCCESS"
3535

3636
# Sleep until after request should have been deleted
37-
time.sleep(5)
37+
time.sleep(1.1)
3838

3939
# Wake the worker manually, under normal operation this will happen when new requests are received
4040
sess.execute(text("select net.wake()"))
4141

4242
sess.commit() # commit so worker wakes
4343

44+
time.sleep(0.1) # wait for deletion
45+
4446
# Ensure the response is now empty
4547
(count,) = sess.execute(
4648
text(
@@ -60,16 +62,25 @@ def test_http_responses_deleted_after_ttl(sess, autocommit_sess):
6062
def test_http_responses_will_complete_deletion(sess, autocommit_sess):
6163
"""Check that http responses will keep being deleted until completion despite no new requests coming"""
6264

63-
sess.execute(text(
65+
(request_id,) = sess.execute(text(
6466
"""
65-
select net.http_get('http://localhost:8080/pathological?status=200') from generate_series(1,4);
67+
select net.http_get('http://localhost:8080/pathological?status=200') from generate_series(1,4) offset 3;
6668
"""
67-
))
69+
)).fetchone()
6870

6971
sess.commit()
7072

71-
# wait for reqs
72-
time.sleep(0.1)
73+
# Collect the last response, waiting as needed
74+
response = sess.execute(
75+
text(
76+
"""
77+
select * from net._http_collect_response(:request_id, async:=false);
78+
"""
79+
),
80+
{"request_id": request_id},
81+
).fetchone()
82+
assert response is not None
83+
assert response[0] == "SUCCESS"
7384

7485
(count,) = sess.execute(
7586
text(
@@ -122,16 +133,25 @@ def test_http_responses_will_complete_deletion(sess, autocommit_sess):
122133
def test_http_responses_will_delete_despite_restart(sess, autocommit_sess):
123134
"""Check that http responses will keep being despite no new requests coming" and despite restart"""
124135

125-
sess.execute(text(
136+
(request_id,) = sess.execute(text(
126137
"""
127-
select net.http_get('http://localhost:8080/pathological?status=200') from generate_series(1,4);
138+
select net.http_get('http://localhost:8080/pathological?status=200') from generate_series(1,4) offset 3;
128139
"""
129-
))
140+
)).fetchone()
130141

131142
sess.commit()
132143

133-
# wait for reqs
134-
time.sleep(0.1)
144+
# Collect the last response, waiting as needed
145+
response = sess.execute(
146+
text(
147+
"""
148+
select * from net._http_collect_response(:request_id, async:=false);
149+
"""
150+
),
151+
{"request_id": request_id},
152+
).fetchone()
153+
assert response is not None
154+
assert response[0] == "SUCCESS"
135155

136156
(count,) = sess.execute(
137157
text(

test/test_stat_statements.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ def test_wakes_at_commit_time(sess):
106106
# wait for reqs
107107
time.sleep(2)
108108

109-
110109
(commit_calls,) = sess.execute(text(
111110
"""
112111
select coalesce(sum(calls), 0)
@@ -118,6 +117,7 @@ def test_wakes_at_commit_time(sess):
118117
)).fetchone()
119118

120119
assert commit_calls == initial_calls + 4 # only 4 queries should be made for the above requests
120+
# 2 queries at wake, 2 extra to check if there are more rows to be processed
121121

122122
# if the new requests are rollbacked/aborted, then no new queries will be made by the bg worker
123123
sess.execute(text(

0 commit comments

Comments
 (0)