-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#33 Implemented custom IEndpointCollection implementation for Loopbac…
…kConfigration to support sending to arbitrary named endpoints
- Loading branch information
1 parent
89394d8
commit f98f9c3
Showing
8 changed files
with
167 additions
and
221 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Platibus.Config; | ||
using Platibus.InMemory; | ||
using Xunit; | ||
|
||
namespace Platibus.UnitTests | ||
{ | ||
public class LoopbackTests | ||
{ | ||
protected LoopbackConfiguration Configuration = new LoopbackConfiguration(); | ||
protected object MessageContent = Guid.NewGuid().ToString(); | ||
protected SendOptions SendOptions = new SendOptions {ContentType = "text/plain"}; | ||
protected CancellationTokenSource CancellationSource = new CancellationTokenSource(); | ||
|
||
public LoopbackTests() | ||
{ | ||
Configuration.MessageQueueingService = new InMemoryMessageQueueingService(); | ||
} | ||
|
||
[Theory] | ||
[InlineData("loopback")] // Standard loopback endpoint | ||
[InlineData("undefined")] // Undefined/unknown endpoint | ||
public async Task MessageIsHandledWhenSendingToNamedEndpoint(string endpointName) | ||
{ | ||
var handlerTask = GivenHandler(); | ||
await WhenSendingToNamedEndpoint(endpointName); | ||
var handledContent = await handlerTask; | ||
Assert.Equal(MessageContent, handledContent); | ||
} | ||
|
||
[Theory] | ||
[InlineData("urn:localhost/loopback")] // Standard loopback endpoint address | ||
[InlineData("http://localhost/undefined")] // Undefined/unknown endpoint address | ||
public async Task MessageIsHandledWhenSendingToEndpointAddress(string address) | ||
{ | ||
var handlerTask = GivenHandler(); | ||
await WhenSendingToEndpointAddress(new Uri(address)); | ||
var handledContent = await handlerTask; | ||
Assert.Equal(MessageContent, handledContent); | ||
} | ||
|
||
protected Task<object> GivenHandler() | ||
{ | ||
var handlerCompletionSource = new TaskCompletionSource<object>(); | ||
CancellationSource.Token.Register(() => handlerCompletionSource.TrySetCanceled()); | ||
Configuration.AddHandlingRule<object>(".*", (content, ctx) => | ||
{ | ||
handlerCompletionSource.TrySetResult(content); | ||
ctx.Acknowledge(); | ||
}); | ||
return handlerCompletionSource.Task; | ||
} | ||
|
||
protected async Task WhenSendingToNamedEndpoint(EndpointName endpointName) | ||
{ | ||
var host = await LoopbackHost.Start(Configuration, CancellationSource.Token); | ||
await host.Bus.Send(MessageContent, endpointName, SendOptions, CancellationSource.Token); | ||
} | ||
|
||
protected async Task WhenSendingToEndpointAddress(Uri endpointAddress) | ||
{ | ||
var host = await LoopbackHost.Start(Configuration, CancellationSource.Token); | ||
await host.Bus.Send(MessageContent, endpointAddress, SendOptions, CancellationSource.Token); | ||
} | ||
} | ||
} |
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
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
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,33 @@ | ||
using System; | ||
|
||
namespace Platibus | ||
{ | ||
internal class LoopbackEndpoints : EndpointCollection | ||
{ | ||
private readonly Uri _baseUri; | ||
|
||
public LoopbackEndpoints(EndpointName name, Uri baseUri) | ||
{ | ||
_baseUri = baseUri ?? throw new ArgumentNullException(nameof(baseUri)); | ||
base.Add(name, new Endpoint(baseUri)); | ||
} | ||
|
||
public override void Add(EndpointName endpointName, IEndpoint endpoint) | ||
{ | ||
} | ||
|
||
public override IEndpoint this[EndpointName endpointName] => new Endpoint(_baseUri); | ||
|
||
public override bool TryGetEndpointByAddress(Uri address, out IEndpoint endpoint) | ||
{ | ||
endpoint = new Endpoint(_baseUri); | ||
return true; | ||
} | ||
|
||
public override bool Contains(EndpointName endpointName) | ||
{ | ||
return true; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.