Reformat / cleanup
This commit is contained in:
@@ -1,25 +1,19 @@
|
||||
using ZB.MOM.WW.CBDD.Bson;
|
||||
using ZB.MOM.WW.CBDD.Core;
|
||||
using ZB.MOM.WW.CBDD.Core.Collections;
|
||||
using ZB.MOM.WW.CBDD.Core.Indexing;
|
||||
using ZB.MOM.WW.CBDD.Core.Storage;
|
||||
using ZB.MOM.WW.CBDD.Core.Transactions;
|
||||
using ZB.MOM.WW.CBDD.Shared;
|
||||
using ZB.MOM.WW.CBDD.Shared.TestDbContext_TestDbContext_Mappers;
|
||||
using System.Buffers;
|
||||
using Xunit;
|
||||
|
||||
namespace ZB.MOM.WW.CBDD.Tests;
|
||||
|
||||
public class WalIndexTests : IDisposable
|
||||
{
|
||||
private readonly TestDbContext _db;
|
||||
private readonly string _dbPath;
|
||||
private readonly string _walPath;
|
||||
private readonly Shared.TestDbContext _db;
|
||||
private readonly ITestOutputHelper _output;
|
||||
private readonly string _walPath;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WalIndexTests"/> class.
|
||||
/// Initializes a new instance of the <see cref="WalIndexTests" /> class.
|
||||
/// </summary>
|
||||
/// <param name="output">Test output sink.</param>
|
||||
public WalIndexTests(ITestOutputHelper output)
|
||||
@@ -29,11 +23,41 @@ public class WalIndexTests : IDisposable
|
||||
// WAL defaults to .wal next to db
|
||||
_walPath = Path.ChangeExtension(_dbPath, ".wal");
|
||||
|
||||
_db = new Shared.TestDbContext(_dbPath);
|
||||
_db = new TestDbContext(_dbPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies index writes are recorded in the WAL.
|
||||
/// Releases test resources.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
try
|
||||
{
|
||||
_db?.Dispose(); // Safe to call multiple times
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (File.Exists(_dbPath)) File.Delete(_dbPath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (File.Exists(_walPath)) File.Delete(_walPath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies index writes are recorded in the WAL.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void IndexWritesAreLoggedToWal()
|
||||
@@ -71,8 +95,8 @@ public class WalIndexTests : IDisposable
|
||||
_output.WriteLine($"Found {writeRecords.Count} Write records for Txn {txn.TransactionId}");
|
||||
|
||||
// Analyze pages
|
||||
int indexPageCount = 0;
|
||||
int dataPageCount = 0;
|
||||
var indexPageCount = 0;
|
||||
var dataPageCount = 0;
|
||||
|
||||
foreach (var record in writeRecords)
|
||||
{
|
||||
@@ -89,21 +113,18 @@ public class WalIndexTests : IDisposable
|
||||
|
||||
private PageType ParsePageType(byte[]? pageData)
|
||||
{
|
||||
if (pageData == null || pageData.Length < 32) return (PageType)0;
|
||||
if (pageData == null || pageData.Length < 32) return 0;
|
||||
// PageType is at offset 4 (1 byte)
|
||||
return (PageType)pageData[4]; // Casting byte to PageType
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies offline compaction leaves the WAL empty.
|
||||
/// Verifies offline compaction leaves the WAL empty.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Compact_ShouldLeaveWalEmpty_AfterOfflineRun()
|
||||
{
|
||||
for (var i = 0; i < 100; i++)
|
||||
{
|
||||
_db.Users.Insert(new User { Name = $"wal-compact-{i:D3}", Age = i % 30 });
|
||||
}
|
||||
for (var i = 0; i < 100; i++) _db.Users.Insert(new User { Name = $"wal-compact-{i:D3}", Age = i % 30 });
|
||||
|
||||
_db.SaveChanges();
|
||||
_db.Storage.GetWalSize().ShouldBeGreaterThan(0);
|
||||
@@ -121,24 +142,22 @@ public class WalIndexTests : IDisposable
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies WAL recovery followed by compaction preserves data.
|
||||
/// Verifies WAL recovery followed by compaction preserves data.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void Recover_WithCommittedWal_ThenCompact_ShouldPreserveData()
|
||||
{
|
||||
var dbPath = Path.Combine(Path.GetTempPath(), $"test_wal_recover_compact_{Guid.NewGuid():N}.db");
|
||||
var walPath = Path.ChangeExtension(dbPath, ".wal");
|
||||
string dbPath = Path.Combine(Path.GetTempPath(), $"test_wal_recover_compact_{Guid.NewGuid():N}.db");
|
||||
string walPath = Path.ChangeExtension(dbPath, ".wal");
|
||||
var markerPath = $"{dbPath}.compact.state";
|
||||
var expectedIds = new List<ObjectId>();
|
||||
|
||||
try
|
||||
{
|
||||
using (var writer = new Shared.TestDbContext(dbPath))
|
||||
using (var writer = new TestDbContext(dbPath))
|
||||
{
|
||||
for (var i = 0; i < 48; i++)
|
||||
{
|
||||
expectedIds.Add(writer.Users.Insert(new User { Name = $"recover-{i:D3}", Age = i % 10 }));
|
||||
}
|
||||
|
||||
writer.SaveChanges();
|
||||
writer.Storage.GetWalSize().ShouldBeGreaterThan(0);
|
||||
@@ -146,16 +165,13 @@ public class WalIndexTests : IDisposable
|
||||
|
||||
new FileInfo(walPath).Length.ShouldBeGreaterThan(0);
|
||||
|
||||
using (var recovered = new Shared.TestDbContext(dbPath))
|
||||
using (var recovered = new TestDbContext(dbPath))
|
||||
{
|
||||
recovered.Users.Count().ShouldBe(expectedIds.Count);
|
||||
recovered.Compact();
|
||||
recovered.Storage.GetWalSize().ShouldBe(0);
|
||||
|
||||
foreach (var id in expectedIds)
|
||||
{
|
||||
recovered.Users.FindById(id).ShouldNotBeNull();
|
||||
}
|
||||
foreach (var id in expectedIds) recovered.Users.FindById(id).ShouldNotBeNull();
|
||||
}
|
||||
}
|
||||
finally
|
||||
@@ -165,19 +181,4 @@ public class WalIndexTests : IDisposable
|
||||
if (File.Exists(markerPath)) File.Delete(markerPath);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Releases test resources.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
try
|
||||
{
|
||||
_db?.Dispose(); // Safe to call multiple times
|
||||
}
|
||||
catch { }
|
||||
|
||||
try { if (File.Exists(_dbPath)) File.Delete(_dbPath); } catch { }
|
||||
try { if (File.Exists(_walPath)) File.Delete(_walPath); } catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user