Skip to content

Commit 3477087

Browse files
HoshimiPCopilot
andauthored
ci: add ci & unit tests (Starry-OS#1)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 1fbfca4 commit 3477087

2 files changed

Lines changed: 136 additions & 0 deletions

File tree

.github/workflows/ci.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: CI
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
check:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v4
10+
- uses: dtolnay/rust-toolchain@nightly
11+
with:
12+
components: clippy
13+
- name: Clippy
14+
run: cargo clippy --all-features --all-targets -- -Dwarnings
15+
- name: Test
16+
run: cargo test --all-features
17+
doc:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@v4
21+
- uses: dtolnay/rust-toolchain@nightly
22+
- name: Build docs
23+
run: |
24+
cargo doc --all-features --no-deps
25+
printf '<meta http-equiv="refresh" content="0;url=%s/index.html">' $(cargo tree | head -1 | cut -d' ' -f1 | tr '-' '_') > target/doc/index.html
26+
- name: Upload artifact
27+
uses: actions/upload-pages-artifact@v3
28+
with:
29+
path: target/doc
30+
31+
deploy:
32+
runs-on: ubuntu-latest
33+
needs: doc
34+
if: ${{ github.ref == format('refs/heads/{0}', github.event.repository.default_branch) }}
35+
permissions:
36+
contents: read
37+
pages: write
38+
id-token: write
39+
environment:
40+
name: github-pages
41+
steps:
42+
- name: Deploy to GitHub Pages
43+
id: deployment
44+
uses: actions/deploy-pages@v4

tests/tests.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
use std::{
2+
sync::{
3+
Arc,
4+
atomic::{AtomicUsize, Ordering},
5+
},
6+
task::{Context, Wake, Waker},
7+
};
8+
9+
use axpoll::PollSet;
10+
11+
struct Counter(AtomicUsize);
12+
13+
impl Counter {
14+
fn new() -> Arc<Self> {
15+
Arc::new(Self(AtomicUsize::new(0)))
16+
}
17+
18+
fn count(&self) -> usize {
19+
self.0.load(Ordering::SeqCst)
20+
}
21+
22+
fn add(&self) {
23+
self.0.fetch_add(1, Ordering::SeqCst);
24+
}
25+
}
26+
27+
impl Wake for Counter {
28+
fn wake(self: Arc<Self>) {
29+
self.add();
30+
}
31+
32+
fn wake_by_ref(self: &Arc<Self>) {
33+
self.add();
34+
}
35+
}
36+
37+
#[test]
38+
fn register_and_wake() {
39+
let ps = PollSet::new();
40+
let counter = Counter::new();
41+
let w = Waker::from(counter.clone());
42+
ps.register(&w);
43+
assert_eq!(ps.wake(), 1);
44+
assert_eq!(counter.count(), 1);
45+
}
46+
47+
#[test]
48+
fn empty_return() {
49+
let ps = PollSet::new();
50+
assert_eq!(ps.wake(), 0);
51+
}
52+
53+
#[test]
54+
fn full_capacity() {
55+
let ps = PollSet::new();
56+
let counter = Counter::new();
57+
for _ in 0..64 {
58+
let w = Waker::from(counter.clone());
59+
let cx = Context::from_waker(&w);
60+
ps.register(cx.waker());
61+
}
62+
let woke = ps.wake();
63+
assert_eq!(woke, 64);
64+
assert_eq!(counter.count(), 64);
65+
}
66+
67+
#[test]
68+
fn overwrite() {
69+
let ps = PollSet::new();
70+
let counters = (0..65).map(|_| Counter::new()).collect::<Vec<_>>();
71+
for c in &counters {
72+
let w = Waker::from(c.clone());
73+
let cx = Context::from_waker(&w);
74+
ps.register(cx.waker());
75+
}
76+
assert_eq!(ps.wake(), 64);
77+
let total: usize = counters.iter().map(|c| c.count()).sum();
78+
assert_eq!(total, 65);
79+
}
80+
81+
#[test]
82+
fn drop_wakes() {
83+
let ps = PollSet::new();
84+
let counters = Counter::new();
85+
for _ in 0..10 {
86+
let w = Waker::from(counters.clone());
87+
let cx = Context::from_waker(&w);
88+
ps.register(cx.waker());
89+
}
90+
drop(ps);
91+
assert_eq!(counters.count(), 10);
92+
}

0 commit comments

Comments
 (0)