Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isDaprHealthy function to client #100

Merged
merged 3 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}