Added Method to check if a service is stopped

This commit is contained in:
Jan Kevin Dick 2019-11-18 10:14:39 +01:00
parent 5a484c1896
commit 34ad48d16c
4 changed files with 25 additions and 1 deletions

View File

@ -95,6 +95,9 @@ public:
bool isRunning() const;
/// Returns true if the service is currently running.
bool isStopped() const;
/// Returns true if the service is currently stopped.
void start();
/// Starts the service.

View File

@ -164,6 +164,15 @@ bool WinService::isRunning() const
return ss.dwCurrentState == SERVICE_RUNNING;
}
bool WinService::isStopped() const
{
open();
SERVICE_STATUS ss;
if (!QueryServiceStatus(_svcHandle, &ss))
throw SystemException("cannot query service status", _name);
return ss.dwCurrentState == SERVICE_STOPPED;
}
void WinService::start()
{

View File

@ -1,7 +1,6 @@
#include "WinServiceTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/Util/WinService.h"
using Poco::Util::WinService;
@ -25,12 +24,21 @@ void WinServiceTest::testServiceCouldCreatedWithExistingConnection() {
assertTrue(spoolerService.isRegistered());
}
void WinServiceTest::testServiceReturnsTrueIfStopped() {
spoolerService_.stop();
assertTrue(spoolerService_.isStopped());
}
void WinServiceTest::setUp() {
}
void WinServiceTest::tearDown() {
if (spoolerService_.isStopped()) {
spoolerService_.start();
}
}
@ -38,6 +46,7 @@ CppUnit::Test* WinServiceTest::suite() {
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("WinServiceTest");
CppUnit_addTest(pSuite, WinServiceTest, testServiceCouldCreatedWithExistingConnection);
CppUnit_addTest(pSuite, WinServiceTest, testServiceReturnsTrueIfStopped);
return pSuite;
}

View File

@ -4,6 +4,7 @@
#include "Poco/Util/Util.h"
#include "CppUnit/TestCase.h"
#include "Poco/Util/WinService.h"
class WinServiceTest : public CppUnit::TestCase {
@ -12,6 +13,7 @@ public:
~WinServiceTest();
void testServiceCouldCreatedWithExistingConnection();
void testServiceReturnsTrueIfStopped();
void setUp();
void tearDown();
@ -19,6 +21,7 @@ public:
static CppUnit::Test* suite();
private:
Poco::Util::WinService spoolerService_{ "Spooler" };
};