public async Task WriteToActualManyThreadsTest()
        {
            var loggerFactory      = new LoggerFactory();
            var streamService      = new StreamServiceExample(loggerFactory.CreateLogger("StreamServiceBaseTests"), true, 100);
            var cts                = new CancellationTokenSource();
            var serverStreamWriter = new ServerStreamWriterFake();
            var streamInfo         = new StreamInfo <StreamItemCollection>()
            {
                CancelationToken = cts.Token,
                Keys             = new[] { "tenantId" },
                Peer             = "127.0.0.1:5000",
                Stream           = serverStreamWriter
            };

            var streamData = await streamService.RegisterStream(streamInfo, new Filter());

            var tcs            = new TaskCompletionSource <int>();
            var completionTask = streamData.GetCompletionTask();

            streamService.SwitchToReady(streamData);

            var task1 = Task.Run(() =>
            {
                for (int i = 0; i < 68; i++)
                {
                    streamService.WriteToStreamActual(new StreamItemCollection(new StreamItem[]
                    {
                        new StreamItem()
                        {
                            StreamItemId = i
                        },
                    }));
                }
            });

            var task2 = Task.Run(() =>
            {
                for (int i = 0; i < 68; i++)
                {
                    streamService.WriteToStreamActual(new StreamItemCollection(new StreamItem[]
                    {
                        new StreamItem()
                        {
                            StreamItemId = i
                        },
                    }));
                }
            });

            await Task.WhenAll(task1, task2);

            var timeoutTask    = Task.Delay(TimeSpan.FromMilliseconds(10_000));
            var firstCompleted = Task.WaitAny(new Task[] { completionTask, tcs.Task, timeoutTask });


            Assert.True(serverStreamWriter.Messages.Count >= 67);

            streamService.Dispose();
            serverStreamWriter.Dispose();
        }
Example #2
0
        public void Setup()
        {
            _arrayOfData = new StreamItemCollection[_totalCount];

            for (int i = 0; i < _totalCount; i++)
            {
                _arrayOfData[i] = new StreamItemCollection(new []
                {
                    new StreamItem()
                    {
                        StreamItemId = i
                    }
                });
            }

            var loggerFactory = new LoggerFactory();

            _streamService = new StreamServiceExample(loggerFactory.CreateLogger("StreamServiceBenchmark"), false);
            var cts = new CancellationTokenSource();
            var serverStreamWriter = new ServerStreamWriterFake();
            var streamInfo         = new StreamInfo <StreamItemCollection>()
            {
                CancelationToken = cts.Token,
                Keys             = new[] { "tenantId" },
                Peer             = "127.0.0.1:5000",
                Stream           = serverStreamWriter
            };

            _streamData = _streamService.RegisterStream(streamInfo, new Filter()).GetAwaiter().GetResult();
            _streamService.SwitchToReady(_streamData);
        }
        public async Task CheckBeforeAndAfterProcessingTest()
        {
            var afterStreamRemovedProcessed     = false;
            var beforeStreamRegisteredProcessed = false;

            var loggerFactory = new LoggerFactory();
            var streamService = new StreamServiceExample(
                loggerFactory.CreateLogger("StreamServiceBaseTests"), false, 100,
                data =>
            {
                afterStreamRemovedProcessed = true;
                return(Task.CompletedTask);
            },
                data =>
            {
                beforeStreamRegisteredProcessed = true;
                return(Task.CompletedTask);
            });
            var cts = new CancellationTokenSource();
            var serverStreamWriter = new ServerStreamWriterFake();
            var streamInfo         = new StreamInfo <StreamItemCollection>()
            {
                CancelationToken = cts.Token,
                Keys             = new[] { "tenantId" },
                Peer             = "127.0.0.1:5000",
                Stream           = serverStreamWriter
            };

            var streamData = await streamService.RegisterStream(streamInfo, new Filter());

            var completionTask = streamData.GetCompletionTask();

            streamService.SwitchToReady(streamData);

            streamService.WriteToStreamActual(new StreamItemCollection(new StreamItem[]
            {
                new StreamItem()
                {
                    StreamItemId = 0
                },
            }));

            var timeoutTask    = Task.Delay(TimeSpan.FromMilliseconds(10_000));
            var firstCompleted = Task.WaitAny(new Task[] { completionTask, timeoutTask });

            streamService.Dispose();
            serverStreamWriter.Dispose();

            Assert.True(afterStreamRemovedProcessed);
            Assert.True(beforeStreamRegisteredProcessed);
        }
Example #4
0
        public void Setup()
        {
            _arrayOfData = new StreamItemCollection[TotalCount];

            for (int i = 0; i < TotalCount; i++)
            {
                _arrayOfData[i] = new StreamItemCollection(new[]
                {
                    new StreamItem()
                    {
                        StreamItemId = i
                    }
                });
            }

            var loggerFactory = new LoggerFactory();

            _streamService = new StreamServiceExample(loggerFactory.CreateLogger("StreamServiceMultipleClientsBenchmark"), false);
            var cts = new CancellationTokenSource();

            var list = new List <StreamData <StreamItemCollection, StreamItem, long> >();

            for (int i = 0; i < ConnectedStreams; i++)
            {
                var serverStreamWriter = new ServerStreamWriterFake();
                var streamInfo         = new StreamInfo <StreamItemCollection>()
                {
                    CancelationToken = cts.Token,
                    Keys             = new[] { "tenantId" },
                    Peer             = "127.0.0.1:5000",
                    Stream           = serverStreamWriter
                };

                var streamDataItem = _streamService.RegisterStream(streamInfo, new Filter())
                                     .GetAwaiter().GetResult();
                _streamService.SwitchToReady(streamDataItem);
                list.Add(streamDataItem);
            }

            _streamData = list;
        }
        public async Task WriteToActualTest()
        {
            var loggerFactory      = new LoggerFactory();
            var streamService      = new StreamServiceExample(loggerFactory.CreateLogger("StreamServiceBaseTests"), false);
            var cts                = new CancellationTokenSource();
            var serverStreamWriter = new ServerStreamWriterFake();
            var streamInfo         = new StreamInfo <StreamItemCollection>()
            {
                CancelationToken = cts.Token,
                Keys             = new[] { "tenantId" },
                Peer             = "127.0.0.1:5000",
                Stream           = serverStreamWriter
            };

            var streamData = await streamService.RegisterStream(streamInfo, new Filter());

            var tcs     = new TaskCompletionSource <int>();
            var counter = 0;

            serverStreamWriter.MessageReceived += (sender, collection) =>
            {
                counter++;

                if (counter == 21)
                {
                    tcs.TrySetResult(1);
                }
            };
            var completionTask = streamData.GetCompletionTask();
            await streamService.WriteToStreamHistorical(new StreamItemCollection(new StreamItem[]
            {
                new StreamItem()
                {
                    StreamItemId = 10
                },
                new StreamItem()
                {
                    StreamItemId = 11
                },
                new StreamItem()
                {
                    StreamItemId = 12
                },
                new StreamItem()
                {
                    StreamItemId = 13
                },
            }));

            for (int i = 14; i < 24; i++)
            {
                streamService.WriteToStreamActual(new StreamItemCollection(new StreamItem[]
                {
                    new StreamItem()
                    {
                        StreamItemId = 14
                    },
                }));
            }

            streamService.SwitchToReady(streamData);

            for (int i = 24; i < 34; i++)
            {
                streamService.WriteToStreamActual(new StreamItemCollection(new StreamItem[]
                {
                    new StreamItem()
                    {
                        StreamItemId = 14
                    },
                }));
            }

            var timeoutTask    = Task.Delay(TimeSpan.FromMinutes(1));
            var firstCompleted = Task.WaitAny(new Task[] { completionTask, tcs.Task, timeoutTask });

            Assert.True(firstCompleted < 2);

            Assert.True(serverStreamWriter.Messages.Count == 21);

            streamService.Dispose();
            serverStreamWriter.Dispose();
        }