Upload manager tests
This commit is contained in:
@@ -12,6 +12,61 @@ using namespace Sia::Api;
|
||||
|
||||
namespace UnitTests
|
||||
{
|
||||
class CEventAccumulator
|
||||
{
|
||||
public:
|
||||
CEventAccumulator()
|
||||
{
|
||||
CEventSystem::EventSystem.AddEventConsumer([this](const CEvent& event)
|
||||
{
|
||||
this->ProcessEvent(event);
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
std::deque<std::shared_ptr<CEvent>> _events;
|
||||
std::mutex _eventMutex;
|
||||
|
||||
private:
|
||||
void ProcessEvent(const CEvent& event)
|
||||
{
|
||||
std::lock_guard<std::mutex> l(_eventMutex);
|
||||
_events.push_back(event.Clone());
|
||||
}
|
||||
|
||||
public:
|
||||
template<typename T>
|
||||
bool WaitForEvent(DWORD timeoutMs = 5000)
|
||||
{
|
||||
bool found = false;
|
||||
do
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> l(_eventMutex);
|
||||
auto it = std::find_if(_events.begin(), _events.end(), [](const std::shared_ptr<CEvent> event)->bool
|
||||
{
|
||||
return (dynamic_cast<const T*>(event.get()) != nullptr);
|
||||
});
|
||||
|
||||
found = it != _events.end();
|
||||
}
|
||||
|
||||
if (!found && timeoutMs--)
|
||||
{
|
||||
::Sleep(1);
|
||||
}
|
||||
} while (!found && timeoutMs);
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool Contains()
|
||||
{
|
||||
return WaitForEvent<T>(1);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CLASS(UnitTests)
|
||||
{
|
||||
private:
|
||||
@@ -20,11 +75,6 @@ namespace UnitTests
|
||||
static CDebugConsumer _debugConsumer;
|
||||
|
||||
public:
|
||||
TEST_CLASS_INITIALIZE(ClassInit)
|
||||
{
|
||||
CEventSystem::EventSystem.Start();
|
||||
}
|
||||
|
||||
TEST_METHOD_INITIALIZE(Initialize)
|
||||
{
|
||||
// Always delete DB before next test
|
||||
@@ -37,11 +87,13 @@ namespace UnitTests
|
||||
TEST_METHOD_CLEANUP(DestroyTest)
|
||||
{
|
||||
siad->Stop();
|
||||
CEventSystem::EventSystem.Stop();
|
||||
}
|
||||
|
||||
TEST_CLASS_CLEANUP(DestroyClass)
|
||||
{
|
||||
siad->Stop();
|
||||
CEventSystem::EventSystem.Stop();
|
||||
}
|
||||
|
||||
TEST_METHOD(AddOrUpdateNoExisting)
|
||||
@@ -49,6 +101,10 @@ namespace UnitTests
|
||||
siad->Start(SiadTestType::UploadFile);
|
||||
try
|
||||
{
|
||||
CEventAccumulator eventAccumulator;
|
||||
|
||||
CEventSystem::EventSystem.Start();
|
||||
|
||||
CSiaDriveConfig driveConfig;
|
||||
CSiaCurl siaCurl(hostConfig);
|
||||
String version = siaCurl.GetServerVersion();
|
||||
@@ -56,15 +112,25 @@ namespace UnitTests
|
||||
|
||||
CUploadManager uploadManager(siaCurl, &driveConfig);
|
||||
uploadManager.AddOrUpdate(L"/test1/test.rtf", L"./TestCacheFolder/test1/test.rtf");
|
||||
Sleep(5000);
|
||||
|
||||
Assert::IsTrue(eventAccumulator.WaitForEvent<NewUploadAdded>(5000));
|
||||
Assert::IsTrue(eventAccumulator.WaitForEvent<CreatingTemporarySiaDriveFile>(5000));
|
||||
Assert::IsTrue(eventAccumulator.WaitForEvent<RenamingTemporarySiaDriveFile>(5000));
|
||||
|
||||
Assert::IsFalse(eventAccumulator.Contains<DeleteSiaDriveFileFailed>());
|
||||
Assert::IsFalse(eventAccumulator.Contains<RenamingTemporarySiaDriveFileFailed>());
|
||||
Assert::IsFalse(eventAccumulator.Contains<DeleteTemporarySiaDriveFileFailed>());
|
||||
Assert::IsFalse(eventAccumulator.Contains<CreatingTemporarySiaDriveFileFailed>());
|
||||
Assert::IsFalse(eventAccumulator.Contains<CreatingTemporarySiaDriveFileFailed>());
|
||||
}
|
||||
catch (SQLite::Exception e)
|
||||
{
|
||||
siad->Stop();
|
||||
throw;
|
||||
CEventSystem::EventSystem.Stop();
|
||||
}
|
||||
|
||||
siad->Stop();
|
||||
CEventSystem::EventSystem.Stop();
|
||||
}
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user