Skip to content

Commit

Permalink
Rename await to flush and add it to the client
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive committed May 7, 2019
1 parent 5f48b93 commit 69bc1e6
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 51 deletions.
11 changes: 6 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Sentry\Integration\Handler;
use Sentry\Integration\IntegrationInterface;
use Sentry\State\Scope;
use Sentry\Transport\AsyncTransportInterface;
use Sentry\Transport\TransportInterface;

/**
Expand Down Expand Up @@ -70,13 +71,13 @@ public function getOptions(): Options
}

/**
* Returns the transport of the client.
*
* @return TransportInterface
* Flush the event queue and make sure all events are sent if possible.
*/
public function getTransport(): TransportInterface
public function flush(): void
{
return $this->transport;
if ($this->transport instanceof AsyncTransportInterface) {
$this->transport->flush();
}
}

/**
Expand Down
16 changes: 0 additions & 16 deletions src/State/Hub.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,22 +201,6 @@ public function addBreadcrumb(Breadcrumb $breadcrumb): bool
return null !== $breadcrumb;
}

/**
* {@inheritdoc}
*/
public function await(): void
{
$client = $this->getClient();

if ($client instanceof Client) {
$transport = $client->getTransport();

if ($transport instanceof AsyncTransportInterface) {
$transport->await();
}
}
}

/**
* {@inheritdoc}
*/
Expand Down
6 changes: 0 additions & 6 deletions src/State/HubInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,6 @@ public function captureLastError(): ?string;
*/
public function addBreadcrumb(Breadcrumb $breadcrumb): bool;

/**
* Await all async transports, this effectively forces all async event to be
* transported immediately.
*/
public function await(): void;

/**
* Returns the current global Hub.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Transport/AsyncTransportInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ interface AsyncTransportInterface extends TransportInterface
/**
* Wait for all events to be transported.
*/
public function await(): void;
public function flush(): void;
}
12 changes: 6 additions & 6 deletions src/Transport/HttpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct(Options $config, HttpAsyncClient $httpClient, Reques
// By calling the cleanupPendingRequests function from a shutdown function
// registered inside another shutdown function we can be confident that it
// will be executed last
register_shutdown_function('register_shutdown_function', \Closure::fromCallable([$this, 'await']));
register_shutdown_function('register_shutdown_function', \Closure::fromCallable([$this, 'flush']));
}

/**
Expand All @@ -63,7 +63,7 @@ public function __construct(Options $config, HttpAsyncClient $httpClient, Reques
*/
public function __destruct()
{
$this->await();
$this->flush();
}

/**
Expand Down Expand Up @@ -99,12 +99,12 @@ public function send(Event $event): ?string
}

/**
* Cleanups the pending requests by forcing them to be sent. Any error that
* occurs will be ignored.
* Cleanup the pending requests by forcing them to be sent.
* Any error that occurs will be ignored to prevent recursion.
*/
public function await(): void
public function flush(): void
{
while ($pendingRequest = array_pop($this->pendingRequests)) {
foreach ($this->pendingRequests as $pendingRequest) {
try {
$pendingRequest->wait();
} catch (\Throwable $exception) {
Expand Down
16 changes: 16 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Sentry\Serializer\SerializerInterface;
use Sentry\Severity;
use Sentry\Stacktrace;
use Sentry\Transport\AsyncTransportInterface;
use Sentry\Transport\TransportInterface;

class ClientTest extends TestCase
Expand Down Expand Up @@ -431,6 +432,21 @@ public function testAttachStacktraceShouldNotWorkWithCaptureEvent(): void
$client->captureEvent([]);
}

public function testClientFlushEvents(): void
{
/** @var AsyncTransportInterface|MockObject $asyncTransport */
$asyncTransport = $this->createMock(AsyncTransportInterface::class);
$asyncTransport->expects($this->once())
->method('flush');

/** @var Client $client */
$client = ClientBuilder::create()
->setTransport($asyncTransport)
->getClient();

$client->flush();
}

/**
* @see https://github.com/symfony/polyfill/blob/52332f49d18c413699d2dccf465234356f8e0b2c/src/Php70/Php70.php#L52-L61
*/
Expand Down
16 changes: 0 additions & 16 deletions tests/State/HubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,22 +296,6 @@ public function testCaptureEvent(): void
$this->assertEquals('2b867534eead412cbdb882fd5d441690', $hub->captureEvent(['message' => 'test']));
}

public function testClientAwaitAsync(): void
{
/** @var AsyncTransportInterface|MockObject $asyncTransport */
$asyncTransport = $this->createMock(AsyncTransportInterface::class);
$asyncTransport->expects($this->once())
->method('await');

/** @var Client $client */
$client = ClientBuilder::create()
->setTransport($asyncTransport)
->getClient();

$hub = new Hub($client);
$hub->await();
}

private function getScope(HubInterface $hub): Scope
{
$method = new \ReflectionMethod($hub, 'getScope');
Expand Down
2 changes: 1 addition & 1 deletion tests/Transport/HttpTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function testCleanupPendingRequests(): void

$this->assertAttributeNotEmpty('pendingRequests', $transport);

$transport->await();
$transport->flush();
}

public function testSendFailureCleanupPendingRequests(): void
Expand Down

0 comments on commit 69bc1e6

Please sign in to comment.