Skip to content

Commit b8da67f

Browse files
committed
Tests for caching related awareness storage.
1 parent 2e35aa2 commit b8da67f

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,52 @@ public function test_collaboration_awareness_client_reactivates_after_expiry():
13421342
$this->assertSame( 1, $row_count, 'Should have exactly one awareness row after reactivation.' );
13431343
}
13441344

1345+
/**
1346+
* Ensure awareness updates are not stored in the DB for sites using a persistent cache.
1347+
*/
1348+
public function test_awareness_uses_persistent_object_cache() {
1349+
if ( ! wp_using_ext_object_cache() ) {
1350+
$this->markTestSkipped( 'This test requires that an external object cache is in use.' );
1351+
}
1352+
1353+
$storage = new WP_Collaboration_Table_Storage();
1354+
$db_calls_initial = get_num_queries();
1355+
$storage->set_awareness_state( 'test-room', 'test-client', array( 'name' => 'Test Client' ), 1 );
1356+
$db_calls_after = get_num_queries();
1357+
1358+
$this->assertSame( 0, $db_calls_after - $db_calls_initial, 'Awareness update should not trigger database queries when using persistent object cache.' );
1359+
$this->assertSame( 0, $this->get_awareness_row_count(), 'Awareness row should not be stored in database when using persistent object cache.' );
1360+
}
1361+
1362+
/**
1363+
* Ensure awareness retrieval uses in-memory cache within a single request, even when a persistent cache is in use.
1364+
*/
1365+
public function test_awareness_uses_in_memory_cache() {
1366+
if ( wp_using_ext_object_cache() ) {
1367+
$this->markTestSkipped( 'This test requires that an external object cache is not in use.' );
1368+
}
1369+
1370+
$storage = new WP_Collaboration_Table_Storage();
1371+
$db_calls_initial = get_num_queries();
1372+
$storage->set_awareness_state( 'test-room', 'test-client', array( 'name' => 'Test Client' ), 1 );
1373+
$db_calls_after = get_num_queries();
1374+
1375+
$this->assertSame( 2, $db_calls_after - $db_calls_initial, 'Awareness update should not trigger database queries when using persistent object cache.' );
1376+
$this->assertSame( 1, $this->get_awareness_row_count(), 'Awareness row should not be stored in database when using persistent object cache.' );
1377+
1378+
$db_calls_initial = get_num_queries();
1379+
$storage->get_awareness_state( 'test-room' );
1380+
$db_calls_after = get_num_queries();
1381+
1382+
$this->assertSame( 1, $db_calls_after - $db_calls_initial, 'Initial awareness retrieval should query database.' );
1383+
1384+
$db_calls_initial = get_num_queries();
1385+
$storage->get_awareness_state( 'test-room' );
1386+
$db_calls_after = get_num_queries();
1387+
1388+
$this->assertSame( 0, $db_calls_after - $db_calls_initial, 'Subsequent awareness retrieval should use in-memory cache and not query database.' );
1389+
}
1390+
13451391
/*
13461392
* Multiple rooms tests.
13471393
*/

0 commit comments

Comments
 (0)