Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Essentials.Tests/PersistenceProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,56 @@ public async Task Store_Null_Removes_The_Key_On_Every_Provider()
}
}

[TestMethod]
public async Task Store_Of_An_Unserializable_Value_Throws_And_Keeps_The_Previous_Value()
{
// Serialize ignored TrySerialize's false and returned "", so the file-backed providers replaced the last
// good value with an empty file and StoreAsync reported success (issue #70).
string fileSystemDir = Path.Join(Path.GetTempPath(), "PersistenceTests_FS_" + Guid.NewGuid().ToString("N")[..8]);
NativeFileSystemProvider fs = new();
JsonSerializationProvider serializer = new();
using TempPersistenceProvider<string> temp = new(fs, serializer, "PersistenceTests_Temp_" + Guid.NewGuid().ToString("N")[..8]);

try
{
IPersistenceProvider<string>[] providers =
[
new FileSystemPersistenceProvider<string>(fs, serializer, fileSystemDir),
temp,
];

foreach (IPersistenceProvider<string> persistence in providers)
{
string name = persistence.ProviderName;

await persistence.StoreAsync("k", new CyclicNode { Name = "good" }, TestContext.CancellationToken).ConfigureAwait(false);

CyclicNode cyclic = new() { Name = "bad" };
cyclic.Next = cyclic;

await Assert.ThrowsExactlyAsync<PersistenceProviderException>(
() => persistence.StoreAsync("k", cyclic, TestContext.CancellationToken),
$"{name} reported success for a value it could not serialize").ConfigureAwait(false);

CyclicNode? retrieved = await persistence.RetrieveAsync<CyclicNode>("k", TestContext.CancellationToken).ConfigureAwait(false);
Assert.IsNotNull(retrieved, $"{name} lost the previous value");
Assert.AreEqual("good", retrieved.Name, $"{name} replaced the previous value");
}
}
finally
{
CleanupDirectory(fileSystemDir);
temp.CleanupDirectory();
}
}

public sealed class CyclicNode
{
public string Name { get; set; } = string.Empty;

public CyclicNode? Next { get; set; }
}

public record struct ValueData()
{
public int Value { get; set; } = 50;
Expand Down
19 changes: 19 additions & 0 deletions Essentials.Tests/SerializationProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ public void Configuration_Serialize_And_Deserialize_Roundtrip(ISerializationProv
Assert.AreEqual(original.Enabled, loaded.Enabled, $"{providerName} should preserve Enabled");
}

[TestMethod]
public void Serialize_Throws_When_TrySerialize_Fails()
{
// Serialize discarded TrySerialize's false and returned an empty string (issue #70).
ISerializationProvider provider = new JsonSerializationProvider();
CyclicConfig cyclic = new();
cyclic.Self = cyclic;

Assert.ThrowsExactly<InvalidOperationException>(() => provider.Serialize(cyclic));

using StringWriter writer = new();
Assert.ThrowsExactly<InvalidOperationException>(() => provider.Serialize(cyclic, writer));
}

[TestMethod]
[DynamicData(nameof(ConfigurationProviders))]
public void Configuration_TrySerialize_And_Deserialize(ISerializationProvider configProvider, string providerName)
Expand Down Expand Up @@ -116,4 +130,9 @@ public sealed class TestConfig
public int Count { get; set; }
public bool Enabled { get; set; }
}

public sealed class CyclicConfig
{
public CyclicConfig? Self { get; set; }
}
}
12 changes: 10 additions & 2 deletions Essentials/ISerializationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ public Task<bool> TrySerializeAsync(object obj, TextWriter writer, CancellationT
/// </summary>
/// <param name="obj">The object to serialize.</param>
/// <returns>A string containing the serialized data.</returns>
/// <exception cref="InvalidOperationException">The object could not be serialized.</exception>
public string Serialize(object obj)
{
using StringWriter writer = new();
TrySerialize(obj, writer);
Serialize(obj, writer);
return writer.ToString();
}

Expand All @@ -128,15 +129,22 @@ public string Serialize(object obj)
/// </summary>
/// <param name="obj">The object to serialize.</param>
/// <param name="writer">The writer to write the serialized data to.</param>
/// <exception cref="InvalidOperationException">The object could not be serialized.</exception>
public void Serialize(object obj, TextWriter writer)
=> TrySerialize(obj, writer);
{
if (!TrySerialize(obj, writer))
{
throw new InvalidOperationException("The object could not be serialized.");
}
}

/// <summary>
/// Serializes the specified object asynchronously.
/// </summary>
/// <param name="obj">The object to serialize.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A string containing the serialized data.</returns>
/// <exception cref="InvalidOperationException">The object could not be serialized.</exception>
public Task<string> SerializeAsync(object obj, CancellationToken cancellationToken = default)
=> ProviderHelpers.RunAsync(() => Serialize(obj), cancellationToken);
}
Loading