Skip to content

Commit d750a9a

Browse files
committed
test and document support for async-std
1 parent 47f7501 commit d750a9a

File tree

3 files changed

+33
-39
lines changed

3 files changed

+33
-39
lines changed

Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@ readme = "./README.md"
1414
[dependencies]
1515
anyhow = "1.0.75"
1616
bincode = "1.3.3"
17-
cfg-if = "1.0.0"
1817
futures = "0.3.29"
1918
lazy_static = "1.4.0"
2019
tokio-test = "0.4.3"
2120

2221
[dependencies.async-std]
2322
version = "1.12.0"
2423
optional = true
25-
26-
features = ["alloc"]
24+
features = ["alloc","attributes"]
2725
[dependencies.serde]
2826
version = "1.0.190"
2927
features = ["derive"]

src/lib.rs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
1919
```
2020
use async_event_emitter::AsyncEventEmitter;
21-
2221
#[tokio::main]
2322
async fn main() {
2423
let mut event_emitter = AsyncEventEmitter::new();
@@ -29,9 +28,7 @@
2928
3029
}
3130
```
32-
3331
## Basic Usage
34-
3532
We can emit and listen to values of any type so long as they implement the Debug trait and serde's Serialize and Deserialize traits.
3633
A single EventEmitter instance can have listeners to values of multiple types.
3734
@@ -107,8 +104,16 @@
107104
// When the <"Hello"> event is emitted in main.rs then print <"Random stuff!">
108105
EVENT_EMITTER.lock().await.on("Hello", |_: ()| async { println!("Random stuff!")});
109106
}
110-
111107
```
108+
## Using async-std instead of tokio
109+
Tokio is the default runtime for this library but async-std support can be able enabled by disabling default-features on the crate and enable the ```use-async-std``` feature.
110+
<br>
111+
**Note**: Use simply replace tokio::main with async-std::main and tokio::test with async-std::test (provided you've enabled the "attributes" feature on the crate.
112+
113+
### Testing
114+
Run the tests on this crate with all-features enabled as follows:
115+
``` cargo test --all-features```
116+
112117
113118
License: MIT
114119
*/
@@ -156,6 +161,11 @@ impl AsyncEventEmitter {
156161
where
157162
T: Serialize + Deserialize<'a> + Send + Sync + 'a + std::fmt::Debug,
158163
{
164+
#[cfg(feature = "use-async-std")]
165+
use async_std::task::spawn;
166+
#[cfg(not(feature = "use-async-std"))]
167+
use tokio::spawn;
168+
159169
let mut callback_handlers: Vec<_> = Vec::new();
160170

161171
if let Some(listeners) = self.listeners.get_mut(event) {
@@ -167,33 +177,11 @@ impl AsyncEventEmitter {
167177

168178
match listener.limit {
169179
None => {
170-
cfg_if::cfg_if! {
171-
if #[cfg(feature = "use-async-std")] {
172-
use async_std::task::spawn;
173-
callback_handlers.push(spawn(async move {
174-
callback(bytes).await;
175-
}));
176-
} else {
177-
use tokio::spawn;
178-
callback_handlers
179-
.push(spawn(async move { callback(bytes).await }));
180-
}
181-
182-
};
180+
callback_handlers.push(spawn(async move { callback(bytes).await }));
183181
}
184182
Some(limit) => {
185183
if limit != 0 {
186-
cfg_if::cfg_if! {
187-
if #[cfg(feature = "use-async-std")] {
188-
callback_handlers.push(async_std::task::spawn(async move {
189-
callback(bytes).await;
190-
}));
191-
} else {
192-
callback_handlers
193-
.push(tokio::spawn(async move { callback(bytes).await }));
194-
}
195-
196-
};
184+
callback_handlers.push(spawn(async move { callback(bytes).await }));
197185

198186
listener.limit = Some(limit - 1);
199187
} else {
@@ -210,7 +198,7 @@ impl AsyncEventEmitter {
210198
}
211199

212200
for handles in callback_handlers {
213-
handles.await;
201+
_ = handles.await;
214202
}
215203

216204
Ok(())

tests/emitter.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
mod tester {
2+
/// use
3+
#[cfg(feature = "use-async-std")]
4+
pub use async_std::test;
5+
#[cfg(not(feature = "use-async-std"))]
6+
pub use tokio::test;
7+
}
8+
19
#[cfg(test)]
210

311
mod async_event_emitter {
@@ -26,8 +34,7 @@ mod async_event_emitter {
2634
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
2735
struct DateTime(Date, Time);
2836

29-
#[tokio::test]
30-
37+
#[tester::test]
3138
async fn test_async_event() -> anyhow::Result<()> {
3239
let mut event_emitter = AsyncEventEmitter::new();
3340

@@ -50,7 +57,7 @@ mod async_event_emitter {
5057
Ok(())
5158
}
5259

53-
#[tokio::test]
60+
#[tester::test]
5461
async fn test_emit_multiple_args() -> anyhow::Result<()> {
5562
let mut event_emitter = AsyncEventEmitter::new();
5663
let time = Time {
@@ -78,7 +85,7 @@ mod async_event_emitter {
7885
Ok(())
7986
}
8087

81-
#[tokio::test]
88+
#[tester::test]
8289
async fn listens_once_with_multiple_emits() -> anyhow::Result<()> {
8390
let mut event_emitter = AsyncEventEmitter::new();
8491
let name = "LOG_DATE".to_string();
@@ -118,7 +125,7 @@ mod async_event_emitter {
118125

119126
Ok(())
120127
}
121-
#[tokio::test]
128+
#[tester::test]
122129
async fn remove_listeners() -> anyhow::Result<()> {
123130
let mut event_emitter = AsyncEventEmitter::new();
124131
let dt = DateTime(
@@ -163,7 +170,7 @@ mod async_event_emitter {
163170
Ok(())
164171
}
165172

166-
#[tokio::test]
173+
#[tester::test]
167174
#[should_panic]
168175
async fn panics_on_different_values_for_same_event() {
169176
let mut event_emitter = AsyncEventEmitter::new();
@@ -176,7 +183,7 @@ mod async_event_emitter {
176183
.unwrap();
177184
event_emitter.emit("value", 12).await.unwrap();
178185
}
179-
#[tokio::test]
186+
#[tester::test]
180187

181188
async fn global_event_emitter() {
182189
// We need to maintain a lock through the mutex so we can avoid data races
@@ -189,6 +196,7 @@ mod async_event_emitter {
189196

190197
// tests/listener_tests.rs
191198

199+
use crate::tester;
192200
use async_event_emitter::AsyncListener;
193201

194202
#[test]

0 commit comments

Comments
 (0)