Skip to content

Commit

Permalink
bugfix: ensure that execution events are synchronized. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
DingpingZhang committed Nov 16, 2021
1 parent c81663d commit bb5436d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
5 changes: 4 additions & 1 deletion HandyIpc.Generator/ClientProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public class {nameof(ClientProxy)}{className}{typeParameters} : {interfaceType}
private readonly string _key;
private readonly AwaiterManager _awaiterManager;
{events.For(item => $@"private event {item.Type.ToTypeDeclaration()} _{item.Name};")}
{events.For(item => $@"
private event {item.Type.ToTypeDeclaration()} _{item.Name};
")}
{events.For(item =>
{
Expand Down Expand Up @@ -63,6 +65,7 @@ public class {nameof(ClientProxy)}{className}{typeParameters} : {interfaceType}
}}
}}
}}
";
})}
Expand Down
12 changes: 8 additions & 4 deletions HandyIpc.Tests/EventTypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ public void TestEventHandlerWithNamedPipe()
TestEventHandlerSubscribeAndUnsubscribe(instance);
}

private void TestEventHandlerSubscribeAndUnsubscribe(IEventType instance)
private static void TestEventHandlerSubscribeAndUnsubscribe(IEventType instance)
{
// Some issues will occur only when the number of tests is high.
// In particular, it tests whether the event calls are synchronized.
const int testCount = 10000;

int count1 = 0;
int count2 = 0;
int count3 = 0;
Expand All @@ -49,7 +53,7 @@ private void TestEventHandlerSubscribeAndUnsubscribe(IEventType instance)
instance.Changed += handler2;
instance.Changed += handler3;

for (int i = 0; i < 10; i++)
for (int i = 0; i < testCount; i++)
{
instance.RaiseChanged(EventArgs.Empty);
Assert.Equal(i + 1, count1);
Expand All @@ -65,7 +69,7 @@ private void TestEventHandlerSubscribeAndUnsubscribe(IEventType instance)
instance.Changed -= handler2;
instance.Changed -= handler3;

for (int i = 0; i < 10; i++)
for (int i = 0; i < testCount; i++)
{
instance.RaiseChanged(EventArgs.Empty);
Assert.Equal(0, count1);
Expand All @@ -79,7 +83,7 @@ private void TestEventHandlerSubscribeAndUnsubscribe(IEventType instance)
instance.Changed += handler2;
instance.Changed += handler3;

for (int i = 0; i < 10; i++)
for (int i = 0; i < testCount; i++)
{
instance.RaiseChanged(EventArgs.Empty);
Assert.Equal(2 * (i + 1), count1);
Expand Down
15 changes: 11 additions & 4 deletions HandyIpc/Core/AwaiterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public void Unsubscribe(string name)
{
if (_pool.TryRemove(name, out _))
{
using var rented = _sender.ConnectionPool.Rent();
byte[] removeResult = rented.Value.Invoke(Subscription.Remove(_key, name, _serializer));
byte[] removeResult = _sender.Invoke(Subscription.Remove(_key, name, _serializer));
if (!removeResult.IsUnit())
{
// TODO: Logging.
Expand All @@ -58,8 +57,16 @@ private static void LoopWait(Awaiter awaiter)
break;
}

connection.Write(Signals.Unit);
awaiter.Handler(input);
try
{
// The Read() and Write() are used to ensure that calls are synchronized (blocked)
// and that the Write() must be called after the execution of the Handler() is completed.
awaiter.Handler(input);
}
finally
{
connection.Write(Signals.Unit);
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions HandyIpc/Core/NotifierManager.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;

namespace HandyIpc.Core
{
public class NotifierManager
{
private readonly object _locker = new();
private readonly string _key = Guid.NewGuid().ToString();
private readonly ISerializer _serializer;
private readonly Dictionary<string, Notifier> _notifiers = new();

Expand Down

0 comments on commit bb5436d

Please sign in to comment.