Example #1
0
        public async Task InvokeTwice_DuringTimeout() {
            var count = 0;
            var action = new DelayedAsyncAction(300, () => Task.FromResult(Interlocked.Increment(ref count)));

            action.Invoke();
            await Task.Delay(100);
            action.Invoke();
            await Task.Delay(500);

            count.Should().Be(1, "DelayedAction should be called only once");
        }
Example #2
0
        public async Task Invoke_Concurrent_ChangeAction() {
            var counts = new int[4];
            var action = new DelayedAsyncAction(200);

            ParallelTools.Invoke(4, i => {
                action.Invoke(() => Task.FromResult(Interlocked.Increment(ref counts[i])));
            });
            await Task.Delay(400);

            counts.Should().ContainSingle(i => i == 1, "DelayedAction should be called only once")
                .And.OnlyContain(i => i <= 1, "DelayedAction should be called more than once");
        }
Example #3
0
        public async Task InvokeTwice_DuringTimeout_ChangeAction() {
            var count1 = 0;
            var count2 = 0;
            var action = new DelayedAsyncAction(250);

            action.Invoke(() => Task.FromResult(Interlocked.Increment(ref count1)));
            await Task.Delay(50);
            action.Invoke(() => Task.FromResult(Interlocked.Increment(ref count2)));
            await Task.Delay(450);

            count1.Should().Be(0, "DelayedAction should not be called for the first action");
            count2.Should().Be(1, "DelayedAction should be called only once for the second action");
        }
Example #4
0
        public async Task Invoke_BackgroundThread() {
            var threadId = UIThreadHelper.Instance.Thread.ManagedThreadId;
            var action = new DelayedAsyncAction(0, () => {
                threadId = Thread.CurrentThread.ManagedThreadId;
                return Task.CompletedTask;
            });

            await InUI(() => action.Invoke());
            await Task.Delay(200);

            threadId.Should().NotBe(UIThreadHelper.Instance.Thread.ManagedThreadId);
        }