Example #1
0
        public void Should_have_ability_to_send_multiple_messages()
        {
            const string mmfName = "Local\\test";
            const string message1 = "message1";
            const string message2 = "message2";
            string       result1 = null, result2 = null;

            RunToCompletion(
                () =>
            {
                using (var messageSender = new MemoryMappedFileMessageSender(mmfName))
                {
                    messageSender.SendMessage(x => WriteString(x, message1));
                    messageSender.SendMessage(x => WriteString(x, message2));
                }
            },
                () =>
            {
                using (var messageReceiver = new MemoryMappedFileMessageReceiver(mmfName))
                {
                    result1 = messageReceiver.ReceiveMessage(ReadString);
                    result2 = messageReceiver.ReceiveMessage(ReadString);
                }
            });

            result1.ShouldEqual(message1);
            result2.ShouldEqual(message2);
        }
Example #2
0
        public void Should_be_able_to_send_messages_much_larger_than_size_of_stream()
        {
            const string mmfName = "Local\\test";
            var          message = RandomBuffer((int)(Environment.SystemPageSize * 2.5));

            byte[] result = null;

            RunToCompletion(
                () =>
            {
                using (var messageSender = new MemoryMappedFileMessageSender(mmfName))
                {
                    messageSender.SendMessage(x => x.Write(message, 0, message.Length));
                }
            },
                () =>
            {
                using (var messageReceiver = new MemoryMappedFileMessageReceiver(mmfName))
                {
                    result = messageReceiver.ReceiveMessage(ReadBytes);
                }
            });

            result.Length.ShouldEqual(message.Length);

            for (var i = 0; i < result.Length; ++i)
            {
                if (result[i] != message[i])
                {
                    throw new EqualException(message[i], result[i], string.Format("Buffer doesn't match at position {0} (Expected {1} was {2})", i, message[i], result[i]));
                }
            }
        }
Example #3
0
        public void Should_be_able_to_use_binary_formatter()
        {
            const string mmfName = "Local\\test";
            var          message = new Foo {
                Bar = "FooBar"
            };
            Foo result    = null;
            var formatter = new BinaryFormatter();

            RunToCompletion(
                () =>
            {
                using (var messageSender = new MemoryMappedFileMessageSender(mmfName))
                {
                    messageSender.SendMessage(x => formatter.Serialize(x, message));
                }
            },
                () =>
            {
                using (var messageReceiver = new MemoryMappedFileMessageReceiver(mmfName))
                {
                    result = (Foo)messageReceiver.ReceiveMessage(formatter.Deserialize);
                }
            });

            result.Bar.ShouldEqual(message.Bar);
        }
Example #4
0
        public void Should_be_able_to_cancel_message_sending_with_a_cancellation_token()
        {
            const string mmfName          = "Local\\test";
            var          messageCancelled = new EventWaitHandle(false, EventResetMode.ManualReset, mmfName + "_MessageCancelled");

            messageCancelled.Set();

            using (var messageReceiver = new MemoryMappedFileMessageSender(mmfName))
                using (var cancellationTokenSource = new CancellationTokenSource())
                {
                    var task = new Task(() => messageReceiver.SendMessage(x => WriteString(x, "message"), cancellationTokenSource.Token));

                    task.Start();

                    var isSet = true;

                    while (isSet)
                    {
                        isSet = messageCancelled.WaitOne(0);
                    }

                    cancellationTokenSource.Cancel();

                    task.Wait();
                }
        }
Example #5
0
        public void Should_cancel_a_message_if_exception_occurs_during_receiving()
        {
            const string mmfName   = "Local\\test";
            var          result    = "not null";
            Exception    exception = null;

            RunToCompletion(
                () =>
            {
                using (var messageSender = new MemoryMappedFileMessageSender(mmfName))
                {
                    messageSender.SendMessage(x => WriteString(x, "message"));
                }
            },
                () =>
            {
                using (var messageReceiver = new MemoryMappedFileMessageReceiver(mmfName))
                {
                    try
                    {
                        result = messageReceiver.ReceiveMessage <string>(x =>
                        {
                            ReadString(x);
                            throw new Exception();
                        });
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                    }
                }
            });

            exception.ShouldNotBeNull();
            result.ShouldNotBeNull();
        }
Example #6
0
        public void Should_cancel_a_message_if_exception_occurs_during_sending_and_return_null_on_receiving_end()
        {
            const string mmfName   = "Local\\test";
            string       result    = null;
            Exception    exception = null;

            RunToCompletion(
                () =>
            {
                using (var messageSender = new MemoryMappedFileMessageSender(mmfName))
                {
                    try
                    {
                        messageSender.SendMessage(x =>
                        {
                            WriteString(x, "message");
                            throw new Exception();
                        });
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                    }
                }
            },
                () =>
            {
                using (var messageReceiver = new MemoryMappedFileMessageReceiver(mmfName))
                {
                    result = messageReceiver.ReceiveMessage(ReadString);
                }
            });

            exception.ShouldNotBeNull();
            result.ShouldBeNull();
        }
Example #7
0
        public void Should_have_ability_to_send_multiple_messages()
        {
            const string mmfName = "Local\\test";
            const string message1 = "message1";
            const string message2 = "message2";
            string result1 = null, result2 = null;

            RunToCompletion(
                () =>
                {
                    using (var messageSender = new MemoryMappedFileMessageSender(mmfName))
                    {
                        messageSender.SendMessage(x => WriteString(x, message1));
                        messageSender.SendMessage(x => WriteString(x, message2));
                    }
                },
                () =>
                {
                    using (var messageReceiver = new MemoryMappedFileMessageReceiver(mmfName))
                    {
                        result1 = messageReceiver.ReceiveMessage(ReadString);
                        result2 = messageReceiver.ReceiveMessage(ReadString);
                    }
                });

            result1.ShouldEqual(message1);
            result2.ShouldEqual(message2);
        }
Example #8
0
        public void Should_cancel_a_message_if_exception_occurs_during_sending_and_return_null_on_receiving_end()
        {
            const string mmfName = "Local\\test";
            string result = null;
            Exception exception = null;

            RunToCompletion(
                () =>
                {
                    using (var messageSender = new MemoryMappedFileMessageSender(mmfName))
                    {
                        try
                        {
                            messageSender.SendMessage(x =>
                            {
                                WriteString(x, "message");
                                throw new Exception();
                            });
                        }
                        catch (Exception ex)
                        {
                            exception = ex;
                        }
                    }
                },
                () =>
                {
                    using (var messageReceiver = new MemoryMappedFileMessageReceiver(mmfName))
                    {
                        result = messageReceiver.ReceiveMessage(ReadString);
                    }
                });

            exception.ShouldNotBeNull();
            result.ShouldBeNull();
        }
Example #9
0
        public void Should_cancel_a_message_if_exception_occurs_during_receiving()
        {
            const string mmfName = "Local\\test";
            var result = "not null";
            Exception exception = null;

            RunToCompletion(
                () =>
                {
                    using (var messageSender = new MemoryMappedFileMessageSender(mmfName))
                    {
                        messageSender.SendMessage(x => WriteString(x, "message"));
                    }
                },
                () =>
                {
                    using (var messageReceiver = new MemoryMappedFileMessageReceiver(mmfName))
                    {
                        try
                        {
                            result = messageReceiver.ReceiveMessage<string>(x =>
                            {
                                ReadString(x);
                                throw new Exception();
                            });
                        }
                        catch (Exception ex)
                        {
                            exception = ex;
                        }
                    }
                });

            exception.ShouldNotBeNull();
            result.ShouldNotBeNull();
        }
Example #10
0
        public void Should_be_able_to_use_binary_formatter()
        {
            const string mmfName = "Local\\test";
            var message = new Foo {Bar = "FooBar"};
            Foo result = null;
            var formatter = new BinaryFormatter();

            RunToCompletion(
                () =>
                {
                    using (var messageSender = new MemoryMappedFileMessageSender(mmfName))
                    {
                        messageSender.SendMessage(x => formatter.Serialize(x, message));
                    }
                },
                () =>
                {
                    using (var messageReceiver = new MemoryMappedFileMessageReceiver(mmfName))
                    {
                        result = (Foo) messageReceiver.ReceiveMessage(formatter.Deserialize);
                    }
                });

            result.Bar.ShouldEqual(message.Bar);
        }
Example #11
0
        public void Should_be_able_to_send_messages_much_larger_than_size_of_stream()
        {
            const string mmfName = "Local\\test";
            var message = RandomBuffer((int) (Environment.SystemPageSize*2.5));
            byte[] result = null;

            RunToCompletion(
                () =>
                {
                    using (var messageSender = new MemoryMappedFileMessageSender(mmfName))
                    {
                        messageSender.SendMessage(x => x.Write(message, 0, message.Length));
                    }
                },
                () =>
                {
                    using (var messageReceiver = new MemoryMappedFileMessageReceiver(mmfName))
                    {
                        result = messageReceiver.ReceiveMessage(ReadBytes);
                    }
                });

            result.Length.ShouldEqual(message.Length);

            for (var i = 0; i < result.Length; ++i)
            {
                if (result[i] != message[i])
                {
                    throw new EqualException(message[i], result[i], string.Format("Buffer doesn't match at position {0} (Expected {1} was {2})", i, message[i], result[i]));
                }
            }
        }
Example #12
0
        public void Should_be_able_to_cancel_message_sending_with_a_cancellation_token()
        {
            const string mmfName = "Local\\test";
            var messageCancelled = new EventWaitHandle(false, EventResetMode.ManualReset, mmfName + "_MessageCancelled");

            messageCancelled.Set();

            using (var messageReceiver = new MemoryMappedFileMessageSender(mmfName))
            using (var cancellationTokenSource = new CancellationTokenSource())
            {
                var task = new Task(() => messageReceiver.SendMessage(x => WriteString(x, "message"), cancellationTokenSource.Token));

                task.Start();

                var isSet = true;

                while (isSet)
                    isSet = messageCancelled.WaitOne(0);

                cancellationTokenSource.Cancel();

                task.Wait();
            }
        }