コード例 #1
0
        public async Task TryGet_Blocks_The_Call_If_Close_Not_Called_Returns_Added_Element_If_Available()
        {
            var instance = new PpcBuffer <object>(ConcurrentBuffer.Unbounded, CancellationToken.None);

#if !NETFRAMEWORK && !NETCOREAPP2_2
            await using (instance.ConfigureAwait(false))
#else
            using (instance)
#endif
            {
                var    obj        = new object();
                object outObj     = null;
                var    tryGetTask = Task.Run(() => instance.TryGet(Timeout.Infinite, CancellationToken.None, out outObj));
                Assert.True(tryGetTask.Status != TaskStatus.RanToCompletion);
                instance.Add(obj, CancellationToken.None);
                Assert.True(await tryGetTask.ConfigureAwait(false) && ReferenceEquals(outObj, obj));
                tryGetTask = Task.Run(() => instance.TryGet(Timeout.Infinite, CancellationToken.None, out outObj));
                Assert.True(tryGetTask.Status != TaskStatus.RanToCompletion);
                instance.Close();
                Assert.False(await tryGetTask.ConfigureAwait(false));
                Assert.True(instance.Finished);
            }

            await Task.CompletedTask;
        }
コード例 #2
0
        public async Task TryAdd_Returns_False_After_Timeout_When_Buffer_Is_Full()
        {
            var instance = new PpcBuffer <object>(ConcurrentBuffer.MinSize, CancellationToken.None);

#if !NETFRAMEWORK && !NETCOREAPP2_2
            await using (instance.ConfigureAwait(false))
#else
            using (instance)
#endif
            {
                instance.Add(new object(), CancellationToken.None);
                Assert.False(instance.TryAdd(new object(), 0, CancellationToken.None));
            }

            await Task.CompletedTask;
        }
コード例 #3
0
        public async Task TryGet_Returns_False_After_Close()
        {
            var instance = new PpcBuffer <object>(ConcurrentBuffer.Unbounded, CancellationToken.None);

#if !NETFRAMEWORK && !NETCOREAPP2_2
            await using (instance.ConfigureAwait(false))
#else
            using (instance)
#endif
            {
                instance.Close();
                Assert.False(instance.TryGet(Timeout.Infinite, CancellationToken.None, out _));
            }

            await Task.CompletedTask;
        }
コード例 #4
0
        public async Task Add_Throws_Error_When_Called_After_Close()
        {
            var instance = new PpcBuffer <object>(ConcurrentBuffer.Unbounded, CancellationToken.None);

#if !NETFRAMEWORK && !NETCOREAPP2_2
            await using (instance.ConfigureAwait(false))
#else
            using (instance)
#endif
            {
                instance.Close();
                Assert.Throws <InvalidOperationException>(() => instance.Add(new object(), CancellationToken.None));
            }

            await Task.CompletedTask;
        }
コード例 #5
0
        public async Task TryGet_Throws_Error_After_Dispose()
        {
            var instance = new PpcBuffer <object>(ConcurrentBuffer.Unbounded, CancellationToken.None);

#if !NETFRAMEWORK && !NETCOREAPP2_2
            await using (instance.ConfigureAwait(false))
#else
            using (instance)
#endif
            {
                instance.Dispose();
                Assert.Throws <NullReferenceException>(() =>
                                                       instance.TryGet(Timeout.Infinite, CancellationToken.None, out _));
            }

            await Task.CompletedTask;
        }
コード例 #6
0
        public async Task Add_Throws_Error_When_Cancellation_Is_Demanded_Through_Param_Token()
        {
            using (var cts = new CancellationTokenSource())
            {
                cts.Cancel();
                var instance = new PpcBuffer <object>(ConcurrentBuffer.Unbounded, CancellationToken.None);
#if !NETFRAMEWORK && !NETCOREAPP2_2
                await using (instance.ConfigureAwait(false))
#else
                using (instance)
#endif
                {
                    Assert.Throws <OperationCanceledException>(() => instance.Add(new object(), cts.Token));
                }

                await Task.CompletedTask;
            }
        }
コード例 #7
0
        public async Task TryGet_And_Add_Harmonize()
        {
            var instance = new PpcBuffer <object>(ConcurrentBuffer.Unbounded, CancellationToken.None);

#if !NETFRAMEWORK && !NETCOREAPP2_2
            await using (instance.ConfigureAwait(false))
#else
            using (instance)
#endif
            {
                var obj = new object();
                instance.Add(obj, CancellationToken.None);
                Assert.True(instance.TryGet(Timeout.Infinite, CancellationToken.None, out var newObj) &&
                            ReferenceEquals(newObj, obj));
                instance.Close();
            }

            await Task.CompletedTask;
        }