Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(derive): Adds more test coverage to Stages #673

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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