Skip to content

Commit

Permalink
more comments and adjustments for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
deminy committed May 30, 2018
1 parent f6bf0a2 commit c8c5c4f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 30 deletions.
14 changes: 7 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ RUN \
docker-php-ext-install zip && \
yes '' | pecl install apcu && \
docker-php-ext-enable apcu && \
curl \
-sf \
--connect-timeout 5 \
--max-time 15 \
--retry 5 \
--retry-delay 2 \
--retry-max-time 60 \
curl \
-sf \
--connect-timeout 5 \
--max-time 15 \
--retry 5 \
--retry-delay 2 \
--retry-max-time 60 \
http://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer

COPY . /var/www/html
Expand Down
4 changes: 4 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ services:
- php
php:
build: .
volumes:
# The container works fine without this volume. However, adding this volume allows us to update the PHP
# script for testing purpose while the Docker instances are running.
- ./index.php:/var/www/html/index.php
55 changes: 36 additions & 19 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
<?php
/**
* This PHP page is used under Nginx/PHP-FPM for unit test purpose. It accepts two request parameters:
* 1. parameter "start" to store given value in APCu and return it back in HTTP response;
* 1. parameter "end" to store given value in APCu after HTTP response sent back from PHP-FPM/Nginx.
* This PHP page is used under Nginx/PHP-FPM for unit test purpose. It accepts three request parameters:
* 1. Parameter "reset" to wipe data cached in APCu.
* 2. If parameter "reset" not set:
* 2.1. Parameter "start" to store given value in APCu and return it back in HTTP response;
* 2.2. Parameter "end" to store given value in APCu after HTTP response sent back from PHP-FPM/Nginx.
*
* Examples:
* # Wipe cached data.
* curl -vi "http://127.0.0.1/?reset"
*
* # Store variable $start in cache and return it back to HTTP client, then store variable $end to cache.
* curl -vi "http://127.0.0.1/?start=1&end=2"
*
* # Print out cached data.
* curl -vi "http://127.0.0.1/"
*/

use CrowdStar\BackgroundProcessing\BackgroundProcessing;
Expand All @@ -12,21 +24,26 @@

$cache = new ApcuCache();

// Set the cache entry with data in request field "start" if not empty.
if (!empty($_REQUEST['start'])) {
$cache->set('key', $_REQUEST['start']);
}
// Add a task to background to set the cache entry with data in request field "end" if not empty.
if (!empty($_REQUEST['end'])) {
BackgroundProcessing::add(
function () use ($cache) {
$cache->set('key', $_REQUEST['end']);
}
);
}
if (!isset($_REQUEST['reset'])) {
// Set the cache entry with data in request field "start" if not empty.
if (!empty($_REQUEST['start'])) {
$cache->set('key', $_REQUEST['start']);
}
// Add a background task to set the cache entry with data in request field "end" if not empty.
if (!empty($_REQUEST['end'])) {
BackgroundProcessing::add(
function () use ($cache) {
$cache->set('key', $_REQUEST['end']);
}
);
}

// Print out whatever in the cache entry.
echo $cache->get('key', '');
// Print out whatever in the cache entry. This is the response that will be send to HTTP client.
echo $cache->get('key', '');

// Run tasks added to background. At this point, HTTP response has already been sent back to client.
BackgroundProcessing::run();
// Send HTTP response back to the client first, then run background task(s).
BackgroundProcessing::run();
} else {
// Wipe cached data.
$cache->clear();
}
32 changes: 28 additions & 4 deletions tests/CrowdStar/BackgroundProcessing/DockerizedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,32 @@ public function dataRun(): array
{
return [
[
'a',
'b',
'a',
'b',
'expectedHttpResponse' => '',
'expectedFinalValue' => '',
'start' => '0',
'end' => '0',
'desc' => 'Start and end value are empty (string "0"); cached value is always an empty string.',
],
[
'expectedHttpResponse' => '',
'expectedFinalValue' => '1',
'start' => '0',
'end' => '1',
'desc' => 'Start value is empty ("0") but end value is "1"; cached value is updated accordingly.',
],
[
'expectedHttpResponse' => '1',
'expectedFinalValue' => '1',
'start' => '1',
'end' => '0',
'desc' => 'Start value is "1" but end value is empty (string "0"); cached value is not updated.',
],
[
'expectedHttpResponse' => '1',
'expectedFinalValue' => '2',
'start' => '1',
'end' => '2',
'desc' => 'Start value is "1" but end value is "2"; cached value is updated accordingly.',
],
];
}
Expand All @@ -40,6 +62,8 @@ public function dataRun(): array
public function testRun(string $expectedHttpResponse, string $expectedFinalValue, string $start, string $end)
{
$client = new Client(['base_uri' => 'http://127.0.0.1']);
$client->get('/', ['query' => ['reset' => true]]); // Wipe cached data.

$this->assertSame(
$expectedHttpResponse,
(string) $client->get('/', ['query' => ['start' => $start, 'end' => $end]])->getBody(),
Expand Down

0 comments on commit c8c5c4f

Please sign in to comment.