improve multi mock behavior#979
Conversation
Concern: Auto-removing mocks changes the API contractThis PR changes mock behavior in a way that may surprise callers — mocks have always persisted until explicitly removed via
Two changes in this PR
Suggested alternative: round-robin cyclingInstead of destroying mocks, keep them all and cycle through them with an index. This preserves the existing API contract while still supporting sequenced responses. In In if (matchingMocks.empty()) if (matchingMocks.size() == 1) } Clear Benefits of cycling over auto-removal
|
http_internal_map<http_internal_string, size_t> m_mockCycleIndex;
if (matchingMocks.empty()) { return false; }
if (matchingMocks.size() == 1)
{
mock = matchingMocks[0];
}
else
{
// Build a key from method+url to track cycling position
http_internal_string key{ originalCall->method };
key += "|";
key += originalCall->url;
auto& idx = httpSingleton->m_mockCycleIndex[key];
mock = matchingMocks[idx % matchingMocks.size()];
++idx;
}Am I correctly reading that m_mockCycleIndex is never pushed to? How do you anticipate it being used? |
Good question — auto& idx = httpSingleton->m_mockCycleIndex[key];On the first call for a given method+url, it inserts The broader suggestion is to use round-robin cycling instead of auto-removal, so mocks persist and are reusable across test replays without re-registration. |
|
Thanks for the explanation. Pushed. |
|
The cycling logic looks great, but the old auto-removal code is still present (lines 149-165 in the new diff). The
The fix is just deleting this block: // If this is not the only mock that matches, remove it from the list of mocks so that multiple can be used
// in sequence
auto countMatching = std::count_if(
mocks.begin(),
mocks.end(),
[originalCall](auto m)
{
return DoesMockCallMatch(m, originalCall);
});
if (countMatching > 1)
{
HCMockRemoveMock(mock);
}The round-robin cycling already handles sequencing — no removal needed. |
…ibHttpClient into user/pablo/multimock
jasonsandlin
left a comment
There was a problem hiding this comment.
Looks good — clean round-robin cycling, mocks persist as expected, cycle index properly cleared on removal.
Replicated #553
This PR updates the mocking behavior to match what's advertised by the doc header above HCMockAddMock:
Previously, the last-set mock for a particular URL was the only one that would ever return. Now the behavior matches the comment: Each matching mock is used and consumed in order they were created, with the last mock repeated forever.