Exemplo n.º 1
0
        public void SwitchConnection_ExecuteBackground()
        {
            // Verify the ExecuteBackground command.

            var handlerDone = false;
            var gotCommand  = false;
            var jobID       = Guid.NewGuid();

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

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

                    AuthHandshake(serverConnection, false);

                    packet     = serverConnection.ReceivePacket();
                    gotCommand = packet.PacketType == SwitchPacketType.Command &&
                                 packet.CommandText == "bgapi status" &&
                                 packet.Headers.Count == 0;

                    serverConnection.SendReply(null, new NameValue("Job-UUID", jobID.ToString("D")));
                    SendEvent(serverConnection, SwitchEventCode.BackgroundJob, string.Empty, new NameValue("Job-UUID", jobID.ToString("D")));

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

            SwitchConnection.InboundConnection += connectHandler;

            try
            {
                SwitchConnection.StartListener(binding, 10);

                SwitchConnection   connection      = new SwitchConnection(binding, SwitchConnection.DefaultPassword);
                bool               gotJobCompleted = false;
                CommandDisposition disposition;

                connection.JobCompleted +=
                    (s, a) =>
                {
                    gotJobCompleted = a.JobID == jobID;
                };

                connection.Connect();
                disposition = connection.ExecuteBackground("status");

                Assert.IsTrue(disposition.Success);
                Assert.AreEqual(jobID, disposition.JobID);

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

                Assert.IsTrue(gotCommand);
                Assert.IsTrue(gotJobCompleted);
            }
            finally
            {
                SwitchConnection.InboundConnection -= connectHandler;
                SwitchConnection.StopListener();
            }
        }