public async Task PubAndUnSub()
        {
            int count = 0;
            var mq    = new LocalMessageQueue(null);

            mq.Subscribe("topic", msg => { Interlocked.Increment(ref count); });

            int i = 0;

            Task.Factory.StartNew(async() =>
            {
                for (; i < 50; ++i)
                {
                    await mq.PublishAsync("topic", "a");
                    await Task.Delay(100);
                }
            }).ConfigureAwait(false).GetAwaiter();
            await Task.Delay(1500);

            mq.Unsubscribe("topic");

            while (i < 50)
            {
                await Task.Delay(100);
            }

            Assert.True(count < 100);
        }
예제 #2
0
        public async Task PubAndSub()
        {
            int count = 0;
            var mq    = new LocalMessageQueue(CreateLogger <LocalMessageQueue>());

            mq.Subscribe("topic", msg =>
            {
                Interlocked.Increment(ref count);
                return(Task.CompletedTask);
            });
            for (int i = 0; i < 100; ++i)
            {
                await mq.PublishAsync("topic", "a");
            }

            int j = 0;

            while (count < 100 && j < 150)
            {
                Thread.Sleep(500);
                ++j;
            }

            Assert.Equal(100, count);
        }
예제 #3
0
        public async Task PubAndSub()
        {
            int count = 0;
            var mq    = new LocalMessageQueue();

            mq.Subscribe("topic", msg => { Interlocked.Increment(ref count); });
            for (int i = 0; i < 1000; ++i)
            {
                await mq.PublishAsync("topic", "a");
            }

            Thread.Sleep(2000);
            Assert.Equal(1000, count);
        }
        public void ParallelPubAndSub()
        {
            int count = 0;
            var mq    = new LocalMessageQueue(null);

            mq.Subscribe("topic", msg => { Interlocked.Increment(ref count); });

            Parallel.For(0, 100, async(i) => { await mq.PublishAsync("topic", "a"); });
            int j = 0;

            while (count < 100 && j < 150)
            {
                Thread.Sleep(500);
                ++j;
            }

            Assert.Equal(100, count);
        }
예제 #5
0
        public void ParallelPubAndSub()
        {
            int count = 0;
            var mq    = new LocalMessageQueue(CreateLogger <LocalMessageQueue>());

            mq.Subscribe <string>("topic", msg => { Interlocked.Increment(ref count); });

            Parallel.For(0, 100, async i =>
            {
                await mq.PublishAsync("topic", new MessageData <string>
                {
                    Data = "a"
                });
            });
            int j = 0;

            while (count < 100 && j < 150)
            {
                Thread.Sleep(500);
                ++j;
            }

            Assert.Equal(100, count);
        }