Skip to content

Commit

Permalink
Added apply_events test
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan770 committed Sep 12, 2020
1 parent 8e5a633 commit 55984e8
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions spartan/node/replication/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,79 @@ where
}
}
}

#[cfg(test)]
mod tests {
use super::Event;
use crate::node::{
replication::{
primary::storage::PrimaryStorage, replica::storage::ReplicaStorage,
storage::ReplicationStorage,
},
DB,
};
use maybe_owned::MaybeOwned;

#[tokio::test]
async fn test_apply_events() {
let queue = DB::default();

let events = vec![
(MaybeOwned::Owned(1), MaybeOwned::Owned(Event::Gc)),
(MaybeOwned::Owned(2), MaybeOwned::Owned(Event::Clear)),
]
.into_boxed_slice();

queue
.prepare_replication(
|_| false,
|| ReplicationStorage::Replica(ReplicaStorage::default()),
)
.await;

assert_eq!(
queue
.replication_storage()
.await
.as_mut()
.unwrap()
.get_replica()
.get_index(),
1
);

queue.apply_events(events).await;

assert_eq!(
queue
.replication_storage()
.await
.as_mut()
.unwrap()
.get_replica()
.get_index(),
3
);
}

#[tokio::test]
#[should_panic(expected = "Replication storage is in primary mode.")]
async fn test_apply_events_with_invalid_storage() {
let queue = DB::default();

let events = vec![
(MaybeOwned::Owned(1), MaybeOwned::Owned(Event::Gc)),
(MaybeOwned::Owned(2), MaybeOwned::Owned(Event::Clear)),
]
.into_boxed_slice();

queue
.prepare_replication(
|_| false,
|| ReplicationStorage::Primary(PrimaryStorage::default()),
)
.await;

queue.apply_events(events).await;
}
}

0 comments on commit 55984e8

Please sign in to comment.