public void describe_removal_of_expired_connections()
        {
            var clusterConfig = new GossipClusterConfig {
                ClusterKey = Encoding.UTF8.GetBytes("ClusterKey")
            };
            var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort1));
            var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort2));
            var message     = new RawGossipMessage();

            using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
                using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
                {
                    node1.StartListening();
                    node2.StartListening();

                    node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig2, node2.Authenticator));
                    node2.Cluster.Join(new GossipClusterMember(nodeConfig2.NodeId, nodeConfig1, node1.Authenticator));

                    node1.Cluster.BroadcastMessage(message);

                    var clusterMember = node1.Cluster[nodeConfig2.NodeId];
                    GossipClusterMember.MinConnectionInactivity = TimeSpan.Zero;
                    clusterMember.MaxConnectionInactivity       = TimeSpan.Zero;
                    Thread.Sleep(1);
                    clusterMember.RemoveInactiveConnections(null);
                    Assert.AreEqual(0, clusterMember.NumberOfOpenConnections);
                }
        }
        public void describe_creating_connection_for_broadcasting_message_to_cluster()
        {
            var clusterConfig = new GossipClusterConfig {
                ClusterKey = Encoding.UTF8.GetBytes("ClusterKey")
            };
            var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort1));
            var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort2));
            var message     = new RawGossipMessage();

            using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
                using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
                {
                    node1.StartListening();
                    node2.StartListening();

                    node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig2, node2.Authenticator));
                    node2.Cluster.Join(new GossipClusterMember(nodeConfig2.NodeId, nodeConfig1, node1.Authenticator));
                    Assert.IsNotNull(node1.Cluster[nodeConfig2.NodeId]);
                    Assert.IsNotNull(node2.Cluster[nodeConfig1.NodeId]);

                    node1.Cluster.BroadcastMessage(message);
                    Assert.AreEqual(1, node1.Cluster[nodeConfig2.NodeId].NumberOfOpenConnections);
                    Assert.AreEqual(1, node2.Cluster[nodeConfig1.NodeId].NumberOfOpenConnections);
                }
        }
        public void describe_connecting_to_node_that_is_down()
        {
            var clusterConfig = new GossipClusterConfig {
                ClusterKey = Encoding.UTF8.GetBytes("ClusterKey")
            };
            var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort1));
            var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort2));
            var message     = new RawGossipMessage();

            var connectionFailed = new ManualResetEventSlim(false);

            using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
                using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
                {
                    node1.StartListening();
                    node2.StartListening();

                    var wrongConfig = nodeConfig2.Clone();
                    wrongConfig.BindToEndPoint = new IPEndPoint(wrongConfig.BindToEndPoint.Address, 1);
                    var member = new GossipClusterMember(nodeConfig1.NodeId, wrongConfig, node2.Authenticator);
                    member.ConnectionTimeout   = TimeSpan.FromMilliseconds(300);
                    member.OnConnectionFailed += (m, e) => connectionFailed.Set();

                    node1.Cluster.Join(member);
                    node2.Cluster.Join(new GossipClusterMember(nodeConfig2.NodeId, nodeConfig1, node1.Authenticator));

                    node1.Cluster.BroadcastMessage(message);
                    if (!connectionFailed.Wait(member.ConnectionTimeout.Add(TimeSpan.FromMilliseconds(100))))
                    {
                        Assert.Fail();
                    }
                }
        }
        public void describe_connecting_to_node_that_is_down()
        {
            var clusterConfig = new GossipClusterConfig { ClusterKey = Encoding.UTF8.GetBytes("ClusterKey") };
            var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort1));
            var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort2));
            var message = new RawGossipMessage();

            var connectionFailed = new ManualResetEventSlim(false);

            using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
            using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
            {
                node1.StartListening();
                node2.StartListening();

                var wrongConfig = nodeConfig2.Clone();
                wrongConfig.BindToEndPoint = new IPEndPoint(wrongConfig.BindToEndPoint.Address, 1);
                var member = new GossipClusterMember(nodeConfig1.NodeId, wrongConfig, node2.Authenticator);
                member.ConnectionTimeout = TimeSpan.FromMilliseconds(300);
                member.OnConnectionFailed += (m, e) => connectionFailed.Set();

                node1.Cluster.Join(member);
                node2.Cluster.Join(new GossipClusterMember(nodeConfig2.NodeId, nodeConfig1, node1.Authenticator));

                node1.Cluster.BroadcastMessage(message);
                if (!connectionFailed.Wait(member.ConnectionTimeout.Add(TimeSpan.FromMilliseconds(100))))
                    Assert.Fail();
            }
        }
        public void describe_custom_gossip_message_factory_creating_different_message_types()
        {
            var clusterConfig = new GossipClusterConfig { ClusterKey = Encoding.UTF8.GetBytes("ClusterKey") };
            var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort1));
            var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort2));
            var message1 = new GossipMessage1();
            var message2 = new GossipMessage2();
            GossipMessageFactory.CreateMessage += CreateMessage;

            using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
            using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
            {
                var received = new AutoResetEvent(false);
                GossipMessage receivedMsg = null;
                node1.OnMessageReceived += (node, conn, msg) =>
                                               {
                                                   receivedMsg = msg;
                                                   received.Set();
                                               };

                node1.StartListening();
                node2.StartListening();

                node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig2, node2.Authenticator));
                node2.Cluster.Join(new GossipClusterMember(nodeConfig2.NodeId, nodeConfig1, node1.Authenticator));

                node2.Cluster.BroadcastMessage(message1);
                if (!received.WaitOne(Timeout)) Assert.Fail();
                Assert.IsInstanceOf<GossipMessage1>(receivedMsg);

                node2.Cluster.BroadcastMessage(message2);
                if (!received.WaitOne(Timeout)) Assert.Fail();
                Assert.IsInstanceOf<GossipMessage2>(receivedMsg);
            }
        }
        public void describe_custom_gossip_message_factory_creating_different_message_types()
        {
            var clusterConfig = new GossipClusterConfig {
                ClusterKey = Encoding.UTF8.GetBytes("ClusterKey")
            };
            var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort1));
            var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort2));
            var message1    = new GossipMessage1();
            var message2    = new GossipMessage2();

            GossipMessageFactory.CreateMessage += CreateMessage;

            using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
                using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
                {
                    var           received    = new AutoResetEvent(false);
                    GossipMessage receivedMsg = null;
                    node1.OnMessageReceived += (node, conn, msg) =>
                    {
                        receivedMsg = msg;
                        received.Set();
                    };

                    node1.StartListening();
                    node2.StartListening();

                    node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig2, node2.Authenticator));
                    node2.Cluster.Join(new GossipClusterMember(nodeConfig2.NodeId, nodeConfig1, node1.Authenticator));

                    node2.Cluster.BroadcastMessage(message1);
                    if (!received.WaitOne(Timeout))
                    {
                        Assert.Fail();
                    }
                    Assert.IsInstanceOf <GossipMessage1>(receivedMsg);

                    node2.Cluster.BroadcastMessage(message2);
                    if (!received.WaitOne(Timeout))
                    {
                        Assert.Fail();
                    }
                    Assert.IsInstanceOf <GossipMessage2>(receivedMsg);
                }
        }
        public void describe_connection_with_successful_authentication()
        {
            var clusterConfig = new GossipClusterConfig { ClusterKey = Encoding.UTF8.GetBytes("ClusterKey") };
            var nodeConfig = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort));

            using (var node = new GossipNode(nodeConfig, clusterConfig))
            {
                var signal = new ManualResetEventSlim(false);
                bool? authenticated = null;
                node.OnClientConnectionAuthenticationFailed += (n, c) => { authenticated = false; signal.Set(); };
                node.OnClientConnectionAuthenticationSucceeded += (n, c) => { authenticated = true; signal.Set(); };
                node.StartListening();
                using (GossipConnection.ConnectAsync(nodeConfig.NodeId, nodeConfig.BindToEndPoint, node.Authenticator, c => signal.Set()))
                {
                    signal.Wait();
                    Assert.That(authenticated == true);
                }
            }
        }
        public void describe_connection_with_successful_authentication()
        {
            var clusterConfig = new GossipClusterConfig {
                ClusterKey = Encoding.UTF8.GetBytes("ClusterKey")
            };
            var nodeConfig = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort));

            using (var node = new GossipNode(nodeConfig, clusterConfig))
            {
                var  signal        = new ManualResetEventSlim(false);
                bool?authenticated = null;
                node.OnClientConnectionAuthenticationFailed    += (n, c) => { authenticated = false; signal.Set(); };
                node.OnClientConnectionAuthenticationSucceeded += (n, c) => { authenticated = true; signal.Set(); };
                node.StartListening();
                using (GossipConnection.ConnectAsync(nodeConfig.NodeId, nodeConfig.BindToEndPoint, node.Authenticator, c => signal.Set()))
                {
                    signal.Wait();
                    Assert.That(authenticated == true);
                }
            }
        }
示例#9
0
        static void Main(string[] args)
        {
            while (true)
            {
                var clusterConfig = new GossipClusterConfig {
                    ClusterKey = Encoding.UTF8.GetBytes("ClusterKey")
                };
                var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30001), "tag2");
                var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30002), "tag2");
                using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
                    using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
                    {
                        node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig2, node2.Authenticator));

                        node1.OnMessageReceived += ProcessSimpleTextMessage;

                        node1.StartListening();

                        node1.Cluster.BroadcastMessageAsync(new RawGossipMessage(1, Encoding.UTF8.GetBytes("Node 1 Info"))).Wait();
                    }

                Console.ReadLine();
                //var port = Convert.ToInt32(ConfigurationManager.AppSettings["Port"]);
                //string startPort = ConfigurationManager.AppSettings["StartPort"];

                //if (startPort != "")
                //{
                //    Console.WriteLine("Join Cluster");
                //    var startEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), Convert.ToInt32(startPort));
                //    Node node = new Node(new IPEndPoint(IPAddress.Parse("127.0.0.1"), port), "ClusterKey", startEndPoint);
                //}
                //else
                //{
                //    Console.WriteLine("First Seed");
                //    Node node = new Node(new IPEndPoint(IPAddress.Parse("127.0.0.1"), port), "ClusterKey");
                //}
            }
        }
        public void describe_creating_connection_for_broadcasting_message_to_cluster()
        {
            var clusterConfig = new GossipClusterConfig { ClusterKey = Encoding.UTF8.GetBytes("ClusterKey") };
            var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort1));
            var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort2));
            var message = new RawGossipMessage();

            using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
            using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
            {
                node1.StartListening();
                node2.StartListening();

                node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig2, node2.Authenticator));
                node2.Cluster.Join(new GossipClusterMember(nodeConfig2.NodeId, nodeConfig1, node1.Authenticator));
                Assert.IsNotNull(node1.Cluster[nodeConfig2.NodeId]);
                Assert.IsNotNull(node2.Cluster[nodeConfig1.NodeId]);

                node1.Cluster.BroadcastMessage(message);
                Assert.AreEqual(1, node1.Cluster[nodeConfig2.NodeId].NumberOfOpenConnections);
                Assert.AreEqual(1, node2.Cluster[nodeConfig1.NodeId].NumberOfOpenConnections);
            }
        }
        public void describe_keeping_of_nonexpired_connections()
        {
            var clusterConfig = new GossipClusterConfig { ClusterKey = Encoding.UTF8.GetBytes("ClusterKey") };
            var nodeConfig1 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort1));
            var nodeConfig2 = new GossipNodeConfig(new IPEndPoint(IPAddress.Loopback, NodePort2));
            var message = new RawGossipMessage();

            using (var node1 = new GossipNode(nodeConfig1, clusterConfig))
            using (var node2 = new GossipNode(nodeConfig2, clusterConfig))
            {
                node1.StartListening();
                node2.StartListening();

                node1.Cluster.Join(new GossipClusterMember(nodeConfig1.NodeId, nodeConfig2, node2.Authenticator));
                node2.Cluster.Join(new GossipClusterMember(nodeConfig2.NodeId, nodeConfig1, node1.Authenticator));

                node1.Cluster.BroadcastMessage(message);

                var clusterMember = node1.Cluster[nodeConfig2.NodeId];
                Thread.Sleep(1);
                clusterMember.RemoveInactiveConnections(null);
                Assert.AreEqual(1, clusterMember.NumberOfOpenConnections);
            }
        }