Exemplo n.º 1
0
 public function testGeneratedDataCollectionWithCustomMapper()
 {
     $this->forAll(Generator\seq(Generator\nat()))->withMaxSize(10)->hook(Listener\collectFrequencies(function ($array) {
         return count($array);
     }))->then(function ($array) {
         $this->assertEquals(count($array), count(array_reverse($array)));
     });
 }
Exemplo n.º 2
0
 public function testSizeIncreasesEvenIfEvaluationsAreSkippedDueToAntecedentsNotBeingSatisfied()
 {
     $this->forAll(Generator\seq(Generator\elements(1, 2, 3)))->when(function ($seq) {
         return count($seq) > 0;
     })->then(function ($seq) {
         $this->assertGreaterThan(0, count($seq));
     });
 }
Exemplo n.º 3
0
 public function testEqualToNative()
 {
     $this->forAll(Generator\seq(Generator\int()))->__invoke(function ($arr) {
         $e = array_product($arr);
         $this->assertEquals($e, lazy_product($arr)->resolve());
         $this->assertEquals($e, lazy_product(new \ArrayObject($arr))->resolve());
     });
 }
Exemplo n.º 4
0
 public function testArraySorting()
 {
     $this->forAll(Generator\seq(Generator\nat()))->then(function ($array) {
         sort($array);
         for ($i = 0; $i < count($array) - 1; $i++) {
             $this->assertTrue($array[$i] <= $array[$i + 1], "Array is not sorted: " . var_export($array, true));
         }
     });
 }
Exemplo n.º 5
0
 public function testArraySortingIsIdempotent()
 {
     $this->forAll(Generator\seq(Generator\nat()))->then(function ($array) {
         sort($array);
         $expected = $array;
         sort($array);
         $this->assertEquals($expected, $array);
     });
 }
Exemplo n.º 6
0
 public function testNotWithstandingCrashesJobsAreEventuallyPerformed()
 {
     $this->limitTo(100)->forAll(Generator\bind(Generator\choose(1, 4), function ($workers) {
         return Generator\tuple(Generator\constant($workers), Generator\seq(Generator\oneOf(Generator\map(function ($durationAndTag) {
             list($duration, $tag) = $durationAndTag;
             return ['enqueueJob', $duration, $tag];
         }, Generator\tuple(Generator\nat(), Generator\elements(['generic', 'fast-lane']))), Generator\map(function ($workerIndex) {
             return ['restartWorkerGracefully', $workerIndex];
         }, Generator\choose(0, $workers - 1)), Generator\map(function ($workerIndex) {
             return ['restartWorkerByKilling', $workerIndex];
         }, Generator\choose(0, $workers - 1)), Generator\constant('restartRecruiterGracefully'), Generator\constant('restartRecruiterByKilling'), Generator\map(function ($milliseconds) {
             return ['sleep', $milliseconds];
         }, Generator\choose(1, 1000)))));
     }))->hook(Listener\log('/tmp/recruiter-test-iterations.log'))->hook(Listener\collectFrequencies())->disableShrinking()->then(function ($tuple) {
         list($workers, $actions) = $tuple;
         $this->clean();
         $this->start($workers);
         foreach ($actions as $action) {
             $this->logAction($action);
             if (is_array($action)) {
                 $arguments = $action;
                 $method = array_shift($arguments);
                 call_user_func_array([$this, $method], $arguments);
             } else {
                 $this->{$action}();
             }
         }
         $estimatedTime = max(count($actions) * 4, 60);
         Timeout::inSeconds($estimatedTime, function () {
             return "all {$this->jobs} jobs to be performed. Now is " . date('c') . " Logs: " . $this->files();
         })->until(function () {
             return $this->jobRepository->countArchived() === $this->jobs;
         });
         $at = T\now();
         $statistics = $this->recruiter->statistics($tag = null, $at);
         $this->assertInvariantsOnStatistics($statistics);
         // TODO: remove duplication
         $statisticsByTag = [];
         $cumulativeThroughput = 0;
         foreach (['generic', 'fast-lane'] as $tag) {
             $statisticsByTag[$tag] = $this->recruiter->statistics($tag, $at);
             $this->assertInvariantsOnStatistics($statisticsByTag[$tag]);
             $cumulativeThroughput += $statisticsByTag[$tag]['throughput']['value'];
         }
         var_dump($statistics, $statisticsByTag);
         // TODO: add tolerance
         $this->assertEquals($statistics['throughput']['value'], $cumulativeThroughput);
     });
 }
 public function testDoesNotPerformTheCheckTooManyTimes()
 {
     $this->forAll(Generator\date(), Generator\choose(10, 30), Generator\seq(Generator\choose(1, 60)))->then(function ($startingDate, $period, $deltas) {
         $clock = new SettableClock($startingDate);
         $check = PeriodicalCheck::every($period, $clock);
         $this->counter = 0;
         $check->onFire(function () {
             $this->counter++;
         });
         $check->__invoke();
         foreach ($deltas as $delta) {
             $clock->advance($delta);
             $check->__invoke();
         }
         $totalInterval = array_sum($deltas);
         $maximumNumberOfCalls = ceil($totalInterval / $period);
         $actualNumberOfCallsExcludingTheFirst = $this->counter - 1;
         $this->assertLessThanOrEqual($maximumNumberOfCalls, $actualNumberOfCallsExcludingTheFirst);
     });
 }
Exemplo n.º 8
0
 public function testArrayReverse()
 {
     $this->forAll(Generator\seq(Generator\nat()))->then(function ($array) {
         $this->assertEquals($array, array_reverse(array_reverse($array)));
     });
 }
Exemplo n.º 9
0
 /**
  * @group long
  */
 public function testPropertyBased()
 {
     $this->iteration = 0;
     $this->forAll(Generator\vector(2, Generator\seq(Generator\elements(['acquire', 'release']))))->when(function ($sequencesOfSteps) {
         foreach ($sequencesOfSteps as $sequence) {
             if (!$sequence) {
                 return false;
             }
         }
         return true;
     })->then(function ($sequencesOfSteps) {
         $this->lockCollection->drop();
         $log = "/tmp/mongolock_{$this->iteration}.log";
         if (file_exists($log)) {
             unlink($log);
         }
         $processes = [];
         foreach ($sequencesOfSteps as $i => $sequence) {
             $processName = "p{$i}";
             $steps = implode(',', $sequence);
             $process = new Process("exec php " . __DIR__ . "/mongolock.php {$processName} {$steps} >> {$log}");
             $process->start();
             $processes[] = $process;
         }
         foreach ($processes as $process) {
             $process->wait();
             $this->assertExitedCorrectly($process, "Error in MongoLock run");
         }
         $process = new Process("exec java -jar " . __DIR__ . "/knossos-onebip.jar mongo-lock {$log}");
         $process->run();
         $this->assertExitedCorrectly($process, "Non-linearizable history in {$log}");
         $this->iteration++;
     });
 }