faucet-source-redis 1.2.1

Redis source connector 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
//! Integration tests for the convenience `RedisSource::fetch_all` and the
//! `Source::fetch_with_context` paths against a real Redis instance via
//! testcontainers. These exercise the batch (non-streaming) read APIs for all
//! three source modes — list `LRANGE`, stream `XREAD` / `XREADGROUP`, and
//! key-pattern `SCAN` + `MGET` — plus `max_records` truncation, empty/missing
//! results, and the `${ctx}` key substitution path.
//!
//! These tests require Docker. Each test boots its own container and seeds its
//! own keyspace so they are fully isolated and safe to run in parallel.

use faucet_core::Source;
use faucet_source_redis::{RedisSource, RedisSourceConfig, RedisSourceType};
use redis::AsyncCommands;
use std::collections::HashMap;
use testcontainers::{ContainerAsync, runners::AsyncRunner};
use testcontainers_modules::redis::{REDIS_PORT, Redis};

/// Start a Redis container and return both the container handle and a
/// connection URL. The container is kept alive by the returned handle.
async fn start_redis() -> (ContainerAsync<Redis>, String) {
    let container: ContainerAsync<Redis> = Redis::default()
        .start()
        .await
        .expect("redis container start");
    let host = container.get_host().await.expect("redis host");
    let port = container
        .get_host_port_ipv4(REDIS_PORT)
        .await
        .expect("redis port");
    let url = format!("redis://{host}:{port}");
    // Drive a PING through the same retry path used by the source so the
    // container is fully reachable from the host before any test code runs.
    let _ = open_conn(&url).await;
    (container, url)
}

/// Open a multiplexed async connection for seeding the test container. Retries
/// briefly on the initial connect — the "Ready to accept connections" log line
/// testcontainers waits on can race with the port binding on some Docker hosts.
async fn open_conn(url: &str) -> redis::aio::MultiplexedConnection {
    let client = redis::Client::open(url).expect("redis client open");
    let mut last_err: Option<redis::RedisError> = None;
    for _ in 0..30 {
        match client.get_multiplexed_async_connection().await {
            Ok(conn) => return conn,
            Err(e) => {
                last_err = Some(e);
                tokio::time::sleep(std::time::Duration::from_millis(200)).await;
            }
        }
    }
    panic!("redis connect: {:?}", last_err);
}

// ── List mode (fetch_all) ─────────────────────────────────────────────────

#[tokio::test(flavor = "multi_thread")]
async fn fetch_all_list_returns_all_elements_in_order() {
    let (_container, url) = start_redis().await;
    let mut conn = open_conn(&url).await;
    // RPUSH preserves insertion order; LRANGE 0 -1 returns head→tail.
    let _: i64 = conn.rpush("items", "alpha").await.unwrap();
    let _: i64 = conn.rpush("items", "beta").await.unwrap();
    let _: i64 = conn.rpush("items", "gamma").await.unwrap();

    let source = RedisSource::new(RedisSourceConfig::new(
        &url,
        RedisSourceType::List {
            key: "items".into(),
        },
    ))
    .unwrap();

    let records = source.fetch_all().await.expect("fetch_all ok");
    assert_eq!(
        records,
        vec!["alpha", "beta", "gamma"],
        "non-JSON list elements come back as bare JSON strings, in list order"
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn fetch_all_list_parses_json_elements_and_falls_back_to_string() {
    let (_container, url) = start_redis().await;
    let mut conn = open_conn(&url).await;
    // First element is valid JSON (parsed into an object), second is not
    // (kept as a bare string) — exercises both arms of the parse/fallback.
    let _: i64 = conn.rpush("mixed", "{\"n\":7}").await.unwrap();
    let _: i64 = conn.rpush("mixed", "not-json").await.unwrap();

    let source = RedisSource::new(RedisSourceConfig::new(
        &url,
        RedisSourceType::List {
            key: "mixed".into(),
        },
    ))
    .unwrap();

    let records = source.fetch_all().await.expect("fetch_all ok");
    assert_eq!(records.len(), 2);
    assert_eq!(records[0]["n"], 7, "valid JSON element is parsed");
    assert_eq!(
        records[1], "not-json",
        "invalid JSON falls back to a string"
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn fetch_all_list_respects_max_records() {
    let (_container, url) = start_redis().await;
    let mut conn = open_conn(&url).await;
    for i in 0..10 {
        let _: i64 = conn.rpush("capped", format!("e-{i}")).await.unwrap();
    }

    let source = RedisSource::new(
        RedisSourceConfig::new(
            &url,
            RedisSourceType::List {
                key: "capped".into(),
            },
        )
        .max_records(3),
    )
    .unwrap();

    let records = source.fetch_all().await.expect("fetch_all ok");
    assert_eq!(
        records,
        vec!["e-0", "e-1", "e-2"],
        "truncated to max_records"
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn fetch_all_list_missing_key_returns_empty() {
    let (_container, url) = start_redis().await;
    // Don't seed — LRANGE on a missing key returns an empty list, not an error.
    let source = RedisSource::new(RedisSourceConfig::new(
        &url,
        RedisSourceType::List {
            key: "no-such-list".into(),
        },
    ))
    .unwrap();

    let records = source.fetch_all().await.expect("fetch_all ok");
    assert!(records.is_empty());
}

// ── Stream mode (fetch_all, no consumer group → XREAD) ────────────────────

#[tokio::test(flavor = "multi_thread")]
async fn fetch_all_stream_no_group_reads_whole_stream() {
    let (_container, url) = start_redis().await;
    let mut conn = open_conn(&url).await;
    let _: String = conn.xadd("evt", "*", &[("name", "first")]).await.unwrap();
    let _: String = conn.xadd("evt", "*", &[("name", "second")]).await.unwrap();

    let source = RedisSource::new(RedisSourceConfig::new(
        &url,
        RedisSourceType::Stream {
            key: "evt".into(),
            group: None,
            consumer: None,
            count: None,
        },
    ))
    .unwrap();

    let records = source.fetch_all().await.expect("fetch_all ok");
    assert_eq!(records.len(), 2);
    assert_eq!(records[0]["fields"]["name"], "first");
    assert_eq!(records[1]["fields"]["name"], "second");
    let id0 = records[0]["id"].as_str().expect("id string");
    assert!(id0.contains('-'), "stream id must be 'ms-seq', got {id0}");
}

#[tokio::test(flavor = "multi_thread")]
async fn fetch_all_stream_no_group_with_count_caps_read() {
    let (_container, url) = start_redis().await;
    let mut conn = open_conn(&url).await;
    for i in 0..5 {
        let _: String = conn
            .xadd("capped-stream", "*", &[("i", i.to_string())])
            .await
            .unwrap();
    }

    // An explicit `count` is the caller's own XREAD cap (no consumer group).
    let source = RedisSource::new(RedisSourceConfig::new(
        &url,
        RedisSourceType::Stream {
            key: "capped-stream".into(),
            group: None,
            consumer: None,
            count: Some(2),
        },
    ))
    .unwrap();

    let records = source.fetch_all().await.expect("fetch_all ok");
    assert_eq!(records.len(), 2, "XREAD COUNT caps the single read");
}

#[tokio::test(flavor = "multi_thread")]
async fn fetch_all_stream_no_group_empty_stream_returns_empty() {
    let (_container, url) = start_redis().await;
    let source = RedisSource::new(RedisSourceConfig::new(
        &url,
        RedisSourceType::Stream {
            key: "absent-stream".into(),
            group: None,
            consumer: None,
            count: None,
        },
    ))
    .unwrap();

    let records = source.fetch_all().await.expect("fetch_all ok");
    assert!(records.is_empty());
}

// ── Stream mode (fetch_all, consumer group → XREADGROUP) ──────────────────

#[tokio::test(flavor = "multi_thread")]
async fn fetch_all_stream_group_respects_max_records() {
    // The XREADGROUP drain loop should stop once `max_records` is reached even
    // when more entries are pending in the group.
    let (_container, url) = start_redis().await;
    let mut conn = open_conn(&url).await;
    let _: () = redis::cmd("XGROUP")
        .arg("CREATE")
        .arg("mstream")
        .arg("grp")
        .arg("0")
        .arg("MKSTREAM")
        .query_async(&mut conn)
        .await
        .expect("XGROUP CREATE");
    for i in 0..250 {
        let _: String = conn
            .xadd("mstream", "*", &[("i", i.to_string())])
            .await
            .unwrap();
    }

    let source = RedisSource::new(
        RedisSourceConfig::new(
            &url,
            RedisSourceType::Stream {
                key: "mstream".into(),
                group: Some("grp".into()),
                consumer: Some("worker".into()),
                count: Some(100),
            },
        )
        .max_records(120),
    )
    .unwrap();

    let records = source.fetch_all().await.expect("fetch_all ok");
    assert_eq!(
        records.len(),
        120,
        "consumer-group drain must stop at max_records"
    );
}

// ── Keys mode (fetch_all → SCAN + MGET) ───────────────────────────────────

#[tokio::test(flavor = "multi_thread")]
async fn fetch_all_keys_returns_key_value_pairs() {
    let (_container, url) = start_redis().await;
    let mut conn = open_conn(&url).await;
    let _: () = conn.set("user:alice", "{\"age\":30}").await.unwrap();
    let _: () = conn.set("user:bob", "{\"age\":25}").await.unwrap();
    // A key outside the pattern must be excluded by SCAN MATCH.
    let _: () = conn.set("other:zed", "{\"age\":99}").await.unwrap();

    let source = RedisSource::new(RedisSourceConfig::new(
        &url,
        RedisSourceType::Keys {
            pattern: "user:*".into(),
        },
    ))
    .unwrap();

    let mut records = source.fetch_all().await.expect("fetch_all ok");
    assert_eq!(records.len(), 2, "SCAN MATCH must exclude 'other:zed'");
    records.sort_by(|a, b| a["key"].as_str().cmp(&b["key"].as_str()));
    assert_eq!(records[0]["key"], "user:alice");
    assert_eq!(records[0]["value"]["age"], 30);
    assert_eq!(records[1]["key"], "user:bob");
    assert_eq!(records[1]["value"]["age"], 25);
}

#[tokio::test(flavor = "multi_thread")]
async fn fetch_all_keys_non_json_value_falls_back_to_string() {
    let (_container, url) = start_redis().await;
    let mut conn = open_conn(&url).await;
    let _: () = conn.set("plain:k", "hello-world").await.unwrap();

    let source = RedisSource::new(RedisSourceConfig::new(
        &url,
        RedisSourceType::Keys {
            pattern: "plain:*".into(),
        },
    ))
    .unwrap();

    let records = source.fetch_all().await.expect("fetch_all ok");
    assert_eq!(records.len(), 1);
    assert_eq!(records[0]["key"], "plain:k");
    assert_eq!(
        records[0]["value"], "hello-world",
        "non-JSON value is preserved as a bare string"
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn fetch_all_keys_no_match_returns_empty() {
    let (_container, url) = start_redis().await;
    // Empty keyspace — SCAN matches nothing, the early `keys.is_empty()` branch
    // returns before any MGET round-trip.
    let source = RedisSource::new(RedisSourceConfig::new(
        &url,
        RedisSourceType::Keys {
            pattern: "ghost:*".into(),
        },
    ))
    .unwrap();

    let records = source.fetch_all().await.expect("fetch_all ok");
    assert!(records.is_empty());
}

// ── fetch_with_context: empty ctx delegates, non-empty ctx substitutes ────

#[tokio::test(flavor = "multi_thread")]
async fn fetch_with_context_empty_delegates_to_fetch_all() {
    let (_container, url) = start_redis().await;
    let mut conn = open_conn(&url).await;
    let _: i64 = conn.rpush("plain-list", "x").await.unwrap();
    let _: i64 = conn.rpush("plain-list", "y").await.unwrap();

    let source = RedisSource::new(RedisSourceConfig::new(
        &url,
        RedisSourceType::List {
            key: "plain-list".into(),
        },
    ))
    .unwrap();

    let ctx: HashMap<String, serde_json::Value> = HashMap::new();
    let records = source.fetch_with_context(&ctx).await.expect("ok");
    assert_eq!(records, vec!["x", "y"]);
}

#[tokio::test(flavor = "multi_thread")]
async fn fetch_with_context_substitutes_list_key() {
    let (_container, url) = start_redis().await;
    let mut conn = open_conn(&url).await;
    let _: i64 = conn.rpush("list:42", "only").await.unwrap();

    let source = RedisSource::new(RedisSourceConfig::new(
        &url,
        RedisSourceType::List {
            key: "list:{tenant}".into(),
        },
    ))
    .unwrap();

    let mut ctx: HashMap<String, serde_json::Value> = HashMap::new();
    ctx.insert("tenant".into(), serde_json::json!("42"));
    let records = source.fetch_with_context(&ctx).await.expect("ok");
    assert_eq!(
        records,
        vec!["only"],
        "key '{{tenant}}' resolved to 'list:42'"
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn fetch_with_context_substitutes_stream_key() {
    let (_container, url) = start_redis().await;
    let mut conn = open_conn(&url).await;
    let _: String = conn
        .xadd("stream:eu", "*", &[("region", "eu")])
        .await
        .unwrap();

    let source = RedisSource::new(RedisSourceConfig::new(
        &url,
        RedisSourceType::Stream {
            key: "stream:{region}".into(),
            group: None,
            consumer: None,
            count: None,
        },
    ))
    .unwrap();

    let mut ctx: HashMap<String, serde_json::Value> = HashMap::new();
    ctx.insert("region".into(), serde_json::json!("eu"));
    let records = source.fetch_with_context(&ctx).await.expect("ok");
    assert_eq!(records.len(), 1);
    assert_eq!(records[0]["fields"]["region"], "eu");
}

#[tokio::test(flavor = "multi_thread")]
async fn fetch_with_context_substitutes_keys_pattern_and_respects_max_records() {
    let (_container, url) = start_redis().await;
    let mut conn = open_conn(&url).await;
    for i in 0..6 {
        let _: () = conn
            .set(format!("acct:7:{i}"), format!("{{\"i\":{i}}}"))
            .await
            .unwrap();
    }

    let source = RedisSource::new(
        RedisSourceConfig::new(
            &url,
            RedisSourceType::Keys {
                pattern: "acct:{id}:*".into(),
            },
        )
        .max_records(2),
    )
    .unwrap();

    let mut ctx: HashMap<String, serde_json::Value> = HashMap::new();
    ctx.insert("id".into(), serde_json::json!("7"));
    let records = source.fetch_with_context(&ctx).await.expect("ok");
    assert_eq!(
        records.len(),
        2,
        "pattern 'acct:{{id}}:*' resolved to 'acct:7:*' and truncated to max_records"
    );
    for r in &records {
        assert!(
            r["key"].as_str().unwrap().starts_with("acct:7:"),
            "every key matches the resolved pattern"
        );
    }
}