Skip to content

Commit 1e645e6

Browse files
Fix sonarcube issues in tests
1 parent e977590 commit 1e645e6

2 files changed

Lines changed: 1472 additions & 19 deletions

File tree

AppDataStorage.Test/AppDataTests.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -268,17 +268,17 @@ public void TestQueueSaveSetsSaveQueuedTime()
268268
}
269269

270270
[TestMethod]
271-
public void TestSaveIfRequiredSavesDataAfterDebounceTime()
271+
public async Task TestSaveIfRequiredSavesDataAfterDebounceTime()
272272
{
273273
using TestAppData appData = CreateTestAppDataWithContent(TestDataString);
274274
appData.QueueSave();
275-
Thread.Sleep(appData.SaveDebounceTime + TimeSpan.FromMilliseconds(100)); // Wait for more than debounce time
275+
await Task.Delay(appData.SaveDebounceTime + TimeSpan.FromMilliseconds(100)).ConfigureAwait(false); // Wait for more than debounce time
276276

277277
appData.SaveIfRequired();
278278

279279
AssertFileExists(appData.FilePath, "File was not saved.");
280280

281-
TestAppData? fileContents = JsonSerializer.Deserialize<TestAppData>(AppData.FileSystem.File.ReadAllText(appData.FilePath), AppData.JsonSerializerOptions);
281+
TestAppData? fileContents = JsonSerializer.Deserialize<TestAppData>(await AppData.FileSystem.File.ReadAllTextAsync(appData.FilePath).ConfigureAwait(false), AppData.JsonSerializerOptions);
282282
Assert.IsNotNull(fileContents, "Saved data should be deserialized correctly.");
283283
Assert.AreEqual(TestDataString, fileContents.Data, "Saved data does not match.");
284284
}
@@ -337,14 +337,14 @@ public void TestIsSaveQueuedReturnsCorrectValue()
337337
}
338338

339339
[TestMethod]
340-
public void TestIsDebounceTimeElapsedReturnsCorrectValue()
340+
public async Task TestIsDebounceTimeElapsedReturnsCorrectValue()
341341
{
342342
using TestAppData appData = CreateTestAppData();
343343

344344
appData.QueueSave();
345345
Assert.IsFalse(appData.IsDoubounceTimeElapsed(), "Debounce should not have elapsed immediately after QueueSave.");
346346

347-
Thread.Sleep(appData.SaveDebounceTime + TimeSpan.FromMilliseconds(100));
347+
await Task.Delay(appData.SaveDebounceTime + TimeSpan.FromMilliseconds(100)).ConfigureAwait(false);
348348

349349
Assert.IsTrue(appData.IsDoubounceTimeElapsed(), "Debounce should have elapsed after waiting.");
350350
}
@@ -363,7 +363,7 @@ public void TestLoadOrCreateHandlesCorruptFile()
363363
}
364364

365365
[TestMethod]
366-
public void TestMultipleSavesOnlyWriteOnceWithinDebouncePeriod()
366+
public async Task TestMultipleSavesOnlyWriteOnceWithinDebouncePeriod()
367367
{
368368
using TestAppData appData = CreateTestAppDataWithContent("Data1");
369369
appData.Save();
@@ -372,14 +372,14 @@ public void TestMultipleSavesOnlyWriteOnceWithinDebouncePeriod()
372372
appData.QueueSave();
373373
appData.SaveIfRequired();
374374

375-
TestAppData? fileContent = JsonSerializer.Deserialize<TestAppData>(AppData.FileSystem.File.ReadAllText(appData.FilePath), AppData.JsonSerializerOptions);
375+
TestAppData? fileContent = JsonSerializer.Deserialize<TestAppData>(await AppData.FileSystem.File.ReadAllTextAsync(appData.FilePath).ConfigureAwait(false), AppData.JsonSerializerOptions);
376376
Assert.IsNotNull(fileContent, "File should not be empty after save.");
377377
Assert.AreEqual("Data1", fileContent.Data, "Data should not be updated due to debounce.");
378378

379-
Thread.Sleep(appData.SaveDebounceTime + TimeSpan.FromMilliseconds(100));
379+
await Task.Delay(appData.SaveDebounceTime + TimeSpan.FromMilliseconds(100)).ConfigureAwait(false);
380380
appData.SaveIfRequired();
381381

382-
fileContent = JsonSerializer.Deserialize<TestAppData>(AppData.FileSystem.File.ReadAllText(appData.FilePath), AppData.JsonSerializerOptions);
382+
fileContent = JsonSerializer.Deserialize<TestAppData>(await AppData.FileSystem.File.ReadAllTextAsync(appData.FilePath).ConfigureAwait(false), AppData.JsonSerializerOptions);
383383
Assert.IsNotNull(fileContent, "File should not be empty after debounce period.");
384384
Assert.AreEqual("Data2", fileContent.Data, "Data should be updated after debounce period.");
385385
}
@@ -481,31 +481,31 @@ public void TestEnsureDirectoryExistsWithNullFilePath()
481481
{
482482
AbsoluteFilePath path = null!;
483483
AppData.EnsureDirectoryExists(path);
484-
// No exception should be thrown
484+
Assert.IsNotNull(AppData.Path, "AppData path should remain valid after handling null file path.");
485485
}
486486

487487
[TestMethod]
488488
public void TestEnsureDirectoryExistsWithEmptyFilePath()
489489
{
490490
AbsoluteFilePath path = new();
491491
AppData.EnsureDirectoryExists(path);
492-
// No exception should be thrown
492+
Assert.IsNotNull(AppData.Path, "AppData path should remain valid after handling empty file path.");
493493
}
494494

495495
[TestMethod]
496496
public void TestEnsureDirectoryExistsWithNullDirectoryPath()
497497
{
498498
AbsoluteDirectoryPath path = null!;
499499
AppData.EnsureDirectoryExists(path);
500-
// No exception should be thrown
500+
Assert.IsNotNull(AppData.Path, "AppData path should remain valid after handling null directory path.");
501501
}
502502

503503
[TestMethod]
504504
public void TestEnsureDirectoryExistsWithEmptyDirectoryPath()
505505
{
506506
AbsoluteDirectoryPath path = new();
507507
AppData.EnsureDirectoryExists(path);
508-
// No exception should be thrown
508+
Assert.IsNotNull(AppData.Path, "AppData path should remain valid after handling empty directory path.");
509509
}
510510

511511
[TestMethod]
@@ -648,12 +648,12 @@ public void TestQueueSaveStaticMethod()
648648

649649
[TestMethod]
650650
[DoNotParallelize]
651-
public void TestSaveIfRequiredStaticMethod()
651+
public async Task TestSaveIfRequiredStaticMethod()
652652
{
653653
// This test uses the static singleton which is shared across tests,
654654
// so it cannot run in parallel with other tests that use TestAppData.Get()
655655
TestAppData.QueueSave();
656-
Thread.Sleep(TestAppData.Get().SaveDebounceTime + TimeSpan.FromMilliseconds(100));
656+
await Task.Delay(TestAppData.Get().SaveDebounceTime + TimeSpan.FromMilliseconds(100)).ConfigureAwait(false);
657657

658658
TestAppData.SaveIfRequired();
659659

@@ -1104,6 +1104,9 @@ public void TestWriteTextHandlesFileOperationErrors()
11041104
{
11051105
// Expected behavior when file is readonly
11061106
}
1107+
1108+
string content = AppData.FileSystem.File.ReadAllText(appData.FilePath);
1109+
Assert.AreEqual("existing data", content, "Original file content should be preserved after failed write attempt.");
11071110
}
11081111

11091112
[TestMethod]
@@ -1313,7 +1316,7 @@ public void TestFileSystemEdgeCaseEmptyPaths()
13131316
AppData.EnsureDirectoryExists(AbsoluteFilePath.Create(string.Empty));
13141317
AppData.EnsureDirectoryExists(AbsoluteDirectoryPath.Create(string.Empty));
13151318

1316-
// Should not throw and handle gracefully - if we get here, the method handled empty paths correctly
1319+
Assert.IsNotNull(AppData.Path, "AppData path should remain valid after handling empty paths.");
13171320
}
13181321

13191322
[TestMethod]
@@ -1344,11 +1347,11 @@ public void TestDisposeMultipleTimesWithSaveQueued()
13441347
appData.Dispose();
13451348
appData.Dispose();
13461349

1347-
// Should handle multiple disposes gracefully - if we get here, the method succeeded
1350+
AssertFileExists(appData.FilePath, "Queued save should be flushed on first dispose.");
13481351
}
13491352

13501353
[TestMethod]
1351-
public void TestSaveDebounceTimingPrecision()
1354+
public async Task TestSaveDebounceTimingPrecision()
13521355
{
13531356
using TestAppData appData = new() { Data = TestDataString };
13541357

@@ -1358,7 +1361,7 @@ public void TestSaveDebounceTimingPrecision()
13581361
Assert.IsFalse(appData.IsDoubounceTimeElapsed());
13591362

13601363
// Wait for debounce time + buffer
1361-
Thread.Sleep(appData.SaveDebounceTime.Add(TimeSpan.FromMilliseconds(100)));
1364+
await Task.Delay(appData.SaveDebounceTime.Add(TimeSpan.FromMilliseconds(100))).ConfigureAwait(false);
13621365

13631366
Assert.IsTrue(appData.IsDoubounceTimeElapsed());
13641367
}

0 commit comments

Comments
 (0)