Skip to content

Commit bae721c

Browse files
committed
Add previously ignored test index files
1 parent 1932d48 commit bae721c

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
vendor/
22
.idea/
3-
index.php
3+
/index.php
4+

test/container/index.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
require __DIR__ . '/vendor/autoload.php';
3+
4+
echo "Connecting to redis", PHP_EOL;
5+
$client = new Predis\Client(['host' => 'redis']);
6+
echo "Connected!", PHP_EOL;
7+
8+
for ($i = 0; $i < 3; $i += 1) {
9+
echo "Starting the loop", PHP_EOL;
10+
$semaphore = new \CodeOrange\RedisCountingSemaphore\Semaphore($client, 'test1', 10, 50);
11+
$semaphore->acquire(0.5, 1000000);
12+
// BEGIN critical section
13+
echo "In the critical section!", PHP_EOL;
14+
$critical = $client->incr('critical');
15+
$client->publish('chan_critical', $critical);
16+
sleep(rand(5, 15));
17+
echo "Leaving the critical section!", PHP_EOL;
18+
$client->decr('critical');
19+
// END critical sections
20+
$semaphore->release();
21+
}

test/index.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
use Symfony\Component\Process\Process;
3+
4+
require __DIR__ . '/../vendor/autoload.php';
5+
6+
chdir(__DIR__);
7+
8+
$nr_containers = 10;
9+
$per_container = 3;
10+
11+
$resource_limit = 10;
12+
13+
$docker_ip = $argv[1] ?? 'localhost';
14+
15+
// Build a docker image from the contains of `container/`
16+
echo "Building docker image for test processes...";
17+
$docker_process = new Process('docker build -t code-orange/redis-counting-semaphore-test ./container/');
18+
$docker_process->run();
19+
if (!$docker_process->isSuccessful()) {
20+
echo "Docker container failed to build";
21+
exit(-1);
22+
}
23+
echo "done", PHP_EOL;
24+
// Get a redis docker image and start it
25+
echo "Starting redis image...";
26+
$redis_container = new Process('docker run --name semaphore-redis -p 6379:6379 -d redis redis-server --appendonly no --save "" --timeout 0');
27+
$redis_container->run();
28+
if (!$redis_container->isSuccessful()) {
29+
echo "Redis container failed to start";
30+
exit(-1);
31+
}
32+
echo "done", PHP_EOL;
33+
34+
// Setup listener
35+
$client = new Predis\Client(['host' => $docker_ip, 'read_write_timeout' => -1]);
36+
$l = $client->pubSubLoop(['subscribe' => 'chan_critical']);
37+
echo "Starting listening...", PHP_EOL;
38+
39+
// Start 100 containers based on the docker image in `container/`
40+
echo "Spawning processes for the tests...";
41+
for ($i = 0; $i < $nr_containers; $i += 1) {
42+
$container = new Process('docker run --link semaphore-redis:redis -d code-orange/redis-counting-semaphore-test');
43+
$container->run();
44+
if (!$container->isSuccessful()) {
45+
echo "One of the containers didn't start successfully", PHP_EOL;
46+
exit(-1);
47+
}
48+
}
49+
echo "done", PHP_EOL;
50+
51+
// These containers will all try to obtain the same counting semaphore with a limit of 10, 3 times
52+
// Check that the amount of containers in their critical section is never great than the limit of 10
53+
$count = 0;
54+
foreach ($l as $msg) {
55+
if ($msg->kind == 'message') {
56+
$count++;
57+
if ($msg->payload > $resource_limit) {
58+
// More containers are in the critical section than should be
59+
echo "There are currently {$msg->payload} users in the critical section, so the test has failed.";
60+
exit(-1);
61+
}
62+
if ($count >= ($nr_containers * $per_container)) {
63+
// We've reached the total number of times the critical section is entered
64+
$l->stop();
65+
}
66+
}
67+
}
68+
69+
echo "Done! All containers have finished and the resource limit was never exceeded.", PHP_EOL;
70+
71+
// Stop the redis container
72+
echo "Stopping redis...";
73+
$redis_container_stop = new Process('docker kill semaphore-redis && docker rm semaphore-redis');
74+
$redis_container_stop->run();
75+
if (!$redis_container_stop->isSuccessful()) {
76+
echo "Redis container failed to stop";
77+
exit(-1);
78+
}
79+
echo "done", PHP_EOL;

0 commit comments

Comments
 (0)