コード例 #1
0
        public void TestSetFilterByControlMessage()
        {
            // create messages to send to the queue
            IPipeMessage message = new Message(Message.NORMAL, new { width = 100, height = 20 });

            // create filter, attach an anonymous listener to the filter output to receive the message,
            // pass in an anonymous function and an anonymous parameter object
            IPipeFitting filter = new Filter("scale", new PipeListener(this, CallBackMethod), (IPipeMessage msg, object _params) => {
                msg.Header = new
                {
                    width  = ((dynamic)msg.Header).width * ((dynamic)_params).factor,
                    height = ((dynamic)msg.Header).height * ((dynamic)_params).factor
                };
                return(true);
            }, new { factor = 10 });

            // create setFilter control message
            IPipeMessage setFilterMessage = new FilterControlMessage(FilterControlMessage.SET_FILTER, "scale", (IPipeMessage msg, object _params) =>
            {
                msg.Header = new
                {
                    width  = ((dynamic)msg.Header).width / ((dynamic)_params).factor,
                    height = ((dynamic)msg.Header).height / ((dynamic)_params).factor
                };
                return(true);
            });

            // write filter control message to the filter
            bool setFilterWritten = filter.Write(setFilterMessage);

            // write normal message to the filter
            bool written = filter.Write(message);

            // test assertions
            Assert.IsTrue(message is IPipeMessage, "Expecting message is IPipeMessage");
            Assert.IsTrue(filter is Filter, "Expecting filter is Filter");
            Assert.IsTrue(setFilterWritten, "Expecting wrote message to filter");
            Assert.IsTrue(written, "Expecting wrote normal message to filter");
            Assert.IsTrue(messagesReceived.Count == 1, "Expecting received 1 messages");

            // test filtered message assertions (message filtered with overridden filter function)
            IPipeMessage received = messagesReceived[0];

            messagesReceived.RemoveAt(0);
            Assert.IsTrue(received is Message, "Expecting received is Message");
            Assert.IsTrue(received == message, "Expecting received == message");
            Assert.IsTrue(((dynamic)received.Header).width == 10, "Expecting ((dynamic)received2.Header).width == 10");
            Assert.IsTrue(((dynamic)received.Header).height == 2, ((dynamic)received.Header).height + "Expecting ((dynamic)received2.Header).height == 2");
        }
コード例 #2
0
        public void TestBypassAndFilterModeToggle()
        {
            // create messages to send to the queue
            IPipeMessage message = new Message(Message.NORMAL, new { width = 10, height = 2 });

            // create filter, attach an anonymous listener to the filter output to receive the message,
            // pass in an anonymous function an parameter object
            IPipeFitting filter = new Filter("scale", new PipeListener(this, CallBackMethod), (IPipeMessage msg, object _params) => {
                msg.Header = new
                {
                    width  = ((dynamic)msg.Header).width * ((dynamic)_params).factor,
                    height = ((dynamic)msg.Header).height * ((dynamic)_params).factor
                };
                return(true);
            }, new { factor = 10 });

            // create bypass control message
            IPipeMessage byPassMessage = new FilterControlMessage(FilterControlMessage.BYPASS, "scale");

            // write bypass control message to the filter
            bool bypassWritten = filter.Write(byPassMessage);

            // write normal message to the filter
            bool written1 = filter.Write(message);

            // test assertions
            Assert.IsTrue(message is IPipeMessage, "Expecting message is IPipeMessage");
            Assert.IsTrue(filter is Filter, "Expecting filter is Filter");
            Assert.IsTrue(bypassWritten, "Expecting wrote bypass message to filter");
            Assert.IsTrue(written1, "Expecting wrote normal message to filter");
            Assert.IsTrue(messagesReceived.Count == 1, "Expecting received 1 messages");

            // test filtered message assertions (no change to message)
            IPipeMessage received1 = messagesReceived[0];

            messagesReceived.RemoveAt(0);
            Assert.IsTrue(received1 == message, "Expecting received == message");
            Assert.IsTrue(((dynamic)received1.Header).width == 10, "Expecting ((dynamic)received1.Header).width == 10");
            Assert.IsTrue(((dynamic)received1.Header).height == 2, "((dynamic)received1.Header).height == 2");

            // create filter control message
            IPipeMessage filterMessage = new FilterControlMessage(FilterControlMessage.FILTER, "scale");

            // write bypass control message to the filter
            bool filterWritten = filter.Write(filterMessage);

            // write normal message to the filter again
            bool written2 = filter.Write(message);

            // test assertions
            Assert.IsTrue(filterWritten, "Expecing worte filter message to filter");
            Assert.IsTrue(written2, "Expecting wrote normal message to filter");
            Assert.IsTrue(messagesReceived.Count == 1, "Expecing received 1 messages");

            // test filtered message assertions (message filtered)
            IPipeMessage received2 = messagesReceived[0];

            messagesReceived.RemoveAt(0);
            Assert.IsTrue(received2 is IPipeMessage, "Expecting received 2 is IPipeMessage");
            Assert.IsTrue(received2 == message, "received2 == message");
            Assert.IsTrue(((dynamic)received2.Header).width == 100, "Expecting ((dynamic)received2.Header).width == 100");
            Assert.IsTrue(((dynamic)received2.Header).height == 20, "Expecting ((dynamic)received2.Header).height == 20");
        }