Rename tests and function name.

This commit is contained in:
Yuke Peng
2023-02-27 18:41:52 +08:00
committed by Tate, Hongliang Tian
parent e4323f808c
commit df12904c5a
18 changed files with 89 additions and 85 deletions

View File

@ -58,16 +58,18 @@ pub fn init_component(_: TokenStream, input: TokenStream) -> proc_macro::TokenSt
/// Automatically generate all component information required by the component system.
///
/// It is often used with `component::init`.
/// It mainly uses the output of the command `cargo metadata` to automatically generate information about all components, and also checks whether `Components.toml` contains all the components.
///
/// It is often used with `component::init_all`.
///
/// Example:
///
/// ```rust
/// component::init(component::component_generate!());
/// component::init_all(component::parse_metadata!());
/// ```
///
#[proc_macro]
pub fn generate_information(_: TokenStream) -> proc_macro::TokenStream {
pub fn parse_metadata(_: TokenStream) -> proc_macro::TokenStream {
let out = priority::component_generate();
let path = priority::get_component_toml_path();
quote! {

View File

@ -11,7 +11,7 @@ Registering a crate as component by marking a function in the lib.rs with `#[ini
### Component initialization
Component system need to be initialized by calling `componet::init` function and it needs information about all components. Usually it is used with the `component::generate_information` macro.
Component system need to be initialized by calling `componet::init_all` function and it needs information about all components. Usually it is used with the `component::parse_metadata` macro.
## Example
@ -45,13 +45,13 @@ fn init() -> Result<(), component::ComponentInitError> {
}
fn main(){
component::init(component::generate_information!()).unwrap();
component::init_all(component::parse_metadata!()).unwrap();
assert_eq!(INIT_COUNT.load(Relaxed),2);
}
```
## Notes
- Currently, initialization requires the presence of a `Components.toml` file, which stores some information about components and access control. The [tests](tests/kernel/Components.toml) provides a sample file of it. If the components declared inside `Components.toml` is inconsistent with the component found by `generate_information` macro (i.e. A crate depends on the component library but is not declared in `Components.toml`), then a compilation error will occur.
- Currently, initialization requires the presence of a `Components.toml` file, which stores some information about components and access control. The [tests](tests/kernel/Components.toml) provides a sample file of it. If the components declared inside `Components.toml` is inconsistent with the component found by `parse_metadata` macro (i.e. A crate depends on the component library but is not declared in `Components.toml`), then a compilation error will occur.
- The `generate_information` macro will generate the information of all components. But ultimately which functions are called still depends on which `#[init_component]` macros are extended. If you want to test a component. Then, other components with a lower priority than it or other unused high-priority components will not be initialized at runtime.
- The `parse_metadata` macro will generate the information of all components. But ultimately which functions are called still depends on which `#[init_component]` macros are extended. If you want to test a component. Then, other components with a lower priority than it or other unused high-priority components will not be initialized at runtime.

View File

@ -102,13 +102,13 @@ pub enum ComponentSystemInitError {
/// Component system initialization. It will collect invoke all functions that are marked by init_component based on dependencies between crates.
///
/// The collection of ComponentInfo usually generate by `component_generate` macro.
/// The collection of ComponentInfo usually generate by `parse_metadata` macro.
///
/// ```rust
/// component::init(component::component_generate!());
/// component::init_all(component::parse_metadata!());
/// ```
///
pub fn init(components: Vec<ComponentInfo>) -> Result<(), ComponentSystemInitError> {
pub fn init_all(components: Vec<ComponentInfo>) -> Result<(), ComponentSystemInitError> {
let components_info = parse_input(components);
match_and_call(components_info)?;
Ok(())

View File

@ -1,5 +1,5 @@
[package]
name = "kernel"
name = "init-order"
version = "0.1.0"
edition = "2021"
@ -7,11 +7,14 @@ edition = "2021"
[dependencies]
component = {path="../../../component"}
foo = {path="foo"}
bar = {path="bar"}
first-init = {path="first-init"}
second-init = {path="second-init"}
simple_logger = "4.0.0"
log = "0.4"
[workspace]
foo = {path="foo"}
bar = {path="bar"}
members = [
"first-init",
"second-init"
]

View File

@ -0,0 +1,5 @@
# template
[components]
init-order = {name = "init-order"}
first-init = {name = "first-init"}
second-init = {name = "second-init"}

View File

@ -1,5 +1,5 @@
[package]
name = "bar"
name = "first-init"
version = "0.1.0"
edition = "2021"

View File

@ -0,0 +1,13 @@
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::Relaxed;
use component::init_component;
pub static HAS_INIT: AtomicBool = AtomicBool::new(false);
#[init_component]
fn bar_init() -> Result<(), component::ComponentInitError> {
assert_eq!(HAS_INIT.load(Relaxed), false);
HAS_INIT.store(true, Relaxed);
Ok(())
}

View File

@ -1,5 +1,5 @@
[package]
name = "foo"
name = "second-init"
version = "0.1.0"
edition = "2021"
@ -7,7 +7,7 @@ edition = "2021"
[dependencies]
component = {path="../../../../component"}
bar = {path = "../bar"}
first-init = {path = "../first-init"}
log = "0.4"
[dev-dependencies]

View File

@ -0,0 +1,13 @@
use std::sync::atomic::{Ordering::Relaxed, AtomicBool};
use component::init_component;
pub static HAS_INIT: AtomicBool = AtomicBool::new(false);
#[init_component]
fn foo_init() -> Result<(), component::ComponentInitError> {
assert_eq!(first_init::HAS_INIT.load(Relaxed), true);
assert_eq!(HAS_INIT.load(Relaxed), false);
HAS_INIT.store(true, Relaxed);
Ok(())
}

View File

@ -0,0 +1,9 @@
use second_init::HAS_INIT;
use std::sync::atomic::Ordering::Relaxed;
#[test]
fn test() {
simple_logger::init_with_level(log::Level::Debug).unwrap();
component::init_all(component::parse_metadata!()).unwrap();
assert_eq!(HAS_INIT.load(Relaxed), true);
}

View File

@ -0,0 +1,22 @@
use std::sync::atomic::{Ordering::Relaxed, AtomicBool};
use component::init_component;
static HAS_INIT: AtomicBool = AtomicBool::new(false);
#[init_component]
fn kernel_init() -> Result<(), component::ComponentInitError> {
assert_eq!(first_init::HAS_INIT.load(Relaxed), true);
assert_eq!(second_init::HAS_INIT.load(Relaxed), true);
assert_eq!(HAS_INIT.load(Relaxed), false);
HAS_INIT.store(true, Relaxed);
Ok(())
}
fn main() {
simple_logger::init_with_level(log::Level::Info).unwrap();
component::init_all(component::parse_metadata!()).unwrap();
assert_eq!(first_init::HAS_INIT.load(Relaxed), true);
assert_eq!(second_init::HAS_INIT.load(Relaxed), true);
assert_eq!(HAS_INIT.load(Relaxed), true);
}

View File

@ -1,17 +1,15 @@
use bar::INIT_COUNT;
use first_init::HAS_INIT;
use component::init_component;
use std::sync::atomic::Ordering::Relaxed;
#[init_component]
fn kernel_init() -> Result<(), component::ComponentInitError> {
assert_eq!(INIT_COUNT.load(Relaxed), 1);
INIT_COUNT.fetch_add(1, Relaxed);
Ok(())
}
#[test]
fn test() {
simple_logger::init_with_level(log::Level::Debug).unwrap();
component::init(component::generate_information!()).unwrap();
assert_eq!(INIT_COUNT.load(Relaxed), 2);
component::init_all(component::parse_metadata!()).unwrap();
assert_eq!(HAS_INIT.load(Relaxed), true);
}

View File

@ -1,5 +0,0 @@
# template
[components]
kernel = {name = "kernel"}
foo = {name = "foo"}
bar = {name = "bar"}

View File

@ -1,13 +0,0 @@
use std::sync::atomic::AtomicU16;
use std::sync::atomic::Ordering::Relaxed;
use component::init_component;
pub static INIT_COUNT: AtomicU16 = AtomicU16::new(0);
#[init_component]
fn bar_init() -> Result<(), component::ComponentInitError> {
assert_eq!(INIT_COUNT.load(Relaxed), 0);
INIT_COUNT.fetch_add(1, Relaxed);
Ok(())
}

View File

@ -1,15 +0,0 @@
use std::sync::atomic::Ordering::Relaxed;
use std::sync::Once;
use bar::INIT_COUNT;
use component::init_component;
pub static FOO_VALUE: Once = Once::new();
#[init_component]
fn foo_init() -> Result<(), component::ComponentInitError> {
assert_eq!(INIT_COUNT.load(Relaxed), 1);
INIT_COUNT.fetch_add(1, Relaxed);
FOO_VALUE.call_once(|| {});
Ok(())
}

View File

@ -1,9 +0,0 @@
use bar::INIT_COUNT;
use std::sync::atomic::Ordering::Relaxed;
#[test]
fn test() {
simple_logger::init_with_level(log::Level::Debug).unwrap();
component::init(component::generate_information!()).unwrap();
assert_eq!(INIT_COUNT.load(Relaxed), 1);
}

View File

@ -1,19 +0,0 @@
use std::sync::atomic::Ordering::Relaxed;
use bar::INIT_COUNT;
use component::init_component;
use foo::FOO_VALUE;
#[init_component]
fn kernel_init() -> Result<(), component::ComponentInitError> {
assert_eq!(INIT_COUNT.load(Relaxed), 2);
assert!(FOO_VALUE.is_completed());
INIT_COUNT.fetch_add(1, Relaxed);
Ok(())
}
fn main() {
simple_logger::init_with_level(log::Level::Info).unwrap();
component::init(component::generate_information!()).unwrap();
assert_eq!(INIT_COUNT.load(Relaxed), 3);
}

View File

@ -17,7 +17,7 @@ fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
test_main();
jinux_frame::init(boot_info);
println!("[kernel] finish init jinux_frame");
component::init(component::generate_information!()).unwrap();
component::init_all(component::parse_metadata!()).unwrap();
jinux_std::init();
jinux_std::run_first_process();
}