コード例 #1
0
        public async Task ReceivesUpdatesAndRespectsTheCancellationToken()
        {
            var bot = new MockTelegramBotClient("start-end", "foo");

            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

            int updateCount = 0;

            async Task HandleUpdate(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
            {
                updateCount++;
                Assert.Contains(update.Message.Text, "start end");
                await Task.Delay(10, cancellationTokenSource.Token);

                if (update.Message.Text == "end")
                {
                    cancellationTokenSource.Cancel();
                }
            }

            var updateHandler = new DefaultUpdateHandler(
                HandleUpdate,
                errorHandler: async(client, e, token) => await Task.Delay(10, token)
                );

            var cancellationToken = cancellationTokenSource.Token;
            await bot.ReceiveAsync(updateHandler, cancellationToken);

            Assert.True(cancellationToken.IsCancellationRequested);
            Assert.Equal(2, updateCount);
            Assert.Equal(1, bot.MessageGroupsLeft);
        }
コード例 #2
0
        public async Task WorksAsync()
        {
            ITelegramBotClient bot = new MockTelegramBotClient("hello-world", "foo-bar-123");

            var updates = await bot.MakeRequestAsync(new GetUpdatesRequest());

            Assert.Equal(2, updates.Length);
            Assert.Equal("hello", updates[0].Message.Text);
            Assert.Equal("world", updates[1].Message.Text);

            updates = await bot.MakeRequestAsync(new GetUpdatesRequest());

            Assert.Equal(3, updates.Length);
            Assert.Equal("foo", updates[0].Message.Text);
            Assert.Equal("bar", updates[1].Message.Text);
            Assert.Equal("123", updates[2].Message.Text);

            updates = await bot.MakeRequestAsync(new GetUpdatesRequest());

            Assert.Empty(updates);
        }
コード例 #3
0
        public async Task UserExceptionsPropagateToSurface()
        {
            var bot = new MockTelegramBotClient("foo-bar", "throw");

            int updateCount = 0;

            async Task HandleUpdate(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
            {
                updateCount++;
                await Task.Delay(10, cancellationToken);

                if (update.Message.Text == "throw")
                {
                    throw new InvalidOperationException("Oops");
                }
            }

            var updateHandler = new DefaultUpdateHandler(
                HandleUpdate,
                errorHandler: async(client, e, token) => await Task.Delay(10, token)
                );

            try
            {
                await bot.ReceiveAsync(updateHandler);

                Assert.True(false);
            }
            catch (Exception ex)
            {
                Assert.IsType <InvalidOperationException>(ex);
                Assert.Contains("Oops", ex.Message);
            }

            Assert.Equal(3, updateCount);
            Assert.Equal(0, bot.MessageGroupsLeft);
        }