Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed May 12, 2024
1 parent 0598c1c commit 31817a6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ features = ["ntex/tokio"]

[dependencies]
ntex = "1.2"
ntex-io = "1.2"
bitflags = "2"
log = "0.4"
pin-project-lite = "0.2"
Expand Down
59 changes: 59 additions & 0 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1014,4 +1014,63 @@ mod tests {

assert_eq!(&data.lock().unwrap().borrow()[..], &[0]);
}

#[ntex::test]
async fn test_read_timeout() {
let (client, server) = Io::create();
client.remote_buffer_cap(1024);

let data = Arc::new(Mutex::new(RefCell::new(Vec::new())));
let data2 = data.clone();

let config = DispatcherConfig::default();
config.set_keepalive_timeout(Seconds::ZERO).set_frame_read_rate(
Seconds(1),
Seconds(2),
2,
);

let (disp, state) = Dispatcher::new_debug_cfg(
nio::Io::new(server),
BytesLenCodec(8),
config,
ntex::service::fn_service(move |msg: DispatchItem<BytesLenCodec>| {
let data = data2.clone();
async move {
match msg {
DispatchItem::Item(bytes) => {
data.lock().unwrap().borrow_mut().push(0);
return Ok::<_, ()>(Some(bytes.freeze()));
}
DispatchItem::ReadTimeout => {
data.lock().unwrap().borrow_mut().push(1);
}
_ => (),

Check warning on line 1048 in src/io.rs

View check run for this annotation

Codecov / codecov/patch

src/io.rs#L1048

Added line #L1048 was not covered by tests
}
Ok(None)
}
}),
);
ntex::rt::spawn(async move {
let _ = disp.await;
});

client.write("12345678");
let buf = client.read().await.unwrap();
assert_eq!(buf, Bytes::from_static(b"12345678"));

client.write("1");
sleep(Millis(1000)).await;
assert!(!state.flags().contains(nio::Flags::IO_STOPPING));
client.write("23");
sleep(Millis(1000)).await;
assert!(!state.flags().contains(nio::Flags::IO_STOPPING));
client.write("4");
sleep(Millis(2000)).await;

// write side must be closed, dispatcher should fail with keep-alive
assert!(state.flags().contains(nio::Flags::IO_STOPPING));
assert!(client.is_closed());
assert_eq!(&data.lock().unwrap().borrow()[..], &[0, 1]);
}
}

0 comments on commit 31817a6

Please sign in to comment.