Skip to content

Commit

Permalink
Add isDaprHealthy function to client (#100)
Browse files Browse the repository at this point in the history
* Add healthz endpoint support

* Add doc string

* Add tests
  • Loading branch information
withinboredom authored Jul 16, 2021
1 parent debcd71 commit f87e3d3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/lib/Client/DaprClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,13 @@ abstract public function getBulkSecretAsync(string $storeName, array $metadata =
*/
abstract public function getBulkSecret(string $storeName, array $metadata = []): array;

/**
* Check if the daprd instance is up and running.
*
* @return bool True if it is running, else false.
*/
abstract public function isDaprHealthy(): bool;

/**
* @param string $token
* @return null|array{dapr-api-token: string}
Expand Down
13 changes: 13 additions & 0 deletions src/lib/Client/DaprHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,17 @@ public function __construct(
]
);
}

public function isDaprHealthy(): bool
{
try {
$result = $this->httpClient->get('/v1.0/healthz');
if (200 === $result->getStatusCode()) {
return true;
}
return false;
} catch (\Throwable $exception) {
return false;
}
}
}
48 changes: 48 additions & 0 deletions tests/HealthTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;

/**
* Class HealthTest
*/
class HealthTest extends DaprTests
{
public function testIsHealthy()
{
$container = $this->get_http_client_stack(
[
new Response(200)
]
);
$client = $this->get_new_client_with_http($container->client);
$this->assertTrue($client->isDaprHealthy());
$request = $container->history[0]['request'];
$this->assertRequestUri('/v1.0/healthz', $request);
}

public function testIsNotHealthy() {
$container = $this->get_http_client_stack(
[
new Response(500)
]
);
$client = $this->get_new_client_with_http($container->client);
$this->assertFalse($client->isDaprHealthy());
$request = $container->history[0]['request'];
$this->assertRequestUri('/v1.0/healthz', $request);
}

public function testTimeout() {
$container = $this->get_http_client_stack(
[
new RequestException('timed out', new Request('GET', 'test'))
]
);
$client = $this->get_new_client_with_http($container->client);
$this->assertFalse($client->isDaprHealthy());
$request = $container->history[0]['request'];
$this->assertRequestUri('/v1.0/healthz', $request);
}
}

0 comments on commit f87e3d3

Please sign in to comment.