-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Remove test-only method - Extract DirectoryPeerSelector
- Loading branch information
Showing
4 changed files
with
109 additions
and
35 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
src/Abc.Zebus.Tests/Directory/DirectoryPeerSelectorTests.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,40 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Abc.Zebus.Core; | ||
using Abc.Zebus.Directory; | ||
using Abc.Zebus.Testing.Extensions; | ||
using NUnit.Framework; | ||
|
||
namespace Abc.Zebus.Tests.Directory | ||
{ | ||
[TestFixture] | ||
public class DirectoryPeerSelectorTests | ||
{ | ||
private BusConfiguration _configuration; | ||
private DirectoryPeerSelector _selector; | ||
|
||
[SetUp] | ||
public void SetUp() | ||
{ | ||
_configuration = new BusConfiguration(new[] { "tcp://dir1:129", "tcp://dir2:129" }); | ||
_selector = new DirectoryPeerSelector(_configuration); | ||
} | ||
|
||
[Test] | ||
public void should_get_peers_randomly() | ||
{ | ||
// Arrange | ||
var results = new List<Peer[]>(); | ||
|
||
// Act | ||
for (int i = 0; i < 25; i++) | ||
{ | ||
results.Add(_selector.GetPeers().ToArray()); | ||
} | ||
|
||
// Assert | ||
var firstResult = results[0]; | ||
results.Any(x => x[0] != firstResult[0]).ShouldBeTrue(); | ||
} | ||
} | ||
} |
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,44 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Abc.Zebus.Util.Extensions; | ||
|
||
namespace Abc.Zebus.Directory | ||
{ | ||
internal class DirectoryPeerSelector | ||
{ | ||
private readonly IBusConfiguration _configuration; | ||
private string[]? _cachedEndPoints; | ||
|
||
public DirectoryPeerSelector(IBusConfiguration configuration) | ||
{ | ||
_configuration = configuration; | ||
} | ||
|
||
public IEnumerable<Peer> GetPeers() | ||
{ | ||
_cachedEndPoints = _configuration.DirectoryServiceEndPoints; | ||
|
||
return GetPeersImpl(_cachedEndPoints); | ||
} | ||
|
||
public IEnumerable<Peer> GetPeersFromCache() | ||
{ | ||
var endPoints = _cachedEndPoints ?? _configuration.DirectoryServiceEndPoints; | ||
|
||
return GetPeersImpl(endPoints); | ||
} | ||
|
||
private IEnumerable<Peer> GetPeersImpl(string[] endPoints) | ||
{ | ||
var peers = endPoints.Select(CreateDirectoryPeer); | ||
|
||
return _configuration.IsDirectoryPickedRandomly ? peers.Shuffle() : peers; | ||
} | ||
|
||
private static Peer CreateDirectoryPeer(string endPoint, int index) | ||
{ | ||
var peerId = new PeerId("Abc.Zebus.DirectoryService." + index); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ocoanet
Author
Member
|
||
return new Peer(peerId, endPoint); | ||
} | ||
} | ||
} |
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 is a bit of a hack that relies that could break things if the configuration returns the directory services in a different order.