Mock siad
This commit is contained in:
@@ -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<CMockClient*> connected;
|
||||
std::vector<CMockClient*> disconnected;
|
||||
auto ClearDisconnected = [&]()
|
||||
{
|
||||
std::lock_guard<std::mutex> 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<const struct sockaddr *>(&sockAddr), sizeof(sockAddr)) != SOCKET_ERROR));
|
||||
while (listening && !(stopRequested = (::WaitForSingleObject(_stopEvent, 1) == WAIT_TIMEOUT)))
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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);
|
||||
}
|
||||
}
|
@@ -3,6 +3,11 @@
|
||||
|
||||
using namespace Sia::Api;
|
||||
|
||||
enum class SiadTestType
|
||||
{
|
||||
UploadFile
|
||||
};
|
||||
|
||||
class CMockSiad
|
||||
{
|
||||
public:
|
||||
@@ -16,7 +21,7 @@ private:
|
||||
std::unique_ptr<std::thread> _serverThread;
|
||||
|
||||
public:
|
||||
void Start();
|
||||
void Start(const SiadTestType& testType);
|
||||
void Stop();
|
||||
};
|
||||
|
||||
|
@@ -185,6 +185,10 @@
|
||||
</ClCompile>
|
||||
<ClCompile Include="SiaCurlTests.cpp" />
|
||||
<ClCompile Include="UnitTestConfig.cpp" />
|
||||
<ClCompile Include="UploadManagerTest.cpp">
|
||||
<SubType>
|
||||
</SubType>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SiaDrive.Api\SiaDrive.Api.vcxproj">
|
||||
|
@@ -53,5 +53,8 @@
|
||||
<ClCompile Include="MockSiad.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="UploadManagerTest.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
40
UnitTests/UploadManagerTest.cpp
Normal file
40
UnitTests/UploadManagerTest.cpp
Normal file
@@ -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<CMockSiad> 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);
|
||||
}
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user