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 void Test()
        {
            string               fieldName         = "field";
            string               fieldInitialValue = "value";
            string               changedValue      = "changedValue";
            XrmFakedContext      context           = new XrmFakedContext();
            IOrganizationService service           = context.GetOrganizationService();
            DefaultUpdateHandler handler           = new DefaultUpdateHandler();
            Entity               initialEntity     = new Entity("entity", Guid.NewGuid())
            {
                [fieldName] = fieldInitialValue
            };

            context.Initialize(initialEntity);
            initialEntity[fieldName] = changedValue;
            handler.Execute(initialEntity, service);
            Entity post = service.Retrieve(initialEntity.LogicalName, initialEntity.Id, new ColumnSet(fieldName));

            Assert.AreEqual(changedValue, post[fieldName]);
        }
예제 #3
0
        public void TestUpdateWithNoChanges()
        {
            string               fieldName         = "field";
            string               fieldInitialValue = "value";
            string               changedValue      = "changedValue";
            DateTime             lastModified      = new DateTime(2011, 1, 1);
            XrmFakedContext      context           = new XrmFakedContext();
            IOrganizationService service           = context.GetOrganizationService();
            DefaultUpdateHandler handler           = new DefaultUpdateHandler();
            Entity               initialEntity     = new Entity("entity", Guid.NewGuid())
            {
                [fieldName]    = fieldInitialValue,
                ["modifiedon"] = lastModified
            };

            context.Initialize(initialEntity);
            Entity changedEntity = new Entity("entity", initialEntity.Id);

            handler.Execute(changedEntity, service);
            Entity post = service.Retrieve(initialEntity.LogicalName, initialEntity.Id, new ColumnSet("modifiedon"));

            Assert.AreEqual(lastModified, post["modifiedon"]);
        }
        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);
        }
예제 #5
0
    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);
    }