Skip to content

Commit c32a197

Browse files
committed
Fix stuck unit tests
1 parent ebc9999 commit c32a197

7 files changed

Lines changed: 105 additions & 26 deletions

File tree

LiteDB.Stress/Test/TestExecution.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public void Execute()
4747
this.CreateThreads();
4848

4949
// start report thread
50-
var t = new Thread(() => this.ReportThread());
50+
var t = new Thread(() => this.ReportThread())
51+
{
52+
IsBackground = true
53+
};
5154
t.Name = "REPORT";
5255
t.Start();
5356
}
@@ -100,7 +103,10 @@ private void CreateThreads()
100103
info.Counter++;
101104
info.LastRun = DateTime.Now;
102105
}
103-
});
106+
})
107+
{
108+
IsBackground = true
109+
};
104110

105111
_threads[thread.ManagedThreadId] = new ThreadInfo
106112
{

LiteDB.Tests/Engine/Rebuild_Crash_Tests.cs

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
using FluentAssertions;
1+
using FluentAssertions;
22
using LiteDB.Engine;
33
using System;
44
using System.IO;
55
using System.Linq;
6+
using System.Threading;
67
using System.Threading.Tasks;
78

89
using Xunit;
910
using Xunit.Abstractions;
1011

11-
#if DEBUG || TESTING
12+
#if DEBUG
1213
namespace 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
}

LiteDB.Tests/Engine/Recursion_Tests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace LiteDB.Tests.Engine;
55

6+
[Collection("SharedDemoDatabase")]
67
public class Recursion_Tests
78
{
89
[Fact]

LiteDB.Tests/Engine/Transactions_Tests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,10 @@ public void Test_Transaction_ReleaseWhenFailToStart()
370370
blockingStream.ShouldBlock = true;
371371
db.Checkpoint();
372372
db.Dispose();
373-
});
373+
})
374+
{
375+
IsBackground = true
376+
};
374377
lockerThread.Start();
375378
blockingStream.Blocked.WaitOne(200).Should().BeTrue();
376379
Assert.Throws<LiteException>(() => db.GetCollection<Person>().Insert(new Person())).Message.Should().Contain("timeout");
@@ -418,4 +421,4 @@ private static void SetEngineTimeout(LiteDatabase database, TimeSpan timeout)
418421
setter.Invoke(pragmas, new object[] { timeout });
419422
}
420423
}
421-
}
424+
}

LiteDB.Tests/Issues/Issue2534_Tests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace LiteDB.Tests.Issues;
44

5+
[Collection("SharedDemoDatabase")]
56
public class Issue2534_Tests
67
{
78
[Fact]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace LiteDB.Tests;
2+
3+
using System;
4+
using System.IO;
5+
using Xunit;
6+
7+
[CollectionDefinition("SharedDemoDatabase", DisableParallelization = true)]
8+
public sealed class SharedDemoDatabaseCollection : ICollectionFixture<SharedDemoDatabaseFixture>
9+
{
10+
}
11+
12+
public sealed class SharedDemoDatabaseFixture : IDisposable
13+
{
14+
private readonly string _filename;
15+
16+
public SharedDemoDatabaseFixture()
17+
{
18+
_filename = Path.GetFullPath("Demo.db");
19+
TryDeleteFile();
20+
}
21+
22+
public void Dispose()
23+
{
24+
TryDeleteFile();
25+
}
26+
27+
private void TryDeleteFile()
28+
{
29+
try
30+
{
31+
if (File.Exists(_filename))
32+
{
33+
File.Delete(_filename);
34+
}
35+
}
36+
catch (IOException)
37+
{
38+
}
39+
catch (UnauthorizedAccessException)
40+
{
41+
}
42+
}
43+
}

tests.ci.runsettings

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RunSettings>
3+
<RunConfiguration>
4+
<!-- Increase timeouts for CI environment -->
5+
<!-- <TestSessionTimeout>600000</TestSessionTimeout> 10 minutes -->
6+
<TestSessionTimeout>180000</TestSessionTimeout> <!-- 3 minutes -->
7+
<TestCaseTimeout>60000</TestCaseTimeout> <!-- 60 seconds per test -->
8+
9+
<!-- Optimize for CI -->
10+
<MaxCpuCount>1</MaxCpuCount> <!-- Single-threaded to avoid resource contention -->
11+
<DisableParallelization>true</DisableParallelization>
12+
</RunConfiguration>
13+
14+
<!-- CI-specific test filters -->
15+
<MSTest>
16+
<DeploymentEnabled>false</DeploymentEnabled>
17+
</MSTest>
18+
</RunSettings>

0 commit comments

Comments
 (0)