|
| 1 | +// <copyright file="CrashRecoveryTests.cs" company="MPCoreDeveloper"> |
| 2 | +// Copyright (c) 2025-2026 MPCoreDeveloper and GitHub Copilot. All rights reserved. |
| 3 | +// Licensed under the MIT License. See LICENSE file in the project root for full license information. |
| 4 | +// </copyright> |
| 5 | + |
| 6 | +namespace SharpCoreDB.Tests.Storage; |
| 7 | + |
| 8 | +using System; |
| 9 | +using System.IO; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using SharpCoreDB.Storage; |
| 12 | +using SharpCoreDB.Storage.Scdb; |
| 13 | +using Xunit; |
| 14 | +using Xunit.Abstractions; |
| 15 | + |
| 16 | +/// <summary> |
| 17 | +/// Crash recovery tests for Phase 3 WAL implementation. |
| 18 | +/// Validates zero data loss guarantee and transaction ACID properties. |
| 19 | +/// C# 14: Modern test patterns with async/await. |
| 20 | +/// </summary> |
| 21 | +public sealed class CrashRecoveryTests : IDisposable |
| 22 | +{ |
| 23 | + private readonly ITestOutputHelper _output; |
| 24 | + private readonly string _testDbPath; |
| 25 | + |
| 26 | + public CrashRecoveryTests(ITestOutputHelper output) |
| 27 | + { |
| 28 | + _output = output; |
| 29 | + _testDbPath = Path.Combine(Path.GetTempPath(), $"crash_test_{Guid.NewGuid():N}.scdb"); |
| 30 | + } |
| 31 | + |
| 32 | + public void Dispose() |
| 33 | + { |
| 34 | + try |
| 35 | + { |
| 36 | + if (File.Exists(_testDbPath)) |
| 37 | + { |
| 38 | + File.Delete(_testDbPath); |
| 39 | + } |
| 40 | + } |
| 41 | + catch |
| 42 | + { |
| 43 | + // Ignore cleanup errors |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // ======================================== |
| 48 | + // Basic Recovery Tests |
| 49 | + // ======================================== |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public async Task BasicRecovery_CommittedTransaction_DataPersists() |
| 53 | + { |
| 54 | + // Arrange - Write data in transaction |
| 55 | + using (var provider = CreateProvider()) |
| 56 | + { |
| 57 | + provider.WalManager.BeginTransaction(); |
| 58 | + |
| 59 | + var testData = new byte[100]; |
| 60 | + Array.Fill(testData, (byte)42); |
| 61 | + |
| 62 | + await provider.WriteBlockAsync("test_block", testData); |
| 63 | + await provider.WalManager.CommitTransactionAsync(); |
| 64 | + |
| 65 | + // Simulate crash - dispose without proper flush |
| 66 | + } |
| 67 | + |
| 68 | + // Act - Reopen and recover |
| 69 | + RecoveryInfo recoveryInfo; |
| 70 | + using (var provider = CreateProvider()) |
| 71 | + { |
| 72 | + var recoveryManager = new RecoveryManager(provider, provider.WalManager); |
| 73 | + recoveryInfo = await recoveryManager.RecoverAsync(); |
| 74 | + |
| 75 | + // Assert - Data should be recovered |
| 76 | + var recovered = await provider.ReadBlockAsync("test_block"); |
| 77 | + Assert.NotNull(recovered); |
| 78 | + Assert.Equal(100, recovered.Length); |
| 79 | + Assert.All(recovered, b => Assert.Equal(42, b)); |
| 80 | + } |
| 81 | + |
| 82 | + _output.WriteLine(recoveryInfo.ToString()); |
| 83 | + Assert.True(recoveryInfo.RecoveryNeeded); |
| 84 | + Assert.Equal(1, recoveryInfo.CommittedTransactions); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public async Task BasicRecovery_UncommittedTransaction_DataLost() |
| 89 | + { |
| 90 | + // Arrange - Write data but don't commit |
| 91 | + using (var provider = CreateProvider()) |
| 92 | + { |
| 93 | + provider.WalManager.BeginTransaction(); |
| 94 | + |
| 95 | + var testData = new byte[100]; |
| 96 | + Array.Fill(testData, (byte)99); |
| 97 | + |
| 98 | + await provider.WriteBlockAsync("uncommitted_block", testData); |
| 99 | + |
| 100 | + // Simulate crash - no commit, no flush |
| 101 | + } |
| 102 | + |
| 103 | + // Act - Reopen and recover |
| 104 | + RecoveryInfo recoveryInfo; |
| 105 | + using (var provider = CreateProvider()) |
| 106 | + { |
| 107 | + var recoveryManager = new RecoveryManager(provider, provider.WalManager); |
| 108 | + recoveryInfo = await recoveryManager.RecoverAsync(); |
| 109 | + |
| 110 | + // Assert - Data should NOT be present |
| 111 | + var exists = provider.BlockExists("uncommitted_block"); |
| 112 | + Assert.False(exists); |
| 113 | + } |
| 114 | + |
| 115 | + _output.WriteLine(recoveryInfo.ToString()); |
| 116 | + Assert.Equal(1, recoveryInfo.UncommittedTransactions); |
| 117 | + } |
| 118 | + |
| 119 | + // ======================================== |
| 120 | + // Multi-Transaction Tests |
| 121 | + // ======================================== |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public async Task MultiTransaction_MixedCommits_OnlyCommittedRecovered() |
| 125 | + { |
| 126 | + // Arrange - Multiple transactions, some committed |
| 127 | + using (var provider = CreateProvider()) |
| 128 | + { |
| 129 | + // Transaction 1: Committed |
| 130 | + provider.WalManager.BeginTransaction(); |
| 131 | + await provider.WriteBlockAsync("block1", new byte[50]); |
| 132 | + await provider.WalManager.CommitTransactionAsync(); |
| 133 | + |
| 134 | + // Transaction 2: Uncommitted |
| 135 | + provider.WalManager.BeginTransaction(); |
| 136 | + await provider.WriteBlockAsync("block2", new byte[50]); |
| 137 | + // No commit |
| 138 | + |
| 139 | + // Transaction 3: Committed |
| 140 | + provider.WalManager.BeginTransaction(); |
| 141 | + await provider.WriteBlockAsync("block3", new byte[50]); |
| 142 | + await provider.WalManager.CommitTransactionAsync(); |
| 143 | + |
| 144 | + // Simulate crash |
| 145 | + } |
| 146 | + |
| 147 | + // Act - Recover |
| 148 | + using (var provider = CreateProvider()) |
| 149 | + { |
| 150 | + var recoveryManager = new RecoveryManager(provider, provider.WalManager); |
| 151 | + var info = await recoveryManager.RecoverAsync(); |
| 152 | + |
| 153 | + // Assert |
| 154 | + _output.WriteLine($"Recovery: {info}"); |
| 155 | + Assert.Equal(2, info.CommittedTransactions); // T1 and T3 |
| 156 | + Assert.Equal(1, info.UncommittedTransactions); // T2 |
| 157 | + |
| 158 | + Assert.True(provider.BlockExists("block1")); |
| 159 | + Assert.False(provider.BlockExists("block2")); // Uncommitted |
| 160 | + Assert.True(provider.BlockExists("block3")); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + // ======================================== |
| 165 | + // Checkpoint Tests |
| 166 | + // ======================================== |
| 167 | + |
| 168 | + [Fact] |
| 169 | + public async Task CheckpointRecovery_OnlyReplaysAfterCheckpoint() |
| 170 | + { |
| 171 | + // Arrange |
| 172 | + using (var provider = CreateProvider()) |
| 173 | + { |
| 174 | + // Before checkpoint |
| 175 | + provider.WalManager.BeginTransaction(); |
| 176 | + await provider.WriteBlockAsync("before_cp", new byte[50]); |
| 177 | + await provider.WalManager.CommitTransactionAsync(); |
| 178 | + await provider.FlushAsync(); |
| 179 | + |
| 180 | + // Checkpoint |
| 181 | + await provider.WalManager.CheckpointAsync(); |
| 182 | + |
| 183 | + // After checkpoint |
| 184 | + provider.WalManager.BeginTransaction(); |
| 185 | + await provider.WriteBlockAsync("after_cp", new byte[50]); |
| 186 | + await provider.WalManager.CommitTransactionAsync(); |
| 187 | + |
| 188 | + // Simulate crash |
| 189 | + } |
| 190 | + |
| 191 | + // Act - Recover |
| 192 | + using (var provider = CreateProvider()) |
| 193 | + { |
| 194 | + var recoveryManager = new RecoveryManager(provider, provider.WalManager); |
| 195 | + var info = await recoveryManager.RecoverAsync(); |
| 196 | + |
| 197 | + // Assert - Should only replay transactions after checkpoint |
| 198 | + _output.WriteLine($"Recovery: {info}"); |
| 199 | + // In real implementation, this would verify only 1 transaction replayed |
| 200 | + Assert.True(info.RecoveryNeeded); |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + // ======================================== |
| 205 | + // Corruption Tests |
| 206 | + // ======================================== |
| 207 | + |
| 208 | + [Fact] |
| 209 | + public async Task CorruptedWalEntry_GracefulHandling() |
| 210 | + { |
| 211 | + // Arrange - Write valid transaction |
| 212 | + using (var provider = CreateProvider()) |
| 213 | + { |
| 214 | + provider.WalManager.BeginTransaction(); |
| 215 | + await provider.WriteBlockAsync("valid_block", new byte[50]); |
| 216 | + await provider.WalManager.CommitTransactionAsync(); |
| 217 | + await provider.FlushAsync(); |
| 218 | + } |
| 219 | + |
| 220 | + // Corrupt WAL file |
| 221 | + using (var fs = new FileStream(_testDbPath, FileMode.Open, FileAccess.ReadWrite)) |
| 222 | + { |
| 223 | + // Corrupt some bytes in WAL region |
| 224 | + fs.Position = 1024 * 1024; // Somewhere in WAL |
| 225 | + fs.WriteByte(0xFF); |
| 226 | + fs.WriteByte(0xFF); |
| 227 | + } |
| 228 | + |
| 229 | + // Act - Attempt recovery |
| 230 | + using (var provider = CreateProvider()) |
| 231 | + { |
| 232 | + var recoveryManager = new RecoveryManager(provider, provider.WalManager); |
| 233 | + |
| 234 | + // Should not throw, handle corruption gracefully |
| 235 | + var exception = await Record.ExceptionAsync(async () => |
| 236 | + { |
| 237 | + await recoveryManager.RecoverAsync(); |
| 238 | + }); |
| 239 | + |
| 240 | + Assert.Null(exception); // Should handle gracefully |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + // ======================================== |
| 245 | + // Performance Tests |
| 246 | + // ======================================== |
| 247 | + |
| 248 | + [Fact] |
| 249 | + public async Task Recovery_1000Transactions_UnderOneSecond() |
| 250 | + { |
| 251 | + // Arrange - Write 1000 transactions |
| 252 | + using (var provider = CreateProvider()) |
| 253 | + { |
| 254 | + for (int i = 0; i < 1000; i++) |
| 255 | + { |
| 256 | + provider.WalManager.BeginTransaction(); |
| 257 | + await provider.WriteBlockAsync($"block_{i}", new byte[100]); |
| 258 | + await provider.WalManager.CommitTransactionAsync(); |
| 259 | + } |
| 260 | + |
| 261 | + // Simulate crash |
| 262 | + } |
| 263 | + |
| 264 | + // Act - Measure recovery time |
| 265 | + var sw = System.Diagnostics.Stopwatch.StartNew(); |
| 266 | + using (var provider = CreateProvider()) |
| 267 | + { |
| 268 | + var recoveryManager = new RecoveryManager(provider, provider.WalManager); |
| 269 | + var info = await recoveryManager.RecoverAsync(); |
| 270 | + sw.Stop(); |
| 271 | + |
| 272 | + // Assert |
| 273 | + _output.WriteLine($"Recovery: {info}"); |
| 274 | + _output.WriteLine($"Time: {sw.ElapsedMilliseconds}ms"); |
| 275 | + |
| 276 | + Assert.Equal(1000, info.CommittedTransactions); |
| 277 | + Assert.True(sw.ElapsedMilliseconds < 1000, |
| 278 | + $"Recovery took {sw.ElapsedMilliseconds}ms, expected <1000ms"); |
| 279 | + } |
| 280 | + } |
| 281 | + |
| 282 | + [Fact] |
| 283 | + public async Task Recovery_LargeWAL_Efficient() |
| 284 | + { |
| 285 | + // Arrange - Fill WAL with many entries |
| 286 | + using (var provider = CreateProvider()) |
| 287 | + { |
| 288 | + for (int i = 0; i < 100; i++) |
| 289 | + { |
| 290 | + provider.WalManager.BeginTransaction(); |
| 291 | + |
| 292 | + // Multiple operations per transaction |
| 293 | + for (int j = 0; j < 10; j++) |
| 294 | + { |
| 295 | + await provider.WriteBlockAsync($"block_{i}_{j}", new byte[50]); |
| 296 | + } |
| 297 | + |
| 298 | + await provider.WalManager.CommitTransactionAsync(); |
| 299 | + } |
| 300 | + } |
| 301 | + |
| 302 | + // Act - Recover |
| 303 | + var sw = System.Diagnostics.Stopwatch.StartNew(); |
| 304 | + using (var provider = CreateProvider()) |
| 305 | + { |
| 306 | + var recoveryManager = new RecoveryManager(provider, provider.WalManager); |
| 307 | + var info = await recoveryManager.RecoverAsync(); |
| 308 | + sw.Stop(); |
| 309 | + |
| 310 | + // Assert |
| 311 | + _output.WriteLine($"Recovery: {info}"); |
| 312 | + _output.WriteLine($"Time: {sw.ElapsedMilliseconds}ms"); |
| 313 | + _output.WriteLine($"Operations: {info.OperationsReplayed}"); |
| 314 | + |
| 315 | + Assert.Equal(100, info.CommittedTransactions); |
| 316 | + Assert.True(info.OperationsReplayed > 0); |
| 317 | + } |
| 318 | + } |
| 319 | + |
| 320 | + // ======================================== |
| 321 | + // Edge Cases |
| 322 | + // ======================================== |
| 323 | + |
| 324 | + [Fact] |
| 325 | + public async Task Recovery_EmptyWAL_NoRecoveryNeeded() |
| 326 | + { |
| 327 | + // Arrange - Fresh database |
| 328 | + using (var provider = CreateProvider()) |
| 329 | + { |
| 330 | + await provider.FlushAsync(); |
| 331 | + } |
| 332 | + |
| 333 | + // Act - Recover |
| 334 | + using (var provider = CreateProvider()) |
| 335 | + { |
| 336 | + var recoveryManager = new RecoveryManager(provider, provider.WalManager); |
| 337 | + var info = await recoveryManager.RecoverAsync(); |
| 338 | + |
| 339 | + // Assert |
| 340 | + Assert.False(info.RecoveryNeeded); |
| 341 | + Assert.Equal(0, info.TotalEntries); |
| 342 | + Assert.Equal(0, info.CommittedTransactions); |
| 343 | + } |
| 344 | + } |
| 345 | + |
| 346 | + [Fact] |
| 347 | + public async Task Recovery_AbortedTransaction_NoReplay() |
| 348 | + { |
| 349 | + // Arrange - Transaction with explicit abort |
| 350 | + using (var provider = CreateProvider()) |
| 351 | + { |
| 352 | + provider.WalManager.BeginTransaction(); |
| 353 | + await provider.WriteBlockAsync("aborted_block", new byte[50]); |
| 354 | + provider.WalManager.RollbackTransaction(); |
| 355 | + |
| 356 | + // Simulate crash |
| 357 | + } |
| 358 | + |
| 359 | + // Act - Recover |
| 360 | + using (var provider = CreateProvider()) |
| 361 | + { |
| 362 | + var recoveryManager = new RecoveryManager(provider, provider.WalManager); |
| 363 | + var info = await recoveryManager.RecoverAsync(); |
| 364 | + |
| 365 | + // Assert - Aborted transaction should not be replayed |
| 366 | + Assert.False(provider.BlockExists("aborted_block")); |
| 367 | + Assert.Equal(0, info.CommittedTransactions); |
| 368 | + } |
| 369 | + } |
| 370 | + |
| 371 | + // ======================================== |
| 372 | + // Helper Methods |
| 373 | + // ======================================== |
| 374 | + |
| 375 | + private SingleFileStorageProvider CreateProvider() |
| 376 | + { |
| 377 | + var options = new DatabaseOptions |
| 378 | + { |
| 379 | + StorageMode = StorageMode.SingleFile, |
| 380 | + PageSize = 4096, |
| 381 | + }; |
| 382 | + |
| 383 | + return SingleFileStorageProvider.Open(_testDbPath, options); |
| 384 | + } |
| 385 | +} |
0 commit comments