@@ -145,6 +145,34 @@ void test_sync_while_reading() {
145145 cmp_data (db_primary , db_backup );
146146}
147147
148+ // This test case writes to a local database, force checkpoint, and then
149+ //verifies that checkpoint sequence number incremented, and the WAL is
150+ //truncated.
151+ static void test_checkpoint (void ) {
152+ sqlite3 * db_primary ;
153+ unsigned int max_frame , ckpt_seq ;
154+
155+ open_db ("primary_test_checkpoint.db" , & db_primary );
156+
157+ ensure (sqlite3_exec (db_primary , "CREATE TABLE t (x)" , 0 , 0 , 0 ) == SQLITE_OK , "failed to insert data\n" );
158+ ensure (sqlite3_exec (db_primary , "INSERT INTO t VALUES (randomblob(4 * 1024))" , 0 , 0 , 0 ) == SQLITE_OK , "failed to insert data\n" );
159+ ensure (sqlite3_exec (db_primary , "INSERT INTO t VALUES (randomblob(1 * 1024))" , 0 , 0 , 0 ) == SQLITE_OK , "failed to insert data\n" );
160+ ensure (sqlite3_exec (db_primary , "INSERT INTO t VALUES (randomblob(1 * 1024))" , 0 , 0 , 0 ) == SQLITE_OK , "failed to insert data\n" );
161+
162+ ensure (libsql_wal_checkpoint_seq_count (db_primary , & ckpt_seq ) == SQLITE_OK , "failed to get checkpoint sequence count: %s\n" , sqlite3_errmsg (db_primary ));
163+ assert (ckpt_seq == 0 );
164+ ensure (libsql_wal_frame_count (db_primary , & max_frame ) == SQLITE_OK , "failed to get frames count: %s\n" , sqlite3_errmsg (db_primary ));
165+ assert (max_frame == 7 );
166+
167+ // force checkpoint
168+ ensure (sqlite3_wal_checkpoint_v2 (db_primary , NULL , SQLITE_CHECKPOINT_TRUNCATE , NULL , NULL ) == SQLITE_OK , "failed to checkpoint: %s\n" , sqlite3_errmsg (db_primary ));
169+
170+ ensure (libsql_wal_checkpoint_seq_count (db_primary , & ckpt_seq ) == SQLITE_OK , "failed to get checkpoint sequence count: %s\n" , sqlite3_errmsg (db_primary ));
171+ assert (ckpt_seq == 1 );
172+ ensure (libsql_wal_frame_count (db_primary , & max_frame ) == SQLITE_OK , "failed to get frames count: %s\n" , sqlite3_errmsg (db_primary ));
173+ assert (max_frame == 0 );
174+ }
175+
148176int main (int argc , char * argv [])
149177{
150178 test_huge_payload ();
@@ -156,5 +184,8 @@ int main(int argc, char *argv[])
156184 test_sync_while_reading ();
157185 printf ("============= OK test_sync_while_reading\n" );
158186
187+ test_checkpoint ();
188+ printf ("============= OK test_checkpoint\n" );
189+
159190 return 0 ;
160191}
0 commit comments