faucet-core 1.14.0

Shared types, traits, and utilities for the faucet-stream ecosystem
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
//! The messaging family (RFC 0014): produce with confirmations, transactions
//! for exactly-once, consume with acknowledgements.
//!
//! A [`MessageDriver`] encodes one record into a message and publishes a
//! group of messages; [`MessageSinkDriver`] drives everything around that —
//! grouping, the per-group deadline, which rows a timeout or a cancel leaves
//! unconfirmed, per-row outcomes, and (with [`MessageTransactions`]) the
//! exactly-once commit. A [`MessageConsumer`] receives and acknowledges;
//! [`MessageSourceDriver`] acknowledges a page only once the pipeline has
//! written it.

use super::sink::{CommitTokenDriver, PageWrite, RowPolicy, SinkDriver};
use super::source::{PageStream, SourceDriver};
use crate::accounting::WriteReport;
use crate::check::{CheckContext, CheckReport, Probe};
use crate::dlq::BatchAtomicity;
use crate::error::FaucetError;
use crate::io::{CallContext, CallSlot};
use crate::pipeline::StreamPage;
use crate::traits::RowOutcome;
use async_trait::async_trait;
use futures::StreamExt;
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Mutex;
use std::time::Duration;

/// One message to publish.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct OutboundMessage {
    /// Where it goes: a subject, topic, queue or routing key.
    pub destination: String,
    /// The partition / ordering key, when the backend has one.
    pub key: Option<Vec<u8>>,
    /// The body.
    pub payload: Vec<u8>,
    /// Headers, in order.
    pub headers: Vec<(String, String)>,
}

impl OutboundMessage {
    /// A message with a body and no key or headers.
    pub fn new(destination: impl Into<String>, payload: impl Into<Vec<u8>>) -> Self {
        Self {
            destination: destination.into(),
            key: None,
            payload: payload.into(),
            headers: Vec::new(),
        }
    }

    /// The same message with an ordering key.
    pub fn with_key(mut self, key: impl Into<Vec<u8>>) -> Self {
        self.key = Some(key.into());
        self
    }

    /// The same message with one more header.
    pub fn with_header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
        self.headers.push((name.into(), value.into()));
        self
    }
}

/// A producer's primitives (RFC 0014). Wrap it in a [`MessageSinkDriver`],
/// then a [`DriverSink`](super::DriverSink).
#[async_trait]
pub trait MessageDriver: Send + Sync {
    /// The connector's name.
    fn connector_name(&self) -> &'static str;

    /// The JSON Schema of the connector's config.
    fn config_schema(&self) -> Value {
        serde_json::json!({"type": "object", "properties": {}})
    }

    /// The destination, for lineage (credential-free).
    fn dataset_uri(&self) -> String {
        format!("{}://unknown", self.connector_name())
    }

    /// The connector's own call defaults.
    fn call_context(&self) -> CallContext {
        CallContext::new().without_timeout()
    }

    /// A non-mutating preflight probe.
    async fn check(
        &self,
        calls: &CallSlot,
        ctx: &CheckContext,
    ) -> Result<CheckReport, FaucetError> {
        let _ = (calls, ctx);
        Ok(CheckReport::not_implemented())
    }

    /// What a failed write leaves behind.
    fn batch_atomicity(&self) -> BatchAtomicity {
        BatchAtomicity::BestEffort
    }

    /// The message for `record`. An error fails that row alone.
    fn encode(&self, record: &Value) -> Result<OutboundMessage, FaucetError>;

    /// Records per publish group; `0` publishes the whole page as one.
    fn group_size(&self) -> usize {
        0
    }

    /// How long one group may take. On expiry its unconfirmed rows, and
    /// every row after them, fail with a retriable timeout. `None`: only the
    /// run's call deadline applies.
    fn group_timeout(&self) -> Option<Duration> {
        None
    }

    /// Open the connection, once; called before the first group of every
    /// page and before a flush. Default: nothing to open.
    async fn connect(&self, calls: &CallSlot) -> Result<(), FaucetError> {
        let _ = calls;
        Ok(())
    }

    /// Publish a group, returning one outcome per message (`Ok` once the
    /// backend confirmed it). An `Err` is a transport failure of the whole
    /// page.
    async fn publish(
        &self,
        calls: &CallSlot,
        group: Vec<OutboundMessage>,
    ) -> Result<Vec<RowOutcome>, FaucetError>;

    /// Make published messages durable (wait for the client's buffer).
    async fn flush(&self, calls: &CallSlot) -> Result<(), FaucetError> {
        let _ = calls;
        Ok(())
    }

    /// The transaction primitives, for exactly-once.
    fn transactions(&self) -> Option<&dyn MessageTransactions> {
        None
    }

    /// Whether the adapter runs each [`publish`](Self::publish) and
    /// [`flush`](Self::flush) as one backend call (op `publish` / `flush`,
    /// bounded by [`group_timeout`](Self::group_timeout) and the run's call
    /// context). Return `false` for a driver whose `publish` makes its own
    /// calls through the slot — its own op labels, request chunking, retries
    /// and bounds; the adapter then awaits it directly. Default: `true`.
    fn publish_is_one_call(&self) -> bool {
        true
    }

    /// The error a whole-page write (the plain and reported paths) fails
    /// with when rows of `offered` failed, given `(index, error)` for each
    /// in index order. `None` (the default): the first failed row's own
    /// error.
    fn page_error(&self, offered: usize, failed: &[(usize, FaucetError)]) -> Option<FaucetError> {
        let _ = (offered, failed);
        None
    }
}

/// A producer's transaction primitives. With them, [`MessageSinkDriver`]
/// commits a page's messages and its commit token in one transaction.
#[async_trait]
pub trait MessageTransactions: Send + Sync {
    /// Start a transaction for `scope`.
    async fn begin(&self, calls: &CallSlot, scope: &str) -> Result<(), FaucetError>;

    /// Record `token` for `scope` inside the open transaction and commit
    /// everything published since [`begin`](Self::begin).
    async fn commit(&self, calls: &CallSlot, scope: &str, token: &str) -> Result<(), FaucetError>;

    /// Abandon the open transaction; nothing published in it becomes visible.
    async fn abort(&self, calls: &CallSlot, scope: &str);

    /// The last token committed for `scope`.
    async fn last_token(
        &self,
        calls: &CallSlot,
        scope: &str,
    ) -> Result<Option<String>, FaucetError>;
}

/// A [`MessageDriver`] as a [`SinkDriver`].
pub struct MessageSinkDriver<D> {
    driver: D,
}

impl<D: MessageDriver> MessageSinkDriver<D> {
    /// The sink driver over `driver`.
    pub fn new(driver: D) -> Self {
        Self { driver }
    }

    /// The message driver.
    pub fn driver(&self) -> &D {
        &self.driver
    }

    /// The message driver, mutably.
    pub fn driver_mut(&mut self) -> &mut D {
        &mut self.driver
    }

    fn timeout(&self, what: &str, after: Duration) -> FaucetError {
        FaucetError::Timeout {
            operation: format!("{} {what}", self.driver.connector_name()),
            after,
        }
    }

    /// Run `fut` as one backend call named `what`, bounded by the group
    /// timeout. The outer error is the call's own (its timeout, the run's
    /// deadline or cancel); the inner one is the driver's.
    async fn bounded<T: Send>(
        &self,
        calls: &CallSlot,
        what: &'static str,
        fut: impl std::future::Future<Output = Result<T, FaucetError>> + Send,
    ) -> Result<Result<T, FaucetError>, FaucetError> {
        let timeout = self.driver.group_timeout();
        if !self.driver.publish_is_one_call() {
            return match timeout {
                Some(after) => tokio::time::timeout(after, fut)
                    .await
                    .map_err(|_| self.timeout(what, after)),
                None => Ok(fut.await),
            };
        }
        calls
            .call_once(what, async {
                match timeout {
                    Some(after) => tokio::time::timeout(after, fut)
                        .await
                        .map_err(|_| self.timeout(what, after)),
                    None => Ok(fut.await),
                }
            })
            .await
    }

    /// Publish every record in groups; one outcome per record.
    async fn publish_all(
        &self,
        calls: &CallSlot,
        rows: &[Value],
    ) -> Result<Vec<RowOutcome>, FaucetError> {
        let mut outcomes: Vec<RowOutcome> = rows.iter().map(|_| Ok(())).collect();
        if rows.is_empty() {
            return Ok(outcomes);
        }
        self.driver.connect(calls).await?;
        let size = match self.driver.group_size() {
            0 => rows.len(),
            n => n,
        };
        for (g, chunk) in rows.chunks(size).enumerate() {
            let base = g * size;
            let mut positions = Vec::with_capacity(chunk.len());
            let mut group = Vec::with_capacity(chunk.len());
            for (i, record) in chunk.iter().enumerate() {
                match self.driver.encode(record) {
                    Ok(m) => {
                        positions.push(base + i);
                        group.push(m);
                    }
                    Err(e) => outcomes[base + i] = Err(e),
                }
            }
            if group.is_empty() {
                continue;
            }
            let sent = group.len();
            match self
                .bounded(calls, "publish", self.driver.publish(calls, group))
                .await
            {
                Ok(Ok(results)) if results.len() == sent => {
                    for (pos, r) in positions.into_iter().zip(results) {
                        outcomes[pos] = r;
                    }
                }
                Ok(Ok(results)) => {
                    return Err(FaucetError::Sink(format!(
                        "{}: {} outcomes for a group of {sent} messages",
                        self.driver.connector_name(),
                        results.len()
                    )));
                }
                Ok(Err(e)) if g == 0 => return Err(e),
                Ok(Err(e)) => {
                    let name = self.driver.connector_name();
                    fail_open(&mut outcomes[base..], || cut(name, &e));
                    return Ok(outcomes);
                }
                Err(FaucetError::Timeout { after, .. }) => {
                    let after = self.driver.group_timeout().unwrap_or(after);
                    fail_open(&mut outcomes[base..], || self.timeout("publish", after));
                    return Ok(outcomes);
                }
                Err(cause) => {
                    let name = self.driver.connector_name();
                    fail_open(&mut outcomes[base..], || cut(name, &cause));
                    return Ok(outcomes);
                }
            }
        }
        Ok(outcomes)
    }
}

/// The error a row gets when its group's call was cut short by `cause` (a
/// cancel, or the call itself failing).
fn cut(connector: &str, cause: &FaucetError) -> FaucetError {
    match cause {
        FaucetError::Cancelled { operation } => FaucetError::Cancelled {
            operation: operation.clone(),
        },
        other => FaucetError::Sink(format!("{connector} publish: {other}")),
    }
}

/// Fail every row of `outcomes` not already decided.
fn fail_open(outcomes: &mut [RowOutcome], err: impl Fn() -> FaucetError) {
    for o in outcomes.iter_mut().filter(|o| o.is_ok()) {
        *o = Err(err());
    }
}

#[async_trait]
impl<D: MessageDriver> SinkDriver for MessageSinkDriver<D> {
    fn connector_name(&self) -> &'static str {
        self.driver.connector_name()
    }

    fn config_schema(&self) -> Value {
        self.driver.config_schema()
    }

    fn dataset_uri(&self) -> String {
        self.driver.dataset_uri()
    }

    fn call_context(&self) -> CallContext {
        self.driver.call_context()
    }

    async fn check(
        &self,
        calls: &CallSlot,
        ctx: &CheckContext,
    ) -> Result<CheckReport, FaucetError> {
        self.driver.check(calls, ctx).await
    }

    fn batch_atomicity(&self) -> BatchAtomicity {
        self.driver.batch_atomicity()
    }

    async fn write(
        &self,
        calls: &CallSlot,
        rows: &[Value],
        policy: RowPolicy,
    ) -> Result<PageWrite, FaucetError> {
        let write = PageWrite::from_outcomes(self.publish_all(calls, rows).await?);
        if policy == RowPolicy::WholePage
            && !write.failed.is_empty()
            && let Some(e) = self.driver.page_error(rows.len(), &write.failed)
        {
            return Err(e);
        }
        Ok(write)
    }

    async fn flush(&self, calls: &CallSlot) -> Result<(), FaucetError> {
        self.driver.connect(calls).await?;
        self.bounded(calls, "flush", self.driver.flush(calls))
            .await?
    }

    fn commit_tokens(&self) -> Option<&dyn CommitTokenDriver> {
        self.driver
            .transactions()
            .map(|_| self as &dyn CommitTokenDriver)
    }
}

#[async_trait]
impl<D: MessageDriver> CommitTokenDriver for MessageSinkDriver<D> {
    async fn last_token(
        &self,
        calls: &CallSlot,
        scope: &str,
    ) -> Result<Option<String>, FaucetError> {
        match self.driver.transactions() {
            Some(t) => t.last_token(calls, scope).await,
            None => Ok(None),
        }
    }

    async fn write_with_token(
        &self,
        calls: &CallSlot,
        rows: &[Value],
        scope: &str,
        token: &str,
    ) -> Result<WriteReport, FaucetError> {
        let tx = self.driver.transactions().ok_or_else(|| {
            FaucetError::Sink(format!(
                "{}: exactly-once needs a transactional producer",
                self.driver.connector_name()
            ))
        })?;
        tx.begin(calls, scope).await?;
        let outcomes = match self.publish_all(calls, rows).await {
            Ok(o) => o,
            Err(e) => {
                tx.abort(calls, scope).await;
                return Err(e);
            }
        };
        if let Some(e) = outcomes.into_iter().find_map(Result::err) {
            tx.abort(calls, scope).await;
            return Err(e);
        }
        if let Err(e) = tx.commit(calls, scope, token).await {
            tx.abort(calls, scope).await;
            return Err(e);
        }
        Ok(WriteReport::accepted(rows.len()))
    }
}

/// An opaque handle a consumer acknowledges a message by.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct AckToken(pub String);

/// One received message.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct InboundMessage {
    /// Where it came from: a subject, topic or queue.
    pub source: String,
    /// The body.
    pub payload: Vec<u8>,
    /// How to acknowledge it.
    pub ack: AckToken,
}

impl InboundMessage {
    /// A received message.
    pub fn new(source: impl Into<String>, payload: impl Into<Vec<u8>>, ack: AckToken) -> Self {
        Self {
            source: source.into(),
            payload: payload.into(),
            ack,
        }
    }
}

/// A consumer's primitives (RFC 0014). Wrap it in a [`MessageSourceDriver`],
/// then a [`DriverSource`](super::DriverSource).
#[async_trait]
pub trait MessageConsumer: Send + Sync {
    /// The connector's name.
    fn connector_name(&self) -> &'static str;

    /// The JSON Schema of the connector's config.
    fn config_schema(&self) -> Value {
        serde_json::json!({"type": "object", "properties": {}})
    }

    /// The source, for lineage (credential-free).
    fn dataset_uri(&self) -> String {
        format!("{}://unknown", self.connector_name())
    }

    /// The connector's own call defaults.
    fn call_context(&self) -> CallContext {
        CallContext::new()
    }

    /// Receive up to `max` messages, waiting at most the consumer's own idle
    /// wait. An empty result means nothing arrived in that wait.
    async fn receive(
        &self,
        calls: &CallSlot,
        max: usize,
    ) -> Result<Vec<InboundMessage>, FaucetError>;

    /// Acknowledge messages so the broker does not redeliver them.
    async fn ack(&self, calls: &CallSlot, tokens: Vec<AckToken>) -> Result<(), FaucetError>;

    /// The record for a received message.
    fn decode(&self, message: &InboundMessage) -> Result<Value, FaucetError>;

    /// Stop after this many messages; `None`: only idleness stops the read.
    fn max_messages(&self) -> Option<usize> {
        None
    }

    /// Consecutive empty receives that end the read.
    fn idle_receives_to_stop(&self) -> u32 {
        1
    }

    /// A preflight probe that consumes nothing (connect, look up the queue).
    /// `None` (the default): the probe is skipped, because reading a page
    /// would take messages off the queue.
    async fn check(
        &self,
        calls: &CallSlot,
        ctx: &CheckContext,
    ) -> Result<Option<CheckReport>, FaucetError> {
        let _ = (calls, ctx);
        Ok(None)
    }
}

/// A [`MessageConsumer`] as a [`SourceDriver`].
///
/// Each page carries a bookmark, so the pipeline flushes the sink after
/// writing it; the page's messages are acknowledged when the next page is
/// requested (or the stream is polled to its end) — that is, only once the
/// pipeline has written and flushed them. A run that fails before then
/// acknowledges nothing it did not write, and the broker redelivers it.
pub struct MessageSourceDriver<C> {
    consumer: C,
    pending: Mutex<Vec<AckToken>>,
}

impl<C: MessageConsumer> MessageSourceDriver<C> {
    /// The source driver over `consumer`.
    pub fn new(consumer: C) -> Self {
        Self {
            consumer,
            pending: Mutex::new(Vec::new()),
        }
    }

    /// The consumer.
    pub fn consumer(&self) -> &C {
        &self.consumer
    }

    async fn ack_pending(&self, calls: &CallSlot) -> Result<(), FaucetError> {
        let tokens = std::mem::take(&mut *self.pending.lock().unwrap_or_else(|e| e.into_inner()));
        if tokens.is_empty() {
            return Ok(());
        }
        self.consumer.ack(calls, tokens).await
    }
}

#[async_trait]
impl<C: MessageConsumer> SourceDriver for MessageSourceDriver<C> {
    fn connector_name(&self) -> &'static str {
        self.consumer.connector_name()
    }

    fn config_schema(&self) -> Value {
        self.consumer.config_schema()
    }

    fn dataset_uri(&self) -> String {
        self.consumer.dataset_uri()
    }

    fn call_context(&self) -> CallContext {
        self.consumer.call_context()
    }

    fn consumes_destructively(&self) -> bool {
        true
    }

    fn read<'a>(
        &'a self,
        calls: &'a CallSlot,
        _context: &'a HashMap<String, Value>,
        batch_size: usize,
    ) -> PageStream<'a> {
        self.pages(calls, batch_size, true, self.consumer.max_messages())
    }

    async fn fetch(
        &self,
        calls: &CallSlot,
        _context: &HashMap<String, Value>,
    ) -> Option<Result<(Vec<Value>, Option<Value>), FaucetError>> {
        let cap = self
            .consumer
            .max_messages()
            .unwrap_or(crate::pipeline::DEFAULT_BATCH_SIZE);
        let mut pages = self.pages(calls, 0, false, Some(cap));
        let mut rows = Vec::new();
        let mut bookmark = None;
        while let Some(page) = pages.next().await {
            match page {
                Ok(page) => {
                    rows.extend(page.records);
                    bookmark = page.bookmark;
                }
                Err(e) => return Some(Err(e)),
            }
        }
        Some(Ok((rows, bookmark)))
    }

    async fn check(
        &self,
        calls: &CallSlot,
        ctx: &CheckContext,
    ) -> Result<Option<CheckReport>, FaucetError> {
        if let Some(report) = self.consumer.check(calls, ctx).await? {
            return Ok(Some(report));
        }
        Ok(Some(CheckReport::single(Probe::skip(
            "read",
            "reading a page would consume messages; no consumer probe is defined",
        ))))
    }
}

impl<C: MessageConsumer> MessageSourceDriver<C> {
    /// The page stream, ending after `limit` messages. A queue has no end, so
    /// `batch_size` 0 pages by the default size rather than meaning one page.
    /// `ack: false` never acknowledges, so a buffered read leaves every message
    /// for the broker to redeliver; its `limit` keeps a redelivered message
    /// from extending the read forever.
    fn pages<'a>(
        &'a self,
        calls: &'a CallSlot,
        batch_size: usize,
        ack: bool,
        limit: Option<usize>,
    ) -> PageStream<'a> {
        let per_page = if batch_size == 0 {
            crate::pipeline::DEFAULT_BATCH_SIZE
        } else {
            batch_size
        };
        Box::pin(async_stream::try_stream! {
            let mut consumed = 0usize;
            let mut idle = 0u32;
            loop {
                if ack {
                    self.ack_pending(calls).await?;
                }
                let room = match limit {
                    Some(max) if consumed >= max => break,
                    Some(max) => per_page.min(max - consumed),
                    None => per_page,
                };
                let messages = self.consumer.receive(calls, room).await?;
                if messages.is_empty() {
                    idle += 1;
                    if idle >= self.consumer.idle_receives_to_stop() {
                        break;
                    }
                    continue;
                }
                idle = 0;
                let mut records = Vec::with_capacity(messages.len());
                let mut tokens = Vec::with_capacity(messages.len());
                for m in &messages {
                    records.push(self.consumer.decode(m)?);
                    tokens.push(m.ack.clone());
                }
                consumed += records.len();
                if ack {
                    *self.pending.lock().unwrap_or_else(|e| e.into_inner()) = tokens;
                }
                yield StreamPage {
                    records,
                    bookmark: Some(serde_json::json!({ "consumed": consumed })),
                };
            }
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_cut_short_group_keeps_a_cancel_and_words_anything_else() {
        let cancel = FaucetError::Cancelled {
            operation: "publish call".into(),
        };
        assert!(matches!(
            cut("broker", &cancel),
            FaucetError::Cancelled { operation } if operation == "publish call"
        ));
        let other = cut("broker", &FaucetError::Source("x".into()));
        assert!(
            matches!(&other, FaucetError::Sink(m) if m.starts_with("broker publish: ")),
            "{other}"
        );
        let mut outcomes: Vec<RowOutcome> = vec![Err(FaucetError::Sink("own".into())), Ok(())];
        fail_open(&mut outcomes, || cut("broker", &cancel));
        assert!(matches!(&outcomes[0], Err(FaucetError::Sink(m)) if m == "own"));
        assert!(matches!(&outcomes[1], Err(FaucetError::Cancelled { .. })));
    }
}