Commit aaa5b514 authored by Nick Craver's avatar Nick Craver

Tests: enhance DeslaveGoesToPrimary

This doesn't resolve the issue, but adds several confirmations and more information. At the moment we're not recognizing :6380 as a slaved instance though it clearly is from the outside. There's more to dig into on recognition here. This just improves the test quite a bit, before it was throwing in the middle without much info.
parent ca1bd01a
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Threading.Tasks;
using Xunit; using Xunit;
using Xunit.Abstractions; using Xunit.Abstractions;
...@@ -33,13 +34,13 @@ public void CannotFlushSlave() ...@@ -33,13 +34,13 @@ public void CannotFlushSlave()
} }
[Fact] [Fact]
public void DeslaveGoesToPrimary() public async Task DeslaveGoesToPrimary()
{ {
ConfigurationOptions config = GetMasterSlaveConfig(); ConfigurationOptions config = GetMasterSlaveConfig();
using (var conn = ConnectionMultiplexer.Connect(config)) using (var conn = ConnectionMultiplexer.Connect(config))
{ {
var primary = conn.GetServer(new IPEndPoint(IPAddress.Parse(TestConfig.Current.MasterServer), TestConfig.Current.MasterPort)); var primary = conn.GetServer(new IPEndPoint(IPAddress.Parse(TestConfig.Current.MasterServer), TestConfig.Current.MasterPort));
var secondary = conn.GetServer(new IPEndPoint(IPAddress.Parse(TestConfig.Current.MasterServer), TestConfig.Current.SlavePort)); var secondary = conn.GetServer(new IPEndPoint(IPAddress.Parse(TestConfig.Current.SlaveServer), TestConfig.Current.SlavePort));
primary.Ping(); primary.Ping();
secondary.Ping(); secondary.Ping();
...@@ -47,6 +48,8 @@ public void DeslaveGoesToPrimary() ...@@ -47,6 +48,8 @@ public void DeslaveGoesToPrimary()
primary.MakeMaster(ReplicationChangeOptions.SetTiebreaker); primary.MakeMaster(ReplicationChangeOptions.SetTiebreaker);
secondary.MakeMaster(ReplicationChangeOptions.None); secondary.MakeMaster(ReplicationChangeOptions.None);
await Task.Delay(2000).ConfigureAwait(false);
primary.Ping(); primary.Ping();
secondary.Ping(); secondary.Ping();
...@@ -62,39 +65,54 @@ public void DeslaveGoesToPrimary() ...@@ -62,39 +65,54 @@ public void DeslaveGoesToPrimary()
var db = conn.GetDatabase(); var db = conn.GetDatabase();
RedisKey key = Me(); RedisKey key = Me();
EndPoint demandMaster, preferMaster, preferSlave, demandSlave; Assert.Equal(primary.EndPoint, db.IdentifyEndpoint(key, CommandFlags.PreferMaster));
preferMaster = db.IdentifyEndpoint(key, CommandFlags.PreferMaster); Assert.Equal(primary.EndPoint, db.IdentifyEndpoint(key, CommandFlags.DemandMaster));
demandMaster = db.IdentifyEndpoint(key, CommandFlags.DemandMaster); Assert.Equal(primary.EndPoint, db.IdentifyEndpoint(key, CommandFlags.PreferSlave));
preferSlave = db.IdentifyEndpoint(key, CommandFlags.PreferSlave);
Assert.Equal(primary.EndPoint, demandMaster); var ex = Assert.Throws<RedisConnectionException>(() => db.IdentifyEndpoint(key, CommandFlags.DemandSlave));
Assert.Equal(primary.EndPoint, preferMaster); Assert.StartsWith("No connection is available to service this operation: EXISTS DeslaveGoesToPrimary", ex.Message);
Assert.Equal(primary.EndPoint, preferSlave);
try primary.MakeMaster(ReplicationChangeOptions.Broadcast | ReplicationChangeOptions.EnslaveSubordinates | ReplicationChangeOptions.SetTiebreaker, Writer);
{
demandSlave = db.IdentifyEndpoint(key, CommandFlags.DemandSlave); // Give the servers a chance to sort themselves
Assert.True(false, "this should not have worked"); await conn.ReconfigureAsync(false, false, Writer, primary.EndPoint, "Re-analyze after topology change").ConfigureAwait(false);
}
catch (RedisConnectionException ex)
{
Assert.StartsWith("No connection is available to service this operation: EXISTS DeslaveGoesToPrimary", ex.Message);
}
primary.MakeMaster(ReplicationChangeOptions.Broadcast | ReplicationChangeOptions.EnslaveSubordinates | ReplicationChangeOptions.SetTiebreaker); await Task.Delay(5000).ConfigureAwait(false);
primary.Ping(); primary.Ping();
secondary.Ping(); secondary.Ping();
preferMaster = db.IdentifyEndpoint(key, CommandFlags.PreferMaster); Assert.True(primary.IsConnected, $"{primary.EndPoint} is not connected.");
demandMaster = db.IdentifyEndpoint(key, CommandFlags.DemandMaster); Assert.True(secondary.IsConnected, $"{secondary.EndPoint} is not connected.");
preferSlave = db.IdentifyEndpoint(key, CommandFlags.PreferSlave);
demandSlave = db.IdentifyEndpoint(key, CommandFlags.DemandSlave); Writer.WriteLine($"{primary.EndPoint}: {primary.ServerType}");
Writer.WriteLine($"{secondary.EndPoint}: {secondary.ServerType}");
// Create a separate multiplexer with a valid view of the world to distinguish between failures of
// server topology changes from failures to recognize those changes
using (var conn2 = ConnectionMultiplexer.Connect(config))
{
var primary2 = conn.GetServer(new IPEndPoint(IPAddress.Parse(TestConfig.Current.MasterServer), TestConfig.Current.MasterPort));
var secondary2 = conn.GetServer(new IPEndPoint(IPAddress.Parse(TestConfig.Current.SlaveServer), TestConfig.Current.SlavePort));
Assert.False(primary2.IsSlave, $"{primary2.EndPoint} should be a master (verification conneciton).");
Assert.True(secondary2.IsSlave, $"{secondary2.EndPoint} should be a slave (verification conneciton).");
var db2 = conn.GetDatabase();
Assert.Equal(primary2.EndPoint, db2.IdentifyEndpoint(key, CommandFlags.PreferMaster));
Assert.Equal(primary2.EndPoint, db2.IdentifyEndpoint(key, CommandFlags.DemandMaster));
Assert.Equal(secondary2.EndPoint, db2.IdentifyEndpoint(key, CommandFlags.PreferSlave));
Assert.Equal(secondary2.EndPoint, db2.IdentifyEndpoint(key, CommandFlags.DemandSlave));
}
Assert.False(primary.IsSlave, $"{primary.EndPoint} should be a master.");
Assert.True(secondary.IsSlave, $"{secondary.EndPoint} should be a slave.");
Assert.Equal(primary.EndPoint, demandMaster); Assert.Equal(primary.EndPoint, db.IdentifyEndpoint(key, CommandFlags.PreferMaster));
Assert.Equal(primary.EndPoint, preferMaster); Assert.Equal(primary.EndPoint, db.IdentifyEndpoint(key, CommandFlags.DemandMaster));
Assert.Equal(secondary.EndPoint, preferSlave); Assert.Equal(secondary.EndPoint, db.IdentifyEndpoint(key, CommandFlags.PreferSlave));
Assert.Equal(secondary.EndPoint, preferSlave); Assert.Equal(secondary.EndPoint, db.IdentifyEndpoint(key, CommandFlags.DemandSlave));
} }
} }
...@@ -107,7 +125,7 @@ private static ConfigurationOptions GetMasterSlaveConfig() ...@@ -107,7 +125,7 @@ private static ConfigurationOptions GetMasterSlaveConfig()
EndPoints = EndPoints =
{ {
{ TestConfig.Current.MasterServer, TestConfig.Current.MasterPort }, { TestConfig.Current.MasterServer, TestConfig.Current.MasterPort },
{ TestConfig.Current.MasterServer, TestConfig.Current.SlavePort }, { TestConfig.Current.SlaveServer, TestConfig.Current.SlavePort },
} }
}; };
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment