Skip to content

Commit

Permalink
Stop the request timer if it's still running when we send the request
Browse files Browse the repository at this point in the history
  • Loading branch information
asgrim committed Oct 10, 2019
1 parent 3dcf743 commit 378b2c0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ public function send() : bool
return false;
}

$this->request->stopIfRunning();

return $this->connector->sendCommand($this->request);
} catch (NotConnected $notConnected) {
$this->logger->error($notConnected->getMessage());
Expand Down
9 changes: 9 additions & 0 deletions src/Events/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ public function __construct()
$this->currentCommand = $this;
}

public function stopIfRunning() : void
{
if ($this->timer->getStop() !== null) {
return;
}

$this->stop();
}

public function stop(?float $overrideTimestamp = null) : void
{
$this->timer->stop($overrideTimestamp);
Expand Down
31 changes: 30 additions & 1 deletion tests/Unit/Events/Request/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

use PHPUnit\Framework\TestCase;
use Scoutapm\Events\Request\Request;
use function json_decode;
use function json_encode;
use function next;
use function reset;
use function time;

/** @covers \Scoutapm\Events\Request\Request */
final class RequestTest extends TestCase
Expand All @@ -21,8 +24,34 @@ public function testCanBeInitialized() : void
public function testCanBeStopped() : void
{
$request = new Request();

self::assertNull(json_decode(json_encode($request), true)['BatchCommand']['commands'][1]['FinishRequest']['timestamp']);

$request->stop();
self::assertNotNull($request);

self::assertIsString(json_decode(json_encode($request), true)['BatchCommand']['commands'][1]['FinishRequest']['timestamp']);
}

public function testRequestIsStoppedIfRunning() : void
{
$request = new Request();

self::assertNull(json_decode(json_encode($request), true)['BatchCommand']['commands'][1]['FinishRequest']['timestamp']);

$request->stopIfRunning();

self::assertIsString(json_decode(json_encode($request), true)['BatchCommand']['commands'][1]['FinishRequest']['timestamp']);
}

public function testRequestFinishTimestampIsNotChangedWhenStopIfRunningIsCalledOnAStoppedRequest() : void
{
$request = new Request();
$request->stop(time() - 100.0);
$originalStopTime = json_decode(json_encode($request), true)['BatchCommand']['commands'][1]['FinishRequest']['timestamp'];

$request->stopIfRunning();

self::assertSame($originalStopTime, json_decode(json_encode($request), true)['BatchCommand']['commands'][1]['FinishRequest']['timestamp']);
}

public function testJsonSerializes() : void
Expand Down

0 comments on commit 378b2c0

Please sign in to comment.