Skip to content

Commit

Permalink
Test different Identifier types in adapter tests (#64)
Browse files Browse the repository at this point in the history
* test different identifier types in adapter tests

* cleanup
  • Loading branch information
pd0wm authored Jun 30, 2024
1 parent be53518 commit 6148c23
Showing 1 changed file with 41 additions and 15 deletions.
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

0 comments on commit 6148c23

Please sign in to comment.