apalis-libsql 0.1.0

Background task processing for rust using apalis and libSQL
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
//! Tests for sink functionality

use apalis_core::task::Task;
use apalis_libsql::{
    CompactType, Config, SqlContext,
    sink::{LibsqlSink, push_tasks},
};
use futures::Sink;
use libsql::Builder;
use std::sync::Arc;
use tempfile::TempDir;

struct TestDb {
    db: &'static libsql::Database,
    _temp_dir: Arc<TempDir>,
}

async fn setup_test_db() -> TestDb {
    let temp_dir = Arc::new(TempDir::new().unwrap());
    let db_path = temp_dir.path().join("test_sink.db");

    let db = Builder::new_local(db_path.to_str().unwrap())
        .build()
        .await
        .unwrap();

    let db_static: &'static libsql::Database = Box::leak(Box::new(db));

    // Setup schema
    let conn = db_static.connect().unwrap();
    conn.execute_batch(include_str!("../migrations/001_initial.sql"))
        .await
        .unwrap();

    TestDb {
        db: db_static,
        _temp_dir: temp_dir,
    }
}

#[tokio::test]
async fn test_sink_new() {
    let test_db = setup_test_db().await;
    let db = test_db.db;

    let config = Config::new("TestTask");
    let sink = LibsqlSink::<(), ()>::new(db, &config);

    // Verify sink can be created and debug output contains expected info
    let debug_str = format!("{:?}", sink);
    assert!(debug_str.contains("LibsqlSink"));
    assert!(debug_str.contains("TestTask"));
    assert!(debug_str.contains("buffer_len"));
}

#[tokio::test]
async fn test_push_tasks_empty() {
    let test_db = setup_test_db().await;
    let db = test_db.db;

    let config = Config::new("TestTask");

    // Test pushing empty buffer
    let empty_buffer = Vec::new();
    push_tasks(db, &config, empty_buffer).await.unwrap();

    // Verify no tasks were inserted
    let conn = db.connect().unwrap();
    let mut rows = conn
        .query("SELECT COUNT(*) FROM Jobs", libsql::params![])
        .await
        .unwrap();

    if let Some(row) = rows.next().await.unwrap() {
        let count: i64 = row.get(0).unwrap();
        assert_eq!(count, 0);
    }
}

#[tokio::test]
async fn test_push_tasks_batch() {
    let test_db = setup_test_db().await;
    let db = test_db.db;

    let job_type = "TestTask";
    let config = Config::new(job_type);

    // Create some tasks
    let mut tasks = Vec::new();
    for i in 0..3 {
        let ctx = SqlContext::new().with_max_attempts(5);
        let mut task = Task::new(CompactType::from(vec![i as u8]));
        task.parts.ctx = ctx;
        // Note: We can't easily set task_id in this test setup, but the function should work
        tasks.push(task);
    }

    // Push tasks
    push_tasks(db, &config, tasks).await.unwrap();

    // Verify tasks were inserted
    let conn = db.connect().unwrap();
    let mut rows = conn
        .query(
            "SELECT COUNT(*) FROM Jobs WHERE job_type = ?1",
            libsql::params![job_type],
        )
        .await
        .unwrap();

    if let Some(row) = rows.next().await.unwrap() {
        let count: i64 = row.get(0).unwrap();
        assert_eq!(count, 3);
    }
}

#[tokio::test]
async fn test_sink_poll_ready() {
    let test_db = setup_test_db().await;
    let db = test_db.db;

    let config = Config::new("TestTask");
    let mut sink = LibsqlSink::<(), ()>::new(db, &config);

    // Test poll_ready - since LibsqlSink always returns Poll::Ready(Ok(())), this should work
    use std::pin::Pin;
    use std::task::{Context, Poll};

    let mut pinned_sink = Pin::new(&mut sink);
    let cx = &mut Context::from_waker(futures::task::noop_waker_ref());

    let result = pinned_sink.as_mut().poll_ready(cx);
    assert!(matches!(result, Poll::Ready(Ok(()))));
}

#[tokio::test]
async fn test_sink_start_send() {
    let test_db = setup_test_db().await;
    let db = test_db.db;

    let config = Config::new("TestTask");
    let mut sink = LibsqlSink::<(), ()>::new(db, &config);

    // Create a task
    let ctx = SqlContext::new().with_max_attempts(5);
    let mut task = Task::new(CompactType::from(vec![1, 2, 3]));
    task.parts.ctx = ctx;
    // Note: We can't easily set task_id in this test setup

    // Test start_send
    use std::pin::Pin;
    use std::task::Context;

    let mut pinned_sink = Pin::new(&mut sink);
    let cx = &mut Context::from_waker(futures::task::noop_waker_ref());

    // First ensure we're ready
    let _ready = pinned_sink.as_mut().poll_ready(cx);

    // Then send the task
    pinned_sink.as_mut().start_send(task).unwrap();

    // The task should be buffered, not yet written to DB
    let conn = db.connect().unwrap();
    let mut rows = conn
        .query("SELECT COUNT(*) FROM Jobs", libsql::params![])
        .await
        .unwrap();

    if let Some(row) = rows.next().await.unwrap() {
        let count: i64 = row.get(0).unwrap();
        assert_eq!(count, 0); // Should still be 0 since we haven't flushed
    }
}

#[tokio::test]
async fn test_sink_poll_flush_empty() {
    let test_db = setup_test_db().await;
    let db = test_db.db;

    let config = Config::new("TestTask");
    let mut sink = LibsqlSink::<(), ()>::new(db, &config);

    // Test flushing empty sink
    use std::pin::Pin;
    use std::task::{Context, Poll};

    let mut pinned_sink = Pin::new(&mut sink);
    let cx = &mut Context::from_waker(futures::task::noop_waker_ref());

    let result = pinned_sink.as_mut().poll_flush(cx);
    assert!(matches!(result, Poll::Ready(Ok(()))));
}

#[tokio::test]
async fn test_sink_poll_flush_with_tasks() {
    let test_db = setup_test_db().await;
    let db = test_db.db;

    let job_type = "TestTask";
    let config = Config::new(job_type);
    let mut sink = LibsqlSink::<(), ()>::new(db, &config);

    // Create and send some tasks
    for i in 0..2 {
        let ctx = SqlContext::new().with_max_attempts(5);
        let mut task = Task::new(CompactType::from(vec![i as u8]));
        task.parts.ctx = ctx;
        // Note: We can't easily set task_id in this test setup

        use std::pin::Pin;
        use std::task::Context;

        let mut pinned_sink = Pin::new(&mut sink);
        let cx = &mut Context::from_waker(futures::task::noop_waker_ref());

        // Ensure we're ready and send the task
        let _ready = pinned_sink.as_mut().poll_ready(cx);
        pinned_sink.as_mut().start_send(task).unwrap();
    }

    // Test flushing with tasks
    use std::pin::Pin;
    use std::task::{Context, Poll};

    let mut pinned_sink = Pin::new(&mut sink);
    let cx = &mut Context::from_waker(futures::task::noop_waker_ref());

    let result = pinned_sink.as_mut().poll_flush(cx);
    assert!(matches!(result, Poll::Ready(Ok(()))));

    // Verify tasks were inserted
    let conn = db.connect().unwrap();
    let mut rows = conn
        .query(
            "SELECT COUNT(*) FROM Jobs WHERE job_type = ?1",
            libsql::params![job_type],
        )
        .await
        .unwrap();

    if let Some(row) = rows.next().await.unwrap() {
        let count: i64 = row.get(0).unwrap();
        assert_eq!(count, 2);
    }
}

#[tokio::test]
async fn test_sink_poll_close() {
    let test_db = setup_test_db().await;
    let db = test_db.db;

    let config = Config::new("TestTask");
    let mut sink = LibsqlSink::<(), ()>::new(db, &config);

    // Test closing (should flush first)
    use std::pin::Pin;
    use std::task::{Context, Poll};

    let mut pinned_sink = Pin::new(&mut sink);
    let cx = &mut Context::from_waker(futures::task::noop_waker_ref());

    let result = pinned_sink.as_mut().poll_close(cx);
    assert!(matches!(result, Poll::Ready(Ok(()))));
}

#[tokio::test]
async fn test_sink_clone_does_not_copy_buffer() {
    let test_db = setup_test_db().await;
    let db = test_db.db;

    let config = Config::new("TestTask");
    let mut sink1 = LibsqlSink::<(), ()>::new(db, &config);

    // Add a task to the first sink
    let ctx = SqlContext::new().with_max_attempts(5);
    let mut task = Task::new(CompactType::from(vec![1, 2, 3]));
    task.parts.ctx = ctx;

    use std::pin::Pin;
    use std::task::Context;

    let mut pinned_sink = Pin::new(&mut sink1);
    let cx = &mut Context::from_waker(futures::task::noop_waker_ref());

    // Ensure we're ready and send the task
    let _ready = pinned_sink.as_mut().poll_ready(cx);
    pinned_sink.as_mut().start_send(task).unwrap();

    // Verify the original sink has a task in buffer by checking debug output
    // We need to drop the pinned sink first to avoid borrowing conflicts
    let debug_str1 = format!("{:?}", sink1);
    assert!(
        debug_str1.contains("buffer_len: 1"),
        "Original sink should have 1 task in buffer: {}",
        debug_str1
    );

    // Clone the sink while it has buffered tasks
    let sink2 = sink1.clone();

    // Verify the cloned sink has an empty buffer (cloning should not copy buffered tasks)
    let debug_str2 = format!("{:?}", sink2);
    assert!(
        debug_str2.contains("buffer_len: 0"),
        "Cloned sink should have empty buffer: {}",
        debug_str2
    );

    // Re-create the pinned sink to flush the original
    let mut pinned_sink = Pin::new(&mut sink1);
    let _result = pinned_sink.as_mut().poll_flush(cx);

    // Verify task was inserted from original sink
    let conn = db.connect().unwrap();
    let mut rows = conn
        .query("SELECT COUNT(*) FROM Jobs", libsql::params![])
        .await
        .unwrap();

    if let Some(row) = rows.next().await.unwrap() {
        let count: i64 = row.get(0).unwrap();
        assert_eq!(count, 1);
    }
}

#[tokio::test]
async fn test_sink_debug() {
    let test_db = setup_test_db().await;
    let db = test_db.db;

    let config = Config::new("TestTask");
    let sink = LibsqlSink::<(), ()>::new(db, &config);

    let debug_str = format!("{:?}", sink);
    assert!(debug_str.contains("LibsqlSink"));
    assert!(debug_str.contains("TestTask"));
}

#[tokio::test]
async fn test_push_tasks_rollback_on_error() {
    let test_db = setup_test_db().await;
    let db = test_db.db;

    let job_type = "TestTask";
    let config = Config::new(job_type);

    // First, insert a task with a specific ID to create a UNIQUE constraint violation later
    let conn = db.connect().unwrap();
    let duplicate_ulid = ulid::Ulid::new();
    let duplicate_id = duplicate_ulid.to_string();
    conn.execute(
        "INSERT INTO Jobs (job, id, job_type, status, attempts, max_attempts, run_at, priority, metadata) 
         VALUES (?1, ?2, ?3, 'Pending', 0, 3, strftime('%s', 'now'), 0, '{}')",
        libsql::params![b"existing_task", duplicate_id.clone(), job_type],
    )
    .await
    .unwrap();

    // Verify initial task count
    let mut rows = conn
        .query(
            "SELECT COUNT(*) FROM Jobs WHERE job_type = ?1",
            libsql::params![job_type],
        )
        .await
        .unwrap();

    if let Some(row) = rows.next().await.unwrap() {
        let initial_count: i64 = row.get(0).unwrap();
        assert_eq!(initial_count, 1);
    }

    // Create tasks where one will cause a UNIQUE constraint violation
    let mut tasks = Vec::new();

    // First task - this will succeed
    let ctx1 = SqlContext::new().with_max_attempts(5);
    let mut task1 = Task::new(CompactType::from(vec![1u8]));
    task1.parts.ctx = ctx1;
    tasks.push(task1);

    // Second task - set its ID to the duplicate to force a constraint violation
    let ctx2 = SqlContext::new().with_max_attempts(5);
    let mut task2 = Task::new(CompactType::from(vec![2u8]));
    task2.parts.ctx = ctx2;
    // Set the task_id to the duplicate ID to trigger UNIQUE constraint violation
    task2.parts.task_id = Some(apalis_core::task::task_id::TaskId::new(duplicate_ulid));
    tasks.push(task2);

    // Third task - this should not be inserted due to rollback
    let ctx3 = SqlContext::new().with_max_attempts(5);
    let mut task3 = Task::new(CompactType::from(vec![3u8]));
    task3.parts.ctx = ctx3;
    tasks.push(task3);

    // Attempt to push tasks - this should fail due to duplicate ID
    let result = push_tasks(db, &config, tasks).await;

    // The operation should fail
    assert!(
        result.is_err(),
        "Expected push_tasks to fail due to duplicate ID"
    );

    // Verify that NO tasks were inserted (rollback should have occurred)
    let mut rows = conn
        .query(
            "SELECT COUNT(*) FROM Jobs WHERE job_type = ?1",
            libsql::params![job_type],
        )
        .await
        .unwrap();

    if let Some(row) = rows.next().await.unwrap() {
        let final_count: i64 = row.get(0).unwrap();
        assert_eq!(
            final_count, 1,
            "Expected only the original task to remain after rollback"
        );
    }

    // Verify that the job data is still the original task, not any of the failed ones
    let mut rows = conn
        .query(
            "SELECT job FROM Jobs WHERE job_type = ?1 AND id = ?2",
            libsql::params![job_type, duplicate_id.clone()],
        )
        .await
        .unwrap();

    if let Some(row) = rows.next().await.unwrap() {
        let job_data: Vec<u8> = row.get(0).unwrap();
        assert_eq!(
            job_data, b"existing_task",
            "Original task data should be preserved"
        );
    }
}