forked from organicmaps/organicmaps
Taxi NoProvider error code is removed. Fixed available places for providers.
This commit is contained in:
parent
cf88810146
commit
8f37712b9b
8 changed files with 98 additions and 17 deletions
|
@ -10,7 +10,7 @@
|
|||
|
||||
namespace
|
||||
{
|
||||
class TaxiDelegateForTrsting : public taxi::Delegate
|
||||
class BelarusMinskDelegate : public taxi::Delegate
|
||||
{
|
||||
public:
|
||||
storage::TCountriesVec GetCountryIds(ms::LatLon const & latlon) override { return {"Belarus"}; }
|
||||
|
@ -18,6 +18,36 @@ public:
|
|||
std::string GetCityName(ms::LatLon const & latlon) override { return "Minsk"; }
|
||||
};
|
||||
|
||||
class UkraineOdessaDelegate : public taxi::Delegate
|
||||
{
|
||||
public:
|
||||
storage::TCountriesVec GetCountryIds(ms::LatLon const & latlon) override { return {"Ukraine"}; }
|
||||
|
||||
std::string GetCityName(ms::LatLon const & latlon) override { return "Odessa"; }
|
||||
};
|
||||
|
||||
class UsaDelegate : public taxi::Delegate
|
||||
{
|
||||
public:
|
||||
storage::TCountriesVec GetCountryIds(ms::LatLon const & latlon) override
|
||||
{
|
||||
return {"United States of America"};
|
||||
}
|
||||
|
||||
std::string GetCityName(ms::LatLon const & latlon) override { return ""; }
|
||||
};
|
||||
|
||||
class RussiaKonetsDelegate : public taxi::Delegate
|
||||
{
|
||||
public:
|
||||
storage::TCountriesVec GetCountryIds(ms::LatLon const & latlon) override
|
||||
{
|
||||
return {"Russian Federation"};
|
||||
}
|
||||
|
||||
std::string GetCityName(ms::LatLon const & latlon) override { return "Konets"; }
|
||||
};
|
||||
|
||||
std::vector<taxi::Product> GetUberSynchronous(ms::LatLon const & from, ms::LatLon const & to,
|
||||
std::string const & url)
|
||||
{
|
||||
|
@ -184,7 +214,7 @@ UNIT_TEST(TaxiEngine_ResultMaker)
|
|||
maker.Reset(reqId, 3, successCallback, errorNotPossibleCallback);
|
||||
maker.ProcessProducts(reqId, taxi::Provider::Type::Uber, products1);
|
||||
maker.ProcessProducts(reqId, taxi::Provider::Type::Yandex, products2);
|
||||
maker.ProcessError(reqId, taxi::Provider::Type::Uber, taxi::ErrorCode::NoProvider);
|
||||
maker.DecrementRequestCount(reqId);
|
||||
maker.MakeResult(reqId);
|
||||
|
||||
TEST_EQUAL(providers.size(), 2, ());
|
||||
|
@ -218,6 +248,11 @@ UNIT_TEST(TaxiEngine_ResultMaker)
|
|||
TEST_EQUAL(errors[0].m_code, taxi::ErrorCode::NoProducts, ());
|
||||
TEST_EQUAL(errors[1].m_type, taxi::Provider::Type::Yandex, ());
|
||||
TEST_EQUAL(errors[1].m_code, taxi::ErrorCode::RemoteError, ());
|
||||
|
||||
maker.Reset(reqId, 2, successCallback, errorNotPossibleCallback);
|
||||
maker.DecrementRequestCount(reqId);
|
||||
maker.DecrementRequestCount(reqId);
|
||||
maker.MakeResult(reqId);
|
||||
}
|
||||
|
||||
UNIT_TEST(TaxiEngine_Smoke)
|
||||
|
@ -258,7 +293,7 @@ UNIT_TEST(TaxiEngine_Smoke)
|
|||
taxi::Engine engine(
|
||||
{{taxi::Provider::Type::Uber, kTesturl}, {taxi::Provider::Type::Yandex, kTesturl}});
|
||||
|
||||
engine.SetDelegate(my::make_unique<TaxiDelegateForTrsting>());
|
||||
engine.SetDelegate(my::make_unique<BelarusMinskDelegate>());
|
||||
|
||||
taxi::ProvidersContainer const synchronousProviders =
|
||||
GetProvidersSynchronous(engine, from, to, kTesturl);
|
||||
|
@ -295,4 +330,31 @@ UNIT_TEST(TaxiEngine_Smoke)
|
|||
TEST(!providersContainer.empty(), ());
|
||||
TEST(CompareProviders(providersContainer, synchronousProviders), ());
|
||||
}
|
||||
|
||||
UNIT_TEST(TaxiEngine_GetProvidersAtPos)
|
||||
{
|
||||
taxi::Engine engine;
|
||||
// Lat lon is no needed for this test.
|
||||
ms::LatLon latlon(0.0, 0.0);
|
||||
std::vector<taxi::Provider::Type> providers;
|
||||
|
||||
engine.SetDelegate(my::make_unique<BelarusMinskDelegate>());
|
||||
providers = engine.GetProvidersAtPos(latlon);
|
||||
TEST_EQUAL(providers.size(), 1, ());
|
||||
TEST_EQUAL(providers[0], taxi::Provider::Type::Yandex, ());
|
||||
|
||||
engine.SetDelegate(my::make_unique<UkraineOdessaDelegate>());
|
||||
providers = engine.GetProvidersAtPos(latlon);
|
||||
TEST_EQUAL(providers.size(), 1, ());
|
||||
TEST_EQUAL(providers[0], taxi::Provider::Type::Uber, ());
|
||||
|
||||
engine.SetDelegate(my::make_unique<UsaDelegate>());
|
||||
providers = engine.GetProvidersAtPos(latlon);
|
||||
TEST_EQUAL(providers.size(), 1, ());
|
||||
TEST_EQUAL(providers[0], taxi::Provider::Type::Uber, ());
|
||||
|
||||
engine.SetDelegate(my::make_unique<RussiaKonetsDelegate>());
|
||||
providers = engine.GetProvidersAtPos(latlon);
|
||||
TEST(providers.empty(), (providers));
|
||||
}
|
||||
} // namespace
|
||||
|
|
|
@ -5,7 +5,7 @@ namespace taxi
|
|||
bool ApiItem::AreAllCountriesDisabled(storage::TCountriesVec const & countryIds,
|
||||
std::string const & city) const
|
||||
{
|
||||
if (m_disabledCountries.IsEmpty())
|
||||
if (m_disabledCountries.IsEmpty() || countryIds.empty())
|
||||
return false;
|
||||
|
||||
bool isCountryDisabled = true;
|
||||
|
@ -18,7 +18,7 @@ bool ApiItem::AreAllCountriesDisabled(storage::TCountriesVec const & countryIds,
|
|||
bool ApiItem::IsAnyCountryEnabled(storage::TCountriesVec const & countryIds,
|
||||
std::string const & city) const
|
||||
{
|
||||
if (m_enabledCountries.IsEmpty())
|
||||
if (m_enabledCountries.IsEmpty() || countryIds.empty())
|
||||
return true;
|
||||
|
||||
for (auto const & countryId : countryIds)
|
||||
|
|
|
@ -8,7 +8,7 @@ Countries::Countries(std::vector<Country> const & countries) : m_countries(count
|
|||
|
||||
bool Countries::IsEmpty() const { return m_countries.empty(); }
|
||||
|
||||
bool Countries::Has(storage::TCountryId id, std::string const & city) const
|
||||
bool Countries::Has(storage::TCountryId const & id, std::string const & city) const
|
||||
{
|
||||
auto const countryIt =
|
||||
std::find_if(m_countries.cbegin(), m_countries.cend(),
|
||||
|
|
|
@ -19,7 +19,7 @@ public:
|
|||
Countries(std::vector<Country> const & countries);
|
||||
|
||||
bool IsEmpty() const;
|
||||
bool Has(storage::TCountryId id, std::string const & city) const;
|
||||
bool Has(storage::TCountryId const & id, std::string const & city) const;
|
||||
|
||||
private:
|
||||
std::vector<Country> m_countries;
|
||||
|
|
|
@ -42,6 +42,16 @@ void ResultMaker::Reset(uint64_t requestId, size_t requestsCount,
|
|||
m_errors.clear();
|
||||
}
|
||||
|
||||
void ResultMaker::DecrementRequestCount(uint64_t requestId)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_mutex);
|
||||
|
||||
if (m_requestId != requestId)
|
||||
return;
|
||||
|
||||
DecrementRequestCount();
|
||||
}
|
||||
|
||||
void ResultMaker::ProcessProducts(uint64_t requestId, Provider::Type type,
|
||||
std::vector<Product> const & products)
|
||||
{
|
||||
|
@ -86,10 +96,10 @@ void ResultMaker::MakeResult(uint64_t requestId) const
|
|||
errors = m_errors;
|
||||
}
|
||||
|
||||
if (providers.empty())
|
||||
return errorCallback(errors, requestId);
|
||||
if (!providers.empty() || (providers.empty() && errors.empty()))
|
||||
return successCallback(providers, requestId);
|
||||
|
||||
return successCallback(providers, requestId);
|
||||
return errorCallback(errors, requestId);
|
||||
}
|
||||
|
||||
void ResultMaker::DecrementRequestCount()
|
||||
|
@ -101,8 +111,8 @@ void ResultMaker::DecrementRequestCount()
|
|||
// Engine -----------------------------------------------------------------------------------------
|
||||
Engine::Engine(std::vector<ProviderUrl> urls /* = {} */)
|
||||
{
|
||||
AddApi<yandex::Api>(urls, Provider::Type::Yandex, places::kPlaces, {{{}}});
|
||||
AddApi<uber::Api>(urls, Provider::Type::Uber, {{{}}}, places::kPlaces);
|
||||
AddApi<yandex::Api>(urls, Provider::Type::Yandex, places::kYandexEnabledPlaces, {{}});
|
||||
AddApi<uber::Api>(urls, Provider::Type::Uber, {{}}, places::kUberDisabledPlaces);
|
||||
}
|
||||
|
||||
void Engine::SetDelegate(std::unique_ptr<Delegate> delegate) { m_delegate = std::move(delegate); }
|
||||
|
@ -126,7 +136,7 @@ uint64_t Engine::GetAvailableProducts(ms::LatLon const & from, ms::LatLon const
|
|||
|
||||
if (!IsAvailableAtPos(type, from))
|
||||
{
|
||||
maker->ProcessError(reqId, type, ErrorCode::NoProvider);
|
||||
maker->DecrementRequestCount(reqId);
|
||||
maker->MakeResult(reqId);
|
||||
continue;
|
||||
}
|
||||
|
@ -175,7 +185,10 @@ std::vector<Provider::Type> Engine::GetProvidersAtPos(ms::LatLon const & pos) co
|
|||
|
||||
bool Engine::IsAvailableAtPos(Provider::Type type, ms::LatLon const & pos) const
|
||||
{
|
||||
return !AreAllCountriesDisabled(type, pos) || IsAnyCountryEnabled(type, pos);
|
||||
if (AreAllCountriesDisabled(type, pos))
|
||||
return false;
|
||||
|
||||
return IsAnyCountryEnabled(type, pos);
|
||||
}
|
||||
|
||||
bool Engine::AreAllCountriesDisabled(Provider::Type type, ms::LatLon const & latlon) const
|
||||
|
|
|
@ -31,6 +31,8 @@ class ResultMaker
|
|||
public:
|
||||
void Reset(uint64_t requestId, size_t requestsCount, SuccessCallback const & successCallback,
|
||||
ErrorCallback const & errorCallback);
|
||||
/// Reduces number of requests outstanding.
|
||||
void DecrementRequestCount(uint64_t requestId);
|
||||
/// Processes successful callback from taxi api.
|
||||
void ProcessProducts(uint64_t requestId, Provider::Type type,
|
||||
std::vector<Product> const & products);
|
||||
|
|
|
@ -7,7 +7,13 @@ namespace taxi
|
|||
namespace places
|
||||
{
|
||||
// Places which are enabled for yandex taxi and in the same time are disabled for uber taxi.
|
||||
Countries const kPlaces = {
|
||||
Countries const kUberDisabledPlaces = {{{"Armenia", {}},
|
||||
{"Belarus", {}},
|
||||
{"Georgia Region", {}},
|
||||
{"Kazakhstan", {}},
|
||||
{"Russian Federation", {}}}};
|
||||
|
||||
Countries const kYandexEnabledPlaces = {
|
||||
{{"Armenia", {"Yerevan"}},
|
||||
{"Belarus", {"Minsk"}},
|
||||
{"Georgia Region", {"Tbilisi"}},
|
||||
|
|
|
@ -59,7 +59,6 @@ enum class ErrorCode
|
|||
{
|
||||
NoProducts,
|
||||
RemoteError,
|
||||
NoProvider
|
||||
};
|
||||
|
||||
struct ProviderError
|
||||
|
@ -87,7 +86,6 @@ inline std::string DebugPrint(ErrorCode code)
|
|||
{
|
||||
case ErrorCode::NoProducts: return "NoProducts";
|
||||
case ErrorCode::RemoteError: return "RemoteError";
|
||||
case ErrorCode::NoProvider: return "NoProvider";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue