[traffic] Introduce test mode for traffic manager

Signed-off-by: mvglasow <michael -at- vonglasow.com>
This commit is contained in:
mvglasow
2025-06-06 18:08:46 +03:00
parent 5531b1129b
commit ba9980ba36
5 changed files with 113 additions and 24 deletions

View File

@@ -374,6 +374,8 @@ Framework::Framework(FrameworkParams const & params, bool loadMaps)
editor.SetDelegate(make_unique<search::EditorDelegate>(m_featuresFetcher.GetDataSource())); editor.SetDelegate(make_unique<search::EditorDelegate>(m_featuresFetcher.GetDataSource()));
editor.SetInvalidateFn([this](){ InvalidateRect(GetCurrentViewport()); }); editor.SetInvalidateFn([this](){ InvalidateRect(GetCurrentViewport()); });
if (params.m_trafficTestMode)
m_trafficManager.SetTestMode();
m_trafficManager.SetCurrentDataVersion(m_storage.GetCurrentDataVersion()); m_trafficManager.SetCurrentDataVersion(m_storage.GetCurrentDataVersion());
m_trafficManager.SetSimplifiedColorScheme(LoadTrafficSimplifiedColors()); m_trafficManager.SetSimplifiedColorScheme(LoadTrafficSimplifiedColors());

View File

@@ -98,11 +98,22 @@ class Loader;
/// build version for screenshots. /// build version for screenshots.
//#define FIXED_LOCATION //#define FIXED_LOCATION
/**
* @brief Initialization parameters for the framework.
*
* `FrameworkParams` is intended for parameters which are hardcoded rather than read from a
* configuration. It allows test cases to run on a tailored configuration.
*/
struct FrameworkParams struct FrameworkParams
{ {
bool m_enableDiffs = true; bool m_enableDiffs = true;
size_t m_numSearchAPIThreads = 1; size_t m_numSearchAPIThreads = 1;
/**
* @brief Whether the traffic manager should start in test mode.
*/
bool m_trafficTestMode = false;
FrameworkParams() = default; FrameworkParams() = default;
FrameworkParams(bool enableDiffs) FrameworkParams(bool enableDiffs)
: m_enableDiffs(enableDiffs) : m_enableDiffs(enableDiffs)

View File

@@ -126,7 +126,10 @@ void TrafficManager::SetEnabled(bool enabled)
m_drapeEngine.SafeCall(&df::DrapeEngine::EnableTraffic, enabled); m_drapeEngine.SafeCall(&df::DrapeEngine::EnableTraffic, enabled);
if (enabled) if (enabled)
{
Invalidate(); Invalidate();
m_canSetMode = false;
}
else else
m_observer.OnTrafficInfoClear(); m_observer.OnTrafficInfoClear();
} }
@@ -477,6 +480,8 @@ void TrafficManager::ThreadRoutine()
// TODO clean out expired messages // TODO clean out expired messages
if (!IsTestMode())
{
LOG(LINFO, ("start loop, active MWMs changed:", m_activeMwmsChanged, ", poll needed:", m_isPollNeeded)); LOG(LINFO, ("start loop, active MWMs changed:", m_activeMwmsChanged, ", poll needed:", m_isPollNeeded));
// this is a no-op if active MWMs have not changed // this is a no-op if active MWMs have not changed
@@ -500,6 +505,7 @@ void TrafficManager::ThreadRoutine()
// TODO set failed status somewhere and retry // TODO set failed status somewhere and retry
} }
} }
}
LOG(LINFO, (m_feedQueue.size(), "feed(s) in queue")); LOG(LINFO, (m_feedQueue.size(), "feed(s) in queue"));
// consolidate feed queue (remove older messages in favor of newer ones) // consolidate feed queue (remove older messages in favor of newer ones)
@@ -568,6 +574,8 @@ bool TrafficManager::WaitForRequest()
return true; return true;
} }
if (!IsTestMode())
{
// if update interval has elapsed, return immediately // if update interval has elapsed, return immediately
auto const currentTime = steady_clock::now(); auto const currentTime = steady_clock::now();
auto const passedSeconds = currentTime - m_lastResponseTime; auto const passedSeconds = currentTime - m_lastResponseTime;
@@ -578,11 +586,12 @@ bool TrafficManager::WaitForRequest()
return true; return true;
} }
} }
}
LOG(LINFO, ("nothing to do for now, waiting for timeout or notification")); LOG(LINFO, ("nothing to do for now, waiting for timeout or notification"));
bool const timeout = !m_condition.wait_for(lock, kUpdateInterval, [this] bool const timeout = !m_condition.wait_for(lock, kUpdateInterval, [this]
{ {
return !m_isRunning || m_activeMwmsChanged; return !m_isRunning || (m_activeMwmsChanged && !IsTestMode());
}); });
// check again if we got terminated while waiting (or woken up because we got terminated) // check again if we got terminated while waiting (or woken up because we got terminated)
@@ -593,7 +602,7 @@ bool TrafficManager::WaitForRequest()
if (IsEnabled()) if (IsEnabled())
m_isPollNeeded |= timeout; m_isPollNeeded |= timeout;
LOG(LINFO, ("timeout:", timeout, "active MWMs changed:", m_activeMwmsChanged)); LOG(LINFO, ("timeout:", timeout, "active MWMs changed:", m_activeMwmsChanged, "test mode:", IsTestMode()));
return true; return true;
} }
@@ -960,6 +969,16 @@ void TrafficManager::SetSimplifiedColorScheme(bool simplified)
m_drapeEngine.SafeCall(&df::DrapeEngine::SetSimplifiedTrafficColors, simplified); m_drapeEngine.SafeCall(&df::DrapeEngine::SetSimplifiedTrafficColors, simplified);
} }
void TrafficManager::SetTestMode()
{
if (!m_canSetMode)
{
LOG(LWARNING, ("Mode cannot be set once the traffic manager has been enabled"));
return;
}
m_mode = Mode::Test;
}
std::string DebugPrint(TrafficManager::TrafficState state) std::string DebugPrint(TrafficManager::TrafficState state)
{ {
switch (state) switch (state)

View File

@@ -62,6 +62,31 @@ public:
ExpiredApp ExpiredApp
}; };
/**
* @brief The mode for the traffic manager.
*
* Future versions may introduce further test modes. Therefore, always use `TrafficManager::IsTestMode()`
* to verify if the traffic manager is running in test mode.
*/
enum class Mode
{
/**
* Traffic manager mode for normal operation.
*
* This is the default mode unless something else is explicitly set.
*/
Normal,
/**
* Test mode.
*
* This mode will prevent the traffic manager from automatically subscribing to sources and
* polling them. It will still receive and process push feeds.
*
* Future versions may introduce further behavior changes, and/or introduce more test modes.
*/
Test
};
struct MyPosition struct MyPosition
{ {
m2::PointD m_position = m2::PointD(0.0, 0.0); m2::PointD m_position = m2::PointD(0.0, 0.0);
@@ -161,6 +186,24 @@ public:
void SetSimplifiedColorScheme(bool simplified); void SetSimplifiedColorScheme(bool simplified);
bool HasSimplifiedColorScheme() const { return m_hasSimplifiedColorScheme; } bool HasSimplifiedColorScheme() const { return m_hasSimplifiedColorScheme; }
/**
* @brief Whether the traffic manager is operating in test mode.
*/
bool IsTestMode() { return m_mode != Mode::Normal; }
/**
* @brief Switches the traffic manager into test mode.
*
* The mode can only be set before the traffic manager is first enabled. After that, this method
* will log a warning but otherwise do nothing.
*
* In test mode, the traffic manager will not subscribe to sources or poll them automatically.
* It will still receive and process push feeds.
*
* Future versions may introduce further behavior changes.
*/
void SetTestMode();
private: private:
/** /**
* @brief Holds information about pending or previous traffic requests pertaining to an MWM. * @brief Holds information about pending or previous traffic requests pertaining to an MWM.
@@ -481,6 +524,18 @@ private:
std::pair<MyPosition, bool> m_currentPosition = {MyPosition(), false}; std::pair<MyPosition, bool> m_currentPosition = {MyPosition(), false};
std::pair<ScreenBase, bool> m_currentModelView = {ScreenBase(), false}; std::pair<ScreenBase, bool> m_currentModelView = {ScreenBase(), false};
/**
* The mode in which the traffic manager is running.
*/
Mode m_mode = Mode::Normal;
/**
* Whether the traffic manager accepts mode changes.
*
* Mode cannt be set after the traffic manager has been enabled for the first time.
*/
bool m_canSetMode = true;
std::atomic<TrafficState> m_state; std::atomic<TrafficState> m_state;
TrafficStateChangedFn m_onStateChangedFn; TrafficStateChangedFn m_onStateChangedFn;

View File

@@ -32,6 +32,8 @@ int main(int argc, char * argv[])
FrameworkParams params; FrameworkParams params;
params.m_trafficTestMode = true;
Framework framework(params); Framework framework(params);
traffxml::MainWindow mainWindow(framework); traffxml::MainWindow mainWindow(framework);