Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Source/Global/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ typedef struct http_singleton
// Mock state
std::recursive_mutex m_mocksLock;
http_internal_vector<HC_MOCK_CALL*> m_mocks;
http_internal_map<http_internal_string, size_t> m_mockCycleIndex;

std::recursive_mutex m_sharedPtrsLock;
http_internal_unordered_map<void*, std::shared_ptr<void>> m_sharedPtrs;
Expand Down
31 changes: 26 additions & 5 deletions Source/Mock/lhc_mock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,37 @@ bool Mock_Internal_HCHttpCallPerformAsync(
auto& mocks{ httpSingleton->m_mocks };
HC_MOCK_CALL* mock{ nullptr };

// Use the most recently added mock that matches (similar to a stack).
for (auto iter = mocks.rbegin(); iter != mocks.rend(); ++iter)
// Collect all matching mocks in insertion order
http_internal_vector<HC_MOCK_CALL*> matchingMocks;
for (auto& m : mocks)
{
if (DoesMockCallMatch(*iter, originalCall))
if (DoesMockCallMatch(m, originalCall))
{
mock = *iter;
break;
matchingMocks.push_back(m);
}
}

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;
}

if (!mock)
{
return false;
Expand Down
2 changes: 2 additions & 0 deletions Source/Mock/mock_publics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ try
{
mocks.erase(iter);
HCHttpCallCloseHandle(call);
httpSingleton->m_mockCycleIndex.clear();
return S_OK;
}
}
Expand All @@ -139,6 +140,7 @@ try
}

httpSingleton->m_mocks.clear();
httpSingleton->m_mockCycleIndex.clear();
return S_OK;
}
CATCH_RETURN()
Expand Down