Repository: microsoft/garnet
File: test/Garnet.test.cluster/ClusterReplicationTests.cs (C#)

Code before:
                    break;
                _ = Thread.Yield();
            }

            _ = context.clusterTestUtils.ReplicaOf(replicaIndex, newPrimaryIndex, logger: context.logger);
            context.clusterTestUtils.WaitForReplicaRecovery(replicaIndex, context.logger);
            context.clusterTestUtils.WaitForReplicaAofSync(newPrimaryIndex, replicaIndex, context.logger);

            // Check if replica 2 after failover contains keys
            if (disableObjects)
                context.ValidateKVCollectionAgainstReplica(ref context.kvPairs, replicaIndex: replicaIndex, primaryIndex: newPrimaryIndex);
            else
                context.ValidateNodeObjects(ref context.kvPairsObj, nodeIndex: newPrimaryIndex, set: set);
        }

        [Test, Order(21)]
        [Category("REPLICATION")]
        public void ClusterReplicateFails()
        {
            const string UserName = "temp-user";
            const string Password = "temp-password";

            const string ClusterUserName = "cluster-user";
            const string ClusterPassword = "cluster-password";

            // setup a cluster (mimicing the style in which this bug was first found)
            ServerCredential clusterCreds = new(ClusterUserName, ClusterPassword, IsAdmin: true, UsedForClusterAuth: true, IsClearText: true);
            ServerCredential userCreds = new(UserName, Password, IsAdmin: true, UsedForClusterAuth: false, IsClearText: true);

            context.GenerateCredentials([userCreds, clusterCreds]);
            context.CreateInstances(2, disableObjects: true, disablePubSub: true, enableAOF: true, clusterCreds: clusterCreds, useAcl: true, MainMemoryReplication: true, CommitFrequencyMs: -1);
            var primaryEndpoint = (IPEndPoint)context.endpoints.First();
            var replicaEndpoint = (IPEndPoint)context.endpoints.Last();

            Assert.AreNotEqual(primaryEndpoint, replicaEndpoint, "Should have different endpoints for nodes");

            using var primaryConnection = ConnectionMultiplexer.Connect($"{primaryEndpoint.Address}:{primaryEndpoint.Port},user={UserName},password={Password}");
            var primaryServer = primaryConnection.GetServer(primaryEndpoint);

            Assert.AreEqual("OK", (string)primaryServer.Execute("CLUSTER", ["ADDSLOTSRANGE", "0", "16383"], flags: CommandFlags.NoRedirect));
            Assert.AreEqual("OK", (string)primaryServer.Execute("CLUSTER", ["MEET", replicaEndpoint.Address.ToString(), replicaEndpoint.Port.ToString()], flags: CommandFlags.NoRedirect));

            using var replicaConnection = ConnectionMultiplexer.Connect($"{replicaEndpoint.Address}:{replicaEndpoint.Port},user={UserName},password={Password}");
            var replicaServer = replicaConnection.GetServer(replicaEndpoint);

            // try to replicate from a server that doesn't exist
            var exc = Assert.Throws<RedisServerException>(() => replicaServer.Execute("CLUSTER", ["REPLICATE", Guid.NewGuid().ToString()], flags: CommandFlags.NoRedirect));
            Assert.IsTrue(exc.Message.StartsWith("ERR I don't know about node "));
        }
    }
}

Review comment (bug):
```suggestion
            // Setup a cluster (mimicking the style in which this bug was first found)
```

Code after:
                    break;
                _ = Thread.Yield();
            }

            _ = context.clusterTestUtils.ReplicaOf(replicaIndex, newPrimaryIndex, logger: context.logger);
            context.clusterTestUtils.WaitForReplicaRecovery(replicaIndex, context.logger);
            context.clusterTestUtils.WaitForReplicaAofSync(newPrimaryIndex, replicaIndex, context.logger);

            // Check if replica 2 after failover contains keys
            if (disableObjects)
                context.ValidateKVCollectionAgainstReplica(ref context.kvPairs, replicaIndex: replicaIndex, primaryIndex: newPrimaryIndex);
            else
                context.ValidateNodeObjects(ref context.kvPairsObj, nodeIndex: newPrimaryIndex, set: set);
        }

        [Test, Order(21)]
        [Category("REPLICATION")]
        public void ClusterReplicateFails()
        {
            const string UserName = "temp-user";
            const string Password = "temp-password";

            const string ClusterUserName = "cluster-user";
            const string ClusterPassword = "cluster-password";

            // Setup a cluster (mimicking the style in which this bug was first found)
            ServerCredential clusterCreds = new(ClusterUserName, ClusterPassword, IsAdmin: true, UsedForClusterAuth: true, IsClearText: true);
            ServerCredential userCreds = new(UserName, Password, IsAdmin: true, UsedForClusterAuth: false, IsClearText: true);

            context.GenerateCredentials([userCreds, clusterCreds]);
            context.CreateInstances(2, disableObjects: true, disablePubSub: true, enableAOF: true, clusterCreds: clusterCreds, useAcl: true, MainMemoryReplication: true, CommitFrequencyMs: -1);
            var primaryEndpoint = (IPEndPoint)context.endpoints.First();
            var replicaEndpoint = (IPEndPoint)context.endpoints.Last();

            Assert.AreNotEqual(primaryEndpoint, replicaEndpoint, "Should have different endpoints for nodes");

            using var primaryConnection = ConnectionMultiplexer.Connect($"{primaryEndpoint.Address}:{primaryEndpoint.Port},user={UserName},password={Password}");
            var primaryServer = primaryConnection.GetServer(primaryEndpoint);

            Assert.AreEqual("OK", (string)primaryServer.Execute("CLUSTER", ["ADDSLOTSRANGE", "0", "16383"], flags: CommandFlags.NoRedirect));
            Assert.AreEqual("OK", (string)primaryServer.Execute("CLUSTER", ["MEET", replicaEndpoint.Address.ToString(), replicaEndpoint.Port.ToString()], flags: CommandFlags.NoRedirect));

            using var replicaConnection = ConnectionMultiplexer.Connect($"{replicaEndpoint.Address}:{replicaEndpoint.Port},user={UserName},password={Password}");
            var replicaServer = replicaConnection.GetServer(replicaEndpoint);

            // Try to replicate from a server that doesn't exist
            var exc = Assert.Throws<RedisServerException>(() => replicaServer.Execute("CLUSTER", ["REPLICATE", Guid.NewGuid().ToString()], flags: CommandFlags.NoRedirect));
            Assert.IsTrue(exc.Message.StartsWith("ERR I don't know about node "));
        }
    }
}