Skip to content

Commit

Permalink
Development/valuerecorder (#1722)
Browse files Browse the repository at this point in the history
* [core / Tests/unit/core] : Fix build 'test_valuerecorder'.

* [core / Tests/unit/core] : prepare 'test_value_recorder' for unpredictable failure.

* [Tests/unit/core] : fix build 'test_valuerecorder'

* [Tests/unit/core] : redesign 'test_value_recorder'

* Tests/unit/core] : Always reset to a valid position

* [Tests/unit/core] : Explicitly save the content of the writer before employing the reader

* [Tests/unit/core] : Adjust the expectations for multiple files

---------

Co-authored-by: Pierre Wielders <[email protected]>
  • Loading branch information
msieben and pwielders authored Aug 15, 2024
1 parent 7e4fdbd commit 2a4a942
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Tests/unit/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ add_executable(${TEST_RUNNER_NAME}
test_time.cpp
test_timer.cpp
test_tristate.cpp
#test_valuerecorder.cpp
test_valuerecorder.cpp
test_weblinkjson.cpp
test_weblinktext.cpp
test_websocketjson.cpp
Expand Down
178 changes: 84 additions & 94 deletions Tests/unit/core/test_valuerecorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,112 +29,102 @@ namespace Thunder {
namespace Tests {
namespace Core {

const unsigned int BLOCKSIZE = 20;
constexpr unsigned int BLOCKSIZE = 20;

class WriterClass : public RecorderType<uint32_t, BLOCKSIZE>::Writer
class WriterClass
{
public:
WriterClass() = delete;

WriterClass(string filename)
: Writer(filename)
, _file(filename)
{
}

~WriterClass()
{
}

public:
void WriterJob()
{
uint8_t arr[] = {1,2,3};
SetBuffer(arr);
auto object = Create(_file);
Record(10);
uint64_t TimeValue = Time();
std::string storageName = Source();
uint32_t value = Value();
object.Release();
}

private:
string _file;
public:
WriterClass() = delete;

WriterClass(const string& filename)
: _file{filename}
{
_writer = ::Thunder::Core::RecorderType<uint32_t, BLOCKSIZE>::Writer::Create(filename);
}

~WriterClass()
{
_writer.Release();
}

public:
void WriterJob(const uint32_t& value)
{
ASSERT_TRUE(_writer.IsValid());

_writer->Record(value);

EXPECT_STREQ(_writer->Source().c_str(), _file.c_str());
EXPECT_EQ(_writer->Value(), value);
}

void Save()
{
_writer->Save();
}

private:
const string _file;
::Thunder::Core::ProxyType<::Thunder::Core::RecorderType<uint32_t, BLOCKSIZE>::Writer> _writer;
};

class ReaderClass : public RecorderType<uint32_t, BLOCKSIZE>::Reader
class ReaderClass : public ::Thunder::Core::RecorderType<uint32_t, BLOCKSIZE>::Reader
{
public:
ReaderClass() = delete;

ReaderClass(string filename)
: Reader(filename)
, _file(filename)
{
}

ReaderClass(const ProxyType<WriterClass>& recorder, const uint32_t id = static_cast<uint32_t>(~0))
: Reader(recorder->Source())
, _file(recorder->Source())
{
}

~ReaderClass()
{
}

public:
void ReaderJob()
{
Next();
EXPECT_TRUE(IsValid());

uint32_t time = 20;
::Thunder::Core::Time curTime = ::Thunder::Core::Time::Now();
curTime.Add(time);
uint32_t index = Store(curTime.Ticks(), 1);

StepForward();
StepBack();
ClearData();

Reader obj1(_file, 1u);
EXPECT_FALSE(obj1.Previous());
EXPECT_TRUE(obj1.Next());

EXPECT_EQ(StartId(),1u);

if (EndId() == StartId())
EXPECT_EQ(EndId(),1u);
else
EXPECT_EQ(EndId(),2u);

uint32_t id = EndId();
std::string storageName = Source();
}

private:
string _file;
};
public:
ReaderClass() = delete;

TEST(test_valuerecorder, test_writer)
{
string filename = "baseRecorder.txt";
ReaderClass(const string& filename)
: _file{filename}
, Reader(filename)
{
}

~ReaderClass() = default;

public:
void ReaderJob(const uint32_t value)
{
static_assert(std::is_same<::Thunder::Core::Time::microsecondsfromepoch, uint64_t>::value);

const ::Thunder::Core::Time::microsecondsfromepoch readTime = ::Thunder::Core::Time::Now().Ticks();

// Get a valid position
Reset(StartId());

ASSERT_TRUE(IsValid());

EXPECT_EQ(Id(), StartId());

auto obj1 = RecorderType<uint32_t, BLOCKSIZE>::Writer::Create(filename);
EXPECT_STREQ(Source().c_str(), _file.c_str());
EXPECT_EQ(value, Value());

obj1->Copy(*(obj1),1);
obj1->Copy(*(obj1),100);
ASSERT_EQ(Id(), EndId());

static_cast<WriterClass&>(*obj1).WriterJob();
// Load next file if it exist if no additional data exist
EXPECT_FALSE(Next());

// The previous failed so no new files has been loaded and the current file is still used
EXPECT_TRUE(Previous());

EXPECT_LE(Time(), readTime);
}

private:
const string _file;
};

TEST(test_valuerecorder, test_writer)
{
const string filename = "baseRecorder.txt";

ReaderClass obj2(filename);
obj2.ReaderJob();
constexpr uint32_t value = 10;

ReaderClass obj4(ProxyType<WriterClass>(obj3));
WriterClass writer(filename);
writer.WriterJob(value);
writer.Save();

obj1.Release();
ReaderClass reader(filename);
reader.ReaderJob(value);
}

} // Core
Expand Down

0 comments on commit 2a4a942

Please sign in to comment.