Skip to content

Commit c243d8e

Browse files
committed
refactor: update InMemoryStream && add InMemoryStreamTest
1 parent d0f0267 commit c243d8e

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) Weihan Li. All rights reserved.
2+
// Licensed under the Apache license.
3+
4+
using WeihanLi.Common.Helpers;
5+
using WeihanLi.Extensions;
6+
7+
namespace DotNetCoreSample;
8+
internal static class MemoryStreamTest
9+
{
10+
public static async Task MainTest()
11+
{
12+
var stream = new InMemoryStream<long>("test");
13+
var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
14+
await Task.Delay(100);
15+
await stream.AddAsync(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), new Dictionary<string, string>
16+
{
17+
{ "messages", new { name = $"test-{DateTimeOffset.Now}" } .ToJson() }
18+
});
19+
Console.WriteLine("stream message added");
20+
await Task.Delay(1000);
21+
await stream.AddAsync(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), new Dictionary<string, string>
22+
{
23+
{ "messages", new { name = $"test-{DateTimeOffset.Now}" } .ToJson() }
24+
});
25+
Console.WriteLine("stream message added");
26+
//
27+
{
28+
Console.WriteLine("Fetch messages from stream");
29+
await foreach (var item in stream.FetchAsync(timestamp, 2))
30+
{
31+
Console.WriteLine($"{item.Id} - {item.Timestamp}");
32+
Console.WriteLine(item.Fields.ToJson());
33+
}
34+
}
35+
{
36+
Console.WriteLine("Fetch messages from stream again");
37+
await foreach (var item in stream.FetchAsync(timestamp, 2))
38+
{
39+
Console.WriteLine($"{item.Id} - {item.Timestamp}");
40+
Console.WriteLine(item.Fields.ToJson());
41+
}
42+
}
43+
}
44+
}

samples/DotNetCoreSample/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,8 @@
347347

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

350-
InvokeHelper.TryInvoke(LoggerTest.MicrosoftLoggingTest);
350+
// InvokeHelper.TryInvoke(LoggerTest.MicrosoftLoggingTest);
351+
await InvokeHelper.TryInvokeAsync(MemoryStreamTest.MainTest);
351352

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

src/WeihanLi.Common/Helpers/InMemoryStream.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,17 @@ public Task AddAsync(T id, Dictionary<string, string> fields, DateTimeOffset? ti
7373
{
7474
Id = id,
7575
Fields = fields,
76-
Timestamp = timestamp ?? DateTimeOffset.Now,
77-
Properties = properties ?? new()
76+
Timestamp = timestamp ?? DateTimeOffset.Now
7877
};
78+
79+
if (properties is { Count: > 0 })
80+
{
81+
foreach (var item in properties)
82+
{
83+
message.Properties[item.Key] = item.Value;
84+
}
85+
}
86+
7987
_messages.Add(message);
8088

8189
return Task.CompletedTask;

0 commit comments

Comments
 (0)