Skip to content

Commit

Permalink
remove logging exception from ServiceSupport stop,start,tick level (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mararok authored Jan 13, 2017
1 parent e3f0d2c commit eb15d9e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 26 deletions.
41 changes: 22 additions & 19 deletions src/SAREhub/Component/Worker/Service/ServiceSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,12 @@ abstract class ServiceSupport implements Service {
* @throws \Exception When something was wrong.
*/
public function start() {
try {
if (!$this->isStarted()) {
$this->getLogger()->info('service starting ...');
$this->doStart();
$this->started = true;
$this->stopped = false;
$this->getLogger()->info('service started');
}
} catch (\Exception $e) {
$this->getLogger()->error($e);
throw $e;
if (!$this->isStarted()) {
$this->getLogger()->info('service starting ...');
$this->doStart();
$this->started = true;
$this->stopped = false;
$this->getLogger()->info('service started');
}
}

Expand All @@ -44,8 +39,12 @@ public function tick() {
$this->doTick();
}
} catch (\Exception $e) {
$this->getLogger()->error($e);
$this->stop();
try {
$this->stop();
} catch (\Exception $e2) {
// we wants only orginal exception
}

throw $e;
}
}
Expand All @@ -59,9 +58,6 @@ public function stop() {
try {
$this->getLogger()->info('service stopping ...');
$this->doStop();
} catch (\Exception $e) {
$this->getLogger()->error($e);
throw $e;
} finally {
$this->started = false;
$this->stopped = true;
Expand Down Expand Up @@ -107,24 +103,31 @@ public function setLogger(LoggerInterface $logger) {
if ($this->getLogger() === $logger) {
throw new \LogicException('set same logger instance');
}

$this->logger = $logger;
}

/**
* Contains custom worker start logic
* @throws \Exception When something was wrong.
*/
protected abstract function doStart();
protected function doStart() {

}

/**
* Contains custom worker tick logic
* @throws \Exception When something was wrong.
*/
protected abstract function doTick();
protected function doTick() {

}

/**
* Contains custom worker stop logic
* @throws \Exception When something was wrong.
*/
protected abstract function doStop();
protected function doStop() {

}
}
38 changes: 31 additions & 7 deletions tests/unit/SAREhub/Component/Worker/Service/ServiceSupportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,37 @@ public function testTickWhenNotRunningThenNotDoTick() {
$this->assertFalse($spy->hasBeenInvoked());
}

public function testTickWhenDoTickThrowExceptionThenThrowed() {
$this->service->start();
$this->service->method('doTick')->willThrowException(new Exception('exception'));
$this->expectException(\Exception::class);
$this->service->tick();
}

public function testTickWhenDoTickThrowExceptionAndDoStopThrowExceptionThenThrowOrginalException() {
$this->service->start();
$orginalException = new Exception('exception');
$this->service->method('doTick')->willThrowException($orginalException);
$this->service->method('doStop')->willThrowException(new Exception('exception2'));
try {
$this->service->tick();
} catch (\Exception $e) {
$this->assertSame($orginalException, $e);
}
}

public function testTickWhenDoTickThrowExceptionThenStopped() {
$this->service->start();
$this->service->method('doTick')->willThrowException(new Exception('exception'));
try {
$this->service->tick();
} catch (\Exception $e) {
$this->assertFalse($this->service->isRunning());
}
}



public function testSetLoggerWhenSetsAndSetSameThenException() {
$logger = new NullLogger();
$this->service->setLogger($logger);
Expand All @@ -135,13 +166,6 @@ public function testStopWhenDoStopThrowExceptionThenStopped() {
$this->assertTrue($this->service->isStopped());
}

public function testTickWhenDoTickThrowExceptionThenStopped() {
$this->service->start();
$this->service->method('doTick')->willThrowException(new Exception('exception'));
$this->expectException(Exception::class);
$this->service->tick();
$this->assertFalse($this->service->isRunning());
}

private function getMethodSpy($method) {
$this->service->expects($methodSpy = $this->any())->method($method);
Expand Down

0 comments on commit eb15d9e

Please sign in to comment.