Exemplo n.º 1
0
 public void CloseWhileQueueRunsWithFailedTask()
 {
     int x = 0;
     var loggerFactory = new Mock<ILoggerFactory>();
     var perfCounters = new SignalRPerformanceCounterManager(loggerFactory.Object);
     var logger = new Mock<ILogger>();
     var stream = new ScaleoutStream(logger.Object, "0", QueuingBehavior.Always, 1000, perfCounters);
     stream.Open();
     stream.Send(async _ =>
     {
         await Task.Delay(50);
         x++;
     },
     null);
     stream.Send(async _ =>
     {
         await Task.Delay(50);
         await TaskAsyncHelper.FromError(new Exception());
     },
     null);
     stream.Send(async _ =>
     {
         await Task.Delay(50);
         x++;
     },
     null);
     stream.Close();
     Assert.Equal(1, x);
 }
Exemplo n.º 2
0
        public ScaleoutStreamManager(Func <int, IList <Message>, Task> send,
                                     Action <int, ulong, ScaleoutMessage> receive,
                                     int streamCount,
                                     TraceSource trace,
                                     IPerformanceCounterManager performanceCounters,
                                     ScaleoutConfiguration configuration)
        {
            _streams = new ScaleoutStream[streamCount];
            _send    = send;
            _receive = receive;

            var receiveMapping = new ScaleoutMappingStore[streamCount];

            performanceCounters.ScaleoutStreamCountTotal.RawValue     = streamCount;
            performanceCounters.ScaleoutStreamCountBuffering.RawValue = streamCount;
            performanceCounters.ScaleoutStreamCountOpen.RawValue      = 0;

            for (int i = 0; i < streamCount; i++)
            {
                _streams[i]       = new ScaleoutStream(trace, "Stream(" + i + ")", configuration.MaxQueueLength, performanceCounters);
                receiveMapping[i] = new ScaleoutMappingStore();
            }

            Streams = new ReadOnlyCollection <ScaleoutMappingStore>(receiveMapping);
        }
Exemplo n.º 3
0
 public void CloseWhileQueueRuns()
 {
     int x = 0;
     var perfCounters = new Microsoft.AspNet.SignalR.Infrastructure.PerformanceCounterManager();
     var stream = new ScaleoutStream(new TraceSource("Queue"), "0", 1000, perfCounters);
     stream.Open();
     stream.Send(async _ =>
     {
         await Task.Delay(50);
         x++;
     },
     null);
     stream.Send(async _ =>
     {
         await Task.Delay(50);
         x++;
     },
     null);
     stream.Send(async _ =>
     {
         await Task.Delay(50);
         x++;
     },
     null);
     stream.Close();
     Assert.Equal(3, x);
 }
Exemplo n.º 4
0
        public void SendsBeforeOpenedRunOnceOpenedWhenQueuingBehaviorInitialOnly()
        {
            var perfCounters = new Microsoft.AspNet.SignalR.Infrastructure.PerformanceCounterManager();
            var stream = new ScaleoutStream(new TraceSource("Queue"), "0", QueuingBehavior.InitialOnly, 1000, perfCounters);
            
            int x = 0;

            stream.Send(_ =>
            {
                x++;
                return TaskAsyncHelper.Empty;
            },
            null);

            Task task = stream.Send(_ =>
            {
                x++;
                return TaskAsyncHelper.Empty;
            },
            null);

            stream.Open();

            task.Wait();
            
            Assert.Equal(2, x);
        }
Exemplo n.º 5
0
        public void SendAfterOpenAndAfterErrorThrows()
        {
            var perfCounters = new Microsoft.AspNet.SignalR.Infrastructure.PerformanceCounterManager();
            var stream = new ScaleoutStream(new TraceSource("Queue"), "0", QueuingBehavior.InitialOnly, 1000, perfCounters);

            int x = 0;

            var firstSend = stream.Send(_ =>
            {
                x++;
                return TaskAsyncHelper.Empty;
            },
            null);

            stream.Open();
            
            // Wait on the first send to drain
            firstSend.Wait();

            stream.SetError(new Exception("Failed"));

            Assert.Throws<Exception>(() =>
            {
                stream.Send(_ =>
                {
                    x++;
                    return TaskAsyncHelper.Empty;
                },
                null).Wait();
            });

            Assert.Equal(1, x);
        }
Exemplo n.º 6
0
        public void OpenAfterErrorRunsQueue()
        {
            int x = 0;
            var perfCounters = new Microsoft.AspNet.SignalR.Infrastructure.PerformanceCounterManager();
            var stream = new ScaleoutStream(new TraceSource("Queue"), "0", 1000, perfCounters);
            stream.Open();
            stream.Send(_ => { throw new InvalidOperationException(); }, null);

            stream.Open();

            stream.Send(_ =>
            {
                x++;
                return TaskAsyncHelper.Empty;
            },
            null);

            var task = stream.Send(_ =>
            {
                x++;
                return TaskAsyncHelper.Empty;
            },
            null);

            task.Wait();

            Assert.Equal(2, x);
        }
Exemplo n.º 7
0
        public void EnqueueWithoutOpenRaisesOnError()
        {
            var perfCounters = new Microsoft.AspNet.SignalR.Infrastructure.PerformanceCounterManager();
            var stream = new ScaleoutStream(new TraceSource("Queue"), "0", 1000, perfCounters);

            Assert.Throws<InvalidOperationException>(() => stream.Send(_ => { throw new InvalidOperationException(); }, null));
        }
        public ScaleoutStreamManager(Func <int, IList <Message>, Task> send,
                                     Action <int, ulong, ScaleoutMessage> receive,
                                     int streamCount,
                                     TraceSource trace,
                                     IPerformanceCounterManager performanceCounters,
                                     ScaleoutConfiguration configuration)
        {
            if (configuration.QueueBehavior != QueuingBehavior.Disabled && configuration.MaxQueueLength == 0)
            {
                throw new InvalidOperationException(Resources.Error_ScaleoutQueuingConfig);
            }

            _streams = new ScaleoutStream[streamCount];
            _send    = send;
            _receive = receive;

            var receiveMapping = new ScaleoutMappingStore[streamCount];

            performanceCounters.ScaleoutStreamCountTotal.RawValue     = streamCount;
            performanceCounters.ScaleoutStreamCountBuffering.RawValue = streamCount;
            performanceCounters.ScaleoutStreamCountOpen.RawValue      = 0;

            for (int i = 0; i < streamCount; i++)
            {
                _streams[i]       = new ScaleoutStream(trace, "Stream(" + i + ")", configuration.QueueBehavior, configuration.MaxQueueLength, performanceCounters);
                receiveMapping[i] = new ScaleoutMappingStore();
            }

            Streams = new ReadOnlyCollection <ScaleoutMappingStore>(receiveMapping);
        }
Exemplo n.º 9
0
        public ScaleoutStreamManager(Func<int, IList<Message>, Task> send,
                                     Action<int, ulong, ScaleoutMessage> receive,
                                     int streamCount,
                                     TraceSource trace,
                                     IPerformanceCounterManager performanceCounters,
                                     ScaleoutConfiguration configuration)
        {
            _streams = new ScaleoutStream[streamCount];
            _send = send;
            _receive = receive;

            var receiveMapping = new ScaleoutMappingStore[streamCount];

            performanceCounters.ScaleoutStreamCountTotal.RawValue = streamCount;
            performanceCounters.ScaleoutStreamCountBuffering.RawValue = streamCount;
            performanceCounters.ScaleoutStreamCountOpen.RawValue = 0;

            for (int i = 0; i < streamCount; i++)
            {
                _streams[i] = new ScaleoutStream(trace, "Stream(" + i + ")", configuration.MaxQueueLength, performanceCounters);
                receiveMapping[i] = new ScaleoutMappingStore();
            }

            Streams = new ReadOnlyCollection<ScaleoutMappingStore>(receiveMapping);
        }
Exemplo n.º 10
0
        public ScaleoutStreamManager(Func<int, IList<Message>, Task> send,
                                     Action<int, ulong, ScaleoutMessage> receive,
                                     int streamCount,
                                     TraceSource trace,
                                     IPerformanceCounterManager performanceCounters,
                                     ScaleoutConfiguration configuration)
        {
            if (configuration.QueueBehavior != QueuingBehavior.Disabled && configuration.MaxQueueLength == 0)
            {
                throw new InvalidOperationException(Resources.Error_ScaleoutQueuingConfig);
            }

            _streams = new ScaleoutStream[streamCount];
            _send = send;
            _receive = receive;

            var receiveMapping = new ScaleoutMappingStore[streamCount];

            performanceCounters.ScaleoutStreamCountTotal.RawValue = streamCount;
            performanceCounters.ScaleoutStreamCountBuffering.RawValue = streamCount;
            performanceCounters.ScaleoutStreamCountOpen.RawValue = 0;

            for (int i = 0; i < streamCount; i++)
            {
                _streams[i] = new ScaleoutStream(trace, "Stream(" + i + ")", configuration.QueueBehavior, configuration.MaxQueueLength, performanceCounters);
                receiveMapping[i] = new ScaleoutMappingStore();
            }

            Streams = new ReadOnlyCollection<ScaleoutMappingStore>(receiveMapping);
        }
Exemplo n.º 11
0
 public SendContext(ScaleoutStream stream, Func <object, Task> send, object state)
 {
     Stream = stream;
     TaskCompletionSource = new TaskCompletionSource <object>();
     _send  = send;
     _state = state;
 }
Exemplo n.º 12
0
 public void BufferAfterClosedEnqueueThrows()
 {
     var perfCounters = new Microsoft.AspNet.SignalR.Infrastructure.PerformanceCounterManager();
     var stream = new ScaleoutStream(new TraceSource("Queue"), "0", 1000, perfCounters);
     stream.Close();
     stream.SetError(new Exception());
     Assert.Throws<Exception>(() => stream.Send(_ => TaskAsyncHelper.Empty, null));
 }
Exemplo n.º 13
0
 public void BufferAfterClosedEnqueueThrows()
 {
     var loggerFactory = new Mock<ILoggerFactory>();
     var perfCounters = new SignalRPerformanceCounterManager(loggerFactory.Object);
     var logger = new Mock<ILogger>();
     var stream = new ScaleoutStream(logger.Object, "0", QueuingBehavior.Always, 1000, perfCounters);
     stream.Close();
     stream.SetError(new Exception());
     Assert.ThrowsAsync<Exception>(() => stream.Send(_ => TaskAsyncHelper.Empty, null));
 }
Exemplo n.º 14
0
        public void SendBeforeOpenDoesNotThrowWhenQueuingBehaviorInitialOnly()
        {
            var perfCounters = new Microsoft.AspNet.SignalR.Infrastructure.PerformanceCounterManager();
            var stream = new ScaleoutStream(new TraceSource("Queue"), "0", QueuingBehavior.InitialOnly, 1000, perfCounters);

            var sendTask = stream.Send(_ => { return TaskAsyncHelper.Empty; }, null);

            stream.Open();

            Assert.DoesNotThrow(() => sendTask.Wait());
        }
Exemplo n.º 15
0
        public void ErrorOnSendThrowsNextTime()
        {
            var perfCounters = new Microsoft.AspNet.SignalR.Infrastructure.PerformanceCounterManager();
            var stream = new ScaleoutStream(new TraceSource("Queue"), "0", 1000, perfCounters);
            stream.Open();

            Task task = stream.Send(_ =>
            {
                throw new InvalidOperationException();
            },
            null);

            Assert.Throws<AggregateException>(() => task.Wait());
            Assert.Throws<InvalidOperationException>(() => stream.Send(_ => TaskAsyncHelper.Empty, null));
        }
Exemplo n.º 16
0
        public void SendAfterOpenDoesBehavesCorrectlyForConfiguredQueuingBehavior(QueuingBehavior queuingBehavior, int expectedCounter)
        {
            var perfCounters = new Mock<IPerformanceCounterManager>();
            var queueLengthCounter = new Mock<IPerformanceCounter>();
            var counterIncrementCount = 0;
            queueLengthCounter.Setup(c => c.Increment()).Callback(() => counterIncrementCount++);
            perfCounters.DefaultValue = DefaultValue.Mock;
            perfCounters.SetReturnsDefault(new NoOpPerformanceCounter());
            perfCounters.SetupAllProperties();
            perfCounters.Setup(pc => pc.ScaleoutSendQueueLength).Returns(queueLengthCounter.Object);

            var stream = new ScaleoutStream(new TraceSource("Queue"), "0", queuingBehavior, 1000, perfCounters.Object);

            stream.Send(_ => TaskAsyncHelper.Empty, null);

            stream.Open();

            stream.Send(_ => TaskAsyncHelper.Empty, null).Wait();

            Assert.Equal(expectedCounter, counterIncrementCount);
        }
Exemplo n.º 17
0
 public void OpenAfterClosedEnqueueThrows()
 {
     var perfCounters = new Microsoft.AspNet.SignalR.Infrastructure.PerformanceCounterManager();
     var stream = new ScaleoutStream(new TraceSource("Queue"), "0", QueuingBehavior.Always, 1000, perfCounters);
     stream.Close();
     stream.Open();
     Assert.Throws<InvalidOperationException>(() => stream.Send(_ => TaskAsyncHelper.Empty, null));
 }
Exemplo n.º 18
0
 public void InitialToClosed()
 {
     var perfCounters = new Microsoft.AspNet.SignalR.Infrastructure.PerformanceCounterManager();
     var stream = new ScaleoutStream(new TraceSource("Queue"), "0", QueuingBehavior.Always, 1000, perfCounters);
     stream.Close();
 }
Exemplo n.º 19
0
        public void InitialToBufferingToOpenToSend()
        {
            int x = 0;
            var perfCounters = new Microsoft.AspNet.SignalR.Infrastructure.PerformanceCounterManager();
            var stream = new ScaleoutStream(new TraceSource("Queue"), "0", QueuingBehavior.Always, 1000, perfCounters);
            stream.SetError(new Exception());
            stream.Open();
            stream.Send(async _ =>
            {
                await Task.Delay(20);
                x++;
            },
            null).Wait();

            Assert.Equal(1, x);
        }
Exemplo n.º 20
0
        public void InitialToBufferingToOpenToSend()
        {
            int x = 0;
            var loggerFactory = new Mock<ILoggerFactory>();
            var perfCounters = new SignalRPerformanceCounterManager(loggerFactory.Object);
            var logger = new Mock<ILogger>();
            var stream = new ScaleoutStream(logger.Object, "0", QueuingBehavior.Always, 1000, perfCounters);
            stream.SetError(new Exception());
            stream.Open();
            stream.Send(async _ =>
            {
                await Task.Delay(20);
                x++;
            },
            null).Wait();

            Assert.Equal(1, x);
        }
Exemplo n.º 21
0
 public void SendAfterCloseThenOpenRemainsClosed()
 {
     var loggerFactory = new Mock<ILoggerFactory>();
     var perfCounters = new SignalRPerformanceCounterManager(loggerFactory.Object);
     var logger = new Mock<ILogger>();
     var stream = new ScaleoutStream(logger.Object, "0", QueuingBehavior.Always, 1000, perfCounters);
     stream.Open();
     stream.Send(_ => Task.Delay(50), null);
     stream.Close();
     stream.Open();
     Assert.ThrowsAsync<InvalidOperationException>(() => stream.Send(_ => TaskAsyncHelper.Empty, null));
 }
Exemplo n.º 22
0
 public void CloseWhileQueueRunsWithFailedTask()
 {
     int x = 0;
     var perfCounters = new Microsoft.AspNet.SignalR.Infrastructure.PerformanceCounterManager();
     var stream = new ScaleoutStream(new TraceSource("Queue"), "0", QueuingBehavior.Always, 1000, perfCounters);
     stream.Open();
     stream.Send(async _ =>
     {
         await Task.Delay(50);
         x++;
     },
     null);
     stream.Send(async _ =>
     {
         await Task.Delay(50);
         await TaskAsyncHelper.FromError(new Exception());
     },
     null);
     stream.Send(async _ =>
     {
         await Task.Delay(50);
         x++;
     },
     null);
     stream.Close();
     Assert.Equal(1, x);
 }
Exemplo n.º 23
0
        public void ErrorOnSendThrowsNextTime()
        {
            var loggerFactory = new Mock<ILoggerFactory>();
            var perfCounters = new SignalRPerformanceCounterManager(loggerFactory.Object);
            var logger = new Mock<ILogger>();
            var stream = new ScaleoutStream(logger.Object, "0", QueuingBehavior.Always, 1000, perfCounters);
            stream.Open();

            Task task = stream.Send(_ =>
            {
                throw new InvalidOperationException();
            },
            null);

            Assert.Throws<AggregateException>(() => task.Wait());
            Assert.ThrowsAsync<InvalidOperationException>(() => stream.Send(_ => TaskAsyncHelper.Empty, null));
        }
Exemplo n.º 24
0
 public void InitialToClosed()
 {
     var loggerFactory = new Mock<ILoggerFactory>();
     var perfCounters = new SignalRPerformanceCounterManager(loggerFactory.Object);
     var logger = new Mock<ILogger>();
     var stream = new ScaleoutStream(logger.Object, "0", QueuingBehavior.Always, 1000, perfCounters);
     stream.Close();
 }
Exemplo n.º 25
0
        public void OpenAfterErrorRunsQueue()
        {
            int x = 0;
            var loggerFactory = new Mock<ILoggerFactory>();
            var perfCounters = new SignalRPerformanceCounterManager(loggerFactory.Object);
            var logger = new Mock<ILogger>();
            var stream = new ScaleoutStream(logger.Object, "0", QueuingBehavior.Always, 1000, perfCounters);
            stream.Open();
            stream.Send(_ => { throw new InvalidOperationException(); }, null);

            stream.Open();

            stream.Send(_ =>
            {
                x++;
                return TaskAsyncHelper.Empty;
            },
            null);

            var task = stream.Send(_ =>
            {
                x++;
                return TaskAsyncHelper.Empty;
            },
            null);

            task.Wait();

            Assert.Equal(2, x);
        }
Exemplo n.º 26
0
        public void OpenQueueErrorOpenQueue()
        {
            int x = 0;
            var loggerFactory = new Mock<ILoggerFactory>();
            var perfCounters = new SignalRPerformanceCounterManager(loggerFactory.Object);
            var logger = new Mock<ILogger>();
            var stream = new ScaleoutStream(logger.Object, "0", QueuingBehavior.Always, 1000, perfCounters);
            stream.Open();
            stream.Send(async _ =>
            {
                await Task.Delay(50);
                x++;
            },
            null);

            Task t1 = stream.Send(async _ =>
            {
                await Task.Delay(50);
                await TaskAsyncHelper.FromError(new Exception());
            },
            null);

            Assert.Throws<AggregateException>(() => t1.Wait());

            stream.Open();

            Task t2 = stream.Send(async _ =>
            {
                await Task.Delay(50);
                x++;
            },
            null);

            t2.Wait();

            Assert.Equal(2, x);
        }
Exemplo n.º 27
0
        public void SendAfterOpenAndAfterErrorThrows()
        {
            var loggerFactory = new Mock<ILoggerFactory>();
            var perfCounters = new SignalRPerformanceCounterManager(loggerFactory.Object);
            var logger = new Mock<ILogger>();
            var stream = new ScaleoutStream(logger.Object, "0", QueuingBehavior.InitialOnly, 1000, perfCounters);

            int x = 0;

            var firstSend = stream.Send(_ =>
            {
                x++;
                return TaskAsyncHelper.Empty;
            },
            null);

            stream.Open();

            // Wait on the first send to drain
            firstSend.Wait();

            stream.SetError(new Exception("Failed"));

            Assert.Throws<Exception>(() =>
            {
                stream.Send(_ =>
                {
                    x++;
                    return TaskAsyncHelper.Empty;
                },
                null).Wait();
            });

            Assert.Equal(1, x);
        }
Exemplo n.º 28
0
 public void SendAfterCloseThenOpenRemainsClosed()
 {
     var perfCounters = new Microsoft.AspNet.SignalR.Infrastructure.PerformanceCounterManager();
     var stream = new ScaleoutStream(new TraceSource("Queue"), "0", 1000, perfCounters);
     stream.Open();
     stream.Send(_ => Task.Delay(50), null);
     stream.Close();
     stream.Open();
     Assert.Throws<InvalidOperationException>(() => stream.Send(_ => TaskAsyncHelper.Empty, null));
 }
Exemplo n.º 29
0
        public void SendBeforeOpenDoesNotThrowWhenQueuingBehaviorInitialOnly()
        {
            var loggerFactory = new Mock<ILoggerFactory>();
            var perfCounters = new SignalRPerformanceCounterManager(loggerFactory.Object);
            var logger = new Mock<ILogger>();
            var stream = new ScaleoutStream(logger.Object, "0", QueuingBehavior.InitialOnly, 1000, perfCounters);

            var sendTask = stream.Send(_ => { return TaskAsyncHelper.Empty; }, null);

            stream.Open();

            // (Does not throw)
            sendTask.Wait();
        }
Exemplo n.º 30
0
        public void OpenQueueErrorOpenQueue()
        {
            int x = 0;
            var perfCounters = new Microsoft.AspNet.SignalR.Infrastructure.PerformanceCounterManager();
            var stream = new ScaleoutStream(new TraceSource("Queue"), "0", QueuingBehavior.Always, 1000, perfCounters);
            stream.Open();
            stream.Send(async _ =>
            {
                await Task.Delay(50);
                x++;
            },
            null);

            Task t1 = stream.Send(async _ =>
            {
                await Task.Delay(50);
                await TaskAsyncHelper.FromError(new Exception());
            },
            null);

            Assert.Throws<AggregateException>(() => t1.Wait());

            stream.Open();

            Task t2 = stream.Send(async _ =>
            {
                await Task.Delay(50);
                x++;
            },
            null);

            t2.Wait();

            Assert.Equal(2, x);
        }
Exemplo n.º 31
0
 public SendContext(ScaleoutStream stream, Func<object, Task> send, object state)
 {
     Stream = stream;
     TaskCompletionSource = new TaskCompletionSource<object>();
     _send = send;
     _state = state;
 }
Exemplo n.º 32
0
        public void SendsBeforeOpenedRunOnceOpenedWhenQueuingBehaviorInitialOnly()
        {
            var loggerFactory = new Mock<ILoggerFactory>();
            var perfCounters = new SignalRPerformanceCounterManager(loggerFactory.Object);
            var logger = new Mock<ILogger>();
            var stream = new ScaleoutStream(logger.Object, "0", QueuingBehavior.InitialOnly, 1000, perfCounters);

            int x = 0;

            stream.Send(_ =>
            {
                x++;
                return TaskAsyncHelper.Empty;
            },
            null);

            Task task = stream.Send(_ =>
            {
                x++;
                return TaskAsyncHelper.Empty;
            },
            null);

            stream.Open();

            task.Wait();

            Assert.Equal(2, x);
        }