MCPcopy Create free account
hub / github.com/MaterializeInc/materialize / timeout

Function timeout

src/ore/src/test.rs:65–83  ·  view source on GitHub ↗

Runs a function with a timeout. The provided closure is invoked on a thread. If the thread completes normally within the provided `duration`, its result is returned. If the thread panics within the provided `duration`, the panic is propagated to the thread calling `timeout`. Otherwise, a timeout error is returned. Note that if the invoked function does not complete in the timeout, it is not kill

(duration: Duration, f: F)

Source from the content-addressed store, hash-verified

63/// not killed; it is left to wind down normally. Therefore this function is
64/// only appropriate in tests, where the resource leak doesn't matter.
65pub fn timeout<F, T>(duration: Duration, f: F) -> Result<T, anyhow::Error>
66where
67 F: FnOnce() -> Result<T, anyhow::Error> + Send + 'static,
68 T: Send + 'static,
69{
70 // Use the drop of `tx` to indicate that the thread is finished. This
71 // ensures that `tx` is dropped even if `f` panics. No actual value is ever
72 // sent on `tx`.
73 let (tx, rx) = mpsc::channel();
74 let thread = thread::spawn(|| {
75 let _tx = tx;
76 f()
77 });
78 match rx.recv_timeout(duration) {
79 Ok(()) => unreachable!(),
80 Err(RecvTimeoutError::Disconnected) => thread.join().unwrap(),
81 Err(RecvTimeoutError::Timeout) => bail!("thread timed out"),
82 }
83}

Callers 15

retry_async_cancelingMethod · 0.70
reduce_expressionMethod · 0.50
missing_blob_diagnosticsFunction · 0.50
compact_and_applyMethod · 0.50
connectMethod · 0.50
compare_and_appendMethod · 0.50
append_batchesFunction · 0.50
register_crdsFunction · 0.50
connect_with_timeoutMethod · 0.50

Calls 5

channelFunction · 0.85
spawnFunction · 0.85
recv_timeoutMethod · 0.80
unwrapMethod · 0.80
joinMethod · 0.45

Tested by

no test coverage detected