Skip to content

Commit ac69fa6

Browse files
committed
tests/util/chaosproxy: Return Result from break/restore_networking() fns instead of panicking
1 parent b3013c1 commit ac69fa6

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

src/tests/server_binary.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn startup_without_database() {
4444
// Break the networking *before* starting the binary, to ensure the binary can fully startup
4545
// without a database connection. Most of crates.io should not work when started without a
4646
// database, but unconditional redirects will work.
47-
server_bin.chaosproxy.break_networking();
47+
server_bin.chaosproxy.break_networking().unwrap();
4848

4949
let running_server = server_bin.start().unwrap();
5050

src/tests/unhealthy_database.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ fn download_crate_with_broken_networking_primary_database() {
2525
// do an unconditional redirect to the CDN, without checking whether the crate exists or what
2626
// the exact capitalization of crate name is.
2727

28-
app.primary_db_chaosproxy().break_networking();
28+
app.primary_db_chaosproxy().break_networking().unwrap();
2929
assert_unconditional_redirects(&anon);
3030

3131
// After restoring the network and waiting for the database pool to get healthy again redirects
3232
// should be checked again.
3333

34-
app.primary_db_chaosproxy().restore_networking();
34+
app.primary_db_chaosproxy().restore_networking().unwrap();
3535
app.as_inner()
3636
.primary_database
3737
.wait_until_healthy(DB_HEALTHY_TIMEOUT)
@@ -75,12 +75,12 @@ fn http_error_with_unhealthy_database() {
7575
let response = anon.get::<()>("/api/v1/summary");
7676
assert_eq!(response.status(), StatusCode::OK);
7777

78-
app.primary_db_chaosproxy().break_networking();
78+
app.primary_db_chaosproxy().break_networking().unwrap();
7979

8080
let response = anon.get::<()>("/api/v1/summary");
8181
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
8282

83-
app.primary_db_chaosproxy().restore_networking();
83+
app.primary_db_chaosproxy().restore_networking().unwrap();
8484
app.as_inner()
8585
.primary_database
8686
.wait_until_healthy(DB_HEALTHY_TIMEOUT)
@@ -99,14 +99,14 @@ fn fallback_to_replica_returns_user_info() {
9999
.with_chaos_proxy()
100100
.with_user();
101101
app.db_new_user("foo");
102-
app.primary_db_chaosproxy().break_networking();
102+
app.primary_db_chaosproxy().break_networking().unwrap();
103103

104104
// When the primary database is down, requests are forwarded to the replica database
105105
let response = owner.get::<()>(URL);
106106
assert_eq!(response.status(), 200);
107107

108108
// restore primary database connection
109-
app.primary_db_chaosproxy().restore_networking();
109+
app.primary_db_chaosproxy().restore_networking().unwrap();
110110
app.as_inner()
111111
.primary_database
112112
.wait_until_healthy(DB_HEALTHY_TIMEOUT)
@@ -122,15 +122,15 @@ fn restored_replica_returns_user_info() {
122122
.with_chaos_proxy()
123123
.with_user();
124124
app.db_new_user("foo");
125-
app.primary_db_chaosproxy().break_networking();
126-
app.replica_db_chaosproxy().break_networking();
125+
app.primary_db_chaosproxy().break_networking().unwrap();
126+
app.replica_db_chaosproxy().break_networking().unwrap();
127127

128128
// When both primary and replica database are down, the request returns an error
129129
let response = owner.get::<()>(URL);
130130
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
131131

132132
// Once the replica database is restored, it should serve as a fallback again
133-
app.replica_db_chaosproxy().restore_networking();
133+
app.replica_db_chaosproxy().restore_networking().unwrap();
134134
app.as_inner()
135135
.read_only_replica_database
136136
.as_ref()
@@ -142,7 +142,7 @@ fn restored_replica_returns_user_info() {
142142
assert_eq!(response.status(), StatusCode::OK);
143143

144144
// restore connection
145-
app.primary_db_chaosproxy().restore_networking();
145+
app.primary_db_chaosproxy().restore_networking().unwrap();
146146
app.as_inner()
147147
.primary_database
148148
.wait_until_healthy(DB_HEALTHY_TIMEOUT)
@@ -158,15 +158,15 @@ fn restored_primary_returns_user_info() {
158158
.with_chaos_proxy()
159159
.with_user();
160160
app.db_new_user("foo");
161-
app.primary_db_chaosproxy().break_networking();
162-
app.replica_db_chaosproxy().break_networking();
161+
app.primary_db_chaosproxy().break_networking().unwrap();
162+
app.replica_db_chaosproxy().break_networking().unwrap();
163163

164164
// When both primary and replica database are down, the request returns an error
165165
let response = owner.get::<()>(URL);
166166
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
167167

168168
// Once the replica database is restored, it should serve as a fallback again
169-
app.primary_db_chaosproxy().restore_networking();
169+
app.primary_db_chaosproxy().restore_networking().unwrap();
170170
app.as_inner()
171171
.primary_database
172172
.wait_until_healthy(DB_HEALTHY_TIMEOUT)

src/tests/util/chaosproxy.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ impl ChaosProxy {
6565
Ok((instance, db_url.into()))
6666
}
6767

68-
pub(crate) fn break_networking(&self) {
68+
pub(crate) fn break_networking(&self) -> anyhow::Result<usize> {
6969
self.break_networking_send
7070
.send(())
71-
.expect("failed to send the break_networking message");
71+
.context("Failed to send the break_networking message")
7272
}
7373

74-
pub(crate) fn restore_networking(&self) {
74+
pub(crate) fn restore_networking(&self) -> anyhow::Result<usize> {
7575
self.restore_networking_send
7676
.send(())
77-
.expect("failed to send the restore_networking message");
77+
.context("Failed to send the restore_networking message")
7878
}
7979

8080
async fn server_loop(&self, initial_listener: TcpListener) -> Result<(), Error> {

0 commit comments

Comments
 (0)