Skip to content

Commit

Permalink
Eliminate constructing etag via OD_Tag_Visitor_Registry() unless side…
Browse files Browse the repository at this point in the history
… effects are needed
  • Loading branch information
westonruter committed Dec 2, 2024
1 parent 02e9759 commit 54183af
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Test_OD_URL_Metric_Group_Collection extends WP_UnitTestCase {
* @return array<string, mixed> Data.
*/
public function data_provider_test_construction(): array {
$current_etag = od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry() );
$current_etag = md5( '' );

return array(
'no_breakpoints_ok' => array(
Expand Down Expand Up @@ -402,16 +402,22 @@ static function ( OD_URL_Metric $url_metric ): int {
*/
public function data_provider_test_get_group_for_viewport_width(): array {
$current_time = microtime( true );
$current_etag = md5( '' );

$none_needed_data = array(
'url_metrics' => ( function () use ( $current_time ): array {
'url_metrics' => ( function () use ( $current_time, $current_etag ): array {
return array_merge(
array_fill(
0,
3,
new OD_URL_Metric(
array_merge(
$this->get_sample_url_metric( array( 'viewport_width' => 400 ) )->jsonSerialize(),
$this->get_sample_url_metric(
array(
'viewport_width' => 400,
'etag' => $current_etag,
)
)->jsonSerialize(),
array( 'timestamp' => $current_time )
)
)
Expand All @@ -421,14 +427,20 @@ public function data_provider_test_get_group_for_viewport_width(): array {
3,
new OD_URL_Metric(
array_merge(
$this->get_sample_url_metric( array( 'viewport_width' => 600 ) )->jsonSerialize(),
$this->get_sample_url_metric(
array(
'viewport_width' => 600,
'etag' => $current_etag,
)
)->jsonSerialize(),
array( 'timestamp' => $current_time )
)
)
)
);
} )(),
'current_time' => $current_time,
'current_etag' => $current_etag,
'breakpoints' => array( 480 ),
'sample_size' => 3,
'freshness_ttl' => HOUR_IN_SECONDS,
Expand Down Expand Up @@ -508,6 +520,34 @@ public function data_provider_test_get_group_for_viewport_width(): array {
),
)
),

'url-metric-stale-etag' => array_merge(
( static function ( $data ): array {
$url_metrics_data = $data['url_metrics'][ count( $data['url_metrics'] ) - 1 ]->jsonSerialize();
$url_metrics_data['etag'] = md5( 'something new!' );
$data['url_metrics'][ count( $data['url_metrics'] ) - 1 ] = new OD_URL_Metric( $url_metrics_data );
return $data;
} )( $none_needed_data ),
array(
'expected_return' => array(
array(
'minimumViewportWidth' => 0,
'complete' => true,
),
array(
'minimumViewportWidth' => 481,
'complete' => false,
),
),
'expected_is_group_complete' => array(
200 => true,
400 => true,
480 => true,
481 => false,
500 => false,
),
)
),
);
}

Expand All @@ -523,14 +563,14 @@ public function data_provider_test_get_group_for_viewport_width(): array {
*
* @param OD_URL_Metric[] $url_metrics URL Metrics.
* @param float $current_time Current time.
* @param non-empty-string $current_etag Current ETag.
* @param int[] $breakpoints Breakpoints.
* @param int $sample_size Sample size.
* @param int $freshness_ttl Freshness TTL.
* @param array<int, mixed> $expected_return Expected return.
* @param array<int, bool> $expected_is_group_complete Expected is group complete.
*/
public function test_get_group_for_viewport_width( array $url_metrics, float $current_time, array $breakpoints, int $sample_size, int $freshness_ttl, array $expected_return, array $expected_is_group_complete ): void {
$current_etag = od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry() );
public function test_get_group_for_viewport_width( array $url_metrics, float $current_time, string $current_etag, array $breakpoints, int $sample_size, int $freshness_ttl, array $expected_return, array $expected_is_group_complete ): void {
$group_collection = new OD_URL_Metric_Group_Collection( $url_metrics, $current_etag, $breakpoints, $sample_size, $freshness_ttl );
$this->assertSame(
$expected_return,
Expand Down Expand Up @@ -563,7 +603,7 @@ static function ( OD_URL_Metric_Group $group ): array {
public function test_is_every_group_populated(): void {
$breakpoints = array( 480, 800 );
$sample_size = 3;
$current_etag = od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry() );
$current_etag = md5( '' );
$group_collection = new OD_URL_Metric_Group_Collection(
array(),
$current_etag,
Expand All @@ -573,20 +613,48 @@ public function test_is_every_group_populated(): void {
);
$this->assertFalse( $group_collection->is_every_group_populated() );
$this->assertFalse( $group_collection->is_every_group_complete() );
$group_collection->add_url_metric( $this->get_sample_url_metric( array( 'viewport_width' => 200 ) ) );
$group_collection->add_url_metric(
$this->get_sample_url_metric(
array(
'viewport_width' => 200,
'etag' => $current_etag,
)
)
);
$this->assertFalse( $group_collection->is_every_group_populated() );
$this->assertFalse( $group_collection->is_every_group_complete() );
$group_collection->add_url_metric( $this->get_sample_url_metric( array( 'viewport_width' => 500 ) ) );
$group_collection->add_url_metric(
$this->get_sample_url_metric(
array(
'viewport_width' => 500,
'etag' => $current_etag,
)
)
);
$this->assertFalse( $group_collection->is_every_group_populated() );
$this->assertFalse( $group_collection->is_every_group_complete() );
$group_collection->add_url_metric( $this->get_sample_url_metric( array( 'viewport_width' => 900 ) ) );
$group_collection->add_url_metric(
$this->get_sample_url_metric(
array(
'viewport_width' => 900,
'etag' => $current_etag,
)
)
);
$this->assertTrue( $group_collection->is_every_group_populated() );
$this->assertFalse( $group_collection->is_every_group_complete() );

// Now finish completing all the groups.
foreach ( array_merge( $breakpoints, array( 1000 ) ) as $viewport_width ) {
for ( $i = 0; $i < $sample_size; $i++ ) {
$group_collection->add_url_metric( $this->get_sample_url_metric( array( 'viewport_width' => $viewport_width ) ) );
$group_collection->add_url_metric(
$this->get_sample_url_metric(
array(
'viewport_width' => $viewport_width,
'etag' => $current_etag,
)
)
);
}
}
$this->assertTrue( $group_collection->is_every_group_complete() );
Expand Down
4 changes: 2 additions & 2 deletions tests/class-optimization-detective-test-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ trait Optimization_Detective_Test_Helpers {
*/
public function populate_url_metrics( array $elements, bool $complete = true ): void {
$slug = od_get_url_metrics_slug( od_get_normalized_query_vars() );
$etag = od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry() );
$etag = od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry() ); // Note: Tests rely on the od_current_url_metrics_etag_data filter to set the desired value.
$sample_size = $complete ? od_get_url_metrics_breakpoint_sample_size() : 1;
foreach ( array_merge( od_get_breakpoint_max_widths(), array( 1000 ) ) as $viewport_width ) {
for ( $i = 0; $i < $sample_size; $i++ ) {
Expand Down Expand Up @@ -80,7 +80,7 @@ public function get_sample_dom_rect(): array {
public function get_sample_url_metric( array $params ): OD_URL_Metric {
$params = array_merge(
array(
'etag' => od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry() ),
'etag' => od_get_current_url_metrics_etag( new OD_Tag_Visitor_Registry() ), // Note: Tests rely on the od_current_url_metrics_etag_data filter to set the desired value.
'url' => home_url( '/' ),
'viewport_width' => 480,
'elements' => array(),
Expand Down

0 comments on commit 54183af

Please sign in to comment.