| 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 |
use std::sync::Arc; |
| 52 |
|
| 53 |
use tauri::Manager; |
| 54 |
use tokio::time::{Duration, interval}; |
| 55 |
use tokio_util::sync::CancellationToken; |
| 56 |
use tracing::{debug, error, info}; |
| 57 |
|
| 58 |
use crate::state::{AppState, DESKTOP_USER_ID}; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
const CHECK_INTERVAL_SECS: u64 = 60; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
#[derive(Debug, Clone, Default)] |
| 73 |
pub struct QueuedOp { |
| 74 |
pub id: String, |
| 75 |
pub kind: String, |
| 76 |
pub group_id: Option<String>, |
| 77 |
pub name: Option<String>, |
| 78 |
pub email: Option<String>, |
| 79 |
pub pubkey: Option<String>, |
| 80 |
pub member_user_id: Option<String>, |
| 81 |
|
| 82 |
pub invitation_id: Option<String>, |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
pub invite_token: Option<String>, |
| 88 |
|
| 89 |
pub expires_in_hours: Option<i64>, |
| 90 |
pub attempts: i32, |
| 91 |
pub last_error: Option<String>, |
| 92 |
pub done_at: Option<String>, |
| 93 |
} |
| 94 |
|
| 95 |
impl QueuedOp { |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
#[must_use] |
| 103 |
pub fn describe(&self) -> String { |
| 104 |
self.describe_confirming(None) |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
#[must_use] |
| 120 |
pub fn describe_confirming(&self, fingerprint: Option<&str>) -> String { |
| 121 |
match self.kind.as_str() { |
| 122 |
"create_group" => format!( |
| 123 |
"Create the group {}", |
| 124 |
self.name.as_deref().unwrap_or("(unnamed)") |
| 125 |
), |
| 126 |
"add_member" => format!( |
| 127 |
"Add {} to a group", |
| 128 |
self.email.as_deref().unwrap_or("(no address)") |
| 129 |
), |
| 130 |
"remove_member" => "Remove a member from a group".to_owned(), |
| 131 |
"create_invite" => "Issue an invite code".to_owned(), |
| 132 |
"revoke_invite" => "Cancel an invitation".to_owned(), |
| 133 |
"confirm_invite" => fingerprint.map_or_else( |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|| "Admit somebody, once their fingerprint is checked".to_owned(), |
| 139 |
|fingerprint| format!("Admit the holder of {fingerprint}"), |
| 140 |
), |
| 141 |
"preview_invite" => "Read what an invite code leads to".to_owned(), |
| 142 |
"accept_invite" => "Accept an invite code".to_owned(), |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
other => format!("An action this version does not understand ({other})"), |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
pub fn enqueue(state: &AppState, op: &QueuedOp) -> Result<(), String> { |
| 153 |
let conn = state.db.conn().map_err(|e| e.to_string())?; |
| 154 |
conn.execute( |
| 155 |
"INSERT INTO group_admin_queue \ |
| 156 |
(id, user_id, kind, group_id, name, email, pubkey, member_user_id, \ |
| 157 |
invitation_id, invite_token, expires_in_hours) \ |
| 158 |
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)", |
| 159 |
rusqlite::params![ |
| 160 |
op.id, |
| 161 |
DESKTOP_USER_ID.to_string(), |
| 162 |
op.kind, |
| 163 |
op.group_id, |
| 164 |
op.name, |
| 165 |
op.email, |
| 166 |
op.pubkey, |
| 167 |
op.member_user_id, |
| 168 |
op.invitation_id, |
| 169 |
op.invite_token, |
| 170 |
op.expires_in_hours, |
| 171 |
], |
| 172 |
) |
| 173 |
.map_err(|e| e.to_string())?; |
| 174 |
Ok(()) |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
pub fn pending(state: &AppState) -> Result<Vec<QueuedOp>, String> { |
| 184 |
let conn = state.db.conn().map_err(|e| e.to_string())?; |
| 185 |
let mut stmt = conn |
| 186 |
.prepare( |
| 187 |
"SELECT id, kind, group_id, name, email, pubkey, member_user_id, \ |
| 188 |
invitation_id, invite_token, expires_in_hours, \ |
| 189 |
attempts, last_error, done_at \ |
| 190 |
FROM group_admin_queue WHERE user_id = ?1 ORDER BY queued_at", |
| 191 |
) |
| 192 |
.map_err(|e| e.to_string())?; |
| 193 |
let rows = stmt |
| 194 |
.query_map(rusqlite::params![DESKTOP_USER_ID.to_string()], |row| { |
| 195 |
Ok(QueuedOp { |
| 196 |
id: row.get(0)?, |
| 197 |
kind: row.get(1)?, |
| 198 |
group_id: row.get(2)?, |
| 199 |
name: row.get(3)?, |
| 200 |
email: row.get(4)?, |
| 201 |
pubkey: row.get(5)?, |
| 202 |
member_user_id: row.get(6)?, |
| 203 |
invitation_id: row.get(7)?, |
| 204 |
invite_token: row.get(8)?, |
| 205 |
expires_in_hours: row.get(9)?, |
| 206 |
attempts: row.get(10)?, |
| 207 |
last_error: row.get(11)?, |
| 208 |
done_at: row.get(12)?, |
| 209 |
}) |
| 210 |
}) |
| 211 |
.map_err(|e| e.to_string())? |
| 212 |
.collect::<Result<Vec<_>, _>>() |
| 213 |
.map_err(|e| e.to_string())?; |
| 214 |
Ok(rows) |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
pub fn cancel(state: &AppState, id: &str) -> Result<bool, String> { |
| 222 |
let conn = state.db.conn().map_err(|e| e.to_string())?; |
| 223 |
let changed = conn |
| 224 |
.execute( |
| 225 |
"DELETE FROM group_admin_queue WHERE id = ?1 AND user_id = ?2 AND done_at IS NULL", |
| 226 |
rusqlite::params![id, DESKTOP_USER_ID.to_string()], |
| 227 |
) |
| 228 |
.map_err(|e| e.to_string())?; |
| 229 |
Ok(changed > 0) |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
fn sweep_settled(state: &AppState) { |
| 238 |
let Ok(conn) = state.db.conn() else { return }; |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
let _ = conn.execute( |
| 244 |
"DELETE FROM group_admin_queue \ |
| 245 |
WHERE done_at IS NOT NULL \ |
| 246 |
AND (kind = 'create_group' \ |
| 247 |
AND EXISTS (SELECT 1 FROM sync_groups WHERE name = group_admin_queue.name) \ |
| 248 |
OR done_at < strftime('%Y-%m-%dT%H:%M:%fZ', 'now', '-1 hour'))", |
| 249 |
[], |
| 250 |
); |
| 251 |
} |
| 252 |
|
| 253 |
fn record_failure(state: &AppState, id: &str, message: &str) { |
| 254 |
let Ok(conn) = state.db.conn() else { return }; |
| 255 |
if let Err(error) = conn.execute( |
| 256 |
"UPDATE group_admin_queue SET attempts = attempts + 1, last_error = ?2 WHERE id = ?1", |
| 257 |
rusqlite::params![id, message], |
| 258 |
) { |
| 259 |
error!("Group queue: could not record the failure: {error}"); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
fn record_done(state: &AppState, id: &str) { |
| 264 |
let Ok(conn) = state.db.conn() else { return }; |
| 265 |
if let Err(error) = conn.execute( |
| 266 |
"UPDATE group_admin_queue SET done_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now'), \ |
| 267 |
last_error = NULL WHERE id = ?1", |
| 268 |
rusqlite::params![id], |
| 269 |
) { |
| 270 |
error!("Group queue: could not record the success: {error}"); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
pub async fn start_group_queue_drainer(app: tauri::AppHandle, cancel: CancellationToken) { |
| 276 |
let mut check_interval = interval(Duration::from_secs(CHECK_INTERVAL_SECS)); |
| 277 |
let mut tick: u64 = 0; |
| 278 |
|
| 279 |
info!("Group queue drainer started (checking every {CHECK_INTERVAL_SECS} seconds)"); |
| 280 |
|
| 281 |
loop { |
| 282 |
tokio::select! { |
| 283 |
() = cancel.cancelled() => { |
| 284 |
info!("Group queue drainer shutting down"); |
| 285 |
break; |
| 286 |
} |
| 287 |
_ = check_interval.tick() => {} |
| 288 |
} |
| 289 |
tick = tick.wrapping_add(1); |
| 290 |
|
| 291 |
let Some(state) = app.try_state::<Arc<AppState>>() else { |
| 292 |
debug!("Group queue drainer: state not ready yet"); |
| 293 |
continue; |
| 294 |
}; |
| 295 |
let state: Arc<AppState> = state.inner().clone(); |
| 296 |
|
| 297 |
drain_once(&state, tick).await; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
pub async fn drain_once(state: &Arc<AppState>, tick: u64) { |
| 306 |
sweep_settled(state); |
| 307 |
|
| 308 |
let queued = match pending(state) { |
| 309 |
Ok(queued) => queued, |
| 310 |
Err(error) => { |
| 311 |
error!("Group queue drainer: could not read the queue: {error}"); |
| 312 |
return; |
| 313 |
} |
| 314 |
}; |
| 315 |
|
| 316 |
for op in queued { |
| 317 |
if op.done_at.is_some() { |
| 318 |
continue; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
if !crate::outbox::due_on_tick(op.attempts, tick) { |
| 325 |
continue; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
let Some(client) = state.read_recovering() else { |
| 333 |
continue; |
| 334 |
}; |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
let mut conn = state.db.conn().ok(); |
| 340 |
let outcome = perform(&client, &op, conn.as_deref_mut()).await; |
| 341 |
drop(conn); |
| 342 |
|
| 343 |
match outcome { |
| 344 |
Ok(()) => record_done(state, &op.id), |
| 345 |
Err(message) => record_failure(state, &op.id, &message), |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
async fn perform( |
| 352 |
client: &synckit_client::SyncKitClient, |
| 353 |
op: &QueuedOp, |
| 354 |
conn: Option<&mut rusqlite::Connection>, |
| 355 |
) -> Result<(), String> { |
| 356 |
let group = |raw: &Option<String>| -> Result<synckit_client::GroupId, String> { |
| 357 |
raw.as_deref() |
| 358 |
.ok_or_else(|| "No group on the queued action.".to_owned())? |
| 359 |
.parse::<uuid::Uuid>() |
| 360 |
.map(synckit_client::GroupId::new) |
| 361 |
.map_err(|_| "The queued action names a group id that will not parse.".to_owned()) |
| 362 |
}; |
| 363 |
let invitation = |raw: &Option<String>| -> Result<synckit_client::InvitationId, String> { |
| 364 |
raw.as_deref() |
| 365 |
.ok_or_else(|| "No invitation on the queued action.".to_owned())? |
| 366 |
.parse::<uuid::Uuid>() |
| 367 |
.map(synckit_client::InvitationId::new) |
| 368 |
.map_err(|_| "The queued action names an invitation id that will not parse.".to_owned()) |
| 369 |
}; |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
fn token(raw: Option<&str>) -> Result<&str, String> { |
| 378 |
raw.ok_or_else(|| "No invite code on the queued action.".to_owned()) |
| 379 |
} |
| 380 |
|
| 381 |
match op.kind.as_str() { |
| 382 |
"create_group" => { |
| 383 |
let name = op |
| 384 |
.name |
| 385 |
.as_deref() |
| 386 |
.ok_or_else(|| "No name on the queued action.".to_owned())?; |
| 387 |
let group = client.create_group(name).await.map_err(|e| e.to_string())?; |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
if let Some(conn) = conn { |
| 393 |
let known = synckit_client::store::directory::KnownGroup { |
| 394 |
id: group.id, |
| 395 |
name: group.name, |
| 396 |
gck_version: group.gck_version, |
| 397 |
is_admin: true, |
| 398 |
}; |
| 399 |
if let Err(error) = synckit_client::store::directory::add_group(conn, &known) { |
| 400 |
|
| 401 |
|
| 402 |
error!("Group queue: could not record the new group: {error}"); |
| 403 |
} |
| 404 |
} |
| 405 |
Ok(()) |
| 406 |
} |
| 407 |
"add_member" => { |
| 408 |
let email = op |
| 409 |
.email |
| 410 |
.as_deref() |
| 411 |
.ok_or_else(|| "No address on the queued action.".to_owned())?; |
| 412 |
let pubkey = op |
| 413 |
.pubkey |
| 414 |
.as_deref() |
| 415 |
.ok_or_else(|| "No public key on the queued action.".to_owned())?; |
| 416 |
client |
| 417 |
.add_member(group(&op.group_id)?, email, pubkey) |
| 418 |
.await |
| 419 |
.map_err(|e| e.to_string()) |
| 420 |
} |
| 421 |
"remove_member" => { |
| 422 |
let member = op |
| 423 |
.member_user_id |
| 424 |
.as_deref() |
| 425 |
.ok_or_else(|| "No member on the queued action.".to_owned())? |
| 426 |
.parse::<uuid::Uuid>() |
| 427 |
.map(synckit_client::UserId::new) |
| 428 |
.map_err(|_| "The queued action names a user id that will not parse.".to_owned())?; |
| 429 |
client |
| 430 |
.remove_member(group(&op.group_id)?, member) |
| 431 |
.await |
| 432 |
.map_err(|e| e.to_string()) |
| 433 |
} |
| 434 |
"create_invite" => { |
| 435 |
let group = group(&op.group_id)?; |
| 436 |
let invitation = client |
| 437 |
.create_invitation(group, op.expires_in_hours) |
| 438 |
.await |
| 439 |
.map_err(|e| e.to_string())?; |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
let conn = conn.ok_or_else(|| { |
| 447 |
"The invite was issued and could not be written down: no database \ |
| 448 |
connection. Revoke it from the group and issue another." |
| 449 |
.to_owned() |
| 450 |
})?; |
| 451 |
synckit_client::store::directory::record_issued(conn, group, &invitation).map_err( |
| 452 |
|error| { |
| 453 |
format!( |
| 454 |
"The invite was issued and could not be written down ({error}). \ |
| 455 |
Revoke it from the group and issue another." |
| 456 |
) |
| 457 |
}, |
| 458 |
) |
| 459 |
} |
| 460 |
"revoke_invite" => client |
| 461 |
.revoke_invitation(group(&op.group_id)?, invitation(&op.invitation_id)?) |
| 462 |
.await |
| 463 |
.map_err(|e| e.to_string()), |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
"confirm_invite" => client |
| 469 |
.confirm_invitation(group(&op.group_id)?, invitation(&op.invitation_id)?, None) |
| 470 |
.await |
| 471 |
.map_err(|e| e.to_string()), |
| 472 |
"preview_invite" => { |
| 473 |
let token = token(op.invite_token.as_deref())?; |
| 474 |
let preview = client |
| 475 |
.preview_invitation(token) |
| 476 |
.await |
| 477 |
.map_err(|e| e.to_string())?; |
| 478 |
let conn = conn.ok_or_else(|| { |
| 479 |
"Read the code and could not write down the answer: no database connection." |
| 480 |
.to_owned() |
| 481 |
})?; |
| 482 |
let known = synckit_client::store::directory::KnownPreview { |
| 483 |
token: token.to_owned(), |
| 484 |
group_name: preview.group_name, |
| 485 |
inviter_email: preview.inviter_email, |
| 486 |
redeemable: preview.redeemable, |
| 487 |
state: preview.state, |
| 488 |
expires_at: preview.expires_at.to_rfc3339(), |
| 489 |
}; |
| 490 |
synckit_client::store::directory::write_preview(conn, &known) |
| 491 |
.map_err(|error| format!("Could not write down what the code leads to: {error}")) |
| 492 |
} |
| 493 |
"accept_invite" => { |
| 494 |
client |
| 495 |
.accept_invitation(token(op.invite_token.as_deref())?) |
| 496 |
.await |
| 497 |
.map_err(|e| e.to_string())?; |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
if let Some(conn) = conn |
| 502 |
&& let Err(error) = synckit_client::store::directory::clear_preview(conn) |
| 503 |
{ |
| 504 |
error!("Group queue: could not forget the accepted code: {error}"); |
| 505 |
} |
| 506 |
Ok(()) |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
other => Err(format!( |
| 511 |
"This version does not know how to perform `{other}`. It is kept, not lost." |
| 512 |
)), |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
#[cfg(test)] |
| 517 |
mod tests { |
| 518 |
use super::*; |
| 519 |
|
| 520 |
async fn state() -> Arc<AppState> { |
| 521 |
let (state, _) = crate::test_utils::setup_test_state().await; |
| 522 |
state |
| 523 |
} |
| 524 |
|
| 525 |
fn op(kind: &str, id: &str) -> QueuedOp { |
| 526 |
QueuedOp { |
| 527 |
id: id.to_owned(), |
| 528 |
kind: kind.to_owned(), |
| 529 |
group_id: Some("00000000-0000-0000-0000-000000000001".to_owned()), |
| 530 |
name: Some("The Firm".to_owned()), |
| 531 |
email: Some("them@localhost".to_owned()), |
| 532 |
pubkey: Some("k".to_owned()), |
| 533 |
member_user_id: Some("00000000-0000-0000-0000-000000000002".to_owned()), |
| 534 |
..Default::default() |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
#[test] |
| 541 |
fn each_kind_says_what_it_will_do() { |
| 542 |
assert_eq!( |
| 543 |
op("create_group", "a").describe(), |
| 544 |
"Create the group The Firm" |
| 545 |
); |
| 546 |
assert_eq!( |
| 547 |
op("add_member", "a").describe(), |
| 548 |
"Add them@localhost to a group" |
| 549 |
); |
| 550 |
assert_eq!( |
| 551 |
op("remove_member", "a").describe(), |
| 552 |
"Remove a member from a group" |
| 553 |
); |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
#[test] |
| 559 |
fn a_kind_from_a_newer_build_still_reads_as_something() { |
| 560 |
let described = op("invite_member", "a").describe(); |
| 561 |
assert!(described.contains("does not understand"), "{described}"); |
| 562 |
assert!(described.contains("invite_member"), "{described}"); |
| 563 |
} |
| 564 |
|
| 565 |
#[tokio::test] |
| 566 |
async fn a_queued_action_comes_back_out_in_the_order_it_went_in() { |
| 567 |
let state = state().await; |
| 568 |
enqueue(&state, &op("create_group", "first")).unwrap(); |
| 569 |
enqueue(&state, &op("add_member", "second")).unwrap(); |
| 570 |
|
| 571 |
let queued = pending(&state).unwrap(); |
| 572 |
assert_eq!(queued.len(), 2); |
| 573 |
assert_eq!(queued[0].id, "first"); |
| 574 |
assert_eq!(queued[1].id, "second"); |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
#[tokio::test] |
| 581 |
async fn a_pass_with_no_client_leaves_the_row_untouched() { |
| 582 |
let state = state().await; |
| 583 |
enqueue(&state, &op("create_group", "waiting")).unwrap(); |
| 584 |
|
| 585 |
drain_once(&state, 1).await; |
| 586 |
|
| 587 |
let queued = pending(&state).unwrap(); |
| 588 |
assert_eq!(queued.len(), 1, "still queued"); |
| 589 |
assert_eq!(queued[0].attempts, 0, "and not counted against"); |
| 590 |
assert!(queued[0].last_error.is_none()); |
| 591 |
} |
| 592 |
|
| 593 |
#[tokio::test] |
| 594 |
async fn cancelling_takes_it_out_and_says_whether_it_did() { |
| 595 |
let state = state().await; |
| 596 |
enqueue(&state, &op("create_group", "mistake")).unwrap(); |
| 597 |
|
| 598 |
assert!(cancel(&state, "mistake").unwrap()); |
| 599 |
assert!(pending(&state).unwrap().is_empty()); |
| 600 |
assert!(!cancel(&state, "mistake").unwrap(), "twice is not a lie"); |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
#[tokio::test] |
| 606 |
async fn a_row_the_server_accepted_cannot_be_cancelled() { |
| 607 |
let state = state().await; |
| 608 |
enqueue(&state, &op("create_group", "gone")).unwrap(); |
| 609 |
record_done(&state, "gone"); |
| 610 |
|
| 611 |
assert!(!cancel(&state, "gone").unwrap()); |
| 612 |
let queued = pending(&state).unwrap(); |
| 613 |
assert_eq!(queued.len(), 1); |
| 614 |
assert!(queued[0].done_at.is_some()); |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
#[tokio::test] |
| 621 |
async fn a_done_row_survives_until_the_directory_catches_up() { |
| 622 |
let state = state().await; |
| 623 |
let conn = state.db.conn().unwrap(); |
| 624 |
synckit_client::store::directory::ensure_tables(&conn).unwrap(); |
| 625 |
drop(conn); |
| 626 |
|
| 627 |
enqueue(&state, &op("create_group", "landed")).unwrap(); |
| 628 |
record_done(&state, "landed"); |
| 629 |
|
| 630 |
drain_once(&state, 1).await; |
| 631 |
assert_eq!(pending(&state).unwrap().len(), 1, "the directory has not"); |
| 632 |
|
| 633 |
let mut conn = state.db.conn().unwrap(); |
| 634 |
synckit_client::store::directory::add_group( |
| 635 |
&mut conn, |
| 636 |
&synckit_client::store::directory::KnownGroup { |
| 637 |
id: synckit_client::GroupId::new(uuid::Uuid::from_u128(1)), |
| 638 |
name: "The Firm".to_owned(), |
| 639 |
gck_version: 1, |
| 640 |
is_admin: true, |
| 641 |
}, |
| 642 |
) |
| 643 |
.unwrap(); |
| 644 |
drop(conn); |
| 645 |
|
| 646 |
drain_once(&state, 2).await; |
| 647 |
assert!( |
| 648 |
pending(&state).unwrap().is_empty(), |
| 649 |
"and now it has, so the row has nothing left to say" |
| 650 |
); |
| 651 |
} |
| 652 |
|
| 653 |
#[tokio::test] |
| 654 |
async fn a_failure_is_stamped_with_its_reason_and_counted() { |
| 655 |
let state = state().await; |
| 656 |
enqueue(&state, &op("create_group", "bad")).unwrap(); |
| 657 |
record_failure(&state, "bad", "The server said no."); |
| 658 |
|
| 659 |
let queued = pending(&state).unwrap(); |
| 660 |
assert_eq!(queued[0].attempts, 1); |
| 661 |
assert_eq!(queued[0].last_error.as_deref(), Some("The server said no.")); |
| 662 |
assert!(queued[0].done_at.is_none(), "and it is still queued"); |
| 663 |
} |
| 664 |
} |
| 665 |
|