Skip to content

Commit 62ce8fe

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 62ce8fe

File tree

8 files changed

+228
-76
lines changed

8 files changed

+228
-76
lines changed

Diff for: .github/workflows/ci.yml

+38-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,57 @@ 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+
- name: Install wasm-pack
60+
uses: taiki-e/install-action@wasm-pack
61+
- run: wasm-pack test --node
62+
- run: wasm-pack test --node --no-default-features
63+
- run: wasm-pack test --node --no-default-features --features portable-atomic
4464

4565
msrv:
4666
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']
5267
steps:
5368
- uses: actions/checkout@v4
54-
- name: Install Rust
55-
run: rustup update ${{ matrix.rust }} && rustup default ${{ matrix.rust }}
56-
- run: cargo build
69+
- name: Install cargo-hack
70+
uses: taiki-e/install-action@cargo-hack
71+
- run: cargo hack build --all --rust-version
72+
- run: cargo hack build --all --no-default-features --rust-version
5773

5874
clippy:
5975
runs-on: ubuntu-latest
6076
steps:
6177
- uses: actions/checkout@v4
6278
- name: Install Rust
6379
run: rustup update stable
64-
- run: cargo clippy --all-features --all-targets
80+
- run: cargo clippy --all --all-features --all-targets
6581

6682
fmt:
6783
runs-on: ubuntu-latest
@@ -77,10 +93,12 @@ jobs:
7793
- uses: actions/checkout@v4
7894
- name: Install Rust
7995
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
96+
- run: |
97+
echo "MIRIFLAGS=-Zmiri-strict-provenance -Zmiri-symbolic-alignment-check -Zmiri-disable-isolation" >>"${GITHUB_ENV}"
98+
echo "RUSTFLAGS=${RUSTFLAGS} -Z randomize-layout" >>"${GITHUB_ENV}"
99+
- run: cargo miri test --all
100+
- run: cargo miri test --no-default-features --tests
101+
- run: cargo miri test --no-default-features --features portable-atomic --tests
84102

85103
security_audit:
86104
permissions:
@@ -89,7 +107,7 @@ jobs:
89107
issues: write
90108
runs-on: ubuntu-latest
91109
steps:
92-
- uses: actions/checkout@v3
110+
- uses: actions/checkout@v4
93111
# https://github.com/rustsec/audit-check/issues/2
94112
- uses: rustsec/audit-check@master
95113
with:

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() {

Diff for: src/notify.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! The `Notification` trait for specifying notification.
22
3-
#[cfg(feature = "std")]
43
use core::fmt;
54

65
use crate::loom::atomic::{self, Ordering};
@@ -161,15 +160,13 @@ where
161160
}
162161

163162
/// Use a tag to notify listeners.
164-
#[cfg(feature = "std")]
165163
#[derive(Debug, Clone)]
166164
#[doc(hidden)]
167165
pub struct Tag<N: ?Sized, T> {
168166
tag: T,
169167
inner: N,
170168
}
171169

172-
#[cfg(feature = "std")]
173170
impl<N: ?Sized, T> Tag<N, T> {
174171
/// Create a new `Tag` with the given tag and notification.
175172
fn new(tag: T, inner: N) -> Self
@@ -180,7 +177,6 @@ impl<N: ?Sized, T> Tag<N, T> {
180177
}
181178
}
182179

183-
#[cfg(feature = "std")]
184180
impl<N, T> NotificationPrivate for Tag<N, T>
185181
where
186182
N: Notification + ?Sized,
@@ -206,14 +202,12 @@ where
206202
}
207203

208204
/// Use a function to generate a tag to notify listeners.
209-
#[cfg(feature = "std")]
210205
#[doc(hidden)]
211206
pub struct TagWith<N: ?Sized, F> {
212207
tag: F,
213208
inner: N,
214209
}
215210

216-
#[cfg(feature = "std")]
217211
impl<N: fmt::Debug, F> fmt::Debug for TagWith<N, F> {
218212
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
219213
f.debug_struct("TagWith")
@@ -223,15 +217,13 @@ impl<N: fmt::Debug, F> fmt::Debug for TagWith<N, F> {
223217
}
224218
}
225219

226-
#[cfg(feature = "std")]
227220
impl<N, F> TagWith<N, F> {
228221
/// Create a new `TagFn` with the given tag function and notification.
229222
fn new(tag: F, inner: N) -> Self {
230223
Self { tag, inner }
231224
}
232225
}
233226

234-
#[cfg(feature = "std")]
235227
impl<N, F, T> NotificationPrivate for TagWith<N, F>
236228
where
237229
N: Notification + ?Sized,
@@ -490,7 +482,6 @@ pub trait IntoNotification: __private::Sealed {
490482
/// assert_eq!(listener1.wait(), true);
491483
/// assert_eq!(listener2.wait(), false);
492484
/// ```
493-
#[cfg(feature = "std")]
494485
fn tag<T: Clone>(self, tag: T) -> Tag<Self::Notify, T>
495486
where
496487
Self: Sized + IntoNotification<Tag = ()>,
@@ -524,7 +515,6 @@ pub trait IntoNotification: __private::Sealed {
524515
/// assert_eq!(listener1.wait(), true);
525516
/// assert_eq!(listener2.wait(), false);
526517
/// ```
527-
#[cfg(feature = "std")]
528518
fn tag_with<T, F>(self, tag: F) -> TagWith<Self::Notify, F>
529519
where
530520
Self: Sized + IntoNotification<Tag = ()>,
@@ -568,7 +558,7 @@ impl_for_numeric_types! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
568558
/// Equivalent to `atomic::fence(Ordering::SeqCst)`, but in some cases faster.
569559
#[inline]
570560
pub(super) fn full_fence() {
571-
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), not(miri), not(loom)))]
561+
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), not(miri)))]
572562
{
573563
use core::{arch::asm, cell::UnsafeCell};
574564
// HACK(stjepang): On x86 architectures there are two different ways of executing

0 commit comments

Comments
 (0)