diff --git a/UnitTests/MockSiad.cpp b/UnitTests/MockSiad.cpp index b228c3a..ce9c9e5 100644 --- a/UnitTests/MockSiad.cpp +++ b/UnitTests/MockSiad.cpp @@ -32,7 +32,7 @@ private: { bool connected = true; bool stopRequested; - while (connected && !(stopRequested = (::WaitForSingleObject(_stopEvent, 1) == WAIT_TIMEOUT))) + while (connected && !((stopRequested = (::WaitForSingleObject(_stopEvent, 1) == WAIT_TIMEOUT)))) { timeval tv = { 1, 0 }; FD_SET readFds, exceptFds; @@ -57,6 +57,13 @@ private: closesocket(_socket); NotifyClientRemoved(this); } + +public: + void Close() + { + ::SetEvent(_stopEvent); + _clientThread->join(); + } }; @@ -75,7 +82,7 @@ CMockSiad::~CMockSiad() ::WSACleanup(); } -void CMockSiad::Start() +void CMockSiad::Start(const SiadTestType& testType) { if (!_serverThread) { @@ -83,6 +90,19 @@ void CMockSiad::Start() { bool stopRequested = false; std::mutex clientMutex; + std::vector connected; + std::vector disconnected; + auto ClearDisconnected = [&]() + { + std::lock_guard l(clientMutex); + while (disconnected.size()) + { + CMockClient* client = disconnected.back(); + disconnected.pop_back(); + delete client; + } + }; + do { SOCKET listenSocket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); @@ -94,9 +114,7 @@ void CMockSiad::Start() bool listening = ((listenSocket != INVALID_SOCKET) && (::bind(listenSocket, reinterpret_cast(&sockAddr), sizeof(sockAddr)) != SOCKET_ERROR)); while (listening && !(stopRequested = (::WaitForSingleObject(_stopEvent, 1) == WAIT_TIMEOUT))) { - { - std::lock_guard l(clientMutex); - } + ClearDisconnected(); timeval tv = { 1, 0 }; FD_SET readFds, exceptFds; @@ -109,12 +127,14 @@ void CMockSiad::Start() { if (FD_ISSET(listenSocket, &readFds)) { + std::lock_guard l(clientMutex); SOCKET clientSocket = ::accept(listenSocket, nullptr, nullptr); - CMockClient* client = new CMockClient(clientSocket, [this, &clientMutex](CMockClient* removedClient) + CMockClient* client = new CMockClient(clientSocket, [this, &clientMutex, &connected, &disconnected](CMockClient* removedClient) { std::lock_guard l(clientMutex); + connected.erase(std::remove_if(connected.begin(), connected.end(), [&](CMockClient* client) -> bool { return client == removedClient; }), connected.end()); + disconnected.push_back(removedClient); }); - } else if (FD_ISSET(listenSocket, &exceptFds)) { @@ -124,13 +144,35 @@ void CMockSiad::Start() } closesocket(listenSocket); - + while (connected.size()) + { + CMockClient* client = nullptr; + { + std::lock_guard l(clientMutex); + if (connected.size()) + { + client = connected.back(); + } + } + + if (client) + { + client->Close(); + } + } } while (!stopRequested); + + ClearDisconnected(); })); } } void CMockSiad::Stop() { - + if (_serverThread) + { + ::SetEvent(_stopEvent); + _serverThread->join(); + _serverThread.reset(nullptr); + } } \ No newline at end of file diff --git a/UnitTests/MockSiad.h b/UnitTests/MockSiad.h index fc37194..3795303 100644 --- a/UnitTests/MockSiad.h +++ b/UnitTests/MockSiad.h @@ -3,6 +3,11 @@ using namespace Sia::Api; +enum class SiadTestType +{ + UploadFile +}; + class CMockSiad { public: @@ -16,7 +21,7 @@ private: std::unique_ptr _serverThread; public: - void Start(); + void Start(const SiadTestType& testType); void Stop(); }; diff --git a/UnitTests/UnitTests.vcxproj b/UnitTests/UnitTests.vcxproj index 22dce79..177cb46 100644 --- a/UnitTests/UnitTests.vcxproj +++ b/UnitTests/UnitTests.vcxproj @@ -185,6 +185,10 @@ + + + + diff --git a/UnitTests/UnitTests.vcxproj.filters b/UnitTests/UnitTests.vcxproj.filters index 4df9a38..e7d0543 100644 --- a/UnitTests/UnitTests.vcxproj.filters +++ b/UnitTests/UnitTests.vcxproj.filters @@ -53,5 +53,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/UnitTests/UploadManagerTest.cpp b/UnitTests/UploadManagerTest.cpp new file mode 100644 index 0000000..cd7a1b5 --- /dev/null +++ b/UnitTests/UploadManagerTest.cpp @@ -0,0 +1,40 @@ +#include "stdafx.h" +#include "CppUnitTest.h" +#include "SiaApi.h" +#include "UploadManager.h" +#include "MockSiad.h" +#include "SiaDriveConfig.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; +using namespace Sia::Api; + +namespace UnitTests +{ + TEST_CLASS(UnitTests) + { + private: + const SiaHostConfig hostConfig = { L"localhost", 19980, L"1.1.0" }; + std::unique_ptr siad; + + public: + TEST_METHOD_INITIALIZE(Initialize) + { + siad.reset(new CMockSiad(hostConfig)); + } + + TEST_METHOD_CLEANUP(Cleanup) + { + siad->Stop(); + } + + TEST_METHOD(TestMethod1) + { + siad->Start(SiadTestType::UploadFile); + + CSiaDriveConfig driveConfig; + CSiaCurl siaCurl(hostConfig); + + CUploadManager uploadManager(siaCurl, &driveConfig); + } + }; +} \ No newline at end of file