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

Remove sleep from integration test #102

Merged
merged 11 commits into from
Jul 17, 2021
3 changes: 3 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ jobs:
run: composer run-script lint
build-integration-test-containers:
runs-on: ubuntu-latest
needs:
- lint
- unit-tests
strategy:
matrix:
image: [ 'caddy', 'tests' ]
Expand Down
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

export GITHUB_SHA=latest
export DAPR_VERSION=1.3.0-rc.1

.PHONY: integration-tests
integration-tests: build
docker-compose down -v
docker-compose up &
sleep 10
curl --silent --output /tmp/test-results.json --write-out "%{http_code}" http://localhost:9502/do_tests
docker-compose down -v
cat /tmp/test-results.json | jq .

composer.lock: composer.json
composer update

vendor/autoload.php: composer.lock
composer install
touch vendor/autoload.php

.PHONY: build
build: build-caddy build-tests

.PHONY: build-tests
build-tests: vendor/autoload.php
docker build -t tests:latest -f images/tests.Dockerfile .

.PHONY: build-caddy
build-caddy: vendor/autoload.php
docker build -t caddy:latest -f images/caddy.Dockerfile .
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# A Docker Compose file for spinning up integration tests in CI
# A Docker Compose file for spinning up integration tests in CI, you can run these locally by running `make`
version: "3"
services:
placement:
Expand Down
1 change: 0 additions & 1 deletion images/caddy.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# syntax=docker/dockerfile:labs
FROM caddy AS base
COPY images/Caddyfile /etc/caddy/Caddyfile
COPY . /tests
2 changes: 0 additions & 2 deletions images/tests.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# syntax=docker/dockerfile:labs
# withinboredom/php-base-min == docker build --pull -t withinboredom/php-base-min --target base -f images/tests.Dockerfile .
FROM php:8.0-fpm AS base
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
RUN apt-get update && apt-get install -y wget git unzip && apt-get clean
Expand Down
20 changes: 20 additions & 0 deletions src/index.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

// @codeCoverageIgnoreStart

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../tests/Fixtures/SimpleActor.php';

Expand Down Expand Up @@ -435,6 +437,17 @@ function (
$app->get(
'/do_tests',
function (\Dapr\Client\DaprClient $client) {
while (true) {
sleep(1);
if ($client->isDaprHealthy()) {
$meta = $client->getMetadata();
error_log(print_r($meta, true));
if (!empty($meta->actors)) {
break;
}
}
}

$test_results = [
'test/actors' => null,
'test/binding' => null,
Expand Down Expand Up @@ -463,6 +476,13 @@ function (\Dapr\Client\DaprClient $client) {
];
}

$client->shutdown(afterRequest: false);

while ($client->isDaprHealthy()) {
sleep(1);
error_log('waiting for daprd shutdown...');
}

return new \Nyholm\Psr7\Response($has_failed ? 500 : 200, body: json_encode($test_results));
}
);
Expand Down
14 changes: 14 additions & 0 deletions src/lib/Client/DaprClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,20 @@ abstract public function getBulkSecret(string $storeName, array $metadata = []):
*/
abstract public function isDaprHealthy(): bool;

/**
* Retrieve metadata from the sidecar
*
* @return MetadataResponse
*/
abstract public function getMetadata(): MetadataResponse|null;

/**
* Shutdown the Daprd sidecar
*
* @param bool $afterRequest If true, schedules a php shutdown function, otherwise fires the request immediately.
*/
abstract public function shutdown(bool $afterRequest = true): void;

/**
* @param string $token
* @return null|array{dapr-api-token: string}
Expand Down
24 changes: 22 additions & 2 deletions src/lib/Client/DaprHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Dapr\Deserialization\IDeserializer;
use Dapr\Serialization\ISerializer;
use Dapr\State\StateItem;
use GuzzleHttp\Client;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -49,12 +48,33 @@ public function isDaprHealthy(): bool
{
try {
$result = $this->httpClient->get('/v1.0/healthz');
if (200 === $result->getStatusCode()) {
if (204 === $result->getStatusCode()) {
return true;
}
return false;
} catch (\Throwable $exception) {
return false;
}
}

public function getMetadata(): MetadataResponse|null
{
try {
$result = $this->httpClient->get('/v1.0/metadata');
return $this->deserializer->from_json(MetadataResponse::class, $result->getBody()->getContents());
} catch (\Throwable $exception) {
return null;
}
}

public function shutdown(bool $afterRequest = true): void
{
$shutdown = fn() => $this->httpClient->post('/v1.0/shutdown');
if ($afterRequest) {
register_shutdown_function($shutdown);
return;
}

$shutdown();
}
}
47 changes: 47 additions & 0 deletions src/lib/Client/MetadataResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Dapr\Client;

use Dapr\Deserialization\Attributes\ArrayOf;

/**
* Class MetadataResponse
* @package Dapr\Client
*/
class MetadataResponse
{
/**
* MetadataResponse constructor.
* @param string $id
* @param RegisteredActor[] $actors
* @param array $extended
* @param RegisteredComponent[] $components
*/
public function __construct(
public string $id,
#[ArrayOf(RegisteredActor::class)]
public array $actors,
public array $extended,
#[ArrayOf(RegisteredComponent::class)]
public array $components
) {
}
}

/**
* Class RegisteredActor
* @package Dapr\Client
*/
class RegisteredActor
{
public function __construct(public string $type, public int $count)
{
}
}

class RegisteredComponent
{
public function __construct(public string $name, public string $type, public string $version)
{
}
}
2 changes: 1 addition & 1 deletion tests/HealthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function testIsHealthy()
{
$container = $this->get_http_client_stack(
[
new Response(200)
new Response(204)
]
);
$client = $this->get_new_client_with_http($container->client);
Expand Down
55 changes: 55 additions & 0 deletions tests/MetadataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

require_once __DIR__ . '/DaprTests.php';

/**
* Class MetadataTest
*/
class MetadataTest extends DaprTests
{
public function testSuccessfulMetadata()
{
$stack = $this->get_http_client_stack(
[
new \GuzzleHttp\Psr7\Response(
200, body: json_encode(
[
'id' => 'demo-actor',
'actors' => [
[
'type' => 'DemoActor',
'count' => 1
]
],
'extended' => [
'cliPID' => '12301823',
'appCommand' => 'uvicorn --port 3000 demo_actor_service:app'
],
'components' => [
[
'name' => 'pubsub',
'type' => 'pubsub.redis',
'version' => ''
]
]
]
)
)
]
);
$client = $this->get_new_client_with_http($stack->client);
$result = $client->getMetadata();
$this->assertEquals(
new \Dapr\Client\MetadataResponse(
'demo-actor',
[new \Dapr\Client\RegisteredActor('DemoActor', 1)],
[
'cliPID' => '12301823',
'appCommand' => 'uvicorn --port 3000 demo_actor_service:app'
],
[new \Dapr\Client\RegisteredComponent('pubsub', 'pubsub.redis', '')]
),
$result
);
}
}