Skip to content

Commit 5077e82

Browse files
authored
fix: avoid double-panic on test failure (#929)
1 parent dc8077c commit 5077e82

File tree

1 file changed

+40
-39
lines changed

1 file changed

+40
-39
lines changed

runtime/src/test_utils.rs

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -160,96 +160,98 @@ pub struct Expectations {
160160

161161
impl Expectations {
162162
fn reset(&mut self) {
163+
self.skip_verification_on_drop = true;
163164
*self = Default::default();
164165
}
165166

166167
fn verify(&mut self) {
167-
assert!(!self.expect_validate_caller_any, "expected ValidateCallerAny, not received");
168+
// If we don't reset them, we'll try to re-verify on drop. If something fails, we'll panic
169+
// twice and abort making the tests difficult to debug.
170+
self.skip_verification_on_drop = true;
171+
let this = std::mem::take(self);
172+
173+
assert!(!this.expect_validate_caller_any, "expected ValidateCallerAny, not received");
168174
assert!(
169-
self.expect_validate_caller_addr.is_none(),
175+
this.expect_validate_caller_addr.is_none(),
170176
"expected ValidateCallerAddr {:?}, not received",
171-
self.expect_validate_caller_addr
177+
this.expect_validate_caller_addr
172178
);
173179
assert!(
174-
self.expect_validate_caller_type.is_none(),
180+
this.expect_validate_caller_type.is_none(),
175181
"expected ValidateCallerType {:?}, not received",
176-
self.expect_validate_caller_type
182+
this.expect_validate_caller_type
177183
);
178184
assert!(
179-
self.expect_sends.is_empty(),
185+
this.expect_sends.is_empty(),
180186
"expected all message to be send, unsent messages {:?}",
181-
self.expect_sends
187+
this.expect_sends
182188
);
183189
assert!(
184-
self.expect_create_actor.is_none(),
190+
this.expect_create_actor.is_none(),
185191
"expected actor to be created, uncreated actor: {:?}",
186-
self.expect_create_actor
192+
this.expect_create_actor
187193
);
188194
assert!(
189-
self.expect_delete_actor.is_none(),
195+
this.expect_delete_actor.is_none(),
190196
"expected actor to be deleted: {:?}",
191-
self.expect_delete_actor
197+
this.expect_delete_actor
192198
);
193199
assert!(
194-
self.expect_verify_sigs.is_empty(),
200+
this.expect_verify_sigs.is_empty(),
195201
"expect_verify_sigs: {:?}, not received",
196-
self.expect_verify_sigs
202+
this.expect_verify_sigs
197203
);
198204
assert!(
199-
self.expect_verify_seal.is_none(),
205+
this.expect_verify_seal.is_none(),
200206
"expect_verify_seal {:?}, not received",
201-
self.expect_verify_seal
207+
this.expect_verify_seal
202208
);
203209
assert!(
204-
self.expect_verify_post.is_none(),
210+
this.expect_verify_post.is_none(),
205211
"expect_verify_post {:?}, not received",
206-
self.expect_verify_post
212+
this.expect_verify_post
207213
);
208214
assert!(
209-
self.expect_compute_unsealed_sector_cid.is_empty(),
215+
this.expect_compute_unsealed_sector_cid.is_empty(),
210216
"expect_compute_unsealed_sector_cid: {:?}, not received",
211-
self.expect_compute_unsealed_sector_cid
217+
this.expect_compute_unsealed_sector_cid
212218
);
213219
assert!(
214-
self.expect_verify_consensus_fault.is_none(),
220+
this.expect_verify_consensus_fault.is_none(),
215221
"expect_verify_consensus_fault {:?}, not received",
216-
self.expect_verify_consensus_fault
222+
this.expect_verify_consensus_fault
217223
);
218224
assert!(
219-
self.expect_get_randomness_tickets.is_empty(),
225+
this.expect_get_randomness_tickets.is_empty(),
220226
"expect_get_randomness_tickets {:?}, not received",
221-
self.expect_get_randomness_tickets
227+
this.expect_get_randomness_tickets
222228
);
223229
assert!(
224-
self.expect_get_randomness_beacon.is_empty(),
230+
this.expect_get_randomness_beacon.is_empty(),
225231
"expect_get_randomness_beacon {:?}, not received",
226-
self.expect_get_randomness_beacon
232+
this.expect_get_randomness_beacon
227233
);
228234
assert!(
229-
self.expect_batch_verify_seals.is_none(),
235+
this.expect_batch_verify_seals.is_none(),
230236
"expect_batch_verify_seals {:?}, not received",
231-
self.expect_batch_verify_seals
237+
this.expect_batch_verify_seals
232238
);
233239
assert!(
234-
self.expect_aggregate_verify_seals.is_none(),
240+
this.expect_aggregate_verify_seals.is_none(),
235241
"expect_aggregate_verify_seals {:?}, not received",
236-
self.expect_aggregate_verify_seals
242+
this.expect_aggregate_verify_seals
237243
);
238244
assert!(
239-
self.expect_replica_verify.is_none(),
245+
this.expect_replica_verify.is_none(),
240246
"expect_replica_verify {:?}, not received",
241-
self.expect_replica_verify
247+
this.expect_replica_verify
242248
);
243249
assert!(
244-
self.expect_gas_charge.is_empty(),
250+
this.expect_gas_charge.is_empty(),
245251
"expect_gas_charge {:?}, not received",
246-
self.expect_gas_charge
252+
this.expect_gas_charge
247253
);
248254
}
249-
250-
fn skip_verification_on_drop(&mut self) {
251-
self.skip_verification_on_drop = true;
252-
}
253255
}
254256

255257
impl Default for MockRuntime {
@@ -467,14 +469,13 @@ impl<BS: Blockstore> MockRuntime<BS> {
467469
res
468470
}
469471

470-
/// Verifies that all mock expectations have been met.
472+
/// Verifies that all mock expectations have been met (and resets the expectations).
471473
pub fn verify(&mut self) {
472474
self.expectations.borrow_mut().verify()
473475
}
474476

475477
/// Clears all mock expectations.
476478
pub fn reset(&mut self) {
477-
self.expectations.borrow_mut().skip_verification_on_drop();
478479
self.expectations.borrow_mut().reset();
479480
}
480481

0 commit comments

Comments
 (0)