Skip to content

Commit

Permalink
Add more test.
Browse files Browse the repository at this point in the history
  • Loading branch information
emreyigit committed Aug 28, 2024
1 parent bec87a9 commit d4bf18f
Showing 1 changed file with 85 additions and 26 deletions.
111 changes: 85 additions & 26 deletions src/Hazelcast.Net.Tests/Clustering/MemberPartitionGroupServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ public async Task TestMultiMemberRoutingWorks(RoutingStrategy routingStrategy)
var address2 = "127.0.0.1:5702";
var address3 = "127.0.0.1:5703";

var addreses = new string[] { address1, address2, address3 };

// create a client with the given routing strategy
var client1 = await CreateClient(routingStrategy, address1);
var client2 = await CreateClient(routingStrategy, address2);
var client3 = await CreateClient(routingStrategy, address3);
var client1 = await CreateClient(routingStrategy, new string[] { address1 }, "client1");
var client2 = await CreateClient(routingStrategy, new string[] { address2 }, "client2");
var client3 = await CreateClient(routingStrategy, new string[] { address3 }, "client3");

AssertClientOnlySees(client1, address1);
AssertClientOnlySees(client2, address2);
Expand All @@ -68,7 +70,7 @@ await AssertEx.SucceedsEventually(() =>
{
AssertClientOnlySees(client1, address1);
}, 10_000, 500, "Client1 did not see the correct members");

AssertClientOnlySees(client2, address2);
AssertClientOnlySees(client3, address3);

Expand All @@ -79,37 +81,89 @@ await AssertEx.SucceedsEventually(() =>
AssertClientOnlySees(client1, address1);
AssertClientOnlySees(client2, address2);

var nAddress3 = await AssertClientReConnected(client3, address3);

// Check if client1 is connected to the new member
// cannot use the old member id since we can only either kill or create member
Assert.That(client3.Cluster.Connections.Count, Is.EqualTo(1));
Assert.That(client3.Members.Where(p => p.IsConnected).Select(p => p.Member.ConnectAddress.ToString()), Contains.Item(address3));

AssertClientOnlySees(client1, address1);
AssertClientOnlySees(client2, address2);
AssertClientOnlySees(client3, address3);
}


[TestCase(RoutingStrategy.PartitionGroups)]
public async Task TestMultiMemberRoutingConnectsNextGroupWhenDisconnected(RoutingStrategy routingStrategy)
{
var address1 = "127.0.0.1:5701";
var address2 = "127.0.0.1:5702";
var address3 = "127.0.0.1:5703";

var addreses = new string[] { address1, address2, address3 };

// create a client with the given routing strategy
var client = await CreateClient(routingStrategy, addreses, "client1");

// it should connect to first address
Assert.That(client.Cluster.Connections.Count, Is.EqualTo(1));

var connectedAddress = client.Members.First(p => p.IsConnected).Member.ConnectAddress.ToString();

AssertClientOnlySees(client, connectedAddress);

var effectiveMembers = client.Cluster.Members.GetMembersForConnection();
Assert.That(effectiveMembers.Count(), Is.EqualTo(1));
Assert.That(effectiveMembers.Select(p => p.ConnectAddress.ToString()), Contains.Item(connectedAddress));
// Kill the connected member so that client can go to next group
var connectedMember = RcMembers.Values.Where(m => connectedAddress.Equals($"{m.Host}:{m.Port}")).Select(m => m.Uuid).First();
RemoveMember(connectedMember);

await AssertEx.SucceedsEventually(() => Assert.That(client.State, Is.EqualTo(ClientState.Disconnected)), 10_000, 500);

await AssertEx.SucceedsEventually(() =>
{
Assert.That(client.Cluster.Connections.Count, Is.EqualTo(1));
Assert.That(client.State, Is.EqualTo(ClientState.Connected));
}, 20_000, 500);

var reConnectedAddress = client.Members.First(p => p.IsConnected).Member.ConnectAddress.ToString();
effectiveMembers = client.Cluster.Members.GetMembersForConnection();

Assert.That(reConnectedAddress, Is.Not.EqualTo(connectedAddress));
Assert.That(client.Cluster.Connections.Count, Is.EqualTo(1));
Assert.That(client.Members.Where(p => p.IsConnected).Select(p => p.Member.ConnectAddress.ToString()), Contains.Item(reConnectedAddress));
Assert.That(effectiveMembers.Count(), Is.EqualTo(1));
Assert.That(effectiveMembers.Select(p => p.ConnectAddress.ToString()), Contains.Item(reConnectedAddress));
}

private async Task<NetworkAddress> AssertClientReConnected(HazelcastClient client, string address, bool createMember = true)
{
await AssertEx.SucceedsEventually(()
=>
{
Assert.That(client3.Cluster.Connections.Count, Is.EqualTo(0));
Assert.That(client3.State, Is.EqualTo(ClientState.Disconnected));
Assert.That(client.Cluster.Connections.Count, Is.EqualTo(0));
Assert.That(client.State, Is.EqualTo(ClientState.Disconnected));
},
15_000, 500);

Member member3;
var nAddress3 = NetworkAddress.Parse(address3);
while (true)
Member member;
var nAddress3 = NetworkAddress.Parse(address);
while (createMember)
{
member3 = await AddMember();
var created = new NetworkAddress(member3.Host, member3.Port);
member = await AddMember();
var created = new NetworkAddress(member.Host, member.Port);
if (created == nAddress3) break;

await RemoveMember(member3.Uuid);
await RemoveMember(member.Uuid);
}

await AssertEx.SucceedsEventually(()
=> Assert.That(client3.State, Is.EqualTo(ClientState.Connected)),
=> Assert.That(client.State, Is.EqualTo(ClientState.Connected)),
10_000, 500);

// Check if client1 is connected to the new member
// cannot use the old member id since we can only either kill or create member
Assert.That(client3.Cluster.Connections.Count, Is.EqualTo(1));
Assert.That(client3.Members.Select(p => p.Member.ConnectAddress), Contains.Item(nAddress3));

AssertClientOnlySees(client1, address1);
AssertClientOnlySees(client2, address2);
AssertClientOnlySees(client3, address3);
return nAddress3;
}

[TestCase(RoutingModes.MultiMember)]
Expand All @@ -119,10 +173,11 @@ public async Task TestClientCollectsClusterEvents(RoutingModes routingMode)
{
var address1 = "127.0.0.1:5701";
var address2 = "127.0.0.1:5702";
var addreses = new string[] { address1, address2 };
var keyCount = 3 * 271;

// create a client with the given routing strategy for catching the events.
var client1 = await CreateClient(RoutingStrategy.PartitionGroups, address1, routingMode);
var client1 = await CreateClient(RoutingStrategy.PartitionGroups, addreses, "client1", routingMode);
var client1Count = 0;
var mapName = $"map_{routingMode}";
var map1 = await client1.GetMapAsync<int, int>(mapName);
Expand All @@ -137,7 +192,7 @@ await map1.SubscribeAsync((eventHandler) =>


// create a dummy client for creating events
var client2 = await CreateClient(RoutingStrategy.PartitionGroups, address2, RoutingModes.SingleMember);
var client2 = await CreateClient(RoutingStrategy.PartitionGroups, addreses, "client2", RoutingModes.SingleMember);

var map2 = await client2.GetMapAsync<int, int>(map1.Name);

Expand Down Expand Up @@ -168,13 +223,17 @@ private void AssertClientOnlySees(HazelcastClient client, string address, int cl
Assert.That(members.SubsetClusterMembers.GetSubsetMemberIds().Count(), Is.EqualTo(1));
Assert.That(members.SubsetClusterMembers.GetSubsetMemberIds(), Contains.Item(memberId));
}
private async Task<HazelcastClient> CreateClient(RoutingStrategy routingStrategy, string address, RoutingModes routingMode = RoutingModes.MultiMember)
private async Task<HazelcastClient> CreateClient(RoutingStrategy routingStrategy, string[] address, string clientName, RoutingModes routingMode = RoutingModes.MultiMember)
{
var options = new HazelcastOptionsBuilder()
.With(args =>
{
args.ClientName = $"Client:{address}";
args.Networking.Addresses.Add(address);
for (int i = 0; i < address.Length; i++)
{

args.Networking.Addresses.Add(address[i]);
}
args.ClientName = clientName;
args.ClusterName = RcCluster.Id;
args.Networking.RoutingMode.Mode = routingMode;
args.Networking.RoutingMode.Strategy = routingStrategy;
Expand Down

0 comments on commit d4bf18f

Please sign in to comment.