예제 #1
0
        public async Task CanReceiveAMessage()
        {
            options.Topic   = "foo";
            options.Channel = "bar";
            byte[] expectedData = new byte[] { 1, 2, 3 };

            await EmptyChannelAsync(options.Topic, options.Channel);

            var tcs  = new TaskCompletionSource <Message>();
            var task = tcs.Task;
            var conn = new NsqTcpConnection(endPoint, options);

            using (conn)
            {
                conn.InternalMessages += (_, e) => Trace.WriteLine(e.Message);
                await conn.ConnectAndWaitAsync(async msg =>
                {
                    await msg.FinishAsync();
                    tcs.TrySetResult(msg);
                });

                await conn.SetMaxInFlightAsync(100);

                await prod.PublishAsync(options.Topic, expectedData);

                await Task.WhenAny(task, Task.Delay(1000));

                Assert.Equal(TaskStatus.RanToCompletion, task.Status);

                byte[] receivedData = task.Result.Body;
                Assert.NotNull(receivedData);
                Assert.True(expectedData.SequenceEqual(receivedData));
            }
        }
예제 #2
0
        public async Task CanReceiveAtAnAcceptableRate()
        {
            options.Topic   = "load_test";
            options.Channel = "load_test";
            await EmptyChannelAsync(options.Topic, options.Channel);

            int messagesReceived = 0;
            var conn             = new NsqTcpConnection(endPoint, options);

            using (conn)
            {
                conn.InternalMessages += (_, e) => Trace.WriteLine(e.Message);
                await conn.ConnectAndWaitAsync(async msg =>
                {
                    await msg.FinishAsync().ConfigureAwait(false);
                    Interlocked.Increment(ref messagesReceived);
                });

                await conn.SetMaxInFlightAsync(100);

                var messages = Enumerable.Range(0, 1000).Select(i => (MessageBody)BitConverter.GetBytes(i)).ToArray();
                await prod.PublishAsync(options.Topic, messages);

                await Task.Delay(1000);
            }
            Assert.InRange(messagesReceived, 500, int.MaxValue);
        }
예제 #3
0
        public async Task ConnectionClosesProperly()
        {
            options.Topic   = "foo";
            options.Channel = "bar";
            var conn = new NsqTcpConnection(endPoint, options);

            conn.InternalMessages += (_, e) => Trace.WriteLine(e.Message);
            await conn.ConnectAndWaitAsync(msg => msg.FinishAsync());

            conn.Dispose();
        }
예제 #4
0
        public async Task ProducerCanEmptyAChannel()
        {
            // Put something in the channel
            await prod.PublishAsync(options.Topic, new byte[] { 1 });

            // Now erase it
            await EmptyChannelAsync(options.Topic, options.Channel);

            // Now try to receive anything
            bool receivedData = false;
            conn = new NsqTcpConnection(endPoint, options);
            await conn.ConnectAndWaitAsync(async msg =>
            {
                receivedData = true;
                await msg.FinishAsync();
            });
            await conn.SetMaxInFlightAsync(100);
            await Task.Delay(100);

            Assert.False(receivedData);
        }
예제 #5
0
        public async Task ProducerCanEmptyAChannel()
        {
            // Put something in the channel
            await prod.PublishAsync(options.Topic, new byte[] { 1 });

            // Now erase it
            await EmptyChannelAsync(options.Topic, options.Channel);

            // Now try to receive anything
            bool receivedData = false;

            conn = new NsqTcpConnection(endPoint, options);
            await conn.ConnectAndWaitAsync(async msg =>
            {
                receivedData = true;
                await msg.FinishAsync();
            });

            await conn.SetMaxInFlightAsync(100);

            await Task.Delay(100);

            Assert.False(receivedData);
        }
예제 #6
0
        public async Task ConnectAndWaitIsConnectedImmediatelyAfter()
        {
            options.Topic   = "foo";
            options.Channel = "bar";
            var tcs = new TaskCompletionSource <bool>();

            using (var conn = new NsqTcpConnection(endPoint, options))
            {
                Assert.False(conn.Connected);
                await conn.ConnectAndWaitAsync(msg => { tcs.TrySetResult(true); return(msg.FinishAsync()); });

                Assert.True(conn.Connected);

                // Just for kicks, verify we're working
                await Task.WhenAll(
                    prod.PublishAsync(options.Topic, new byte[] { 1, 2, 3, 4 }),
                    conn.SetMaxInFlightAsync(10));

                var task = tcs.Task;
                var done = await Task.WhenAny(task, Task.Delay(1000));

                Assert.Same(done, task);
            }
        }