#include "stdafx.h" #include "CppUnitTest.h" #include "SiaApi.h" #include "UploadManager.h" #include "MockSiad.h" #include "SiaDriveConfig.h" #include "UnitTestConfig.h" #include 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> _events; std::mutex _eventMutex; private: void ProcessEvent(const CEvent& event) { std::lock_guard l(_eventMutex); _events.push_back(event.Clone()); } public: void Clear() { std::lock_guard l(_eventMutex); _events.clear(); } template bool WaitForEvent(DWORD timeoutMs = 5000) { bool found = false; do { { std::lock_guard l(_eventMutex); auto it = std::find_if(_events.begin(), _events.end(), [](const std::shared_ptr event)->bool { return (dynamic_cast(event.get()) != nullptr); }); found = it != _events.end(); } if (!found && timeoutMs--) { ::Sleep(1); } } while (!found && timeoutMs); return found; } template bool Contains() { return WaitForEvent(1); } }; TEST_CLASS(UnitTests) { private: const SiaHostConfig hostConfig = { L"127.0.0.1", 9988, TEST_SERVER_VERSION }; static std::unique_ptr 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(AddOrUpdateNoExisting) { siad->Start(SiadTestType::UploadFile); try { CEventSystem::EventSystem.Start(); _eventAccumulator.Clear(); CSiaDriveConfig driveConfig; 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(5000)); Assert::IsTrue(_eventAccumulator.WaitForEvent(5000)); Assert::IsTrue(_eventAccumulator.WaitForEvent(5000)); Assert::IsTrue(_eventAccumulator.WaitForEvent(5000)); Assert::IsFalse(_eventAccumulator.Contains()); Assert::IsFalse(_eventAccumulator.Contains()); Assert::IsFalse(_eventAccumulator.Contains()); Assert::IsFalse(_eventAccumulator.Contains()); Assert::IsFalse(_eventAccumulator.Contains()); Assert::IsFalse(_eventAccumulator.Contains()); 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 UnitTests::siad; CDebugConsumer UnitTests::_debugConsumer; CEventAccumulator UnitTests::_eventAccumulator; }