Skip to content

Commit 97ec522

Browse files
committed
Added LockFile connection type
1 parent 0027cdc commit 97ec522

7 files changed

Lines changed: 668 additions & 17 deletions

File tree

LiteDBX.Tests/Database/ConnectionString_Tests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public void ConnectionString_Parser()
3131
full.Password.Should().Be("john-doe ");
3232
full.ReadOnly.Should().BeTrue();
3333
full.InitialSize.Should().Be(10 * 1024 * 1024);
34+
35+
var lockFile = new ConnectionString(@"filename=demo.db;connection=LockFile");
36+
37+
lockFile.Connection.Should().Be(ConnectionType.LockFile);
3438
}
3539

3640
[Fact]
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using FluentAssertions;
6+
using Xunit;
7+
8+
namespace LiteDbX.Tests.Engine;
9+
10+
public class LockFileEngine_Tests
11+
{
12+
[Fact]
13+
public void Constructor_Requires_Physical_Filename()
14+
{
15+
Action act = () => _ = new LockFileEngine(new LiteDbX.Engine.EngineSettings { Filename = ":memory:" });
16+
17+
act.Should().Throw<NotSupportedException>()
18+
.WithMessage("*physical Filename*");
19+
}
20+
21+
[Fact]
22+
public async Task Write_Waits_For_Lock_File_And_Honours_Cancellation()
23+
{
24+
using var file = new TempFile();
25+
var lockFilename = FileHelper.GetLockFile(file.Filename);
26+
27+
await using var database = new LiteDatabase(new ConnectionString
28+
{
29+
Filename = file.Filename,
30+
Connection = ConnectionType.LockFile
31+
});
32+
33+
var collection = database.GetCollection("lockfile");
34+
35+
using (new FileStream(lockFilename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
36+
{
37+
using (var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(250)))
38+
{
39+
await Assert.ThrowsAnyAsync<OperationCanceledException>(async () =>
40+
{
41+
await collection.Insert(new BsonDocument(), cts.Token);
42+
});
43+
}
44+
}
45+
46+
await collection.Insert(new BsonDocument());
47+
(await collection.Count()).Should().Be(1);
48+
}
49+
50+
[Fact]
51+
public async Task Separate_Database_Instances_Can_Write_Sequentially_To_Same_File()
52+
{
53+
using var file = new TempFile();
54+
55+
await using var first = new LiteDatabase(new ConnectionString
56+
{
57+
Filename = file.Filename,
58+
Connection = ConnectionType.LockFile
59+
});
60+
61+
await using var second = new LiteDatabase(new ConnectionString
62+
{
63+
Filename = file.Filename,
64+
Connection = ConnectionType.LockFile
65+
});
66+
67+
var firstCollection = first.GetCollection("items");
68+
var secondCollection = second.GetCollection("items");
69+
70+
await firstCollection.Insert(new BsonDocument { ["name"] = "first" });
71+
await secondCollection.Insert(new BsonDocument { ["name"] = "second" });
72+
73+
(await firstCollection.Count()).Should().Be(2);
74+
(await secondCollection.Count()).Should().Be(2);
75+
}
76+
77+
[Fact]
78+
public async Task Read_First_On_New_File_Creates_Database_Safely()
79+
{
80+
using var file = new TempFile();
81+
82+
await using var database = new LiteDatabase(new ConnectionString
83+
{
84+
Filename = file.Filename,
85+
Connection = ConnectionType.LockFile
86+
});
87+
88+
var collection = database.GetCollection("items");
89+
90+
(await collection.Count()).Should().Be(0);
91+
File.Exists(file.Filename).Should().BeTrue();
92+
}
93+
}
94+

LiteDBX.Tests/Engine/Recursion_Tests.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ namespace LiteDbX.Tests.Engine;
77

88
public class Recursion_Tests
99
{
10-
[Fact]
11-
public async Task UpdateInFindAll()
10+
[Theory]
11+
[InlineData(ConnectionType.Shared)]
12+
[InlineData(ConnectionType.LockFile)]
13+
public async Task UpdateInFindAll(ConnectionType connectionType)
1214
{
13-
await Test(async collection =>
15+
await Test(connectionType, async collection =>
1416
{
1517
var updated = 0;
1618

@@ -25,10 +27,12 @@ await Test(async collection =>
2527
});
2628
}
2729

28-
[Fact]
29-
public async Task InsertDeleteInFindAll()
30+
[Theory]
31+
[InlineData(ConnectionType.Shared)]
32+
[InlineData(ConnectionType.LockFile)]
33+
public async Task InsertDeleteInFindAll(ConnectionType connectionType)
3034
{
31-
await Test(async collection =>
35+
await Test(connectionType, async collection =>
3236
{
3337
var iterations = 0;
3438

@@ -44,10 +48,12 @@ await Test(async collection =>
4448
});
4549
}
4650

47-
[Fact]
48-
public async Task QueryInFindAll()
51+
[Theory]
52+
[InlineData(ConnectionType.Shared)]
53+
[InlineData(ConnectionType.LockFile)]
54+
public async Task QueryInFindAll(ConnectionType connectionType)
4955
{
50-
await Test(async collection =>
56+
await Test(connectionType, async collection =>
5157
{
5258
var iterations = 0;
5359

@@ -61,10 +67,12 @@ await Test(async collection =>
6167
});
6268
}
6369

64-
[Fact]
65-
public async Task BreakInFindAll_ReleasesSharedLease()
70+
[Theory]
71+
[InlineData(ConnectionType.Shared)]
72+
[InlineData(ConnectionType.LockFile)]
73+
public async Task BreakInFindAll_ReleasesSharedLease(ConnectionType connectionType)
6674
{
67-
await Test(async collection =>
75+
await Test(connectionType, async collection =>
6876
{
6977
var iterations = 0;
7078

@@ -82,14 +90,14 @@ await Test(async collection =>
8290
});
8391
}
8492

85-
private static async Task Test(Func<ILiteCollection<BsonDocument>, Task> action)
93+
private static async Task Test(ConnectionType connectionType, Func<ILiteCollection<BsonDocument>, Task> action)
8694
{
8795
using var file = new TempFile();
8896

8997
await using var database = new LiteDatabase(new ConnectionString
9098
{
9199
Filename = file.Filename,
92-
Connection = ConnectionType.Shared
100+
Connection = connectionType
93101
});
94102

95103
var accounts = database.GetCollection("Recursion");

0 commit comments

Comments
 (0)