Skip to content

Commit e9c1f04

Browse files
notgulljbr
andcommitted
tests: Add further testing
Co-authored-by: Jacob Rothstein <[email protected]> Signed-off-by: John Nunley <[email protected]>
1 parent 434b390 commit e9c1f04

File tree

8 files changed

+260
-76
lines changed

8 files changed

+260
-76
lines changed

Diff for: .github/workflows/ci.yml

+70-20
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ on:
1313

1414
env:
1515
CARGO_INCREMENTAL: 0
16+
CARGO_NET_GIT_FETCH_WITH_CLI: true
1617
CARGO_NET_RETRY: 10
1718
CARGO_TERM_COLOR: always
1819
RUST_BACKTRACE: 1
@@ -26,42 +27,80 @@ defaults:
2627

2728
jobs:
2829
test:
29-
runs-on: ${{ matrix.os }}
30+
runs-on: ubuntu-latest
3031
strategy:
3132
fail-fast: false
3233
matrix:
33-
os: [ubuntu-latest]
34-
rust: [nightly, beta, stable]
34+
include:
35+
- rust: stable
36+
- rust: beta
37+
- rust: nightly
38+
- rust: nightly
39+
target: i686-unknown-linux-gnu
3540
steps:
3641
- uses: actions/checkout@v4
3742
- name: Install Rust
3843
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
44+
- name: Install cross-compilation tools
45+
uses: taiki-e/setup-cross-toolchain-action@v1
46+
with:
47+
target: ${{ matrix.target }}
48+
if: matrix.target != ''
3949
- run: cargo build --all --all-features --all-targets
50+
- run: cargo test --all
51+
- run: cargo test --all --release
52+
- run: cargo test --no-default-features --tests
53+
- run: cargo test --no-default-features --tests --release
54+
- name: Install cargo-hack
55+
uses: taiki-e/install-action@cargo-hack
56+
- run: rustup target add thumbv7m-none-eabi
4057
- name: Run cargo check (without dev-dependencies to catch missing feature flags)
41-
if: startsWith(matrix.rust, 'nightly')
42-
run: cargo check -Z features=dev_dep
43-
- run: cargo test
58+
run: cargo hack build --all --no-dev-deps
59+
- run: cargo hack build --all --target thumbv7m-none-eabi --no-default-features --no-dev-deps
60+
- run: cargo hack build --target thumbv7m-none-eabi --no-default-features --no-dev-deps --features portable-atomic
61+
- name: Install wasm-pack
62+
uses: taiki-e/install-action@wasm-pack
63+
- run: wasm-pack test --node
64+
- run: wasm-pack test --node --no-default-features
65+
- run: wasm-pack test --node --no-default-features --features portable-atomic
66+
- name: Clone some dependent crates
67+
run: |
68+
git clone https://github.com/smol-rs/event-listener-strategy.git
69+
git clone https://github.com/smol-rs/async-channel.git
70+
git clone https://github.com/smol-rs/async-lock.git
71+
- name: Patch dependent crates
72+
run: |
73+
echo '[patch.crates-io]' >> event-listener-strategy/Cargo.toml
74+
echo 'event-listener = { path = ".." }' >> event-listener-strategy/Cargo.toml
75+
echo '[patch.crates-io]' >> async-channel/Cargo.toml
76+
echo 'event-listener = { path = ".." }' >> async-channel/Cargo.toml
77+
echo 'event-listener-strategy = { path = "../event-listener-strategy" }' >> async-channel/Cargo.toml
78+
echo '[patch.crates-io]' >> async-lock/Cargo.toml
79+
echo 'event-listener = { path = ".." }' >> async-lock/Cargo.toml
80+
echo 'event-listener-strategy = { path = "../event-listener-strategy" }' >> async-lock/Cargo.toml
81+
echo 'async-channel = { path = "../async-channel" }' >> async-lock/Cargo.toml
82+
- name: Test dependent crates
83+
run: |
84+
cargo test --manifest-path=event-listener-strategy/Cargo.toml
85+
cargo test --manifest-path=async-channel/Cargo.toml
86+
cargo test --manifest-path=async-lock/Cargo.toml
4487
4588
msrv:
4689
runs-on: ubuntu-latest
47-
strategy:
48-
matrix:
49-
# When updating this, the reminder to update the minimum supported
50-
# Rust version in Cargo.toml.
51-
rust: ['1.39']
5290
steps:
5391
- uses: actions/checkout@v4
54-
- name: Install Rust
55-
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
56-
- run: cargo build
92+
- name: Install cargo-hack
93+
uses: taiki-e/install-action@cargo-hack
94+
- run: cargo hack build --all --rust-version
95+
- run: cargo hack build --all --no-default-features --rust-version
5796

5897
clippy:
5998
runs-on: ubuntu-latest
6099
steps:
61100
- uses: actions/checkout@v4
62101
- name: Install Rust
63102
run: rustup update stable
64-
- run: cargo clippy --all-features --all-targets
103+
- run: cargo clippy --all --all-features --all-targets
65104

66105
fmt:
67106
runs-on: ubuntu-latest
@@ -77,10 +116,12 @@ jobs:
77116
- uses: actions/checkout@v4
78117
- name: Install Rust
79118
run: rustup toolchain install nightly --component miri && rustup default nightly
80-
- run: cargo miri test
81-
env:
82-
MIRIFLAGS: -Zmiri-strict-provenance -Zmiri-symbolic-alignment-check -Zmiri-disable-isolation
83-
RUSTFLAGS: ${{ env.RUSTFLAGS }} -Z randomize-layout
119+
- run: |
120+
echo "MIRIFLAGS=-Zmiri-strict-provenance -Zmiri-symbolic-alignment-check -Zmiri-disable-isolation" >>"${GITHUB_ENV}"
121+
echo "RUSTFLAGS=${RUSTFLAGS} -Z randomize-layout" >>"${GITHUB_ENV}"
122+
- run: cargo miri test --all
123+
- run: cargo miri test --no-default-features --tests
124+
- run: cargo miri test --no-default-features --features portable-atomic --tests
84125

85126
security_audit:
86127
permissions:
@@ -89,8 +130,17 @@ jobs:
89130
issues: write
90131
runs-on: ubuntu-latest
91132
steps:
92-
- uses: actions/checkout@v3
133+
- uses: actions/checkout@v4
93134
# https://github.com/rustsec/audit-check/issues/2
94135
- uses: rustsec/audit-check@master
95136
with:
96137
token: ${{ secrets.GITHUB_TOKEN }}
138+
139+
loom:
140+
runs-on: ubuntu-latest
141+
steps:
142+
- uses: actions/checkout@v4
143+
- name: Install Rust
144+
run: rustup update stable
145+
- name: Loom tests
146+
run: RUSTFLAGS="--cfg=loom" cargo test --release --test loom --features loom

Diff for: Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ name = "event-listener"
66
version = "5.3.0"
77
authors = ["Stjepan Glavina <[email protected]>"]
88
edition = "2021"
9-
rust-version = "1.56"
9+
rust-version = "1.63"
1010
description = "Notify async tasks or threads"
1111
license = "Apache-2.0 OR MIT"
1212
repository = "https://github.com/smol-rs/event-listener"
@@ -21,7 +21,7 @@ portable-atomic = ["portable-atomic-crate", "portable-atomic-util"]
2121

2222
[dependencies]
2323
portable-atomic-crate = { version = "1.6.0", package = "portable-atomic", optional = true }
24-
portable-atomic-util = { version = "0.1.5", optional = true }
24+
portable-atomic-util = { version = "0.1.5", optional = true, features = ["alloc"] }
2525

2626
[dev-dependencies]
2727
futures = { version = "0.3", default-features = false, features = ["std"] }
@@ -32,6 +32,7 @@ waker-fn = "1"
3232
[[bench]]
3333
name = "bench"
3434
harness = false
35+
required-features = ["std"]
3536

3637
[lib]
3738
bench = false

Diff for: benches/bench.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
use criterion::{criterion_group, criterion_main, Criterion};
22
use event_listener::{Event, Listener};
3+
use std::iter;
34

45
const COUNT: usize = 8000;
56

67
fn bench_events(c: &mut Criterion) {
78
c.bench_function("notify_and_wait", |b| {
89
let ev = Event::new();
10+
let mut handles = Vec::with_capacity(COUNT);
911
b.iter(|| {
10-
let mut handles = Vec::with_capacity(COUNT);
11-
12-
for _ in 0..COUNT {
13-
handles.push(ev.listen());
14-
}
15-
12+
handles.extend(iter::repeat_with(|| ev.listen()).take(COUNT));
1613
ev.notify(COUNT);
1714

18-
for handle in handles {
15+
for handle in handles.drain(..) {
1916
handle.wait();
2017
}
2118
});

Diff for: examples/mutex.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ impl<T> Mutex<T> {
9090
}
9191
Some(l) => {
9292
// Wait until a notification is received.
93-
if l.wait_deadline(deadline).is_none() {
94-
return None;
95-
}
93+
l.wait_deadline(deadline)?;
9694
}
9795
}
9896
}

Diff for: src/lib.rs

+31-30
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@
7575
)]
7676
#![warn(missing_docs, missing_debug_implementations, rust_2018_idioms)]
7777

78-
use loom::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
79-
use loom::Arc;
78+
use crate::loom::atomic::{self, AtomicPtr, AtomicUsize, Ordering};
79+
use crate::loom::Arc;
8080
use notify::{GenericNotify, Internal, NotificationPrivate};
8181

8282
use std::cell::{Cell, UnsafeCell};
@@ -91,7 +91,6 @@ use std::sync::{Mutex, MutexGuard, TryLockError};
9191
use std::task::{Context, Poll, Waker};
9292
use std::thread::{self, Thread};
9393
use std::time::{Duration, Instant};
94-
use std::usize;
9594

9695
mod notify;
9796
pub use notify::{IntoNotification, Notification};
@@ -385,24 +384,6 @@ impl Event {
385384
pub fn notify_additional_relaxed(&self, n: usize) -> usize {
386385
self.notify(n.additional().relaxed())
387386
}
388-
}
389-
390-
impl<T> Event<T> {
391-
/// Creates a new [`Event`] with a tag.
392-
///
393-
/// # Examples
394-
///
395-
/// ```
396-
/// use event_listener::Event;
397-
///
398-
/// let event = Event::<usize>::with_tag();
399-
/// ```
400-
#[inline]
401-
pub const fn with_tag() -> Event<T> {
402-
Event {
403-
inner: AtomicPtr::new(ptr::null_mut()),
404-
}
405-
}
406387

407388
/// Return the listener count by acquiring a lock.
408389
///
@@ -430,15 +411,32 @@ impl<T> Event<T> {
430411
/// drop(listener2);
431412
/// assert_eq!(event.total_listeners(), 0);
432413
/// ```
433-
#[cfg(feature = "std")]
434414
#[inline]
435415
pub fn total_listeners(&self) -> usize {
436416
if let Some(inner) = self.try_inner() {
437-
inner.total_listeners()
417+
inner.lock().len
438418
} else {
439419
0
440420
}
441421
}
422+
}
423+
424+
impl<T> Event<T> {
425+
/// Creates a new [`Event`] with a tag.
426+
///
427+
/// # Examples
428+
///
429+
/// ```
430+
/// use event_listener::Event;
431+
///
432+
/// let event = Event::<usize>::with_tag();
433+
/// ```
434+
#[inline]
435+
pub const fn with_tag() -> Event<T> {
436+
Event {
437+
inner: AtomicPtr::new(ptr::null_mut()),
438+
}
439+
}
442440

443441
/// Returns a guard listening for a notification.
444442
///
@@ -504,7 +502,7 @@ impl<T> Event<T> {
504502
if let Some(inner) = self.try_inner() {
505503
let limit = if notify.is_additional(Internal::new()) {
506504
// Notify if there is at least one unnotified listener.
507-
std::usize::MAX
505+
usize::MAX
508506
} else {
509507
// Notify if there is at least one unnotified listener and the number of notified
510508
// listeners is less than `n`.
@@ -535,7 +533,8 @@ impl<T> Event<T> {
535533
/// ```
536534
#[inline]
537535
pub fn is_notified(&self) -> bool {
538-
self.try_inner().map_or(false, |inner| inner.notified.load(Ordering::Acquire) > 0)
536+
self.try_inner()
537+
.map_or(false, |inner| inner.notified.load(Ordering::Acquire) > 0)
539538
}
540539

541540
/// Returns a reference to the inner state if it was initialized.
@@ -614,11 +613,13 @@ impl<T> Drop for Event<T> {
614613
impl fmt::Debug for Event {
615614
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
616615
let inner = match self.try_inner() {
617-
None => return f
618-
.debug_tuple("Event")
619-
.field(&format_args!("<uninitialized>"))
620-
.finish(),
621-
Some(inner) => inner
616+
None => {
617+
return f
618+
.debug_tuple("Event")
619+
.field(&format_args!("<uninitialized>"))
620+
.finish()
621+
}
622+
Some(inner) => inner,
622623
};
623624

624625
let guard = match inner.list.try_lock() {

0 commit comments

Comments
 (0)