Esempio n. 1
0
        public async Task RunAsync_SameKey_NonParallel()
        {
            var instance = new TaskSingle <string>();

            var timesRun = 0;

            async Task <string> ThingToRunAsync(string key)
            {
                await Task.Delay(200);

                Interlocked.Increment(ref timesRun);

                return(key);
            }

            await instance.RunAsync("test", (s, token) => ThingToRunAsync(s), CancellationToken.None);

            await instance.RunAsync("test", (s, token) => ThingToRunAsync(s), CancellationToken.None);

            Assert.Equal(2, timesRun);
        }
Esempio n. 2
0
        public async Task RunAsync_MultipleKeys_RunManyTimes()
        {
            var instance = new TaskSingle <string>();

            var timesRun = 0;

            async Task <string> ThingToRunAsync(string key)
            {
                await Task.Delay(200);

                Interlocked.Increment(ref timesRun);

                return(key);
            }

            var results = await Task.WhenAll(
                instance.RunAsync("test1", (s, token) => ThingToRunAsync(s), CancellationToken.None),
                instance.RunAsync("test2", (s, token) => ThingToRunAsync(s), CancellationToken.None),
                instance.RunAsync("test3", (s, token) => ThingToRunAsync(s), CancellationToken.None)
                );

            Assert.Equal(3, timesRun);
            Assert.Equal(3, results.Distinct().Count());
        }