Skip to content

Commit

Permalink
test(node): add basic unit tests for ResourceCollection (matter-labs#…
Browse files Browse the repository at this point in the history
…1054)

## What ❔

Created 3 unit tests for the `ResourceCollection` component. This PR is
second in the range. After, it's expected to add unit test coverage for
other components in the `node_framework`.

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [x] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [x] Code has been formatted via `zk fmt` and `zk lint`.
- [x] Spellcheck has been run via `zk spellcheck`.
- [x] Linkcheck has been run via `zk linkcheck`.
  • Loading branch information
AnastasiiaVashchuk authored Feb 16, 2024
1 parent 4ea1520 commit b4663c3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/node/node_framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ tokio = { version = "1", features = ["rt"] }
[dev-dependencies]
zksync_env_config = { path = "../../lib/env_config" }
vlog = { path = "../../lib/vlog" }
assert_matches = "1.5.0"
60 changes: 60 additions & 0 deletions core/node/node_framework/src/resource/resource_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,63 @@ impl<T: Resource + Clone> ResourceCollection<T> {
(*handle).clone()
}
}

#[cfg(test)]
mod tests {
use assert_matches::assert_matches;
use futures::FutureExt;

use super::*;

#[derive(Debug, Clone, PartialEq)]
struct TestResource(Arc<u8>);

impl Resource for TestResource {
fn resource_id() -> ResourceId {
ResourceId::new("test_resource")
}
}

#[test]
fn test_push() {
let collection = ResourceCollection::<TestResource>::new();
let resource1 = TestResource(Arc::new(1));
collection.clone().push(resource1.clone()).unwrap();

let resource2 = TestResource(Arc::new(2));
collection.clone().push(resource2.clone()).unwrap();

assert_eq!(
*collection.resources.lock().unwrap(),
vec![resource1, resource2]
);
}

#[test]
fn test_already_wired() {
let mut collection = ResourceCollection::<TestResource>::new();
let resource = TestResource(Arc::new(1));

let rc_clone = collection.clone();

collection.on_resource_wired();

assert_matches!(
rc_clone.push(resource),
Err(ResourceCollectionError::AlreadyWired)
);
}

#[test]
fn test_resolve() {
let mut collection = ResourceCollection::<TestResource>::new();
let result = collection.clone().resolve().now_or_never();

assert!(result.is_none());

collection.on_resource_wired();

let resolved = collection.resolve().now_or_never();
assert_eq!(resolved.unwrap(), vec![]);
}
}

0 comments on commit b4663c3

Please sign in to comment.