Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
core/Akka.Interfaced.Tests/InterfacedMessageBuilderTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Xunit; | ||
|
||
namespace Akka.Interfaced | ||
{ | ||
public class InterfacedMessageBuilderTest | ||
{ | ||
[Fact] | ||
public void BuildRequestMessage() | ||
{ | ||
var message = InterfacedMessageBuilder.Request<IBasic>(x => x.CallWithParameter(10)); | ||
var payload = message.InvokePayload as IBasic_PayloadTable.CallWithParameter_Invoke; | ||
Assert.NotNull(payload); | ||
Assert.Equal(10, payload.value); | ||
} | ||
|
||
[Fact] | ||
public void BuildNotificationMessage() | ||
{ | ||
var message = InterfacedMessageBuilder.Notification<ISubject2Observer>(x => x.Event("A")); | ||
var payload = message.InvokePayload as ISubject2Observer_PayloadTable.Event_Invoke; | ||
Assert.NotNull(payload); | ||
Assert.Equal("A", payload.eventName); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Akka.Interfaced | ||
{ | ||
public class InterfacedMessageBuilder | ||
{ | ||
private class SinkRequestWaiter : IRequestWaiter | ||
{ | ||
public RequestMessage Message { get; private set; } | ||
|
||
void IRequestWaiter.SendRequest(IRequestTarget target, RequestMessage requestMessage) | ||
{ | ||
Message = requestMessage; | ||
} | ||
|
||
Task IRequestWaiter.SendRequestAndWait(IRequestTarget target, RequestMessage requestMessage, TimeSpan? timeout) | ||
{ | ||
Message = requestMessage; | ||
return Task.FromResult(0); | ||
} | ||
|
||
Task<TReturn> IRequestWaiter.SendRequestAndReceive<TReturn>(IRequestTarget target, RequestMessage requestMessage, TimeSpan? timeout) | ||
{ | ||
Message = requestMessage; | ||
return Task.FromResult(default(TReturn)); | ||
} | ||
} | ||
|
||
public static RequestMessage Request<T>(Action<T> action) | ||
where T : IInterfacedActor | ||
{ | ||
var waiter = new SinkRequestWaiter(); | ||
var actorRef = InterfacedActorRef.Create(typeof(T)); | ||
InterfacedActorRefModifier.SetRequestWaiter(actorRef, waiter); | ||
action((T)(object)actorRef); | ||
return waiter.Message; | ||
} | ||
|
||
private class SinkNotificationChannel : INotificationChannel | ||
{ | ||
public NotificationMessage Message { get; private set; } | ||
|
||
void INotificationChannel.Notify(NotificationMessage notificationMessage) | ||
{ | ||
Message = notificationMessage; | ||
} | ||
} | ||
|
||
public static NotificationMessage Notification<T>(Action<T> action) | ||
where T : IInterfacedObserver | ||
{ | ||
var channel = new SinkNotificationChannel(); | ||
var observer = InterfacedObserver.Create(typeof(T)); | ||
observer.Channel = channel; | ||
action((T)(object)observer); | ||
return channel.Message; | ||
} | ||
} | ||
} |