|
| 1 | +# Prepare Pipeline Unit Tests |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This test suite provides comprehensive unit tests for PR #79444, which replaces the Bluetooth controller's prepare pipeline implementation from a FIFO-based approach to an ordered linked list. |
| 6 | + |
| 7 | +## Test Organization |
| 8 | + |
| 9 | +The test suite is organized into four main test modules: |
| 10 | + |
| 11 | +### 1. test_ull_prepare_basic.c - Basic Pipeline Operations |
| 12 | +Tests fundamental enqueue/dequeue operations: |
| 13 | +- **test_enqueue_empty_pipeline**: Validates enqueuing to empty pipeline |
| 14 | +- **test_enqueue_multiple_events**: Tests multiple event enqueuing |
| 15 | +- **test_dequeue_get**: Tests peeking at head without removal |
| 16 | +- **test_dequeue_empty_pipeline**: Tests dequeue on empty pipeline |
| 17 | +- **test_resume_event_marking**: Validates resume event marking |
| 18 | +- **test_aborted_event_marking**: Tests aborted event marking |
| 19 | +- **test_callback_assignments**: Verifies callback assignments |
| 20 | + |
| 21 | +### 2. test_ull_prepare_ordering.c - Ordering Behavior |
| 22 | +Tests the ordered insertion behavior based on `ticks_at_expire`: |
| 23 | +- **test_ascending_order_insertion**: Events enqueued in ascending order |
| 24 | +- **test_descending_order_insertion**: Events enqueued in descending order (should be reordered) |
| 25 | +- **test_mixed_order_insertion**: Random order insertion and verification |
| 26 | +- **test_resume_events_at_tail**: Critical test - resume events always at tail regardless of time |
| 27 | +- **test_ordering_with_aborted_events**: Tests ordering with aborted events |
| 28 | +- **test_interleaved_resume_and_normal**: Complex scenario with mixed event types |
| 29 | + |
| 30 | +### 3. test_ull_prepare_iterator.c - Iterator Functionality |
| 31 | +Tests the new `void**` iterator interface (changed from `uint8_t*`): |
| 32 | +- **test_iterator_init_null**: Iterator initialization with NULL starts at head |
| 33 | +- **test_complete_iteration**: Tests iteration through entire list |
| 34 | +- **test_iterator_termination**: Validates iterator returns NULL at end |
| 35 | +- **test_iterator_empty_pipeline**: Iterator on empty pipeline |
| 36 | +- **test_iterator_single_event**: Edge case with single event |
| 37 | +- **test_iterator_mixed_event_types**: Iteration through normal, resume, and aborted events |
| 38 | +- **test_iterator_break_condition**: Tests safe break when !idx |
| 39 | +- **test_iterator_parameter_update**: Validates idx parameter updates |
| 40 | + |
| 41 | +### 4. test_ull_prepare_edge_cases.c - Edge Cases and Boundary Conditions |
| 42 | +Tests edge cases and error handling: |
| 43 | +- **test_empty_pipeline_operations**: All operations on empty pipeline |
| 44 | +- **test_single_element_pipeline**: Operations with single element |
| 45 | +- **test_pipeline_full_condition**: Behavior when pipeline reaches max capacity (EVENT_PIPELINE_MAX) |
| 46 | +- **test_tick_wraparound**: Tick counter wraparound scenarios |
| 47 | +- **test_multiple_dequeue_get_calls**: Validates dequeue_get is non-destructive |
| 48 | +- **test_alternating_enqueue_iterate**: Interleaved operations |
| 49 | +- **test_same_tick_values**: Multiple events with identical ticks_at_expire |
| 50 | +- **test_zero_tick_values**: Edge case with tick value of 0 |
| 51 | +- **test_max_tick_value**: Edge case with UINT32_MAX tick value |
| 52 | + |
| 53 | +## Key Changes Tested (PR #79444) |
| 54 | + |
| 55 | +### 1. Data Structure Change |
| 56 | +- **From**: MFIFO (Multi-producer FIFO) |
| 57 | +- **To**: Ordered linked list |
| 58 | +- **Impact**: Events now maintain order based on `ticks_at_expire` instead of simple FIFO |
| 59 | + |
| 60 | +### 2. Iterator Interface Change |
| 61 | +- **From**: `void *ull_prepare_dequeue_iter(uint8_t *idx)` |
| 62 | +- **To**: `void *ull_prepare_dequeue_iter(void **idx)` |
| 63 | +- **Impact**: More flexible iterator that can handle pointer-based traversal |
| 64 | + |
| 65 | +### 3. Ordered Insertion |
| 66 | +- Non-resume events are inserted in order based on `ticks_at_expire` |
| 67 | +- Resume events always placed at tail |
| 68 | +- Aborted events can be anywhere but don't affect ordering logic |
| 69 | + |
| 70 | +### 4. Simplified Logic |
| 71 | +- Eliminated O(n) loop for finding short prepare events |
| 72 | +- Cleaner preemption handling |
| 73 | +- More efficient event management |
| 74 | + |
| 75 | +## Correctness Invariants Verified |
| 76 | + |
| 77 | +### Ordering Invariants |
| 78 | +1. Non-resume events maintain ascending `ticks_at_expire` order |
| 79 | +2. Resume events always placed at tail of list |
| 80 | +3. Aborted events preserved but don't affect ordering |
| 81 | + |
| 82 | +### Iterator Contract |
| 83 | +1. Starting with `idx = NULL` begins at head |
| 84 | +2. Each call advances to next element |
| 85 | +3. `idx = NULL` return signals end of list |
| 86 | +4. Breaking when `!idx` is safe and correct |
| 87 | + |
| 88 | +### Memory Management |
| 89 | +1. Events properly allocated/deallocated |
| 90 | +2. No memory leaks in dequeue operations |
| 91 | +3. Graceful handling of pool exhaustion |
| 92 | + |
| 93 | +## Building and Running Tests |
| 94 | + |
| 95 | +### Build |
| 96 | +```bash |
| 97 | +west build -p auto -b native_sim tests/bluetooth/controller/ctrl_prepare_pipeline |
| 98 | +``` |
| 99 | + |
| 100 | +### Run |
| 101 | +```bash |
| 102 | +west build -t run |
| 103 | +``` |
| 104 | + |
| 105 | +### Run with Ztest Options |
| 106 | +```bash |
| 107 | +west build -t run -- -v # Verbose output |
| 108 | +``` |
| 109 | + |
| 110 | +## Test Coverage Goals |
| 111 | + |
| 112 | +- **Target**: >90% coverage of prepare pipeline code paths |
| 113 | +- **Focus Areas**: |
| 114 | + - Core enqueue/dequeue operations |
| 115 | + - Ordering logic |
| 116 | + - Iterator functionality |
| 117 | + - Edge cases and error conditions |
| 118 | + - Platform-specific changes (Nordic and OpenISA) |
| 119 | + |
| 120 | +## Implementation Notes |
| 121 | + |
| 122 | +### Mock Requirements |
| 123 | +Tests use simple mock callbacks that return success. Real testing may require: |
| 124 | +- Mock ticker implementation |
| 125 | +- Mock radio hardware |
| 126 | +- Mock mayfly scheduler |
| 127 | + |
| 128 | +### Platform-Specific Tests |
| 129 | +While basic pipeline tests are platform-agnostic, full testing should include: |
| 130 | +- Nordic LLL tests (`lll_disable`, `lll_prepare_resolve`, `preempt`) |
| 131 | +- OpenISA LLL tests (similar functions) |
| 132 | + |
| 133 | +These are not included in the current test suite as they require platform-specific mocking infrastructure. |
| 134 | + |
| 135 | +### Test Limitations |
| 136 | + |
| 137 | +1. **No actual dequeue removal**: Current tests focus on enqueue and iteration. Full dequeue testing with `ull_prepare_dequeue(caller_id)` would require: |
| 138 | + - Mayfly scheduler mocking |
| 139 | + - Ticker system mocking |
| 140 | + - More complex setup |
| 141 | + |
| 142 | +2. **No concurrent access testing**: Tests are single-threaded. Real-world testing should verify: |
| 143 | + - Thread safety |
| 144 | + - Interrupt safety |
| 145 | + - Race conditions |
| 146 | + |
| 147 | +3. **No performance testing**: Tests verify correctness but not performance improvements claimed in PR #79444. |
| 148 | + |
| 149 | +## Future Enhancements |
| 150 | + |
| 151 | +1. Add platform-specific tests for Nordic and OpenISA |
| 152 | +2. Add memory leak detection tests |
| 153 | +3. Add performance benchmarks comparing MFIFO vs ordered list |
| 154 | +4. Add stress tests with rapid enqueue/dequeue cycles |
| 155 | +5. Add tests for actual radio event handling |
| 156 | +6. Add tests under CONFIG_BT_CTLR_LOW_LAT_ULL_DONE |
| 157 | + |
| 158 | +## References |
| 159 | + |
| 160 | +- **PR #79444**: [Bluetooth: Controller: Replace prepare pipeline with ordered list](https://github.com/zephyrproject-rtos/zephyr/pull/79444) |
| 161 | +- **Modified Files**: |
| 162 | + - `subsys/bluetooth/controller/ll_sw/lll.h` |
| 163 | + - `subsys/bluetooth/controller/ll_sw/ull.c` |
| 164 | + - `subsys/bluetooth/controller/ll_sw/nordic/lll/lll.c` |
| 165 | + - `subsys/bluetooth/controller/ll_sw/openisa/lll/lll.c` |
| 166 | + |
| 167 | +## Contributing |
| 168 | + |
| 169 | +When adding new tests: |
| 170 | +1. Follow existing test naming conventions |
| 171 | +2. Include clear documentation of what is being tested |
| 172 | +3. Add test to appropriate test module |
| 173 | +4. Update this README with new test descriptions |
| 174 | +5. Ensure tests are deterministic and don't depend on timing |
0 commit comments