Skip to content

Commit

Permalink
test: Add tests for LifecycleManager (#1003)
Browse files Browse the repository at this point in the history
closes: #977
  • Loading branch information
morgsmccauley authored Aug 11, 2024
1 parent 4af58bc commit 89e3700
Show file tree
Hide file tree
Showing 5 changed files with 941 additions and 85 deletions.
30 changes: 21 additions & 9 deletions coordinator/src/handlers/block_streams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,19 @@ impl BlockStreamsClientWrapperImpl {
}
}

#[cfg(not(test))]
pub use BlockStreamsHandlerImpl as BlockStreamsHandler;
#[cfg(test)]
pub use MockBlockStreamsHandlerImpl as BlockStreamsHandler;

#[derive(Clone)]
pub struct BlockStreamsHandler {
pub struct BlockStreamsHandlerImpl {
client: BlockStreamsClientWrapper,
redis_client: RedisClient,
}

impl BlockStreamsHandler {
#[cfg_attr(test, mockall::automock)]
impl BlockStreamsHandlerImpl {
pub fn connect(block_streamer_url: &str, redis_client: RedisClient) -> anyhow::Result<Self> {
let channel = Channel::from_shared(block_streamer_url.to_string())
.context("Block Streamer URL is invalid")?
Expand Down Expand Up @@ -368,6 +374,12 @@ mod tests {
}
}

impl Clone for MockBlockStreamsHandlerImpl {
fn clone(&self) -> Self {
Self::default()
}
}

#[tokio::test]
async fn resumes_stopped_streams() {
let config = IndexerConfig::default();
Expand Down Expand Up @@ -401,7 +413,7 @@ mod tests {
.expect_get_last_published_block::<IndexerConfig>()
.returning(move |_| Ok(Some(last_published_block)));

let handler = BlockStreamsHandler {
let handler = BlockStreamsHandlerImpl {
client: mock_client,
redis_client: mock_redis,
};
Expand Down Expand Up @@ -463,7 +475,7 @@ mod tests {
.returning(|_| Ok(()))
.once();

let handler = BlockStreamsHandler {
let handler = BlockStreamsHandlerImpl {
client: mock_client,
redis_client: mock_redis,
};
Expand Down Expand Up @@ -507,7 +519,7 @@ mod tests {

let mock_redis = RedisClient::default();

let handler = BlockStreamsHandler {
let handler = BlockStreamsHandlerImpl {
client: mock_client,
redis_client: mock_redis,
};
Expand Down Expand Up @@ -551,7 +563,7 @@ mod tests {
.returning(|_| Ok(()))
.once();

let handler = BlockStreamsHandler {
let handler = BlockStreamsHandlerImpl {
client: mock_client,
redis_client: mock_redis,
};
Expand Down Expand Up @@ -617,7 +629,7 @@ mod tests {
.expect_get_last_published_block::<IndexerConfig>()
.returning(move |_| Ok(Some(last_published_block)));

let handler = BlockStreamsHandler {
let handler = BlockStreamsHandlerImpl {
client: mock_client,
redis_client: mock_redis,
};
Expand Down Expand Up @@ -672,7 +684,7 @@ mod tests {

let mock_redis = RedisClient::default();

let handler = BlockStreamsHandler {
let handler = BlockStreamsHandlerImpl {
client: mock_client,
redis_client: mock_redis,
};
Expand Down Expand Up @@ -723,7 +735,7 @@ mod tests {
.returning(|_| Ok(None))
.once();

let handler = BlockStreamsHandler {
let handler = BlockStreamsHandlerImpl {
client: mock_client,
redis_client: mock_redis,
};
Expand Down
28 changes: 20 additions & 8 deletions coordinator/src/handlers/data_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,18 @@ impl DataLayerClientWrapperImpl {
}
}

#[cfg(not(test))]
pub use DataLayerHandlerImpl as DataLayerHandler;
#[cfg(test)]
pub use MockDataLayerHandlerImpl as DataLayerHandler;

#[derive(Clone)]
pub struct DataLayerHandler {
pub struct DataLayerHandlerImpl {
client: DataLayerClientWrapper,
}

impl DataLayerHandler {
#[cfg_attr(test, mockall::automock)]
impl DataLayerHandlerImpl {
pub fn connect(runner_url: &str) -> anyhow::Result<Self> {
let channel = Channel::from_shared(runner_url.to_string())
.context("Runner URL is invalid")?
Expand Down Expand Up @@ -249,6 +255,12 @@ mod tests {

use mockall::predicate::*;

impl Clone for MockDataLayerHandlerImpl {
fn clone(&self) -> Self {
Self::default()
}
}

#[tokio::test]
async fn provisions_data_layer() {
let config = IndexerConfig::default();
Expand Down Expand Up @@ -290,7 +302,7 @@ mod tests {
})
.once();

let handler = DataLayerHandler {
let handler = DataLayerHandlerImpl {
client: mock_client,
};

Expand Down Expand Up @@ -329,7 +341,7 @@ mod tests {
})
.times(610);

let handler = DataLayerHandler {
let handler = DataLayerHandlerImpl {
client: mock_client,
};

Expand Down Expand Up @@ -371,7 +383,7 @@ mod tests {
})
.once();

let handler = DataLayerHandler {
let handler = DataLayerHandlerImpl {
client: mock_client,
};

Expand Down Expand Up @@ -423,7 +435,7 @@ mod tests {
})
.once();

let handler = DataLayerHandler {
let handler = DataLayerHandlerImpl {
client: mock_client,
};

Expand Down Expand Up @@ -464,7 +476,7 @@ mod tests {
})
.times(610);

let handler = DataLayerHandler {
let handler = DataLayerHandlerImpl {
client: mock_client,
};

Expand Down Expand Up @@ -507,7 +519,7 @@ mod tests {
})
.once();

let handler = DataLayerHandler {
let handler = DataLayerHandlerImpl {
client: mock_client,
};

Expand Down
24 changes: 18 additions & 6 deletions coordinator/src/handlers/executors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,18 @@ impl ExecutorsClientWrapperImpl {
}
}

#[cfg(not(test))]
pub use ExecutorsHandlerImpl as ExecutorsHandler;
#[cfg(test)]
pub use MockExecutorsHandlerImpl as ExecutorsHandler;

#[derive(Clone)]
pub struct ExecutorsHandler {
pub struct ExecutorsHandlerImpl {
client: ExecutorsClientWrapper,
}

impl ExecutorsHandler {
#[cfg_attr(test, mockall::automock)]
impl ExecutorsHandlerImpl {
pub fn connect(runner_url: &str) -> anyhow::Result<Self> {
let channel = Channel::from_shared(runner_url.to_string())
.context("Runner URL is invalid")?
Expand Down Expand Up @@ -225,6 +231,12 @@ mod tests {
}
}

impl Clone for MockExecutorsHandlerImpl {
fn clone(&self) -> Self {
Self::default()
}
}

#[tokio::test]
async fn resumes_stopped_executors() {
let config = IndexerConfig::default();
Expand Down Expand Up @@ -255,7 +267,7 @@ mod tests {
})
.once();

let handler = ExecutorsHandler {
let handler = ExecutorsHandlerImpl {
client: mock_client,
};

Expand Down Expand Up @@ -308,7 +320,7 @@ mod tests {
.returning(move |_| Ok(Response::new(executor.clone())))
.once();

let handler = ExecutorsHandler {
let handler = ExecutorsHandlerImpl {
client: mock_client,
};

Expand Down Expand Up @@ -365,7 +377,7 @@ mod tests {
.returning(move |_| Ok(Response::new(executor.clone())))
.once();

let handler = ExecutorsHandler {
let handler = ExecutorsHandlerImpl {
client: mock_client,
};

Expand Down Expand Up @@ -408,7 +420,7 @@ mod tests {
.with(always())
.returning(move |_| Ok(Response::new(executor.clone())));

let handler = ExecutorsHandler {
let handler = ExecutorsHandlerImpl {
client: mock_client,
};

Expand Down
2 changes: 1 addition & 1 deletion coordinator/src/indexer_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Default for IndexerConfig {
},
created_at_block_height: 1,
updated_at_block_height: Some(2),
deleted_at_block_height: Some(3),
deleted_at_block_height: None,
start_block: StartBlock::Height(100),
}
}
Expand Down
Loading

0 comments on commit 89e3700

Please sign in to comment.