Skip to content

Commit 9f07417

Browse files
committed
Cleanup more test code. Remove tests for behavior of old events, now it is expected things occur in order.
Fixing some tx logic on dispose and commit Add base test and remove duplication
1 parent 2e85e39 commit 9f07417

11 files changed

+128
-140
lines changed

.github/workflows/dotnet-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
steps:
1717
- name: Checkout Lightning.NET
1818
uses: actions/checkout@v4
19-
- name: Setup .NET Core
19+
- name: Setup .NET
2020
uses: actions/setup-dotnet@v4
2121
with:
2222
dotnet-version: |

src/LightningDB.Tests/CursorTests.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,13 @@
55

66
namespace LightningDB.Tests;
77

8-
[Collection("SharedFileSystem")]
9-
public class CursorTests : IDisposable
8+
public class CursorTests : TestBase
109
{
11-
private readonly LightningEnvironment _env;
12-
13-
public CursorTests(SharedFileSystem fileSystem)
10+
public CursorTests(SharedFileSystem fileSystem) : base(fileSystem)
1411
{
15-
var path = fileSystem.CreateNewDirectoryForTest();
16-
_env = new LightningEnvironment(path);
1712
_env.Open();
1813
}
1914

20-
public void Dispose()
21-
{
22-
_env.Dispose();
23-
}
24-
2515
private static byte[][] PopulateCursorValues(LightningCursor cursor, int count = 5, string keyPrefix = "key")
2616
{
2717
var keys = Enumerable.Range(1, count)

src/LightningDB.Tests/DatabaseIOTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ public DatabaseIOTests(SharedFileSystem fileSystem)
2424

2525
public void Dispose()
2626
{
27+
_txn.Dispose();
28+
_db.Dispose();
2729
_env.Dispose();
30+
GC.Collect();
31+
GC.WaitForPendingFinalizers();
2832
}
2933

3034
[Fact]

src/LightningDB.Tests/DatabaseTests.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
1-
using System;
2-
using Xunit;
1+
using Xunit;
32
using static System.Text.Encoding;
43

54
namespace LightningDB.Tests;
65

7-
[Collection("SharedFileSystem")]
8-
public class DatabaseTests : IDisposable
6+
public class DatabaseTests(SharedFileSystem fileSystem) : TestBase(fileSystem)
97
{
10-
private readonly LightningEnvironment _env;
118
private LightningTransaction _txn;
12-
13-
public DatabaseTests(SharedFileSystem fileSystem)
14-
{
15-
var path = fileSystem.CreateNewDirectoryForTest();
16-
_env = new LightningEnvironment(path);
17-
}
18-
19-
public void Dispose()
9+
10+
protected override void Dispose(bool disposing)
2011
{
21-
_env.Dispose();
12+
_txn?.Dispose();
13+
base.Dispose(disposing);
2214
}
2315

2416
[Fact]
@@ -80,12 +72,12 @@ public void NamedDatabaseNameExistsInMaster()
8072

8173
using (var tx = _env.BeginTransaction())
8274
{
83-
tx.OpenDatabase("customdb", new DatabaseConfiguration {Flags = DatabaseOpenFlags.Create});
75+
using var db = tx.OpenDatabase("customdb", new DatabaseConfiguration {Flags = DatabaseOpenFlags.Create});
8476
tx.Commit();
8577
}
8678
using (var tx = _env.BeginTransaction())
8779
{
88-
var db = tx.OpenDatabase();
80+
using var db = tx.OpenDatabase();
8981
using (var cursor = tx.CreateCursor(db))
9082
{
9183
cursor.Next();
@@ -110,13 +102,13 @@ public void ReadonlyTransactionOpenedDatabasesDontGetReused()
110102
}
111103
using (var tx = _env.BeginTransaction(TransactionBeginFlags.ReadOnly))
112104
{
113-
var db = tx.OpenDatabase("custom");
105+
using var db = tx.OpenDatabase("custom");
114106
var result = tx.Get(db, "hello");
115107
Assert.Equal("world", result);
116108
}
117109
using (var tx = _env.BeginTransaction(TransactionBeginFlags.ReadOnly))
118110
{
119-
var db = tx.OpenDatabase("custom");
111+
using var db = tx.OpenDatabase("custom");
120112
var result = tx.Get(db, "hello");
121113
Assert.Equal("world", result);
122114
}
@@ -137,6 +129,7 @@ public void DatabaseShouldBeDropped()
137129
db = _txn.OpenDatabase("notmaster");
138130

139131
db.Drop(_txn);
132+
db.Dispose();
140133
_txn.Commit();
141134
_txn.Dispose();
142135

@@ -155,16 +148,19 @@ public void TruncatingTheDatabase()
155148
var db = _txn.OpenDatabase();
156149

157150
_txn.Put(db, "hello", "world");
151+
db.Dispose();
158152
_txn.Commit();
159153
_txn.Dispose();
160154
_txn = _env.BeginTransaction();
161155
db = _txn.OpenDatabase();
162156
db.Truncate(_txn);
157+
db.Dispose();
163158
_txn.Commit();
164159
_txn.Dispose();
165160
_txn = _env.BeginTransaction();
166161
db = _txn.OpenDatabase();
167162
var result = _txn.Get(db, "hello"u8.ToArray());
163+
db.Dispose();
168164

169165
Assert.Equal(MDBResultCode.NotFound, result.resultCode);
170166
}

src/LightningDB.Tests/EnvironmentTests.cs

Lines changed: 26 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,12 @@
44

55
namespace LightningDB.Tests;
66

7-
[Collection("SharedFileSystem")]
8-
public class EnvironmentTests : IDisposable
7+
public class EnvironmentTests(SharedFileSystem fileSystem) : TestBase(fileSystem, false)
98
{
10-
private readonly string _path, _pathCopy, _pathSpecial;
11-
private LightningEnvironment _env;
12-
13-
public EnvironmentTests(SharedFileSystem fileSystem)
14-
{
15-
_path = fileSystem.CreateNewDirectoryForTest();
16-
_pathCopy = fileSystem.CreateNewDirectoryForTest();
17-
_pathSpecial = fileSystem.CreateNewDirectoryForSpecialCharacterTest();
18-
}
19-
20-
public void Dispose()
21-
{
22-
_env?.Dispose();
23-
_env = null;
24-
}
25-
269
[Fact]
2710
public void EnvironmentShouldBeCreatedIfWithoutFlags()
2811
{
29-
_env = new LightningEnvironment(_path);
12+
CreateEnvironment();
3013
_env.Open();
3114
}
3215

@@ -37,7 +20,7 @@ public void EnvironmentCreatedFromConfig()
3720
const int maxDatabaseExpected = 2;
3821
const int maxReadersExpected = 3;
3922
var config = new EnvironmentConfiguration {MapSize = mapExpected, MaxDatabases = maxDatabaseExpected, MaxReaders = maxReadersExpected};
40-
_env = new LightningEnvironment(_path, config);
23+
CreateEnvironment(config: config);
4124
Assert.Equal(mapExpected, _env.MapSize);
4225
Assert.Equal(maxDatabaseExpected, _env.MaxDatabases);
4326
Assert.Equal(maxReadersExpected, _env.MaxReaders);
@@ -46,15 +29,15 @@ public void EnvironmentCreatedFromConfig()
4629
[Fact]
4730
public void StartingTransactionBeforeEnvironmentOpen()
4831
{
49-
_env = new LightningEnvironment(_path);
32+
CreateEnvironment();
5033
Assert.Throws<InvalidOperationException>(() => _env.BeginTransaction());
5134
}
5235

5336
[Fact]
5437
public void CanGetEnvironmentInfo()
5538
{
5639
const long mapSize = 1024 * 1024 * 200;
57-
_env = new LightningEnvironment(_path, new EnvironmentConfiguration
40+
CreateEnvironment(config: new EnvironmentConfiguration
5841
{
5942
MapSize = mapSize
6043
});
@@ -67,7 +50,7 @@ public void CanGetEnvironmentInfo()
6750
public void CanGetLargeEnvironmentInfo()
6851
{
6952
const long mapSize = 1024 * 1024 * 1024 * 3L;
70-
_env = new LightningEnvironment(_path, new EnvironmentConfiguration
53+
CreateEnvironment(config: new EnvironmentConfiguration
7154
{
7255
MapSize = mapSize
7356
});
@@ -80,12 +63,12 @@ public void CanGetLargeEnvironmentInfo()
8063
public void MaxDatabasesWorksThroughConfigIssue62()
8164
{
8265
var config = new EnvironmentConfiguration { MaxDatabases = 2 };
83-
_env = new LightningEnvironment(_path, config);
66+
CreateEnvironment(config: config);
8467
_env.Open();
8568
using (var tx = _env.BeginTransaction())
8669
{
87-
tx.OpenDatabase("db1", new DatabaseConfiguration {Flags = DatabaseOpenFlags.Create});
88-
tx.OpenDatabase("db2", new DatabaseConfiguration {Flags = DatabaseOpenFlags.Create});
70+
using var db = tx.OpenDatabase("db1", new DatabaseConfiguration {Flags = DatabaseOpenFlags.Create});
71+
using var db2 = tx.OpenDatabase("db2", new DatabaseConfiguration {Flags = DatabaseOpenFlags.Create});
8972
tx.Commit();
9073
}
9174
Assert.Equal(2, _env.MaxDatabases);
@@ -94,25 +77,25 @@ public void MaxDatabasesWorksThroughConfigIssue62()
9477
[Fact]
9578
public void CanLoadAndDisposeMultipleEnvironments()
9679
{
97-
_env = new LightningEnvironment(_path);
80+
CreateEnvironment();
9881
_env.Dispose();
99-
_env = new LightningEnvironment(_path);
82+
CreateEnvironment();
10083
}
10184

10285
[Fact]
10386
public void EnvironmentShouldBeCreatedIfReadOnly()
10487
{
105-
_env = new LightningEnvironment(_path);
88+
CreateEnvironment();
10689
_env.Open(); //readonly requires environment to have been created at least once before
10790
_env.Dispose();
108-
_env = new LightningEnvironment(_path);
91+
CreateEnvironment(_env.Path);
10992
_env.Open(EnvironmentOpenFlags.ReadOnly);
11093
}
11194

11295
[Fact]
11396
public void EnvironmentShouldBeOpened()
11497
{
115-
_env = new LightningEnvironment(_path);
98+
CreateEnvironment();
11699
_env.Open();
117100

118101
Assert.True(_env.IsOpened);
@@ -121,11 +104,9 @@ public void EnvironmentShouldBeOpened()
121104
[Fact]
122105
public void EnvironmentShouldBeClosed()
123106
{
124-
_env = new LightningEnvironment(_path);
107+
CreateEnvironment();
125108
_env.Open();
126-
127109
_env.Dispose();
128-
129110
Assert.False(_env.IsOpened);
130111
}
131112

@@ -134,22 +115,23 @@ public void EnvironmentShouldBeClosed()
134115
[InlineData(false)]
135116
public void EnvironmentShouldBeCopied(bool compact)
136117
{
137-
_env = new LightningEnvironment(_path);
138-
_env.Open();
118+
CreateEnvironment();
119+
_env.Open();
139120

140-
_env.CopyTo(_pathCopy, compact).ThrowOnError();
121+
var newPath = TempPath();
122+
_env.CopyTo(newPath, compact).ThrowOnError();
141123

142-
if (Directory.GetFiles(_pathCopy).Length == 0)
124+
if (Directory.GetFiles(newPath).Length == 0)
143125
Assert.Fail("Copied files doesn't exist");
144126
}
145127

146128
[Fact]
147129
public void EnvironmentShouldFailCopyIfPathIsFile()
148130
{
149-
_env = new LightningEnvironment(_path);
131+
CreateEnvironment();
150132
_env.Open();
151133

152-
string filePath = Path.Combine(_pathCopy, "test.txt");
134+
var filePath = Path.Combine(TempPath(), "test.txt");
153135
File.WriteAllBytes(filePath, Array.Empty<byte>());
154136

155137
MDBResultCode result = _env.CopyTo(filePath);
@@ -159,19 +141,16 @@ public void EnvironmentShouldFailCopyIfPathIsFile()
159141
[Fact]
160142
public void CanOpenEnvironmentMoreThan50Mb()
161143
{
162-
_env = new LightningEnvironment(_path)
163-
{
164-
MapSize = 55 * 1024 * 1024
165-
};
166-
144+
CreateEnvironment();
145+
_env.MapSize = 55 * 1024 * 1024;
167146
_env.Open();
168147
}
169148

170149
[Fact]
171150
public void CanOpenEnvironmentWithSpecialCharacters()
172151
{
173-
_env = new LightningEnvironment(_pathSpecial);
174-
152+
//all include special character now
153+
_env = new LightningEnvironment(TempPath("ß"));
175154
_env.Open();
176155
}
177156
}

src/LightningDB.Tests/SharedFileSystem.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.IO;
3+
using System.Reflection;
34
using Xunit;
5+
using Xunit.Sdk;
46

57
namespace LightningDB.Tests;
68

@@ -21,16 +23,9 @@ public void Dispose()
2123
}
2224
}
2325

24-
public string CreateNewDirectoryForTest()
26+
public string CreateNewDirectoryForTest(string seed = "")
2527
{
26-
var path = Path.Combine(_testTempDir, "TestDb", Guid.NewGuid().ToString());
27-
Directory.CreateDirectory(path);
28-
return path;
29-
}
30-
31-
public string CreateNewDirectoryForSpecialCharacterTest()
32-
{
33-
var path = Path.Combine(_testTempDir, "Testß", Guid.NewGuid().ToString());
28+
var path = Path.Combine(_testTempDir, $"Test{seed}", Guid.NewGuid().ToString());
3429
Directory.CreateDirectory(path);
3530
return path;
3631
}
@@ -39,4 +34,17 @@ public string CreateNewDirectoryForSpecialCharacterTest()
3934
[CollectionDefinition("SharedFileSystem")]
4035
public class SharedFileSystemCollection : ICollectionFixture<SharedFileSystem>
4136
{
37+
}
38+
39+
public class PrintTestMethod : BeforeAfterTestAttribute
40+
{
41+
public override void Before(MethodInfo methodUnderTest)
42+
{
43+
Console.WriteLine("Setup for test '{0}.'", methodUnderTest.Name);
44+
}
45+
46+
public override void After(MethodInfo methodUnderTest)
47+
{
48+
Console.WriteLine("TearDown for test '{0}.'", methodUnderTest.Name);
49+
}
4250
}

0 commit comments

Comments
 (0)