Skip to content

Commit

Permalink
Add test for custom EventHandler delegate type
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Jun 20, 2019
1 parent 6b2bcb9 commit 9ab5de3
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/StreamJsonRpc.Tests/TargetObjectEventsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ public async Task GenericServerEventRaisesCallback()
Assert.Equal(expectedArgs.Seeds, actualArgs.Seeds);
}

[Fact]
public async Task CustomDelegateServerEventRaisesCallback()
{
var tcs = new TaskCompletionSource<MessageEventArgs<string>>();
var expectedArgs = new MessageEventArgs<string> { Message = "a" };
this.client.ServerEventWithCustomGenericDelegateAndArgsRaised = args => tcs.SetResult(args);
this.server.TriggerServerEventWithCustomGenericDelegateAndArgs(expectedArgs);
var actualArgs = await tcs.Task.WithCancellation(this.TimeoutToken);
Assert.Equal<string>(expectedArgs.Message, actualArgs.Message);
}

/// <summary>
/// Verifies that events on target objects are subscribed to, and unsubscribed after the JsonRpc instance is disposed of.
/// </summary>
Expand Down Expand Up @@ -201,21 +212,29 @@ private class Client

internal Action<CustomEventArgs> GenericServerEventRaised { get; set; }

internal Action<MessageEventArgs<string>> ServerEventWithCustomGenericDelegateAndArgsRaised { get; set; }

public void ServerEvent(EventArgs args) => this.ServerEventRaised?.Invoke(args);

public void PublicStaticServerEvent(EventArgs args) => this.PublicStaticServerEventRaised?.Invoke(args);

public void ServerEventWithCustomArgs(CustomEventArgs args) => this.GenericServerEventRaised?.Invoke(args);

public void ServerEventWithCustomGenericDelegateAndArgs(MessageEventArgs<string> args) => this.ServerEventWithCustomGenericDelegateAndArgsRaised?.Invoke(args);
}

private class Server
{
public delegate void MessageReceivedEventHandler<T>(object sender, MessageEventArgs<T> args);

public static event EventHandler PublicStaticServerEvent;

public event EventHandler ServerEvent;

public event EventHandler<CustomEventArgs> ServerEventWithCustomArgs;

public event MessageReceivedEventHandler<string> ServerEventWithCustomGenericDelegateAndArgs;

private static event EventHandler PrivateStaticServerEvent;

private event EventHandler PrivateServerEvent;
Expand All @@ -236,13 +255,17 @@ public void TriggerGenericEvent(CustomEventArgs args)
this.OnServerEventWithCustomArgs(args);
}

public void TriggerServerEventWithCustomGenericDelegateAndArgs(MessageEventArgs<string> args) => this.OnServerEventWithCustomGenericDelegateAndArgs(args);

protected static void OnPrivateStaticServerEvent(EventArgs args) => PrivateStaticServerEvent?.Invoke(null, args);

protected virtual void OnServerEvent(EventArgs args) => this.ServerEvent?.Invoke(this, args);

protected virtual void OnServerEventWithCustomArgs(CustomEventArgs args) => this.ServerEventWithCustomArgs?.Invoke(this, args);

protected virtual void OnPrivateServerEvent(EventArgs args) => this.PrivateServerEvent?.Invoke(this, args);

protected virtual void OnServerEventWithCustomGenericDelegateAndArgs(MessageEventArgs<string> args) => this.ServerEventWithCustomGenericDelegateAndArgs?.Invoke(this, args);
}

private class ServerWithIncompatibleEvents
Expand All @@ -258,4 +281,9 @@ private class CustomEventArgs : EventArgs
{
public int Seeds { get; set; }
}

private class MessageEventArgs<T> : EventArgs
{
public T Message { get; set; }
}
}

0 comments on commit 9ab5de3

Please sign in to comment.