|
| 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