-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
497 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
name: Run Tests for Channel | ||
|
||
on: | ||
workflow_call: | ||
secrets: | ||
cookie: | ||
required: true | ||
|
||
env: | ||
CARGO_TERM_COLOR: always | ||
|
||
jobs: | ||
produce_consume_single_task: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Run test produce_consume_single_task | ||
uses: ./.github/workflows/actions/run-test | ||
with: | ||
cookie: ${{ secrets.cookie }} | ||
category: sync | ||
sub-category: channel | ||
test-name: produce_consume_single_task | ||
|
||
produce_with_overflow: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Run test produce_with_overflow | ||
uses: ./.github/workflows/actions/run-test | ||
with: | ||
cookie: ${{ secrets.cookie }} | ||
category: sync | ||
sub-category: channel | ||
test-name: produce_with_overflow | ||
|
||
try_consume: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Run test try_consume | ||
uses: ./.github/workflows/actions/run-test | ||
with: | ||
cookie: ${{ secrets.cookie }} | ||
category: sync | ||
sub-category: channel | ||
test-name: try_consume | ||
|
||
multiple_producers: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Run test multiple_producers | ||
uses: ./.github/workflows/actions/run-test | ||
with: | ||
cookie: ${{ secrets.cookie }} | ||
category: sync | ||
sub-category: channel | ||
test-name: multiple_producers | ||
|
||
multiple_consumers: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Run test multiple_consumers | ||
uses: ./.github/workflows/actions/run-test | ||
with: | ||
cookie: ${{ secrets.cookie }} | ||
category: sync | ||
sub-category: channel | ||
test-name: multiple_consumers | ||
|
||
concurrency_and_stress: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v4 | ||
|
||
- name: Run test concurrency_and_stress | ||
uses: ./.github/workflows/actions/run-test | ||
with: | ||
cookie: ${{ secrets.cookie }} | ||
category: sync | ||
sub-category: channel | ||
test-name: concurrency_and_stress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
//! Tests the behavior of multiple producer tasks writing to a shared channel | ||
//! (This test is similar to the test "multiple_producers", but includes more tasks and elements produced on the channel) | ||
//! It creates four tasks, each producing a sequence of numbers, and verifies that all numbers are correctly | ||
//! produced and consumed in the expected order. | ||
#![no_main] | ||
#![no_std] | ||
|
||
extern crate alloc; | ||
use alloc::vec; | ||
use alloc::vec::Vec; | ||
use core::sync::atomic::{AtomicUsize, Ordering}; | ||
use hopter::{boot::main, debug::semihosting, hprintln, sync, sync::Producer, task}; | ||
|
||
const NUM_TASKS: usize = 4; | ||
const NUM_ITEMS: usize = 3; // Number of items each task will produce | ||
static TASK_COMPLETION_COUNTER: AtomicUsize = AtomicUsize::new(0); // Counter to track task completion | ||
|
||
#[main] | ||
fn main(_: cortex_m::Peripherals) { | ||
// Create a channel with a buffering capacity of 16 | ||
let (producer, consumer) = sync::create_channel::<usize, 16>(); | ||
|
||
// Create and spawn 4 producer tasks, each with different priorities and a cloned producer | ||
let mut producer2 = producer.clone(); | ||
task::build() | ||
.set_entry(move || fill_channel(&mut producer2, 2)) | ||
.set_priority(1) | ||
.spawn() | ||
.unwrap(); | ||
|
||
let mut producer3 = producer.clone(); | ||
task::build() | ||
.set_entry(move || fill_channel(&mut producer3, 3)) | ||
.set_priority(2) | ||
.spawn() | ||
.unwrap(); | ||
|
||
let mut producer4 = producer.clone(); | ||
task::build() | ||
.set_entry(move || fill_channel(&mut producer4, 4)) | ||
.set_priority(3) | ||
.spawn() | ||
.unwrap(); | ||
|
||
let mut producer5 = producer.clone(); | ||
task::build() | ||
.set_entry(move || fill_channel(&mut producer5, 5)) | ||
.set_priority(3) | ||
.spawn() | ||
.unwrap(); | ||
|
||
// Change the priority of the current task to allow producer tasks to run first | ||
task::change_current_priority(10).unwrap(); | ||
|
||
// Consume the numbers produced by the tasks, storing them in a vector | ||
let mut results = vec![]; | ||
for _ in 0..(NUM_TASKS * NUM_ITEMS) { | ||
results.push(consumer.consume()); // Consume and store each item produced by the tasks | ||
} | ||
|
||
// Create a vector of the expected results for comparison | ||
// compare_vec = [6..18] | ||
let compare_vec = (6..(NUM_TASKS * NUM_TASKS + 2)).collect::<Vec<_>>(); | ||
|
||
// Check if the produced results match the expected sequence | ||
if results != compare_vec { | ||
hprintln!("Test Failed"); | ||
semihosting::terminate(false); | ||
} | ||
hprintln!("Test Passed"); | ||
semihosting::terminate(true); | ||
} | ||
|
||
fn fill_channel(producer: &mut Producer<usize, 16>, task_num: usize) { | ||
// Each task produces a sequence of 3 (NUM_ITEMS) numbers based on its task number | ||
for j in 0..NUM_ITEMS { | ||
// Conditionally yield to add more stress. | ||
if j == task_num { | ||
task::yield_current(); | ||
} | ||
producer.produce(task_num * NUM_ITEMS + j); | ||
} | ||
|
||
TASK_COMPLETION_COUNTER.fetch_add(1, Ordering::SeqCst); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Test Passed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//! Tests the correct behavior of a multi-consumer, single-producer channel | ||
//! It fills a channel with four elements, spawns two consumer tasks with different priorities to consume the elements, | ||
//! and then checks that the channel is empty after all elements have been consumed. | ||
#![no_main] | ||
#![no_std] | ||
|
||
extern crate alloc; | ||
use hopter::{boot::main, debug::semihosting, hprintln, sync, sync::Consumer, task}; | ||
|
||
#[main] | ||
fn main(_: cortex_m::Peripherals) { | ||
// create a channel with a buffer capacity of 4 | ||
let (producer, consumer) = sync::create_channel::<usize, 4>(); | ||
|
||
// fill channel with 4 elements | ||
producer.produce(1); | ||
producer.produce(2); | ||
producer.produce(3); | ||
producer.produce(4); | ||
// Clone the consumer to allow multiple consumers to pull data from the channel | ||
let mut consumer2 = consumer.clone(); | ||
let mut consumer3 = consumer.clone(); | ||
|
||
// Spawn the first consumer task with priority 1 | ||
task::build() | ||
.set_entry(move || consume_function(&mut consumer2)) | ||
.set_priority(1) | ||
.spawn() | ||
.unwrap(); | ||
// Spawn the second consumer task with priority 2 | ||
task::build() | ||
.set_entry(move || consume_function(&mut consumer3)) | ||
.set_priority(2) | ||
.spawn() | ||
.unwrap(); | ||
|
||
// Change the priority of the current task to 10 | ||
// This ensures that both consumer tasks run before the final check | ||
task::change_current_priority(10).unwrap(); | ||
|
||
// Check if the channel is empty after both consumers have finished | ||
if consumer.try_consume_allow_isr() != None { | ||
hprintln!("Channel not empty"); | ||
semihosting::terminate(false); | ||
} | ||
hprintln!("Test Passed"); | ||
semihosting::terminate(true); | ||
} | ||
|
||
fn consume_function(consumer: &mut Consumer<usize, 4>) { | ||
hprintln!("{}", consumer.consume()); | ||
hprintln!("{}", consumer.consume()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
1 | ||
2 | ||
3 | ||
4 | ||
Test Passed |
Oops, something went wrong.