1- using FluentAssertions ;
1+ using FluentAssertions ;
22using LiteDB . Engine ;
33using System ;
44using System . IO ;
55using System . Linq ;
6+ using System . Threading ;
67using System . Threading . Tasks ;
78
89using Xunit ;
910using Xunit . Abstractions ;
1011
11- #if DEBUG || TESTING
12+ #if DEBUG
1213namespace LiteDB . Tests . Engine
1314{
1415 public class Rebuild_Crash_Tests
@@ -20,16 +21,13 @@ public Rebuild_Crash_Tests(ITestOutputHelper output)
2021 _output = output ;
2122 }
2223
23- [ Fact ( Timeout = 30000 ) ]
24- public async Task Rebuild_Crash_IO_Write_Error ( )
24+ [ Fact ]
25+ public void Rebuild_Crash_IO_Write_Error ( )
2526 {
26- var testName = nameof ( Rebuild_Crash_IO_Write_Error ) ;
27-
28- _output . WriteLine ( $ "starting { testName } ") ;
29-
27+ _output . WriteLine ( "Running Rebuild_Crash_IO_Write_Error" ) ;
3028 try
3129 {
32- var N = 1000 ;
30+ var N = 1_000 ;
3331
3432 using ( var file = new TempFile ( ) )
3533 {
@@ -40,31 +38,40 @@ public async Task Rebuild_Crash_IO_Write_Error()
4038 Password = "46jLz5QWd5fI3m4LiL2r"
4139 } ;
4240
43- var initial = new DateTime ( 2024 , 1 , 1 ) ;
44-
4541 var data = Enumerable . Range ( 1 , N ) . Select ( i => new BsonDocument
4642 {
4743 [ "_id" ] = i ,
48- [ "name" ] = $ "user- { i : D4 } " ,
49- [ "age" ] = 18 + ( i % 60 ) ,
50- [ "created" ] = initial . AddDays ( i ) ,
51- [ "lorem" ] = new string ( ( char ) ( 'a' + ( i % 26 ) ) , 800 )
44+ [ "name" ] = Faker . Fullname ( ) ,
45+ [ "age" ] = Faker . Age ( ) ,
46+ [ "created" ] = Faker . Birthday ( ) ,
47+ [ "lorem" ] = Faker . Lorem ( 5 , 25 )
5248 } ) . ToArray ( ) ;
5349
50+ var faultInjected = 0 ;
51+
5452 try
5553 {
5654 using ( var db = new LiteEngine ( settings ) )
5755 {
56+ var writeHits = 0 ;
57+
5858 db . SimulateDiskWriteFail = ( page ) =>
5959 {
6060 var p = new BasePage ( page ) ;
6161
62- if ( p . PageID == 28 )
62+ if ( p . PageType == PageType . Data && p . ColID == 1 )
6363 {
64- p . ColID . Should ( ) . Be ( 1 ) ;
65- p . PageType . Should ( ) . Be ( PageType . Data ) ;
64+ var hit = Interlocked . Increment ( ref writeHits ) ;
6665
67- page . Write ( ( uint ) 123123123 , 8192 - 4 ) ;
66+ if ( hit == 10 )
67+ {
68+ p . PageType . Should ( ) . Be ( PageType . Data ) ;
69+ p . ColID . Should ( ) . Be ( 1 ) ;
70+
71+ page . Write ( ( uint ) 123123123 , 8192 - 4 ) ;
72+
73+ Interlocked . Exchange ( ref faultInjected , 1 ) ;
74+ }
6875 }
6976 } ;
7077
@@ -86,6 +93,8 @@ public async Task Rebuild_Crash_IO_Write_Error()
8693 }
8794 catch ( Exception ex )
8895 {
96+ faultInjected . Should ( ) . Be ( 1 , "the simulated disk write fault should have triggered" ) ;
97+
8998 Assert . True ( ex is LiteException lex && lex . ErrorCode == 999 ) ;
9099 }
91100
@@ -103,12 +112,10 @@ public async Task Rebuild_Crash_IO_Write_Error()
103112
104113 }
105114 }
106-
107- await Task . CompletedTask ;
108115 }
109116 finally
110117 {
111- _output . WriteLine ( $ " { testName } completed ") ;
118+ _output . WriteLine ( "Finished running Rebuild_Crash_IO_Write_Error ") ;
112119 }
113120 }
114121 }
0 commit comments