File tree Expand file tree Collapse file tree
PowerSync/PowerSync.Common/Client
Tests/PowerSync/PowerSync.Common.Tests/Client Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -420,6 +420,30 @@ await Database.WriteTransaction(async tx =>
420420 Closed = true ;
421421 }
422422
423+ private record UploadQueueStatsResult ( int size , int count ) ;
424+ /// <summary>
425+ /// Get upload queue size estimate and count.
426+ /// </summary>
427+ public async Task < UploadQueueStats > GetUploadQueueStats ( bool includeSize = false )
428+ {
429+ if ( includeSize )
430+ {
431+ var result = await Database . Get < UploadQueueStatsResult > (
432+ $ "SELECT SUM(cast(data as blob) + 20) as size, count(*) as count FROM { PSInternalTable . CRUD } "
433+ ) ;
434+
435+ return new UploadQueueStats ( result . count , result . size ) ;
436+ }
437+ else
438+ {
439+ var result = await Database . Get < UploadQueueStatsResult > (
440+ $ "SELECT count(*) as count FROM { PSInternalTable . CRUD } "
441+ ) ;
442+ return new UploadQueueStats ( result . count ) ;
443+ }
444+ }
445+
446+
423447 /// <summary>
424448 /// Get a batch of crud data to upload.
425449 /// <para />
Original file line number Diff line number Diff line change @@ -6,6 +6,9 @@ namespace PowerSync.Common.Tests.Client;
66
77using PowerSync . Common . Client ;
88
9+ /// <summary>
10+ /// dotnet test -v n --framework net8.0 --filter "PowerSyncDatabaseTests"
11+ /// </summary>
912public class PowerSyncDatabaseTests : IAsyncLifetime
1013{
1114 private PowerSyncDatabase db = default ! ;
@@ -454,4 +457,18 @@ await db.WriteTransaction(async tx =>
454457 var afterTx = await db . GetAll < object > ( "SELECT * FROM assets" ) ;
455458 Assert . Equal ( 2 , afterTx . Length ) ;
456459 }
460+
461+ [ Fact ( Timeout = 5000 ) ]
462+ public async Task GetUploadQueueStatsTest ( )
463+ {
464+ for ( var i = 0 ; i < 100 ; i ++ )
465+ {
466+ await db . Execute ( "INSERT INTO assets (id) VALUES(?)" , [ "0" + i + "-writelock" ] ) ;
467+ }
468+
469+ var stats = await db . GetUploadQueueStats ( true ) ;
470+ Assert . Equal ( 100 , stats . Count ) ;
471+ Assert . NotNull ( stats . Size ) ;
472+ }
473+
457474}
You can’t perform that action at this time.
0 commit comments