Skip to content

Commit

Permalink
chore(derive): stage coverage (#673)
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell authored Oct 9, 2024
1 parent 98f9337 commit 19139b8
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
52 changes: 52 additions & 0 deletions crates/derive/src/stages/batch_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,20 @@ mod tests {
BatchReader::from(bytes)
}

#[test]
fn test_pop_next_batch() {
let cfg = Arc::new(RollupConfig::default());
let mock = MockBatchQueueProvider::new(vec![]);
let fetcher = TestL2ChainProvider::default();
let mut bq = BatchQueue::new(cfg, mock, fetcher);
let parent = L2BlockInfo::default();
let sb = SingleBatch::default();
bq.next_spans.push(sb.clone());
let next = bq.pop_next_batch(parent).unwrap();
assert_eq!(next, sb);
assert!(bq.next_spans.is_empty());
}

#[tokio::test]
async fn test_batch_queue_reset() {
let cfg = Arc::new(RollupConfig::default());
Expand Down Expand Up @@ -854,6 +868,44 @@ mod tests {
assert!(logs[0].contains(warn_str));
}

#[tokio::test]
async fn test_next_batch_cached_single_batch() {
let mut reader = new_batch_reader();
let cfg = Arc::new(RollupConfig::default());
let mut batch_vec: Vec<PipelineResult<Batch>> = vec![];
while let Some(batch) = reader.next_batch(cfg.as_ref()) {
batch_vec.push(Ok(batch));
}
let mut mock = MockBatchQueueProvider::new(batch_vec);
mock.origin = Some(BlockInfo::default());
let fetcher = TestL2ChainProvider::default();
let mut bq = BatchQueue::new(cfg, mock, fetcher);
let sb = SingleBatch::default();
bq.next_spans.push(sb.clone());
let next = bq.next_batch(L2BlockInfo::default()).await.unwrap();
assert_eq!(next, sb);
assert!(bq.next_spans.is_empty());
}

#[tokio::test]
async fn test_next_batch_clear_next_spans() {
let mut reader = new_batch_reader();
let cfg = Arc::new(RollupConfig { block_time: 100, ..Default::default() });
let mut batch_vec: Vec<PipelineResult<Batch>> = vec![];
while let Some(batch) = reader.next_batch(cfg.as_ref()) {
batch_vec.push(Ok(batch));
}
let mut mock = MockBatchQueueProvider::new(batch_vec);
mock.origin = Some(BlockInfo::default());
let fetcher = TestL2ChainProvider::default();
let mut bq = BatchQueue::new(cfg, mock, fetcher);
let sb = SingleBatch::default();
bq.next_spans.push(sb.clone());
let res = bq.next_batch(L2BlockInfo::default()).await.unwrap_err();
assert_eq!(res, PipelineError::NotEnoughData.temp());
assert!(bq.is_last_in_span());
}

#[tokio::test]
async fn test_next_batch_not_enough_data() {
let mut reader = new_batch_reader();
Expand Down
70 changes: 70 additions & 0 deletions crates/derive/src/stages/channel_bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,76 @@ mod tests {
);
}

#[test]
fn test_read_channel_canyon_not_active() {
let mock = MockChannelBankProvider::new(vec![]);
let cfg = Arc::new(RollupConfig::default());
let mut channel_bank = ChannelBank::new(cfg, mock);
let id: ChannelId = [0xFF; 16];
channel_bank.channel_queue.push_back(id);
let mut channel = Channel::new(id, BlockInfo::default());
channel
.add_frame(
Frame { id, number: 0, data: b"seven__".to_vec(), is_last: false },
BlockInfo::default(),
)
.unwrap();
channel
.add_frame(
Frame { id, number: 1, data: b"seven__".to_vec(), is_last: false },
BlockInfo::default(),
)
.unwrap();
channel
.add_frame(
Frame { id, number: 2, data: b"seven__".to_vec(), is_last: true },
BlockInfo::default(),
)
.unwrap();
assert!(channel.is_ready());
channel_bank.channels.insert([0xFF; 16], channel);
let frame_data = channel_bank.read().unwrap();
assert_eq!(
frame_data,
Some(alloy_primitives::bytes!("736576656e5f5f736576656e5f5f736576656e5f5f"))
);
}

#[test]
fn test_read_channel_active() {
let mock = MockChannelBankProvider::new(vec![]);
let cfg = Arc::new(RollupConfig { canyon_time: Some(0), ..Default::default() });
let mut channel_bank = ChannelBank::new(cfg, mock);
let id: ChannelId = [0xFF; 16];
channel_bank.channel_queue.push_back(id);
let mut channel = Channel::new(id, BlockInfo::default());
channel
.add_frame(
Frame { id, number: 0, data: b"seven__".to_vec(), is_last: false },
BlockInfo::default(),
)
.unwrap();
channel
.add_frame(
Frame { id, number: 1, data: b"seven__".to_vec(), is_last: false },
BlockInfo::default(),
)
.unwrap();
channel
.add_frame(
Frame { id, number: 2, data: b"seven__".to_vec(), is_last: true },
BlockInfo::default(),
)
.unwrap();
assert!(channel.is_ready());
channel_bank.channels.insert([0xFF; 16], channel);
let frame_data = channel_bank.read().unwrap();
assert_eq!(
frame_data,
Some(alloy_primitives::bytes!("736576656e5f5f736576656e5f5f736576656e5f5f"))
);
}

#[test]
fn test_ingest_empty_origin() {
let mut mock = MockChannelBankProvider::new(vec![]);
Expand Down

0 comments on commit 19139b8

Please sign in to comment.