I was programming today and encountered something that just feels like I'm doing something wrong (maybe?). I've encountered this situation before, but I wanted to reach out and ask if there's a better way to handle this. I have a situation where I realized my function direct_data
could save a lot of execution time by exiting early in situations where there is no room on the two upstream queues, so I wrote a test case:
TEST(data_director_test_group, transmission_and_storage_both_full_do_nothing) {
// Arrange
intakeQueue = QueueMock_Create(1, sizeof(Count));
transmissionQueue = QueueMock_Create(1, sizeof(Count));
storageQueue = QueueMock_Create(1, sizeof(Count));
// Mock Expectations
// create mock that says intake queue has 1 element
mock().expectOneCall("Queue_Length")
.withPointerParameter("queue", intakeQueue)
.andReturnValue(1);
// create mock that says transmission queue has no space available
mock().expectOneCall("Queue_SpacesAvailable")
.withPointerParameter("queue", transmissionQueue)
.andReturnValue(0);
// create mock that says storage queue has no space available
mock().expectOneCall("Queue_SpacesAvailable")
.withPointerParameter("queue", storageQueue)
.andReturnValue(0);
mock().expectNoCall("Queue_Pop");
mock().expectNoCall("Queue_Push");
// Act
direct_data(
true,
intakeQueue,
transmissionQueue,
storageQueue
);
}
I then added this short piece of code near the beginning of direct_data
:
uint64_t transmissionAvailable = Queue_SpacesAvailable(transmissionQueue);
if (transmissionAvailable == 0 && Queue_SpacesAvailable(storageQueue) == 0) return;
The issue is this causes a multitude of other older test cases to fail because it now requires me to add the mock expectation, but saying that these calls to the Queue_SpacesAvailable
function return 1 entry. To make it even worse, in some tests I have to pick and choose whether transmission or storage queue has an element, not both, because it would affect the old functionality of the test (and actually fail because the if statement's structure, it would be inefficient to force it to call both transmission and storage). I would be less interested in efficiency if I wasn't working on a MCU, but I've seen this situation come up in my development in PHP as well. Looking for some guidance as to what I may be doing wrong here... Thanks!