Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test different identifier types in adapter tests #64

Merged
merged 2 commits into from
Jun 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 41 additions & 15 deletions tests/adapter_tests.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@
#![allow(dead_code, unused_imports)]
use automotive::can::AsyncCanAdapter;
use automotive::can::{CanAdapter, Frame};
use automotive::can::{CanAdapter, Frame, Identifier};
use automotive::panda::Panda;
use std::collections::VecDeque;
use std::time::Duration;

static BULK_NUM_FRAMES_SYNC: u64 = 0x100;
static BULK_NUM_FRAMES_ASYNC: u64 = 0x1000;
static BULK_NUM_FRAMES_SYNC: usize = 0x100;
static BULK_NUM_FRAMES_ASYNC: usize = 0x1000;
static BULK_SYNC_TIMEOUT_MS: u64 = 1000;
static BULK_ASYNC_TIMEOUT_MS: u64 = 5000;

/// Sends a large number of frames to a "blocking" adapter, and then reads back all sent messages.
/// This verified the adapter doesn't drop messages and reads them back in the same order as they are sent,
/// which is needed for the async adapter to work correctly.
fn bulk_send_sync<T: CanAdapter>(adapter: &mut T) {
fn get_test_frames(amount: usize) -> Vec<Frame> {
let mut frames = vec![];

for i in 0..BULK_NUM_FRAMES_SYNC {
// Extended ID
frames.push(
Frame::new(
0,
Identifier::Extended(0x1234),
&[0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA],
)
.unwrap(),
);

// Extended ID that also fits in Standard ID
frames.push(
Frame::new(
0,
Identifier::Extended(0x123),
&[0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA],
)
.unwrap(),
);

// Zero length data
frames.push(Frame::new(0, 0x0.into(), &[]).unwrap());

// Add bulk
for i in 0..amount {
frames.push(Frame::new(0, 0x123.into(), &i.to_be_bytes()).unwrap());
}

frames
}

/// Sends a large number of frames to a "blocking" adapter, and then reads back all sent messages.
/// This verified the adapter doesn't drop messages and reads them back in the same order as they are sent,
/// which is needed for the async adapter to work correctly.
fn bulk_send_sync<T: CanAdapter>(adapter: &mut T) {
let frames = get_test_frames(BULK_NUM_FRAMES_SYNC);

let mut to_send: VecDeque<Frame> = frames.clone().into();
while !to_send.is_empty() {
adapter.send(&mut to_send).unwrap();
Expand Down Expand Up @@ -49,11 +79,7 @@ fn bulk_send_sync<T: CanAdapter>(adapter: &mut T) {
/// Sends a large number of frames to the adapter, and awaits them simultaneously.
/// This tests the functionality in [`AsyncCanAdapter`] to resolve the future when the message is ACKed.
async fn bulk_send(adapter: &AsyncCanAdapter) {
let mut frames = vec![];

for i in 0..BULK_NUM_FRAMES_ASYNC {
frames.push(Frame::new(0, 0x123.into(), &i.to_be_bytes()).unwrap());
}
let frames = get_test_frames(BULK_NUM_FRAMES_ASYNC);

let r = frames.iter().map(|frame| adapter.send(frame));
tokio::time::timeout(
Expand Down Expand Up @@ -100,7 +126,7 @@ async fn socketcan_bulk_send_async() {
// #[cfg(feature = "test_socketcan")]
// #[tokio::test]
// #[serial_test::serial]
// async fn vcan_bulk_send_fd() {
// async fn socketcan_send_fd() {
// let adapter = automotive::socketcan::SocketCan::new_async("can0").unwrap();
// adapter.send(&Frame::new(0, 0x123.into(), &[0u8; 64])).await;
// }
Expand All @@ -124,7 +150,7 @@ async fn vcan_bulk_send_async() {
#[cfg(feature = "test_vcan")]
#[tokio::test]
#[serial_test::serial]
async fn vcan_bulk_send_fd() {
async fn vcan_send_fd() {
let adapter = automotive::socketcan::SocketCan::new_async("vcan0").unwrap();
adapter
.send(&Frame::new(0, 0x123.into(), &[0u8; 64]).unwrap())
Expand Down
Loading