Skip to content

Commit 78a3d7a

Browse files
committed
Add test cases for MongoDB store cleanups
1 parent 98aae51 commit 78a3d7a

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

crates/hotfix/tests/mongodb_store_tests.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![cfg(feature = "mongodb")]
22

3+
use chrono::Duration;
34
use hotfix::store::mongodb::{Client, MongoDbMessageStore};
45
use hotfix::store::{MessageStore, StoreError};
56
use testcontainers::runners::AsyncRunner;
@@ -114,3 +115,77 @@ async fn test_state_preserved_after_failed_set_target() {
114115
// State should be unchanged
115116
assert_eq!(store.next_target_seq_number(), initial_target_seq);
116117
}
118+
119+
#[tokio::test]
120+
async fn test_cleanup_removes_old_sequences() {
121+
let (container, mut store) = create_dedicated_container_and_store().await;
122+
123+
// Add a message to the initial sequence
124+
store.add(1, b"message in sequence 1").await.unwrap();
125+
126+
// Reset creates a new sequence, making the first one "old"
127+
store.reset().await.unwrap();
128+
store.add(1, b"message in sequence 2").await.unwrap();
129+
130+
// Reset again to have two old sequences
131+
store.reset().await.unwrap();
132+
store.add(1, b"message in sequence 3").await.unwrap();
133+
134+
// Small delay to ensure old sequences have earlier timestamps than the cutoff
135+
tokio::time::sleep(std::time::Duration::from_millis(2)).await;
136+
137+
// Cleanup with zero duration should delete all old sequences
138+
let deleted = store.cleanup_older_than(Duration::zero()).await.unwrap();
139+
140+
assert_eq!(deleted, 2);
141+
142+
drop(container);
143+
}
144+
145+
#[tokio::test]
146+
async fn test_cleanup_preserves_current_sequence() {
147+
let (container, mut store) = create_dedicated_container_and_store().await;
148+
149+
// Add messages to current sequence
150+
store.add(1, b"message 1").await.unwrap();
151+
store.add(2, b"message 2").await.unwrap();
152+
153+
// Cleanup with zero duration - current sequence should be preserved
154+
let deleted = store.cleanup_older_than(Duration::zero()).await.unwrap();
155+
156+
assert_eq!(deleted, 0);
157+
158+
// Verify messages are still accessible
159+
let messages = store.get_slice(1, 2).await.unwrap();
160+
assert_eq!(messages.len(), 2);
161+
162+
drop(container);
163+
}
164+
165+
#[tokio::test]
166+
async fn test_cleanup_respects_age_threshold() {
167+
let (container, mut store) = create_dedicated_container_and_store().await;
168+
169+
// Create an old sequence
170+
store.reset().await.unwrap();
171+
172+
// Cleanup with a large duration should not delete anything
173+
let deleted = store.cleanup_older_than(Duration::days(365)).await.unwrap();
174+
175+
assert_eq!(deleted, 0);
176+
177+
drop(container);
178+
}
179+
180+
#[tokio::test]
181+
async fn test_cleanup_after_connection_drop() {
182+
let (container, store) = create_dedicated_container_and_store().await;
183+
184+
// Stop the container
185+
container.stop().await.unwrap();
186+
187+
// Attempt cleanup - should fail
188+
let result = store.cleanup_older_than(Duration::zero()).await;
189+
190+
assert!(matches!(result, Err(StoreError::Cleanup(_))));
191+
}

0 commit comments

Comments
 (0)