Skip to content

Commit f7cc4e3

Browse files
committed
Backport should_compact change from Gutenberg
WordPress/gutenberg#75682
1 parent 6826682 commit f7cc4e3

File tree

2 files changed

+99
-12
lines changed

2 files changed

+99
-12
lines changed

src/wp-includes/collaboration/class-wp-http-polling-sync-server.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,8 @@ private function add_update( string $room, int $client_id, string $type, string
457457
* @param int $cursor Return updates after this cursor.
458458
* @param bool $is_compactor True if this client is nominated to perform compaction.
459459
* @return array{
460-
* compaction_request: array|null,
461460
* end_cursor: int,
461+
* should_compact: bool,
462462
* room: string,
463463
* total_updates: int,
464464
* updates: array<int, array{data: string, type: string}>
@@ -481,18 +481,14 @@ private function get_updates( string $room, int $client_id, int $cursor, bool $i
481481
);
482482
}
483483

484-
// Determine if this client should perform compaction.
485-
$compaction_request = null;
486-
if ( $is_compactor && $total_updates > self::COMPACTION_THRESHOLD ) {
487-
$compaction_request = $updates_after_cursor;
488-
}
484+
$should_compact = $is_compactor && $total_updates > self::COMPACTION_THRESHOLD;
489485

490486
return array(
491-
'compaction_request' => $compaction_request,
492-
'end_cursor' => $this->storage->get_cursor( $room ),
493-
'room' => $room,
494-
'total_updates' => $total_updates,
495-
'updates' => $typed_updates,
487+
'end_cursor' => $this->storage->get_cursor( $room ),
488+
'room' => $room,
489+
'should_compact' => $should_compact,
490+
'total_updates' => $total_updates,
491+
'updates' => $typed_updates,
496492
);
497493
}
498494
}

tests/phpunit/tests/rest-api/rest-sync-server.php

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public function test_sync_response_structure() {
285285
$this->assertArrayHasKey( 'updates', $room_data );
286286
$this->assertArrayHasKey( 'end_cursor', $room_data );
287287
$this->assertArrayHasKey( 'total_updates', $room_data );
288-
$this->assertArrayHasKey( 'compaction_request', $room_data );
288+
$this->assertArrayHasKey( 'should_compact', $room_data );
289289
}
290290

291291
public function test_sync_response_room_matches_request() {
@@ -536,6 +536,97 @@ public function test_sync_total_updates_increments() {
536536
$this->assertSame( 3, $data['rooms'][0]['total_updates'] );
537537
}
538538

539+
/*
540+
* Compaction tests.
541+
*/
542+
543+
public function test_sync_should_compact_is_false_below_threshold() {
544+
wp_set_current_user( self::$editor_id );
545+
546+
$room = $this->get_post_room();
547+
$update = array(
548+
'type' => 'update',
549+
'data' => 'dGVzdA==',
550+
);
551+
552+
// Client 1 sends a single update.
553+
$response = $this->dispatch_sync(
554+
array(
555+
$this->build_room( $room, 1, 0, array( 'user' => 'c1' ), array( $update ) ),
556+
)
557+
);
558+
559+
$data = $response->get_data();
560+
$this->assertFalse( $data['rooms'][0]['should_compact'] );
561+
}
562+
563+
public function test_sync_should_compact_is_true_above_threshold_for_compactor() {
564+
wp_set_current_user( self::$editor_id );
565+
566+
$room = $this->get_post_room();
567+
$updates = array();
568+
for ( $i = 0; $i < 51; $i++ ) {
569+
$updates[] = array(
570+
'type' => 'update',
571+
'data' => base64_encode( "update-$i" ),
572+
);
573+
}
574+
575+
// Client 1 sends enough updates to exceed the compaction threshold.
576+
$this->dispatch_sync(
577+
array(
578+
$this->build_room( $room, 1, 0, array( 'user' => 'c1' ), $updates ),
579+
)
580+
);
581+
582+
// Client 2 (lowest connected client) should be told to compact.
583+
$response = $this->dispatch_sync(
584+
array(
585+
$this->build_room( $room, 2, 0, array( 'user' => 'c2' ) ),
586+
)
587+
);
588+
589+
$data = $response->get_data();
590+
$this->assertTrue( $data['rooms'][0]['should_compact'] );
591+
}
592+
593+
public function test_sync_should_compact_is_false_for_non_compactor() {
594+
wp_set_current_user( self::$editor_id );
595+
596+
$room = $this->get_post_room();
597+
$updates = array();
598+
for ( $i = 0; $i < 51; $i++ ) {
599+
$updates[] = array(
600+
'type' => 'update',
601+
'data' => base64_encode( "update-$i" ),
602+
);
603+
}
604+
605+
// Client 1 sends enough updates to exceed the compaction threshold.
606+
$this->dispatch_sync(
607+
array(
608+
$this->build_room( $room, 1, 0, array( 'user' => 'c1' ), $updates ),
609+
)
610+
);
611+
612+
// Connect client 2 (lower ID) so client 3 is not the compactor.
613+
$this->dispatch_sync(
614+
array(
615+
$this->build_room( $room, 2, 0, array( 'user' => 'c2' ) ),
616+
)
617+
);
618+
619+
// Client 3 (higher ID) should not be told to compact.
620+
$response = $this->dispatch_sync(
621+
array(
622+
$this->build_room( $room, 3, 0, array( 'user' => 'c3' ) ),
623+
)
624+
);
625+
626+
$data = $response->get_data();
627+
$this->assertFalse( $data['rooms'][0]['should_compact'] );
628+
}
629+
539630
/*
540631
* Awareness tests.
541632
*/

0 commit comments

Comments
 (0)