Skip to content

Commit cd0f6fc

Browse files
committed
chore: save uncomplete work.
1 parent 68ce56c commit cd0f6fc

File tree

7 files changed

+76
-65
lines changed

7 files changed

+76
-65
lines changed

HandyIpc.Tests/BuildInTypeTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void TestBuildInTypesWithSocket()
3636

3737
private static void TestCases(IBuildInType instance)
3838
{
39-
Assert.Throws<TestException>(instance.TestVoidWithoutParams);
39+
Helper.AssertInnerException<TestException>(instance.TestVoidWithoutParams);
4040

4141
instance.TestDoNothing();
4242

HandyIpc.Tests/EventTypeTest.cs

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading.Tasks;
23
using HandyIpc;
34
using HandyIpcTests.Fixtures;
45
using HandyIpcTests.Interfaces;
@@ -18,76 +19,46 @@ public EventTypeTest(NamedPipeFixture namedPipeFixture, SocketFixture socketFixt
1819
_socketFixture = socketFixture;
1920
}
2021

21-
//[Fact]
22-
public void TestEventHandlerWithSocket()
22+
[Fact]
23+
public async Task TestEventHandlerWithSocket()
2324
{
2425
var instance = _socketFixture.Client.Resolve<IEventType>();
25-
TestEventHandlerSubscribeAndUnsubscribe(instance);
26+
await TestEventHandlerSubscribeAndUnsubscribe(instance);
2627
}
2728

28-
//[Fact]
29-
public void TestEventHandlerWithNamedPipe()
29+
[Fact]
30+
public async Task TestEventHandlerWithNamedPipe()
3031
{
3132
var instance = _namedPipeFixture.Client.Resolve<IEventType>();
32-
TestEventHandlerSubscribeAndUnsubscribe(instance);
33+
await TestEventHandlerSubscribeAndUnsubscribe(instance);
3334
}
3435

35-
private static void TestEventHandlerSubscribeAndUnsubscribe(IEventType instance)
36+
private static async Task TestEventHandlerSubscribeAndUnsubscribe(IEventType instance)
3637
{
3738
// Some issues will occur only when the number of tests is high.
3839
// In particular, it tests whether the event calls are synchronized.
3940
const int testCount = 10000;
4041

41-
int count1 = 0;
42-
int count2 = 0;
43-
int count3 = 0;
44-
45-
// ReSharper disable AccessToModifiedClosure
46-
void Handler1(object? _, EventArgs e) => count1++;
47-
EventHandler handler2 = (_, _) => count2++;
48-
EventHandler handler3 = (_, _) => count3++;
49-
// ReSharper restore AccessToModifiedClosure
50-
51-
instance.Changed += Handler1;
52-
instance.Changed += handler2;
53-
instance.Changed += handler3;
54-
55-
for (int i = 0; i < testCount; i++)
42+
int count = 0;
43+
Task WrapAsAsync(IEventType source)
5644
{
57-
instance.RaiseChanged(EventArgs.Empty);
58-
Assert.Equal(i + 1, count1);
59-
Assert.Equal(i + 1, count2);
60-
Assert.Equal(i + 1, count3);
61-
}
45+
TaskCompletionSource tcs = new();
46+
source.Changed += OnChanged;
47+
source.RaiseChanged(EventArgs.Empty);
48+
return tcs.Task;
6249

63-
count1 = 0;
64-
count2 = 0;
65-
count3 = 0;
66-
67-
instance.Changed -= Handler1;
68-
instance.Changed -= handler2;
69-
instance.Changed -= handler3;
70-
71-
for (int i = 0; i < testCount; i++)
72-
{
73-
instance.RaiseChanged(EventArgs.Empty);
74-
Assert.Equal(0, count1);
75-
Assert.Equal(0, count2);
76-
Assert.Equal(0, count3);
50+
void OnChanged(object? sender, EventArgs e)
51+
{
52+
source.Changed -= OnChanged;
53+
count++;
54+
tcs.SetResult();
55+
}
7756
}
7857

79-
instance.Changed += Handler1;
80-
instance.Changed += Handler1;
81-
instance.Changed += handler2;
82-
instance.Changed += handler2;
83-
instance.Changed += handler3;
84-
8558
for (int i = 0; i < testCount; i++)
8659
{
87-
instance.RaiseChanged(EventArgs.Empty);
88-
Assert.Equal(2 * (i + 1), count1);
89-
Assert.Equal(2 * (i + 1), count2);
90-
Assert.Equal(i + 1, count3);
60+
await WrapAsAsync(instance);
61+
Assert.Equal(i + 1, count);
9162
}
9263
}
9364
}

HandyIpc.Tests/GenericTypeTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public async Task TestCases(IGenericType<ClassWithNewCtor, string> remote)
4747
Assert.Equal(expected, actual);
4848
}
4949

50-
await Assert.ThrowsAsync<TestException>(async () => await remote.TestAsync());
50+
await Helper.AssertInnerException<TestException>(remote.TestAsync);
5151

5252
{
5353
ClassWithNewCtor.InitialName = Guid.NewGuid().ToString();

HandyIpc.Tests/Helper.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using HandyIpc.Exceptions;
4+
using Xunit;
5+
6+
namespace HandyIpcTests
7+
{
8+
public static class Helper
9+
{
10+
public static void AssertInnerException<T>(Action function)
11+
{
12+
try
13+
{
14+
function();
15+
}
16+
catch (IpcException e)
17+
{
18+
Assert.Equal(typeof(T), e.InnerException!.GetType());
19+
}
20+
}
21+
22+
public static async Task AssertInnerException<T>(Func<Task> function)
23+
{
24+
try
25+
{
26+
await function();
27+
}
28+
catch (IpcException e)
29+
{
30+
Assert.Equal(typeof(T), e.InnerException!.GetType());
31+
}
32+
}
33+
}
34+
}

HandyIpc.Tests/TaskReturnTypeTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ private static async Task TestCases(ITaskReturnType instance)
7575

7676
instance.SyncMethod();
7777

78-
Assert.Throws<TestException>(instance.SyncMethodWithException);
78+
Helper.AssertInnerException<TestException>(instance.SyncMethodWithException);
7979

8080
await instance.TestDoNothing();
8181

82-
await Assert.ThrowsAsync<TestException>(instance.TestException);
82+
await Helper.AssertInnerException<TestException>(instance.TestException);
8383

84-
await Assert.ThrowsAsync<TestException>(() => instance.TestGenericException(string.Empty));
84+
await Helper.AssertInnerException<TestException>(() => instance.TestGenericException(string.Empty));
8585

8686
Assert.Equal(await local.TestReturnDouble(), await instance.TestReturnDouble());
8787

HandyIpc/Core/AwaiterManager.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Concurrent;
3+
using System.Threading;
34
using System.Threading.Tasks;
45
using HandyIpc.Exceptions;
56

@@ -35,30 +36,34 @@ public void Subscribe(string name, Action<byte[]> callback)
3536

3637
public void Unsubscribe(string name)
3738
{
38-
if (_awaiters.TryRemove(name, out _))
39+
if (_awaiters.TryRemove(name, out Awaiter awaiter))
3940
{
41+
awaiter.Cancellation.Cancel();
42+
// Send a signal to dispose the remote connection.
4043
_sender.Invoke(Subscription.Remove(_key, name, _serializer));
4144
}
4245
}
4346

4447
private void LoopWait(string name, Awaiter awaiter)
4548
{
4649
using IConnection connection = awaiter.Connection;
47-
while (true)
50+
using CancellationTokenSource cancellation = awaiter.Cancellation;
51+
CancellationToken token = cancellation.Token;
52+
53+
while (!token.IsCancellationRequested)
4854
{
4955
// Will blocked until accepted a notification.
5056
byte[] input = connection.Read();
51-
if (input.Length == 0)
57+
if (token.IsCancellationRequested)
5258
{
53-
// The server unexpectedly closes the connection and the client should retry automatically.
54-
_awaiters.TryRemove(name, out _);
55-
Subscribe(name, awaiter.Handler);
5659
break;
5760
}
5861

59-
// Empty type means this unsubscribe, it is a special signal from the server.
60-
if (input.IsEmpty())
62+
if (input.Length == 0)
6163
{
64+
// The server unexpectedly closes the connection and the client should retry automatically.
65+
_awaiters.TryRemove(name, out _);
66+
Subscribe(name, awaiter.Handler);
6267
break;
6368
}
6469

@@ -81,6 +86,8 @@ private class Awaiter
8186

8287
public IConnection Connection { get; }
8388

89+
public CancellationTokenSource Cancellation { get; } = new();
90+
8491
public Awaiter(Action<byte[]> handler, IConnection connection)
8592
{
8693
Handler = handler;

HandyIpc/Core/NotifierManager.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public void Unsubscribe(int processId)
7272
{
7373
if (_connections.TryRemove(processId, out IConnection connection))
7474
{
75-
// Send a signal to notify end this connection.
7675
connection.Dispose();
7776
}
7877
}

0 commit comments

Comments
 (0)