Skip to content

Commit

Permalink
Improve assertions (#884)
Browse files Browse the repository at this point in the history
  • Loading branch information
peter279k authored Sep 7, 2020
1 parent f8af5c2 commit 58ad9a9
Show file tree
Hide file tree
Showing 17 changed files with 112 additions and 112 deletions.
12 changes: 6 additions & 6 deletions tests/Controller/MonitoringControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public function test_monitored_jobs_can_be_paginated_by_tag()
$results = $response->original['jobs'];

$this->assertCount(25, $results);
$this->assertEquals(49, $results[0]->id);
$this->assertEquals(25, $results[24]->id);
$this->assertSame('49', $results[0]->id);
$this->assertSame('25', $results[24]->id);

// Paginate second set...
$response = $this->actingAs(new Fakes\User)
Expand All @@ -61,10 +61,10 @@ public function test_monitored_jobs_can_be_paginated_by_tag()
$results = $response->original['jobs'];

$this->assertCount(25, $results);
$this->assertEquals(24, $results[0]->id);
$this->assertEquals(0, $results[24]->id);
$this->assertEquals(25, $results[0]->index);
$this->assertEquals(49, $results[24]->index);
$this->assertSame('24', $results[0]->id);
$this->assertSame('0', $results[24]->id);
$this->assertSame('25', $results[0]->index);
$this->assertSame(49, $results[24]->index);
}

public function test_can_paginate_where_jobs_dont_exist()
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/AddSupervisorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function test_add_supervisor_command_creates_new_supervisor_on_master_pro

$this->assertCount(1, $master->supervisors);

$this->assertEquals(
$this->assertSame(
'exec '.$phpBinary.' artisan horizon:supervisor my-supervisor redis --delay=0 --memory=128 --queue="default" --sleep=3 --timeout=60 --tries=0 --balance=off --max-processes=1 --min-processes=1 --nice=0',
$master->supervisors->first()->process->getCommandLine()
);
Expand Down
62 changes: 31 additions & 31 deletions tests/Feature/AutoScalerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ public function test_scaler_attempts_to_get_closer_to_proper_balance_on_each_ite

$scaler->scale($supervisor);

$this->assertEquals(11, $supervisor->processPools['first']->totalProcessCount());
$this->assertEquals(9, $supervisor->processPools['second']->totalProcessCount());
$this->assertSame(11, $supervisor->processPools['first']->totalProcessCount());
$this->assertSame(9, $supervisor->processPools['second']->totalProcessCount());

$scaler->scale($supervisor);

$this->assertEquals(12, $supervisor->processPools['first']->totalProcessCount());
$this->assertEquals(8, $supervisor->processPools['second']->totalProcessCount());
$this->assertSame(12, $supervisor->processPools['first']->totalProcessCount());
$this->assertSame(8, $supervisor->processPools['second']->totalProcessCount());

$scaler->scale($supervisor);

$this->assertEquals(13, $supervisor->processPools['first']->totalProcessCount());
$this->assertEquals(7, $supervisor->processPools['second']->totalProcessCount());
$this->assertSame(13, $supervisor->processPools['first']->totalProcessCount());
$this->assertSame(7, $supervisor->processPools['second']->totalProcessCount());

// Asset scaler stays at target values...
$scaler->scale($supervisor);

$this->assertEquals(13, $supervisor->processPools['first']->totalProcessCount());
$this->assertEquals(7, $supervisor->processPools['second']->totalProcessCount());
$this->assertSame(13, $supervisor->processPools['first']->totalProcessCount());
$this->assertSame(7, $supervisor->processPools['second']->totalProcessCount());
}

public function test_balance_stays_even_when_queue_is_empty()
Expand All @@ -51,23 +51,23 @@ public function test_balance_stays_even_when_queue_is_empty()

$scaler->scale($supervisor);

$this->assertEquals(4, $supervisor->processPools['first']->totalProcessCount());
$this->assertEquals(4, $supervisor->processPools['second']->totalProcessCount());
$this->assertSame(4, $supervisor->processPools['first']->totalProcessCount());
$this->assertSame(4, $supervisor->processPools['second']->totalProcessCount());

$scaler->scale($supervisor);

$this->assertEquals(3, $supervisor->processPools['first']->totalProcessCount());
$this->assertEquals(3, $supervisor->processPools['second']->totalProcessCount());
$this->assertSame(3, $supervisor->processPools['first']->totalProcessCount());
$this->assertSame(3, $supervisor->processPools['second']->totalProcessCount());

$scaler->scale($supervisor);

$this->assertEquals(2, $supervisor->processPools['first']->totalProcessCount());
$this->assertEquals(2, $supervisor->processPools['second']->totalProcessCount());
$this->assertSame(2, $supervisor->processPools['first']->totalProcessCount());
$this->assertSame(2, $supervisor->processPools['second']->totalProcessCount());

$scaler->scale($supervisor);

$this->assertEquals(1, $supervisor->processPools['first']->totalProcessCount());
$this->assertEquals(1, $supervisor->processPools['second']->totalProcessCount());
$this->assertSame(1, $supervisor->processPools['first']->totalProcessCount());
$this->assertSame(1, $supervisor->processPools['second']->totalProcessCount());
}

public function test_balancer_assigns_more_processes_on_busy_queue()
Expand All @@ -80,27 +80,27 @@ public function test_balancer_assigns_more_processes_on_busy_queue()
$scaler->scale($supervisor);
$scaler->scale($supervisor);

$this->assertEquals(3, $supervisor->processPools['first']->totalProcessCount());
$this->assertEquals(1, $supervisor->processPools['second']->totalProcessCount());
$this->assertSame(3, $supervisor->processPools['first']->totalProcessCount());
$this->assertSame(1, $supervisor->processPools['second']->totalProcessCount());

$scaler->scale($supervisor);
$scaler->scale($supervisor);

$this->assertEquals(5, $supervisor->processPools['first']->totalProcessCount());
$this->assertEquals(1, $supervisor->processPools['second']->totalProcessCount());
$this->assertSame(5, $supervisor->processPools['first']->totalProcessCount());
$this->assertSame(1, $supervisor->processPools['second']->totalProcessCount());

$scaler->scale($supervisor);
$scaler->scale($supervisor);

$this->assertEquals(7, $supervisor->processPools['first']->totalProcessCount());
$this->assertEquals(1, $supervisor->processPools['second']->totalProcessCount());
$this->assertSame(7, $supervisor->processPools['first']->totalProcessCount());
$this->assertSame(1, $supervisor->processPools['second']->totalProcessCount());

$scaler->scale($supervisor);
$scaler->scale($supervisor);
$scaler->scale($supervisor);

$this->assertEquals(9, $supervisor->processPools['first']->totalProcessCount());
$this->assertEquals(1, $supervisor->processPools['second']->totalProcessCount());
$this->assertSame(9, $supervisor->processPools['first']->totalProcessCount());
$this->assertSame(1, $supervisor->processPools['second']->totalProcessCount());
}

public function test_balancing_a_single_queue_assigns_it_the_min_workers_with_empty_queue()
Expand All @@ -110,7 +110,7 @@ public function test_balancing_a_single_queue_assigns_it_the_min_workers_with_em
]);

$scaler->scale($supervisor);
$this->assertEquals(1, $supervisor->processPools['first']->totalProcessCount());
$this->assertSame(1, $supervisor->processPools['first']->totalProcessCount());
}

public function test_scaler_will_not_scale_past_max_process_threshold_under_high_load()
Expand All @@ -122,8 +122,8 @@ public function test_scaler_will_not_scale_past_max_process_threshold_under_high

$scaler->scale($supervisor);

$this->assertEquals(10, $supervisor->processPools['first']->totalProcessCount());
$this->assertEquals(10, $supervisor->processPools['second']->totalProcessCount());
$this->assertSame(10, $supervisor->processPools['first']->totalProcessCount());
$this->assertSame(10, $supervisor->processPools['second']->totalProcessCount());
}

public function test_scaler_will_not_scale_below_minimum_worker_threshold()
Expand All @@ -139,13 +139,13 @@ public function test_scaler_will_not_scale_below_minimum_worker_threshold()

$scaler->scale($supervisor);

$this->assertEquals(3, $supervisor->processPools['first']->totalProcessCount());
$this->assertEquals(2, $supervisor->processPools['second']->totalProcessCount());
$this->assertSame(3, $supervisor->processPools['first']->totalProcessCount());
$this->assertSame(2, $supervisor->processPools['second']->totalProcessCount());

$scaler->scale($supervisor);

$this->assertEquals(3, $supervisor->processPools['first']->totalProcessCount());
$this->assertEquals(2, $supervisor->processPools['second']->totalProcessCount());
$this->assertSame(3, $supervisor->processPools['first']->totalProcessCount());
$this->assertSame(2, $supervisor->processPools['second']->totalProcessCount());
}

protected function with_scaling_scenario($maxProcesses, array $pools, array $extraOptions = [])
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/FailedJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ public function test_failed_jobs_are_placed_in_the_failed_job_table()
{
$id = Queue::push(new Jobs\FailingJob);
$this->work();
$this->assertEquals(1, $this->failedJobs());
$this->assertSame(1, $this->failedJobs());
$this->assertGreaterThan(0, Redis::connection('horizon')->ttl($id));

$job = resolve(JobRepository::class)->getJobs([$id])[0];

$this->assertTrue(isset($job->exception));
$this->assertTrue(isset($job->failed_at));
$this->assertSame('failed', $job->status);
$this->assertTrue(is_numeric($job->failed_at));
$this->assertEquals(Jobs\FailingJob::class, $job->name);
$this->assertIsNumeric($job->failed_at);
$this->assertSame(Jobs\FailingJob::class, $job->name);
}

public function test_tags_for_failed_jobs_are_stored_in_redis()
Expand Down
14 changes: 7 additions & 7 deletions tests/Feature/JobRetrievalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ public function test_pending_jobs_can_be_retrieved()
// Test getting all jobs...
$this->assertCount(5, $recent);
$this->assertEquals($ids[4], $recent->first()->id);
$this->assertEquals(Jobs\BasicJob::class, $recent->first()->name);
$this->assertEquals(0, $recent->first()->index);
$this->assertSame(Jobs\BasicJob::class, $recent->first()->name);
$this->assertSame(0, $recent->first()->index);
$this->assertEquals($ids[0], $recent->last()->id);
$this->assertEquals(4, $recent->last()->index);
$this->assertSame(4, $recent->last()->index);

// Test pagination...
$recent = $repository->getRecent(1);
$this->assertCount(3, $recent);
$this->assertEquals($ids[2], $recent->first()->id);
$this->assertEquals(2, $recent->first()->index);
$this->assertSame(2, $recent->first()->index);
$this->assertEquals($ids[0], $recent->last()->id);
$this->assertEquals(4, $recent->last()->index);
$this->assertSame(4, $recent->last()->index);

// Test no results...
$recent = $repository->getRecent(4);
Expand All @@ -59,10 +59,10 @@ public function test_recent_jobs_are_correctly_trimmed_and_expired()
$repository = resolve(JobRepository::class);
Chronos::setTestNow(Chronos::now()->addHours(3));

$this->assertEquals(5, Redis::connection('horizon')->zcard('recent_jobs'));
$this->assertSame(5, Redis::connection('horizon')->zcard('recent_jobs'));

$repository->trimRecentJobs();
$this->assertEquals(0, Redis::connection('horizon')->zcard('recent_jobs'));
$this->assertSame(0, Redis::connection('horizon')->zcard('recent_jobs'));

// Assert job record has a TTL...
$repository->completed(new JobPayload(json_encode(['id' => $ids[0]])));
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/MasterSupervisorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public function test_master_process_restarts_unexpected_exits()
$command = (object) json_decode($commands[0], true);

$this->assertCount(0, $master->supervisors);
$this->assertEquals(AddSupervisor::class, $command->command);
$this->assertSame(AddSupervisor::class, $command->command);
$this->assertSame('default', $command->options['queue']);
}

Expand Down Expand Up @@ -148,7 +148,7 @@ public function test_master_process_loop_processes_pending_commands()

$command = resolve(Commands\FakeMasterCommand::class);

$this->assertEquals(1, $command->processCount);
$this->assertSame(1, $command->processCount);
$this->assertEquals($master, $command->master);
$this->assertEquals(['foo' => 'bar'], $command->options);
}
Expand Down
36 changes: 18 additions & 18 deletions tests/Feature/MetricsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function test_total_throughput_is_stored()
$this->work();
$this->work();

$this->assertEquals(2, resolve(MetricsRepository::class)->throughput());
$this->assertSame(2, resolve(MetricsRepository::class)->throughput());
}

public function test_throughput_is_stored_per_job_class()
Expand All @@ -34,9 +34,9 @@ public function test_throughput_is_stored_per_job_class()
$this->work();
$this->work();

$this->assertEquals(4, resolve(MetricsRepository::class)->throughput());
$this->assertEquals(3, resolve(MetricsRepository::class)->throughputForJob(Jobs\BasicJob::class));
$this->assertEquals(1, resolve(MetricsRepository::class)->throughputForJob(Jobs\ConditionallyFailingJob::class));
$this->assertSame(4, resolve(MetricsRepository::class)->throughput());
$this->assertSame(3, resolve(MetricsRepository::class)->throughputForJob(Jobs\BasicJob::class));
$this->assertSame(1, resolve(MetricsRepository::class)->throughputForJob(Jobs\ConditionallyFailingJob::class));
}

public function test_throughput_is_stored_per_queue()
Expand All @@ -51,8 +51,8 @@ public function test_throughput_is_stored_per_queue()
$this->work();
$this->work();

$this->assertEquals(4, resolve(MetricsRepository::class)->throughput());
$this->assertEquals(4, resolve(MetricsRepository::class)->throughputForQueue('default'));
$this->assertSame(4, resolve(MetricsRepository::class)->throughput());
$this->assertSame(4, resolve(MetricsRepository::class)->throughputForQueue('default'));
}

public function test_average_runtime_is_stored_per_job_class_in_milliseconds()
Expand All @@ -68,7 +68,7 @@ public function test_average_runtime_is_stored_per_job_class_in_milliseconds()
$this->work();
$this->work();

$this->assertEquals(1.5, resolve(MetricsRepository::class)->runtimeForJob(Jobs\BasicJob::class));
$this->assertSame(1.5, resolve(MetricsRepository::class)->runtimeForJob(Jobs\BasicJob::class));
}

public function test_average_runtime_is_stored_per_queue_in_milliseconds()
Expand All @@ -84,7 +84,7 @@ public function test_average_runtime_is_stored_per_queue_in_milliseconds()
$this->work();
$this->work();

$this->assertEquals(1.5, resolve(MetricsRepository::class)->runtimeForQueue('default'));
$this->assertSame(1.5, resolve(MetricsRepository::class)->runtimeForQueue('default'));
}

public function test_list_of_all_jobs_with_metric_information_is_maintained()
Expand All @@ -97,8 +97,8 @@ public function test_list_of_all_jobs_with_metric_information_is_maintained()

$jobs = resolve(MetricsRepository::class)->measuredJobs();
$this->assertCount(2, $jobs);
$this->assertTrue(in_array(Jobs\ConditionallyFailingJob::class, $jobs));
$this->assertTrue(in_array(Jobs\BasicJob::class, $jobs));
$this->assertContains(Jobs\ConditionallyFailingJob::class, $jobs);
$this->assertContains(Jobs\BasicJob::class, $jobs);
}

public function test_snapshot_of_metrics_performance_can_be_stored()
Expand Down Expand Up @@ -173,22 +173,22 @@ public function test_jobs_processed_per_minute_since_last_snapshot_is_calculable
$this->work();
$this->work();

$this->assertEquals(
2, resolve(MetricsRepository::class)->jobsProcessedPerMinute()
$this->assertSame(
2.0, resolve(MetricsRepository::class)->jobsProcessedPerMinute()
);

// Adjust current time...
Chronos::setTestNow(Chronos::now()->addMinutes(2));

$this->assertEquals(
1, resolve(MetricsRepository::class)->jobsProcessedPerMinute()
$this->assertSame(
1.0, resolve(MetricsRepository::class)->jobsProcessedPerMinute()
);

// take snapshot and ensure count is reset...
resolve(MetricsRepository::class)->snapshot();

$this->assertEquals(
0, resolve(MetricsRepository::class)->jobsProcessedPerMinute()
$this->assertSame(
0.0, resolve(MetricsRepository::class)->jobsProcessedPerMinute()
);
}

Expand All @@ -212,12 +212,12 @@ public function test_only_past_24_snapshots_are_retained()
// Check the job snapshots...
$snapshots = resolve(MetricsRepository::class)->snapshotsForJob(Jobs\BasicJob::class);
$this->assertCount(24, $snapshots);
$this->assertEquals(Chronos::now()->getTimestamp() - 1, $snapshots[23]->time);
$this->assertSame(Chronos::now()->getTimestamp() - 1, $snapshots[23]->time);

// Check the queue snapshots...
$snapshots = resolve(MetricsRepository::class)->snapshotsForQueue('default');
$this->assertCount(24, $snapshots);
$this->assertEquals(Chronos::now()->getTimestamp() - 1, $snapshots[23]->time);
$this->assertSame(Chronos::now()->getTimestamp() - 1, $snapshots[23]->time);

Chronos::setTestNow();
}
Expand Down
10 changes: 5 additions & 5 deletions tests/Feature/MonitoringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public function test_can_retrieve_all_monitored_tags()

dispatch(new MonitorTag('second'));
$monitored = $repository->monitoring();
$this->assertTrue(in_array('first', $monitored));
$this->assertTrue(in_array('second', $monitored));
$this->assertContains('first', $monitored);
$this->assertContains('second', $monitored);
$this->assertCount(2, $monitored);
}

Expand Down Expand Up @@ -52,7 +52,7 @@ public function test_completed_jobs_are_stored_in_database_when_one_of_their_tag
dispatch(new MonitorTag('first'));
$id = Queue::push(new Jobs\BasicJob);
$this->work();
$this->assertEquals(1, $this->monitoredJobs('first'));
$this->assertSame(1, $this->monitoredJobs('first'));
$this->assertGreaterThan(0, Redis::connection('horizon')->ttl($id));
}

Expand All @@ -62,7 +62,7 @@ public function test_completed_jobs_are_removed_from_database_when_their_tag_is_
Queue::push(new Jobs\BasicJob);
$this->work();
dispatch(new StopMonitoringTag('first'));
$this->assertEquals(0, $this->monitoredJobs('first'));
$this->assertSame(0, $this->monitoredJobs('first'));
}

public function test_all_completed_jobs_are_removed_from_database_when_their_tag_is_no_longer_monitored()
Expand All @@ -76,6 +76,6 @@ public function test_all_completed_jobs_are_removed_from_database_when_their_tag
$this->work();

dispatch(new StopMonitoringTag('first'));
$this->assertEquals(0, $this->monitoredJobs('first'));
$this->assertSame(0, $this->monitoredJobs('first'));
}
}
Loading

0 comments on commit 58ad9a9

Please sign in to comment.