162 lines
4.1 KiB
C++
162 lines
4.1 KiB
C++
#include "stdafx.h"
|
|
#include "CppUnitTest.h"
|
|
#include "SiaApi.h"
|
|
#include "UploadManager.h"
|
|
#include "MockSiad.h"
|
|
#include "SiaDriveConfig.h"
|
|
#include "UnitTestConfig.h"
|
|
#include <DebugConsumer.h>
|
|
|
|
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
|
using namespace Sia::Api;
|
|
|
|
namespace UnitTests
|
|
{
|
|
class CEventAccumulator
|
|
{
|
|
public:
|
|
CEventAccumulator()
|
|
{
|
|
CEventSystem::EventSystem.AddEventConsumer([this](const CEvent& event)
|
|
{
|
|
this->ProcessEvent(event);
|
|
});
|
|
}
|
|
|
|
~CEventAccumulator()
|
|
{
|
|
Clear();
|
|
}
|
|
|
|
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:
|
|
void Clear()
|
|
{
|
|
std::lock_guard<std::mutex> l(_eventMutex);
|
|
_events.clear();
|
|
}
|
|
|
|
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(UploadManagerTest)
|
|
{
|
|
private:
|
|
const SiaHostConfig hostConfig = { L"127.0.0.1", 9988, TEST_SERVER_VERSION };
|
|
static std::unique_ptr<CMockSiad> siad;
|
|
static CDebugConsumer _debugConsumer;
|
|
static CEventAccumulator _eventAccumulator;
|
|
|
|
public:
|
|
TEST_METHOD_INITIALIZE(Initialize)
|
|
{
|
|
// Always delete DB before next test
|
|
CSiaDriveConfig config;
|
|
::DeleteFileA(config.GetRenter_UploadDbFilePath().c_str());
|
|
|
|
siad.reset(new CMockSiad(hostConfig));
|
|
}
|
|
|
|
TEST_METHOD_CLEANUP(DestroyTest)
|
|
{
|
|
siad->Stop();
|
|
CEventSystem::EventSystem.Stop();
|
|
}
|
|
|
|
TEST_CLASS_CLEANUP(DestroyClass)
|
|
{
|
|
siad->Stop();
|
|
CEventSystem::EventSystem.Stop();
|
|
}
|
|
|
|
TEST_METHOD(AddNewFile)
|
|
{
|
|
siad->Start(SiadTestType::UploadFile);
|
|
try
|
|
{
|
|
CEventSystem::EventSystem.Start();
|
|
_eventAccumulator.Clear();
|
|
|
|
CSiaDriveConfig driveConfig;
|
|
driveConfig.SetCacheFolder("./TestCacheFolder");
|
|
|
|
CSiaCurl siaCurl(hostConfig);
|
|
String version = siaCurl.GetServerVersion();
|
|
Assert::AreEqual(version.c_str(), TEST_SERVER_VERSION); // Connectivity test
|
|
|
|
CUploadManager uploadManager(siaCurl, &driveConfig);
|
|
uploadManager.AddOrUpdate(L"/test1/test.rtf", L"./TestCacheFolder/test1/test.rtf");
|
|
|
|
Assert::IsTrue(_eventAccumulator.WaitForEvent<NewFileAdded>(5000));
|
|
Assert::IsTrue(_eventAccumulator.WaitForEvent<CreatingTemporarySiaDriveFile>(5000));
|
|
Assert::IsTrue(_eventAccumulator.WaitForEvent<RenamingTemporarySiaDriveFile>(5000));
|
|
Assert::IsTrue(_eventAccumulator.WaitForEvent<UploadAddedToQueue>(5000));
|
|
|
|
Assert::IsFalse(_eventAccumulator.Contains<DatabaseInsertFailed>());
|
|
Assert::IsFalse(_eventAccumulator.Contains<DatabaseExceptionOccurred>());
|
|
Assert::IsFalse(_eventAccumulator.Contains<DeleteSiaDriveFileFailed>());
|
|
Assert::IsFalse(_eventAccumulator.Contains<RenamingTemporarySiaDriveFileFailed>());
|
|
Assert::IsFalse(_eventAccumulator.Contains<DeleteTemporarySiaDriveFileFailed>());
|
|
Assert::IsFalse(_eventAccumulator.Contains<CreatingTemporarySiaDriveFileFailed>());
|
|
Assert::IsFalse(_eventAccumulator.Contains<ModifyUploadStatusFailed>());
|
|
Assert::IsFalse(_eventAccumulator.Contains<ExistingUploadFound>());
|
|
|
|
Assert::IsTrue(uploadManager.GetUploadStatus(L"/test1/test.rtf") == UploadStatus::Queued);
|
|
}
|
|
catch (SQLite::Exception e)
|
|
{
|
|
siad->Stop();
|
|
CEventSystem::EventSystem.Stop();
|
|
_eventAccumulator.Clear();
|
|
Assert::Fail(CA2W(e.getErrorStr()));
|
|
}
|
|
|
|
siad->Stop();
|
|
CEventSystem::EventSystem.Stop();
|
|
_eventAccumulator.Clear();
|
|
}
|
|
};
|
|
|
|
std::unique_ptr<CMockSiad> UploadManagerTest::siad;
|
|
CDebugConsumer UploadManagerTest::_debugConsumer;
|
|
CEventAccumulator UploadManagerTest::_eventAccumulator;
|
|
} |