Ejemplo n.º 1
0
        public void SwitchConnection_BlastEvents()
        {
            // Simulate the blasting of events from the switch to the remote connection.

            var handlerDone     = false;
            var cEventsReceived = 0;

            var connectHandler = new EventHandler <SwitchInboundConnectionArgs>(
                (s, a) =>
            {
                a.StartConnectionThread = false;

                Helper.EnqueueAction(() =>
                {
                    SwitchConnection serverConnection = a.Connection;

                    AuthHandshake(serverConnection, false);

                    serverConnection.StartThread();

                    Helper.EnqueueAction(
                        () =>
                    {
                        for (int i = 0; i < TransactionCount; i++)
                        {
                            SendEvent(serverConnection, SwitchEventCode.Heartbeat, "Hello World!", new NameValue("Foo", "Bar"));
                        }
                    });

                    Helper.WaitFor(() => cEventsReceived == TransactionCount, Timeout);

                    serverConnection.Close();
                    handlerDone = true;
                });
            });

            SwitchConnection.InboundConnection += connectHandler;

            try
            {
                SwitchConnection.SendBufferSize        =
                    SwitchConnection.ReceiveBufferSize = SocketBufferSize;

                SwitchConnection.StartListener(binding, 10);

                var connection   = new SwitchConnection(binding, SwitchConnection.DefaultPassword);
                var elapsedTimer = new ElapsedTimer();

                connection.EventReceived +=
                    (s, a) =>
                {
                    if (a.EventCode == SwitchEventCode.Heartbeat &&
                        a.ContentType == "text" &&
                        a.ContentText == "Hello World!")
                    {
                        Interlocked.Increment(ref cEventsReceived);
                    }
                };

                connection.Connect();

                elapsedTimer.Start();
                Helper.WaitFor(() => handlerDone, Timeout);
                elapsedTimer.Stop();

                var rate = cEventsReceived / elapsedTimer.ElapsedTime.TotalSeconds;

                Debug.WriteLine(string.Format("Transaction Rate: {0}/sec", rate));
            }
            finally
            {
                SwitchConnection.ResetGlobals();
                SwitchConnection.InboundConnection -= connectHandler;
                SwitchConnection.StopListener();
            }
        }
Ejemplo n.º 2
0
        public void SwitchConnection_BlastBoth()
        {
            // Simulate the blasting of events from the switch to the remote connection
            // and commands from the remote machine to the switch.

            var handlerDone       = false;
            var cCommandsReceived = 0;
            var cCommandsExecuted = 0;
            var cEventsReceived   = 0;

            var connectHandler = new EventHandler <SwitchInboundConnectionArgs>(
                (s, a) =>
            {
                a.StartConnectionThread = false;

                Helper.EnqueueAction(() =>
                {
                    SwitchConnection serverConnection = a.Connection;

                    serverConnection.CommandReceived +=
                        (s1, a1) =>
                    {
                        if (a1.CommandText == "sendevent HEARTBEAT" &&
                            Helper.ASCIIEncoding.GetString(a1.Content) == "Hello World!" &&
                            a1.Properties["Foo"] == "Bar")
                        {
                            Interlocked.Increment(ref cCommandsReceived);
                        }
                    };

                    AuthHandshake(serverConnection, false);

                    serverConnection.StartThread();

                    Helper.EnqueueAction(
                        () =>
                    {
                        for (int i = 0; i < TransactionCount; i++)
                        {
                            SendEvent(serverConnection, SwitchEventCode.Heartbeat, "Hello World!", new NameValue("Foo", "Bar"));
                        }
                    });

                    Helper.WaitFor(() => cCommandsReceived == TransactionCount && cCommandsExecuted == TransactionCount && cEventsReceived == TransactionCount, Timeout);

                    serverConnection.Close();
                    handlerDone = true;
                });
            });

            SwitchConnection.InboundConnection += connectHandler;

            try
            {
                SwitchConnection.SendBufferSize        =
                    SwitchConnection.ReceiveBufferSize = SocketBufferSize;

                SwitchConnection.StartListener(binding, 10);

                var connection   = new SwitchConnection(binding, SwitchConnection.DefaultPassword);
                var elapsedTimer = new ElapsedTimer();

                connection.EventReceived +=
                    (s, a) =>
                {
                    if (a.EventCode == SwitchEventCode.Heartbeat &&
                        a.ContentType == "text" &&
                        a.ContentText == "Hello World!")
                    {
                        Interlocked.Increment(ref cEventsReceived);
                    }
                };

                connection.Connect();

                elapsedTimer.Start();

                for (int i = 0; i < TransactionCount; i++)
                {
                    var properties = new ArgCollection(ArgCollectionType.Unconstrained);

                    properties["Foo"] = "Bar";
                    connection.SendEvent(SwitchEventCode.Heartbeat, properties, "Hello World!");
                    Interlocked.Increment(ref cCommandsExecuted);
                }

                Helper.WaitFor(() => handlerDone, Timeout);

                elapsedTimer.Stop();

                var rate = (cCommandsReceived + cEventsReceived) / elapsedTimer.ElapsedTime.TotalSeconds;

                Debug.WriteLine(string.Format("Transaction Rate: {0}/sec", rate));
            }
            finally
            {
                SwitchConnection.ResetGlobals();
                SwitchConnection.InboundConnection -= connectHandler;
                SwitchConnection.StopListener();
            }
        }
Ejemplo n.º 3
0
        public void SwitchConnection_EventSend()
        {
            // Verify that an event can be sent to the switch.

            var handlerDone = false;
            var gotEvent    = false;

            var connectHandler = new EventHandler <SwitchInboundConnectionArgs>(
                (s, a) =>
            {
                a.StartConnectionThread = false;

                Helper.EnqueueAction(() =>
                {
                    SwitchConnection serverConnection = a.Connection;

                    serverConnection.CommandReceived +=
                        (s1, a1) =>
                    {
                        gotEvent = a1.CommandText == "sendevent HEARTBEAT" &&
                                   Helper.ASCIIEncoding.GetString(a1.Content) == "Hello World!" &&
                                   a1.Properties["Foo"] == "Bar";
                    };

                    AuthHandshake(serverConnection, false);

                    serverConnection.StartThread();

                    Helper.WaitFor(() => gotEvent, TimeSpan.FromMilliseconds(5000));

                    serverConnection.Close();
                    handlerDone = true;
                });
            });

            SwitchConnection.InboundConnection += connectHandler;

            try
            {
                SwitchConnection.StartListener(binding, 10);

                var connection = new SwitchConnection(binding, SwitchConnection.DefaultPassword);
                var disconnect = false;
                var closeError = false;

                connection.Disconnected +=
                    (s, a) =>
                {
                    disconnect = true;
                    closeError = a.Error != null;
                };

                connection.Connect();

                var properties = new ArgCollection(ArgCollectionType.Unconstrained);

                properties["Foo"] = "Bar";
                connection.SendEvent(SwitchEventCode.Heartbeat, properties, "Hello World!");

                Helper.WaitFor(() => handlerDone, TimeSpan.FromMilliseconds(5000));
                Helper.WaitFor(() => gotEvent, TimeSpan.FromMilliseconds(5000));

                Assert.IsTrue(gotEvent);
                Assert.IsTrue(disconnect);  // Verify that the connection was closed
                Assert.IsTrue(closeError);  // by the server.
            }
            finally
            {
                SwitchConnection.InboundConnection -= connectHandler;
                SwitchConnection.StopListener();
            }
        }