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 token) { 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( updateHandler: HandleUpdate, errorHandler: async(_, _, token) => await Task.Delay(10, token) ); var cancellationToken = cancellationTokenSource.Token; await bot.ReceiveAsync(updateHandler, cancellationToken : cancellationToken); Assert.True(cancellationToken.IsCancellationRequested); Assert.Equal(2, updateCount); Assert.Equal(1, bot.MessageGroupsLeft); }
public async Task WorksAsync() { var bot = new MockTelegramBotClient("hello-world", "foo-bar-123"); Assert.Equal(2, bot.MessageGroupsLeft); var updates = await bot.MakeRequestAsync(new GetUpdatesRequest()); Assert.Equal(2, updates.Length); Assert.Equal(1, bot.MessageGroupsLeft); 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(0, bot.MessageGroupsLeft); 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); Assert.Equal(0, bot.MessageGroupsLeft); }
public async Task StopsIfStoppedAndOutOfUpdates() { var mockClient = new MockTelegramBotClient("1", "2", "3"); var receiver = new QueuedUpdateReceiver(mockClient); receiver.StartReceiving(); Task stopTask = Task.Run(() => { Task.Delay(150).Wait(); receiver.StopReceiving(); }); int updateCount = 0; await foreach (var update in receiver.YieldUpdatesAsync()) { updateCount++; } Assert.Equal(3, updateCount); Assert.Equal(0, mockClient.MessageGroupsLeft); Assert.Equal(0, receiver.PendingUpdates); Assert.False(receiver.IsReceiving); Assert.True(stopTask.IsCompleted); }
public async Task ReturnsReceivedPendingUpdates() { var mockClient = new MockTelegramBotClient("foo-bar", "123"); var receiver = new BlockingUpdateReceiver(mockClient); Assert.Equal(2, mockClient.MessageGroupsLeft); Assert.Equal(0, receiver.PendingUpdates); await foreach (var update in receiver.YieldUpdatesAsync()) { Assert.Equal("foo", update.Message.Text); break; } Assert.Equal(1, mockClient.MessageGroupsLeft); Assert.Equal(1, receiver.PendingUpdates); await foreach (var update in receiver.YieldUpdatesAsync()) { Assert.Equal("bar", update.Message.Text); break; } Assert.Equal(1, mockClient.MessageGroupsLeft); Assert.Equal(0, receiver.PendingUpdates); await foreach (var update in receiver.YieldUpdatesAsync()) { Assert.Equal("123", update.Message.Text); break; } Assert.Equal(0, mockClient.MessageGroupsLeft); Assert.Equal(0, receiver.PendingUpdates); }
public async Task ThrowOutPendingUpdates() { var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(4)); var bot = new MockTelegramBotClient( new MockClientOptions { Messages = new [] { "foo-bar", "baz", "quux" }, HandleNegativeOffset = true, } ); var receiver = new BlockingUpdateReceiver(bot, new() { ThrowPendingUpdates = true }); await using var enumerator = receiver.GetAsyncEnumerator(cancellationTokenSource.Token); try { await enumerator.MoveNextAsync(); } catch (OperationCanceledException) { // ignored } Assert.Equal(0, bot.MessageGroupsLeft); }
public void CallingGetEnumeratorTwiceThrows() { var mockClient = new MockTelegramBotClient(); var receiver = new BlockingUpdateReceiver(mockClient); _ = receiver.GetAsyncEnumerator(); Assert.Throws <InvalidOperationException>(() => receiver.GetAsyncEnumerator()); }
public async Task ReturnsReceivedPendingUpdates() { var mockClient = new MockTelegramBotClient("foo-bar", "123", "one-two-three", "456"); var receiver = new QueuedUpdateReceiver(mockClient); mockClient.Options.RequestDelay = 250; Assert.Equal(4, mockClient.MessageGroupsLeft); Assert.Equal(0, receiver.PendingUpdates); await using var enumerator = receiver.GetAsyncEnumerator(); Assert.True(await enumerator.MoveNextAsync()); Assert.Equal(3, mockClient.MessageGroupsLeft); Assert.Equal(1, receiver.PendingUpdates); Assert.Equal("foo", enumerator.Current.Message !.Text); Assert.True(await enumerator.MoveNextAsync()); Assert.Equal(3, mockClient.MessageGroupsLeft); Assert.Equal(0, receiver.PendingUpdates); Assert.Equal("bar", enumerator.Current.Message !.Text); Assert.True(await enumerator.MoveNextAsync()); Assert.Equal(2, mockClient.MessageGroupsLeft); Assert.Equal(0, receiver.PendingUpdates); Assert.Equal("123", enumerator.Current.Message !.Text); Assert.True(await enumerator.MoveNextAsync()); Assert.Equal(1, mockClient.MessageGroupsLeft); Assert.Equal(2, receiver.PendingUpdates); Assert.Equal("one", enumerator.Current.Message !.Text); Assert.True(await enumerator.MoveNextAsync()); Assert.Equal(1, mockClient.MessageGroupsLeft); Assert.Equal(1, receiver.PendingUpdates); Assert.Equal("two", enumerator.Current.Message !.Text); Assert.True(await enumerator.MoveNextAsync()); Assert.Equal(1, mockClient.MessageGroupsLeft); Assert.Equal(0, receiver.PendingUpdates); Assert.Equal("three", enumerator.Current.Message !.Text); Assert.True(await enumerator.MoveNextAsync()); Assert.Equal(0, mockClient.MessageGroupsLeft); Assert.Equal(0, receiver.PendingUpdates); Assert.Equal("456", enumerator.Current.Message !.Text); }
public async Task MoveNextThrowsIfEnumeratorIsDisposed() { var mockClient = new MockTelegramBotClient("foo"); var receiver = new QueuedUpdateReceiver(mockClient); var enumerator = receiver.GetAsyncEnumerator(); await enumerator.MoveNextAsync(); await enumerator.DisposeAsync(); await Assert.ThrowsAnyAsync <OperationCanceledException>(async() => await enumerator.MoveNextAsync()); }
public async Task ThrowsOnMoveNextIfCancelled() { var mockClient = new MockTelegramBotClient("foo", "bar"); var receiver = new QueuedUpdateReceiver(mockClient); var cts = new CancellationTokenSource(); await using var enumerator = receiver.GetAsyncEnumerator(cts.Token); Assert.True(await enumerator.MoveNextAsync()); Assert.Equal("foo", enumerator.Current.Message !.Text); cts.Cancel(); await Assert.ThrowsAnyAsync <OperationCanceledException>(async() => await enumerator.MoveNextAsync()); }
public async Task ReturnsReceivedPendingUpdates() { var mockClient = new MockTelegramBotClient("foo-bar", "123"); var receiver = new QueuedUpdateReceiver(mockClient); Assert.Equal(2, mockClient.MessageGroupsLeft); Assert.Equal(0, receiver.PendingUpdates); receiver.StartReceiving(); await Task.Delay(200); Assert.Equal(0, mockClient.MessageGroupsLeft); Assert.Equal(3, receiver.PendingUpdates); await foreach (var update in receiver.YieldUpdatesAsync()) { Assert.Equal("foo", update.Message.Text); break; } Assert.Equal(2, receiver.PendingUpdates); await foreach (var update in receiver.YieldUpdatesAsync()) { Assert.Equal("bar", update.Message.Text); break; } Assert.Equal(1, receiver.PendingUpdates); await foreach (var update in receiver.YieldUpdatesAsync()) { Assert.Equal("123", update.Message.Text); break; } Assert.Equal(0, receiver.PendingUpdates); receiver.StopReceiving(); await foreach (var update in receiver.YieldUpdatesAsync()) { // No pending updates and we've called StopReceiving Assert.False(true); } }
public async Task BlocksWhileProcessingAsync() { var mockClient = new MockTelegramBotClient("test", "break", "test"); var receiver = new BlockingUpdateReceiver(mockClient); Assert.Equal(3, mockClient.MessageGroupsLeft); await foreach (var update in receiver.YieldUpdatesAsync()) { if (update.Message.Text == "break") { break; } } Assert.Equal(1, mockClient.MessageGroupsLeft); }
public async Task DoesntReceiveWhileProcessing() { var mockClient = new MockTelegramBotClient("foo", "bar"); var receiver = new BlockingUpdateReceiver(mockClient); Assert.Equal(2, mockClient.MessageGroupsLeft); await foreach (Update update in receiver) { Assert.Equal("foo", update.Message !.Text); await Task.Delay(100); Assert.Equal(1, mockClient.MessageGroupsLeft); break; } Assert.Equal(1, mockClient.MessageGroupsLeft); }
public async Task ReceivesUpdatesInTheBackground() { var mockClient = new MockTelegramBotClient("1", "2", "3"); var receiver = new QueuedUpdateReceiver(mockClient); Assert.Equal(3, mockClient.MessageGroupsLeft); await foreach (var update in receiver) { Assert.Equal("1", update.Message !.Text); await Task.Delay(100); break; } Assert.Equal(0, mockClient.MessageGroupsLeft); Assert.Equal(2, receiver.PendingUpdates); }
public async Task ReceivesOnlyOnMoveNextAsync() { var mockClient = new MockTelegramBotClient("foo", "bar"); var receiver = new BlockingUpdateReceiver(mockClient); Assert.Equal(2, mockClient.MessageGroupsLeft); await using var enumerator = receiver.GetAsyncEnumerator(); Assert.Equal(2, mockClient.MessageGroupsLeft); Assert.True(await enumerator.MoveNextAsync()); Assert.Equal("foo", enumerator.Current.Message !.Text); Assert.Equal(1, mockClient.MessageGroupsLeft); Assert.True(await enumerator.MoveNextAsync()); Assert.Equal("bar", enumerator.Current.Message !.Text); Assert.Equal(0, mockClient.MessageGroupsLeft); }
public async Task DoesntReceiveAfterCancellation() { var mockClient = new MockTelegramBotClient("foo", "bar", "foo"); var receiver = new QueuedUpdateReceiver(mockClient); mockClient.Options.RequestDelay = 200; var cts = new CancellationTokenSource(); await using var enumerator = receiver.GetAsyncEnumerator(cts.Token); Assert.True(await enumerator.MoveNextAsync()); Assert.Equal(2, mockClient.MessageGroupsLeft); Assert.Equal("foo", enumerator.Current.Message !.Text); cts.CancelAfter(50); await Assert.ThrowsAnyAsync <OperationCanceledException>(async() => await enumerator.MoveNextAsync()); await Task.Delay(500); Assert.Equal(2, mockClient.MessageGroupsLeft); }
public async Task ThrowOutPendingUpdates() { var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(4)); var bot = new MockTelegramBotClient( new MockClientOptions { Messages = new [] { "foo-bar", "baz", "quux" }, HandleNegativeOffset = true } ); int handleCount = 0; Task HandleUpdate( ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) { handleCount += 1; return(Task.CompletedTask); }; var updateHandler = new DefaultUpdateHandler( updateHandler: HandleUpdate, errorHandler: (_, _, _) => Task.CompletedTask ); await bot.ReceiveAsync( updateHandler, new() { ThrowPendingUpdates = true }, cancellationTokenSource.Token ); Assert.Equal(0, handleCount); Assert.Equal(0, bot.MessageGroupsLeft); }
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( updateHandler: HandleUpdate, errorHandler: async(_, _, 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); }