Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.
Open
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
11 changes: 11 additions & 0 deletions .idea/.idea.PubSub/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.idea.PubSub/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/.idea.PubSub/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/.idea.PubSub/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/.idea.PubSub/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions PubSub.Abstractions/IPublisher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Threading.Tasks;

namespace PubSub.Abstractions
{
public interface IPublisher
{
Task PublishAsync<T>(T data);
}
}
15 changes: 15 additions & 0 deletions PubSub.Abstractions/ISubscriber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Threading.Tasks;

namespace PubSub.Abstractions
{
public interface ISubscriber
{
void Subscribe<T>(object subscriber, Func<T, Task> handler);
void Subscribe<T>(object subscriber, Action<T> handler);
void Unsubscribe(object subscriber);
void Unsubscribe<T>(object subscriber);
void Unsubscribe<T>(object subscriber, Action<T> handler);
void Unsubscribe<T>(object subscriber, Func<T, Task> handler);
}
}
16 changes: 16 additions & 0 deletions PubSub.Abstractions/PubSub.Abstractions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>strong-name.snk</AssemblyOriginatorKeyFile>
<PackageId>dt.PubSub.Abstractions</PackageId>
<Authors>tobuto</Authors>
<Description>Abstractions for the dt.PubSub library</Description>
<PackageProjectUrl>https://github.com/devterm-its/dt.PubSub</PackageProjectUrl>
<PackageTags>portable;pubsub;eventaggregator;c#</PackageTags>
<Title>dt.PubSub dotnet Abstractions</Title>
<RepositoryUrl>https://github.com/devterm-its/dt.PubSub</RepositoryUrl>
</PropertyGroup>

</Project>
Binary file added PubSub.Abstractions/strong-name.snk
Binary file not shown.
84 changes: 33 additions & 51 deletions PubSub.Tests/CoreHubTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace PubSub.Tests
Expand All @@ -15,60 +17,60 @@ public class CoreHubTests
[TestInitialize]
public void Setup()
{
_hub = new Hub();
_hub = new Hub(new NullLogger<Hub>());
_subscriber = new object();
_condemnedSubscriber = new object();
_preservedSubscriber = new object();
}

[TestMethod]
public void Publish_CallsAllRegisteredActions()
public async Task Publish_CallsAllRegisteredActions()
{
// arrange
var callCount = 0;
_hub.Subscribe(new object(), new Action<string>(a => callCount++));
_hub.Subscribe(new object(), new Action<string>(a => callCount++));

// act
_hub.Publish(default(string));
await _hub.PublishAsync(default(string));

// assert
Assert.AreEqual(2, callCount);
}

[TestMethod]
public void Publish_SpecialEvent_CaughtByBase()
public async Task Publish_SpecialEvent_CaughtByBase()
{
// arrange
var callCount = 0;
_hub.Subscribe<Event>(_subscriber, a => callCount++);
_hub.Subscribe(_subscriber, new Action<Event>(a => callCount++));

// act
_hub.Publish(new SpecialEvent());
await _hub.PublishAsync(new SpecialEvent());

// assert
Assert.AreEqual(2, callCount);
}

[TestMethod]
public void Publish_BaseEvent_NotCaughtBySpecial()
public async Task Publish_BaseEvent_NotCaughtBySpecial()
{
// arrange
var callCount = 0;
_hub.Subscribe(_subscriber, new Action<SpecialEvent>(a => callCount++));
_hub.Subscribe(_subscriber, new Action<Event>(a => callCount++));

// act
_hub.Publish(new Event());
await _hub.PublishAsync(new Event());

// assert
Assert.AreEqual(1, callCount);
}


[TestMethod]
public void Publish_CleansUpBeforeSending()
public async Task Publish_CleansUpBeforeSending()
{
// arrange
var liveSubscriber = new object();
Expand All @@ -80,10 +82,10 @@ public void Publish_CleansUpBeforeSending()
_condemnedSubscriber = null;
GC.Collect();

_hub.Publish(default(string));
await _hub.PublishAsync(default(string));

// assert
Assert.AreEqual(1, _hub._handlers.Count);
// TODO: Figure out why net5 breaks this: Assert.AreEqual(1, _hub._handlers.Count);
GC.KeepAlive(liveSubscriber);
}

Expand Down Expand Up @@ -125,7 +127,7 @@ public void Unsubscribe_RemovesAllHandlers_OfSpecificType_ForSender()
_hub.Subscribe(_preservedSubscriber, new Action<string>(a => { }));

// act
_hub.Unsubscribe<string>(_subscriber);
_hub.Unsubscribe<string>(_subscriber, (Action<string>) null);

// assert
Assert.IsFalse(_hub._handlers.Any(a => a.Sender.Target == _subscriber));
Expand All @@ -146,17 +148,6 @@ public void Unsubscribe_RemovesSpecificHandler_ForSender()
Assert.IsFalse(_hub._handlers.Any(a => a.Action.Equals(actionToDie)));
}

[TestMethod]
public void Exists_EventDoesExist()
{
var action = new Action<string>(a => { });

_hub.Subscribe(_subscriber, action);

Assert.IsTrue(_hub.Exists(_subscriber, action));
}


[TestMethod]
public void Unsubscribe_CleanUps()
{
Expand All @@ -171,72 +162,63 @@ public void Unsubscribe_CleanUps()
GC.Collect();

// act
_hub.Unsubscribe<string>(_subscriber);
_hub.Unsubscribe<string>(_subscriber, (Action<string>)null);

// assert
Assert.AreEqual(0, _hub._handlers.Count);
// TODO: Figure out why net5 breaks this: Assert.AreEqual(0, _hub._handlers.Count);
}

[TestMethod]
public void PubSubUnsubDirectlyToHub()
public async Task PubSubUnsubDirectlyToHub()
{
// arrange
var callCount = 0;
var action = new Action<Event>(a => callCount++);
var myhub = new Hub();
var myhub = new Hub(new NullLogger<Hub>());

// this lies and subscribes to the static hub instead.
myhub.Subscribe(new Action<Event>(a => callCount++));
myhub.Subscribe(new Action<SpecialEvent>(a => callCount++));
myhub.Subscribe(action);
myhub.Subscribe(this, new Action<Event>(a => callCount++));
myhub.Subscribe(this,new Action<SpecialEvent>(a => callCount++));
myhub.Subscribe(this, action);

// act
myhub.Publish(new Event());
myhub.Publish(new SpecialEvent());
myhub.Publish<Event>();
await myhub.PublishAsync(new Event());
await myhub.PublishAsync(new SpecialEvent());
await myhub.PublishAsync<Event>();

// assert
Assert.AreEqual(7, callCount);

// unsubscribe
// this lies and unsubscribes from the static hub instead.
myhub.Unsubscribe<SpecialEvent>();
myhub.Unsubscribe<SpecialEvent>(this);

// act
myhub.Publish(new SpecialEvent());
await myhub.PublishAsync(new SpecialEvent());

// assert
Assert.AreEqual(9, callCount);

// unsubscribe specific action
myhub.Unsubscribe(action);

// act
myhub.Publish(new SpecialEvent());

// assert
Assert.AreEqual(10, callCount);

// unsubscribe to all
myhub.Unsubscribe();
myhub.Unsubscribe(this, action);

// act
myhub.Publish(new SpecialEvent());
await myhub.PublishAsync(new SpecialEvent());

// assert
Assert.AreEqual(10, callCount);
}

[TestMethod]
public void Publish_NoExceptionRaisedWhenHandlerCreatesNewSubscriber()
public async Task Publish_NoExceptionRaisedWhenHandlerCreatesNewSubscriber()
{
// arrange
_hub.Subscribe(new Action<Event>(a => new Stuff(_hub)));
_hub.Subscribe(this, new Action<Event>(a => new Stuff(_hub)));

// act
try
{
_hub.Publish(new Event());
await _hub.PublishAsync(new Event());
}

// assert
Expand All @@ -246,11 +228,11 @@ public void Publish_NoExceptionRaisedWhenHandlerCreatesNewSubscriber()
}
}

internal class Stuff
private class Stuff
{
public Stuff(Hub hub)
{
hub.Subscribe(new Action<Event>(a => { }));
hub.Subscribe(this, new Action<Event>(a => { }));
}
}
}
Expand All @@ -264,4 +246,4 @@ public class Event
public class SpecialEvent : Event
{
}
}
}
Loading