Skip to content

Commit

Permalink
refactor: update InMemoryStream && add InMemoryStreamTest
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Dec 15, 2024
1 parent d0f0267 commit c243d8e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
44 changes: 44 additions & 0 deletions samples/DotNetCoreSample/InMemoryStreamTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Weihan Li. All rights reserved.
// Licensed under the Apache license.

using WeihanLi.Common.Helpers;
using WeihanLi.Extensions;

namespace DotNetCoreSample;
internal static class MemoryStreamTest
{
public static async Task MainTest()
{
var stream = new InMemoryStream<long>("test");
var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
await Task.Delay(100);
await stream.AddAsync(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), new Dictionary<string, string>
{
{ "messages", new { name = $"test-{DateTimeOffset.Now}" } .ToJson() }
});
Console.WriteLine("stream message added");
await Task.Delay(1000);
await stream.AddAsync(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), new Dictionary<string, string>
{
{ "messages", new { name = $"test-{DateTimeOffset.Now}" } .ToJson() }
});
Console.WriteLine("stream message added");
//
{
Console.WriteLine("Fetch messages from stream");
await foreach (var item in stream.FetchAsync(timestamp, 2))
{
Console.WriteLine($"{item.Id} - {item.Timestamp}");
Console.WriteLine(item.Fields.ToJson());
}
}
{
Console.WriteLine("Fetch messages from stream again");
await foreach (var item in stream.FetchAsync(timestamp, 2))
{
Console.WriteLine($"{item.Id} - {item.Timestamp}");
Console.WriteLine(item.Fields.ToJson());
}
}
}
}
3 changes: 2 additions & 1 deletion samples/DotNetCoreSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@

// InvokeHelper.TryInvoke(() => throw null, 3);

InvokeHelper.TryInvoke(LoggerTest.MicrosoftLoggingTest);
// InvokeHelper.TryInvoke(LoggerTest.MicrosoftLoggingTest);
await InvokeHelper.TryInvokeAsync(MemoryStreamTest.MainTest);

ConsoleHelper.ReadKeyWithPrompt("Press any key to exit");

Expand Down
12 changes: 10 additions & 2 deletions src/WeihanLi.Common/Helpers/InMemoryStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,17 @@ public Task AddAsync(T id, Dictionary<string, string> fields, DateTimeOffset? ti
{
Id = id,
Fields = fields,
Timestamp = timestamp ?? DateTimeOffset.Now,
Properties = properties ?? new()
Timestamp = timestamp ?? DateTimeOffset.Now
};

if (properties is { Count: > 0 })
{
foreach (var item in properties)
{
message.Properties[item.Key] = item.Value;
}
}

_messages.Add(message);

return Task.CompletedTask;
Expand Down

0 comments on commit c243d8e

Please sign in to comment.