ADD: added a sanitising function for the tracklist and tests
This commit is contained in:
@@ -98,9 +98,9 @@ IF (${TEST_SIMCONTROLLER_LIBRARY})
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
add_executable(test_podlist tests/test_podlist.cpp)
|
add_executable(test_tracklist tests/test_tracklist.cpp)
|
||||||
target_link_libraries(test_podlist Catch2::Catch2 SimControl loguru)
|
target_link_libraries(test_tracklist Catch2::Catch2 SimControl loguru)
|
||||||
catch_discover_tests(test_podlist)
|
catch_discover_tests(test_tracklist)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -6,13 +6,18 @@
|
|||||||
#include "nlohmann/json_fwd.hpp"
|
#include "nlohmann/json_fwd.hpp"
|
||||||
#include <SimCore/Identifier.hpp>
|
#include <SimCore/Identifier.hpp>
|
||||||
#include <SimCore/Messages/SimTrack.hpp>
|
#include <SimCore/Messages/SimTrack.hpp>
|
||||||
|
#include <cstddef>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace SimControl
|
namespace SimControl
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class TrackList
|
class TrackList
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -26,12 +31,19 @@ namespace SimControl
|
|||||||
|
|
||||||
|
|
||||||
void getJsonTRackList(nlohmann::json &message);
|
void getJsonTRackList(nlohmann::json &message);
|
||||||
|
size_t getTracklistSize();
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mutable std::mutex mx_;
|
mutable std::mutex mx_;
|
||||||
std::map<std::string, std::shared_ptr<SimCore::SimTrack>> TrackStore_;
|
std::map<std::string, std::shared_ptr<SimCore::SimTrack>> TrackStore_;
|
||||||
|
|
||||||
|
std::map<std::string, std::time_t> markedAsDeletedTracks_;
|
||||||
|
|
||||||
|
void checkTracksTimeout();
|
||||||
|
|
||||||
|
const int TrackTimeoutTime = 6;
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
#include "SimCore/SimCore.hpp"
|
#include "SimCore/SimCore.hpp"
|
||||||
#include "nlohmann/json_fwd.hpp"
|
#include "nlohmann/json_fwd.hpp"
|
||||||
#include <SimControl/Tracklist.hpp>
|
#include <SimControl/Tracklist.hpp>
|
||||||
|
#include <ctime>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -17,12 +20,44 @@ namespace SimControl
|
|||||||
void TrackList::addTrack(std::shared_ptr<SimCore::SimTrack> Track)
|
void TrackList::addTrack(std::shared_ptr<SimCore::SimTrack> Track)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mx_);
|
std::lock_guard<std::mutex> lock(mx_);
|
||||||
|
checkTracksTimeout();
|
||||||
|
|
||||||
|
auto it = markedAsDeletedTracks_.find(Track->getIdentifier().getUUID());
|
||||||
|
if (it == markedAsDeletedTracks_.end() )
|
||||||
|
{
|
||||||
auto [iterator, inserted] = TrackStore_.try_emplace(Track->getIdentifier().getUUID(),Track);
|
auto [iterator, inserted] = TrackStore_.try_emplace(Track->getIdentifier().getUUID(),Track);
|
||||||
if (!inserted) { iterator->second = Track; }
|
if (!inserted) { iterator->second = Track; }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void TrackList::checkTracksTimeout()
|
||||||
|
{
|
||||||
|
|
||||||
|
if (markedAsDeletedTracks_.size() == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( std::map<std::string,std::time_t>::iterator it=markedAsDeletedTracks_.begin(); it != markedAsDeletedTracks_.end();)
|
||||||
|
{
|
||||||
|
if (it->second + TrackTimeoutTime < time(0))
|
||||||
|
{
|
||||||
|
it = markedAsDeletedTracks_.erase(it);
|
||||||
|
}else {
|
||||||
|
it++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
std::shared_ptr<SimCore::SimTrack> TrackList::getTrack(const SimCore::Identifier ID)
|
std::shared_ptr<SimCore::SimTrack> TrackList::getTrack(const SimCore::Identifier ID)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mx_);
|
std::lock_guard<std::mutex> lock(mx_);
|
||||||
@@ -42,6 +77,7 @@ namespace SimControl
|
|||||||
void TrackList::deleteTrack(std::string ID)
|
void TrackList::deleteTrack(std::string ID)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mx_);
|
std::lock_guard<std::mutex> lock(mx_);
|
||||||
|
markedAsDeletedTracks_.emplace(std::make_pair(ID, std::time(0)));
|
||||||
auto it = TrackStore_.find(ID);
|
auto it = TrackStore_.find(ID);
|
||||||
TrackStore_.erase(it);
|
TrackStore_.erase(it);
|
||||||
}
|
}
|
||||||
@@ -49,6 +85,7 @@ namespace SimControl
|
|||||||
void TrackList::deleteTrack(SimCore::Identifier ID)
|
void TrackList::deleteTrack(SimCore::Identifier ID)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mx_);
|
std::lock_guard<std::mutex> lock(mx_);
|
||||||
|
markedAsDeletedTracks_.emplace(std::make_pair(ID.getUUID(), std::time(0)));
|
||||||
auto it = TrackStore_.find(ID.getUUID());
|
auto it = TrackStore_.find(ID.getUUID());
|
||||||
TrackStore_.erase(it);
|
TrackStore_.erase(it);
|
||||||
}
|
}
|
||||||
@@ -79,5 +116,14 @@ namespace SimControl
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
size_t TrackList::getTracklistSize()
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mx_);
|
||||||
|
|
||||||
|
return this->TrackStore_.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
110
tests/test_tracklist.cpp
Normal file
110
tests/test_tracklist.cpp
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include "SimControl/Tracklist.hpp"
|
||||||
|
#include "SimCore/Identifier.hpp"
|
||||||
|
#include "SimCore/Messages/SimTrack.hpp"
|
||||||
|
#include "SimCore/Position.hpp"
|
||||||
|
#include "SimCore/SimCore.hpp"
|
||||||
|
#include <fstream>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
#define CATCH_CONFIG_MAIN
|
||||||
|
#include "SimControl/PodList.hpp"
|
||||||
|
#include <catch2/catch.hpp>
|
||||||
|
#include <loguru.hpp>
|
||||||
|
|
||||||
|
#include "string.cpp"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void createScenario(SimControl::TrackList *list,std::vector<std::string> *ids)
|
||||||
|
{
|
||||||
|
GeographicLib::Geodesic geod(GeographicLib::Constants::WGS84_a(), GeographicLib::Constants::WGS84_f());
|
||||||
|
double lat = 54, lon = 1;
|
||||||
|
int counter = 0;
|
||||||
|
double distance = 10000;
|
||||||
|
|
||||||
|
int rasterSize = 5;
|
||||||
|
|
||||||
|
for (int i = 0; i < rasterSize; i++ )
|
||||||
|
{
|
||||||
|
double lonTmp = lon;
|
||||||
|
for (int a = 0; a < rasterSize; a++)
|
||||||
|
{
|
||||||
|
|
||||||
|
std::string name = "test";
|
||||||
|
name += std::to_string(counter);
|
||||||
|
double lat2, lon2;
|
||||||
|
geod.Direct(lat, lonTmp, 90, distance, lat2, lon2);
|
||||||
|
|
||||||
|
SimCore::Identifier id;
|
||||||
|
ids->push_back(id.getUUID());
|
||||||
|
|
||||||
|
SimCore::Position pos;
|
||||||
|
pos.setGeodesicPos(lat2, lon2, 0);
|
||||||
|
auto track = std::make_shared<SimCore::SimTrack>(id,name,SimCore::Kind::EntityKind::SURFACE,SimCore::Side::NEUTRAL);
|
||||||
|
track->setPosition(pos);
|
||||||
|
list->addTrack(track);
|
||||||
|
// SimControl::startNewShip(name, std::to_string(lat2), std::to_string(lon2), "0", "0", "0");
|
||||||
|
lonTmp = lon2;
|
||||||
|
counter ++;
|
||||||
|
}
|
||||||
|
double lat2, lon2;
|
||||||
|
|
||||||
|
geod.Direct(lat, lon, 0, distance, lat2, lon2);
|
||||||
|
|
||||||
|
lat = lat2;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
SCENARIO("Testing the the podlist")
|
||||||
|
{
|
||||||
|
|
||||||
|
std::vector<std::string> ids;
|
||||||
|
SimControl::TrackList list;
|
||||||
|
|
||||||
|
createScenario(&list,&ids);
|
||||||
|
|
||||||
|
auto track = list.getTrackBySringID(ids[3]);
|
||||||
|
|
||||||
|
// list.deleteTrack(track->getIdentifier());
|
||||||
|
|
||||||
|
// list.addTrack(track);
|
||||||
|
// std::this_thread::sleep_for(std::chrono::milliseconds(300));
|
||||||
|
|
||||||
|
|
||||||
|
GIVEN("different Attributes for a Track in different forms")
|
||||||
|
{
|
||||||
|
|
||||||
|
WHEN("constructing Track Object with data")
|
||||||
|
{
|
||||||
|
|
||||||
|
THEN("check if Track attributes are correct")
|
||||||
|
{
|
||||||
|
REQUIRE(ids.size() == 25);
|
||||||
|
REQUIRE(list.getTracklistSize() == 25);
|
||||||
|
|
||||||
|
list.deleteTrack(track->getIdentifier());
|
||||||
|
|
||||||
|
list.addTrack(track);
|
||||||
|
REQUIRE(list.getTracklistSize() == 24);
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(7500));
|
||||||
|
LOG_S(INFO)<<"add saved track";
|
||||||
|
list.addTrack(track);
|
||||||
|
LOG_S(INFO)<<"added saved track";
|
||||||
|
|
||||||
|
REQUIRE(list.getTracklistSize() == 25);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} //THEN
|
||||||
|
} // WHEN
|
||||||
|
} // GIVEN
|
||||||
|
} //SCENARIO
|
||||||
Reference in New Issue
Block a user