public void FillToBrinkReceiveAndFillAgain()
        {
            System.Diagnostics.Debug.WriteLine("FillToBrinkReceiveAndFillAgain");
            var initialSize = 24;

            using (var writeQueue = new MemoryMappedQueue(initialSize, true, AccessName))
            {
                using (var readQueue = new MemoryMappedQueue(initialSize, false, AccessName))
                {
                    // Note that each enqueue operation writes 8 bytes (4 for data and 4 for its length)
                    byte[] testData = { 1, 2, 3, 4 }; // 4 + 4
                    writeQueue.Enqueue(testData);     // 8 bytes filled

                    System.Diagnostics.Debug.WriteLine(writeQueue.Diagnostics());
                    System.Diagnostics.Debug.WriteLine(readQueue.Diagnostics());

                    byte[] testData2 = { 5, 6, 7, 8 }; // 4 + 4
                    writeQueue.Enqueue(testData2);     // 16 bytes filled

                    System.Diagnostics.Debug.WriteLine(writeQueue.Diagnostics());
                    System.Diagnostics.Debug.WriteLine(readQueue.Diagnostics());

                    byte[] testData3 = { 9, 10, 11, 12 }; // 4 + 4
                    writeQueue.Enqueue(testData3);        // 24 bytes filled

                    System.Diagnostics.Debug.WriteLine(writeQueue.Diagnostics());
                    System.Diagnostics.Debug.WriteLine(readQueue.Diagnostics());

                    var receivedData = readQueue.Dequeue();
                    Assert.Equal(testData, receivedData);

                    System.Diagnostics.Debug.WriteLine(writeQueue.Diagnostics());
                    System.Diagnostics.Debug.WriteLine(readQueue.Diagnostics());

                    byte[] testData4 = { 1, 2, 3, 4 }; // 4 + 4
                    writeQueue.Enqueue(testData4);     // 8 bytes filled in the overflow

                    System.Diagnostics.Debug.WriteLine(writeQueue.Diagnostics());
                    System.Diagnostics.Debug.WriteLine(readQueue.Diagnostics());

                    var receivedData2 = readQueue.Dequeue();
                    Assert.Equal(testData2, receivedData2);

                    System.Diagnostics.Debug.WriteLine(writeQueue.Diagnostics());
                    System.Diagnostics.Debug.WriteLine(readQueue.Diagnostics());

                    byte[] testData5 = { 5, 6, 7, 8 }; // 4 + 4
                    writeQueue.Enqueue(testData5);     // 16 bytes filled in the overflow

                    System.Diagnostics.Debug.WriteLine(writeQueue.Diagnostics());
                    System.Diagnostics.Debug.WriteLine(readQueue.Diagnostics());

                    var receivedData3 = readQueue.Dequeue();
                    Assert.Equal(testData3, receivedData3);

                    System.Diagnostics.Debug.WriteLine(writeQueue.Diagnostics());
                    System.Diagnostics.Debug.WriteLine(readQueue.Diagnostics());
                }
            }
        }
        public void MultipleDataTransfersWithOverflow()
        {
            System.Diagnostics.Debug.WriteLine("MultipleDataTransfersWithOverflow");
            var initialSize = 32;

            using (var writeQueue = new MemoryMappedQueue(initialSize, true, AccessName))
            {
                using (var readQueue = new MemoryMappedQueue(initialSize, false, AccessName))
                {
                    byte[] testData = { 1, 2, 3, 4, 5, 6, 7, 8 }; // 8 + 4
                    writeQueue.Enqueue(testData);                 // 12 B filled
                    var receivedData = readQueue.Dequeue();
                    Assert.Equal(testData, receivedData);

                    System.Diagnostics.Debug.WriteLine(writeQueue.Diagnostics());
                    System.Diagnostics.Debug.WriteLine(readQueue.Diagnostics());

                    byte[] testData2 = { 9, 10, 11, 12, 13, 14, 15, 16 }; // 8 + 4
                    writeQueue.Enqueue(testData2);                        // 24 B filled
                    var receivedData2 = readQueue.Dequeue();
                    Assert.Equal(testData2, receivedData2);

                    System.Diagnostics.Debug.WriteLine(writeQueue.Diagnostics());
                    System.Diagnostics.Debug.WriteLine(readQueue.Diagnostics());

                    byte[] testData3 = { 17, 18, 19, 20 }; // 4 + 4
                    writeQueue.Enqueue(testData3);         // 32 B filled
                    var receivedData3 = readQueue.Dequeue();
                    Assert.Equal(testData3, receivedData3);

                    System.Diagnostics.Debug.WriteLine(writeQueue.Diagnostics());
                    System.Diagnostics.Debug.WriteLine(readQueue.Diagnostics());

                    byte[] testData4 = { 21, 22, 23, 24 }; // 4 + 4
                    writeQueue.Enqueue(testData4);         // 40 B filled
                    var receivedData4 = readQueue.Dequeue();
                    Assert.Equal(testData4, receivedData4);

                    System.Diagnostics.Debug.WriteLine(writeQueue.Diagnostics());
                    System.Diagnostics.Debug.WriteLine(readQueue.Diagnostics());
                }
            }
        }
Пример #3
0
        public void OperationPermissionTest()
        {
            var initialSize = 50;

            using (var writeQueue = new MemoryMappedQueue(initialSize, true, AccessName))
            {
                Assert.Throws(typeof(InvalidOperationException), () => writeQueue.Dequeue());
                using (var readQueue = new MemoryMappedQueue(initialSize, false, AccessName))
                {
                    Assert.Throws(typeof(InvalidOperationException), () => readQueue.Enqueue(null));
                }
            }
        }
        public void EmptyQueueGivesNull()
        {
            var initialSize = 50;

            using (var writeQueue = new MemoryMappedQueue(initialSize, true, AccessName))
            {
                using (var readQueue = new MemoryMappedQueue(initialSize, false, AccessName))
                {
                    var receivedData = readQueue.Dequeue();

                    Assert.Null(receivedData);
                }
            }
        }
        public void SimpleDataTransfer()
        {
            var initialSize = 50;

            using (var writeQueue = new MemoryMappedQueue(initialSize, true, AccessName))
            {
                using (var readQueue = new MemoryMappedQueue(initialSize, false, AccessName))
                {
                    byte[] testData = new byte[3];
                    writeQueue.Enqueue(testData);
                    var receivedData = readQueue.Dequeue();

                    Assert.Equal(testData, receivedData);
                }
            }
        }
        public void EncodedStringTransfer(string input)
        {
            var initialSize = 1000;

            using (var writeQueue = new MemoryMappedQueue(initialSize, true, AccessName))
            {
                using (var readQueue = new MemoryMappedQueue(initialSize, false, AccessName))
                {
                    byte[] testData = System.Text.Encoding.Unicode.GetBytes(input);
                    writeQueue.Enqueue(testData);
                    var receivedData   = readQueue.Dequeue();
                    var receivedString = System.Text.Encoding.Unicode.GetString(receivedData);
                    Assert.Equal(input, receivedString);
                }
            }
        }
        public void QueueAcceptsZeros()
        {
            var initialSize = 24;

            using (var writeQueue = new MemoryMappedQueue(initialSize, true, AccessName))
            {
                using (var readQueue = new MemoryMappedQueue(initialSize, false, AccessName))
                {
                    // Note that each enqueue operation writes 8 bytes (4 for data and 4 for its length)
                    byte[] testData = { 0 };      // 4 + 1
                    writeQueue.Enqueue(testData); // 5 bytes filled

                    var receivedData = readQueue.Dequeue();
                    Assert.Equal(testData, receivedData);
                }
            }
        }
        public void StructTransfer()
        {
            System.Diagnostics.Debug.WriteLine("StructTransfer");
            var initialSize = 1000;

            using (var writeQueue = new MemoryMappedQueue(initialSize, true, AccessName))
            {
                using (var readQueue = new MemoryMappedQueue(initialSize, false, AccessName))
                {
                    List <double> numbers = new List <double>();
                    List <double> receivedNumbers;
                    for (int i = 0; i < 5; i++)
                    {
                        numbers.Add(Math.PI * i);
                    }

                    byte[] testData;;

                    BinaryFormatter bf = new BinaryFormatter();
                    using (MemoryStream ms = new MemoryStream())
                    {
                        bf.Serialize(ms, numbers);
                        testData = ms.ToArray();
                    }

                    writeQueue.Enqueue(testData);
                    var receivedData = readQueue.Dequeue();

                    System.Diagnostics.Debug.WriteLine(writeQueue.Diagnostics());
                    System.Diagnostics.Debug.WriteLine(readQueue.Diagnostics());

                    using (MemoryStream ms = new MemoryStream())
                    {
                        ms.Write(receivedData, 0, receivedData.Length);
                        ms.Seek(0, SeekOrigin.Begin);
                        receivedNumbers = (List <double>)bf.Deserialize(ms);
                    }
                    Assert.Equal(numbers, receivedNumbers);
                }
            }
        }
        public void PrimitiveStringTransfer(string input)
        {
            var initialSize = 1000;

            using (var writeQueue = new MemoryMappedQueue(initialSize, true, AccessName))
            {
                using (var readQueue = new MemoryMappedQueue(initialSize, false, AccessName))
                {
                    byte[] testData = new byte[input.Length * sizeof(char)];
                    Buffer.BlockCopy(input.ToCharArray(), 0, testData, 0, testData.Length);

                    writeQueue.Enqueue(testData);
                    var receivedData = readQueue.Dequeue();

                    var receivedCharacters = new char[receivedData.Length / sizeof(char)];
                    Buffer.BlockCopy(receivedData, 0, receivedCharacters, 0, receivedData.Length);
                    var receivedString = new string(receivedCharacters);
                    Assert.Equal(input, receivedString);
                }
            }
        }