コード例 #1
0
        public void should_acquire_and_release_from_multiple_threads()
        {
            // Arrange
            using (var bufferManager = RioBufferManager.Allocate(128, 64))
            {
                // Act
                var tasks = new Task[6];
                for (var i = 0; i < tasks.Length; i++)
                {
                    tasks[i] = Task.Run(() =>
                    {
                        for (var j = 0; j < 50 * 1000; j++)
                        {
                            var buffers = new RioBuffer[10];
                            for (var k = 0; k < buffers.Length; k++)
                            {
                                buffers[k] = bufferManager.AcquireBuffer(TimeSpan.FromMilliseconds(100));
                            }

                            for (var k = 0; k < buffers.Length; k++)
                            {
                                bufferManager.ReleaseBuffer(buffers[k]);
                            }
                        }
                    });
                }

                // Assert
                var allTaskAreCompleted = Task.WaitAll(tasks, TimeSpan.FromSeconds(3));
                Assert.IsTrue(allTaskAreCompleted);
            }
        }
コード例 #2
0
        public void should_unblock_acquiring_and_throw_completing_acquiring()
        {
            // Arrange
            using (var bufferManager = RioBufferManager.Allocate(1, 64))
            {
                bufferManager.AcquireBuffer(TimeSpan.FromMilliseconds(10));

                var acquiringTask = Task.Run(() =>
                {
                    try
                    {
                        bufferManager.AcquireBuffer(TimeSpan.FromMilliseconds(200));
                        return(null);
                    }
                    catch (Exception ex)
                    {
                        return(ex);
                    }
                });

                // Act
                bufferManager.CompleteAcquiring();

                // Assert
                Assert.IsInstanceOf <InvalidOperationException>(acquiringTask.Result);
            }
        }
コード例 #3
0
        public void should_write_and_read_to_and_from_all_acquired_buffers()
        {
            // Arrange
            using (var bufferManager = RioBufferManager.Allocate(10, 64))
            {
                // Act / Assert
                var bufferIds = new List <int>();
                for (var i = 0; i < 10; i++)
                {
                    var buffer = bufferManager.AcquireBuffer(TimeSpan.FromMilliseconds(10));

                    for (var j = 0; j < buffer.Length; j++)
                    {
                        *(buffer.Data + j) = (byte)i;
                    }

                    bufferIds.Add(buffer.Id);
                }

                for (var i = 0; i < bufferIds.Count; i++)
                {
                    var bufferId = bufferIds[i];
                    var buffer   = bufferManager.ReadBuffer(bufferId);
                    for (var j = 0; j < buffer.Length; j++)
                    {
                        var value = *(buffer.Data + j);
                        Assert.AreEqual(i, value);
                    }
                }
            }
        }
コード例 #4
0
 public void SetUp()
 {
     WinSock.EnsureIsInitialized();
     _bufferManager = RioBufferManager.Allocate(64, 64);
     _configuration = new RioConfiguration {
         BufferAcquisitionTimeout = TimeSpan.FromMilliseconds(10)
     };
 }
コード例 #5
0
        public void should_block_when_no_buffer_is_available()
        {
            // Arrange
            using (var bufferManager = RioBufferManager.Allocate(1, 64))
            {
                bufferManager.AcquireBuffer(TimeSpan.FromMilliseconds(10));

                // Act / Assert
                Assert.Throws <TimeoutException>(() => bufferManager.AcquireBuffer(TimeSpan.FromMilliseconds(10)));
            }
        }
コード例 #6
0
        public void should_not_acquire_buffer_when_acquiring_is_completed()
        {
            // Arrange
            using (var bufferManager = RioBufferManager.Allocate(1, 64))
            {
                bufferManager.CompleteAcquiring();

                // Act / Assert
                Assert.Throws <InvalidOperationException>(() => bufferManager.AcquireBuffer(TimeSpan.FromMilliseconds(10)));
            }
        }
コード例 #7
0
        public void should_acquire_buffer()
        {
            // Arrange
            using (var bufferManager = RioBufferManager.Allocate(10, 64))
            {
                // Act
                var buffer = bufferManager.AcquireBuffer(TimeSpan.FromMilliseconds(10));

                // Assert
                Assert.AreEqual(64, buffer.Length);
            }
        }
コード例 #8
0
        public void should_read_an_acquired_buffer()
        {
            // Arrange
            using (var bufferManager = RioBufferManager.Allocate(10, 64))
            {
                var buffer = bufferManager.AcquireBuffer(TimeSpan.FromMilliseconds(10));
                WriteTo(buffer, 42);

                // Act
                var readValue = ReadFrom(bufferManager.ReadBuffer(buffer.Id));

                // Assert
                Assert.AreEqual(42, readValue);
            }
        }
コード例 #9
0
        public void should_allow_acquiring_again_on_reset()
        {
            // Arrange
            using (var bufferManager = RioBufferManager.Allocate(1, 64))
            {
                bufferManager.CompleteAcquiring();

                // Act
                bufferManager.Reset();

                // Assert
                var buffer = bufferManager.AcquireBuffer(TimeSpan.FromMilliseconds(10));
                Assert.IsNotNull(buffer);
            }
        }
コード例 #10
0
        public void should_release_buffers_on_reset()
        {
            // Arrange
            using (var bufferManager = RioBufferManager.Allocate(1, 64))
            {
                bufferManager.AcquireBuffer(TimeSpan.FromMilliseconds(10));

                // Act
                bufferManager.Reset();

                // Assert
                var buffer = bufferManager.AcquireBuffer(TimeSpan.FromMilliseconds(10));
                Assert.IsNotNull(buffer);
            }
        }
コード例 #11
0
        public void should_reset_buffer_on_release()
        {
            // Arrange
            using (var bufferManager = RioBufferManager.Allocate(1, 64))
            {
                var buffer = bufferManager.AcquireBuffer(TimeSpan.FromMilliseconds(10));
                buffer.DataLength = 12;

                // Act
                bufferManager.ReleaseBuffer(buffer);

                // Assert
                buffer = bufferManager.ReadBuffer(buffer.Id);
                Assert.AreEqual(64, buffer.DataLength);
            }
        }
コード例 #12
0
        public RioSession(int sessionId, ISessionConfiguration configuration, RioCompletionQueue sendingCompletionQueue, RioCompletionQueue receivingCompletionQueue, SerializationEngine serializationEngine)
        {
            Id             = sessionId;
            _configuration = configuration;

            _sendingCompletionQueue   = sendingCompletionQueue;
            _receivingCompletionQueue = receivingCompletionQueue;

            _sendingBufferManager   = RioBufferManager.Allocate(configuration.SendingBufferCount, _configuration.SendingBufferLength);
            _receivingBufferManager = RioBufferManager.Allocate(configuration.ReceivingBufferCount, _configuration.ReceivingBufferLength);

            _messageFramer = new MessageFramer(_receivingBufferManager);

            _threadLocalReceivingContext = new ThreadLocal <ReceivingContext>(() => new ReceivingContext(serializationEngine.Encoding));
            _threadLocalSendingContext   = new ThreadLocal <SendingContext>(() => new SendingContext(configuration, _sendingBufferManager, serializationEngine.Encoding));

            _serializationEngine = serializationEngine;
        }
コード例 #13
0
        public void should_unblock_when_a_buffer_is_available_again()
        {
            // Arrange
            using (var bufferManager = RioBufferManager.Allocate(1, 64))
            {
                var releaseBufferSignal = new AutoResetEvent(false);
                var bufferAcquiredFirst = bufferManager.AcquireBuffer(TimeSpan.FromMilliseconds(10));

                var acquiringTask = Task.Run(() =>
                {
                    try
                    {
                        bufferManager.AcquireBuffer(TimeSpan.FromMilliseconds(10));
                    }
                    catch (TimeoutException)
                    {
                        releaseBufferSignal.Set();
                    }

                    try
                    {
                        return(bufferManager.AcquireBuffer(TimeSpan.FromMilliseconds(10)));
                    }
                    catch (TimeoutException)
                    {
                        return(null);
                    }
                });

                if (!releaseBufferSignal.WaitOne(TimeSpan.FromMilliseconds(100)))
                {
                    Assert.Fail();
                }

                // Act
                bufferManager.ReleaseBuffer(bufferAcquiredFirst);

                // Assert
                var bufferAcquiredSecond = acquiringTask.Result;
                Assert.IsNotNull(bufferAcquiredSecond);
            }
        }