use ixgbe_driver::{alloc_pkt, IxgbeError, IxgbeHal, MemPool};
use std::ptr::NonNull;
use std::time::Duration;
struct IntegrationTestHal;
unsafe impl IxgbeHal for IntegrationTestHal {
fn dma_alloc(size: usize) -> (usize, NonNull<u8>) {
let layout = std::alloc::Layout::from_size_align(size, 4096).unwrap();
let ptr = unsafe { std::alloc::alloc(layout) };
if ptr.is_null() {
panic!("Memory allocation failed");
}
(ptr as usize, NonNull::new(ptr).unwrap())
}
unsafe fn dma_dealloc(_paddr: usize, vaddr: NonNull<u8>, size: usize) -> i32 {
let layout = std::alloc::Layout::from_size_align(size, 4096).unwrap();
std::alloc::dealloc(vaddr.as_ptr(), layout);
0
}
unsafe fn mmio_phys_to_virt(_paddr: usize, _size: usize) -> NonNull<u8> {
NonNull::new(0x1000 as *mut u8).unwrap()
}
unsafe fn mmio_virt_to_phys(_vaddr: NonNull<u8>, _size: usize) -> usize {
0x1000
}
fn wait_until(_duration: Duration) -> Result<(), &'static str> {
Ok(())
}
}
#[test]
fn test_memory_pool_packet_integration() {
let pool =
MemPool::allocate::<IntegrationTestHal>(1024, 2048).expect("Failed to create memory pool");
assert_eq!(pool.entry_size(), 2048);
let packet1 = alloc_pkt(&pool, 1500).expect("Failed to allocate packet");
let packet2 = alloc_pkt(&pool, 1500).expect("Failed to allocate packet");
assert_eq!(packet1.len(), 1500);
assert_eq!(packet2.len(), 1500);
}
#[test]
fn test_error_propagation() {
let result = MemPool::allocate::<IntegrationTestHal>(4096, 100); assert!(matches!(result, Err(IxgbeError::PageNotAligned)));
}
#[test]
fn test_resource_management() {
let pool =
MemPool::allocate::<IntegrationTestHal>(10, 2048).expect("Failed to create memory pool");
let mut packets = Vec::new();
for _ in 0..10 {
let packet = alloc_pkt(&pool, 1500).expect("Failed to allocate packet");
packets.push(packet);
}
assert!(alloc_pkt(&pool, 1500).is_none());
packets.clear();
let packet = alloc_pkt(&pool, 1500).expect("Failed to allocate packet after release");
assert_eq!(packet.len(), 1500);
}
#[test]
fn test_performance_baseline() {
let pool =
MemPool::allocate::<IntegrationTestHal>(1000, 2048).expect("Failed to create memory pool");
let start = std::time::Instant::now();
for _ in 0..100 {
let _packet = alloc_pkt(&pool, 1500).expect("Failed to allocate packet");
}
let duration = start.elapsed();
assert!(duration.as_millis() < 1000); }