Repository: dotnet/runtime
File: src/libraries/System.Threading.Tasks.Dataflow/tests/Dataflow/WriteOnceBlockTests.cs (C#)

Code before:
            Assert.Equal(expected: 42, actual: t.Result);
            await wob.Completion;
        }

        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
        public async Task TestReceiveThenPost()
        {
            var wob = new WriteOnceBlock<int>(null);
            var ignored = Task.Run(() => wob.Post(42));
            Assert.Equal(expected: 42, actual: wob.Receive()); // this should always pass, but due to race we may not test what we're hoping to
            await wob.Completion;

            wob = new WriteOnceBlock<int>(null);
            Task<int> t = wob.ReceiveAsync();
            Assert.False(t.IsCompleted);
            wob.Post(16);
            Assert.Equal(expected: 16, actual: await t);
        }

        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
        public async Task TestConcurrentPostAndReceiveAsync()
        {
            // Test for race condition where ReceiveAsync could return default value
            // instead of the posted value when Post and ReceiveAsync are called concurrently.
            for (int iteration = 0; iteration < 10_000; iteration++)
            {
                var wob = new WriteOnceBlock<object>(a => a);
                var postedValue = new object();

                Task postTask = Task.Run(() => wob.Post(postedValue));
                Task<object> receiveTask = Task.Run(() => wob.ReceiveAsync());

                await postTask;
                object receivedValue = await receiveTask;

                Assert.NotNull(receivedValue);
                Assert.Same(postedValue, receivedValue);
            }
        }

        [Fact]
        public async Task TestTryReceiveWithFilter()
        {
            var wob = new WriteOnceBlock<int>(null);
            wob.Post(1);

            int item;
            Assert.True(wob.TryReceive(out item));
            Assert.Equal(expected: 1, actual: item);

            Assert.True(wob.TryReceive(i => i == 1, out item));

Review comment (suggestion):
@copilot, how long does this test take to run? If it's quick, it's good as-is. If it may take a while, it'd be better as outerloop.

Code after:
            Assert.Equal(expected: 42, actual: t.Result);
            await wob.Completion;
        }

        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
        public async Task TestReceiveThenPost()
        {
            var wob = new WriteOnceBlock<int>(null);
            var ignored = Task.Run(() => wob.Post(42));
            Assert.Equal(expected: 42, actual: wob.Receive()); // this should always pass, but due to race we may not test what we're hoping to
            await wob.Completion;

            wob = new WriteOnceBlock<int>(null);
            Task<int> t = wob.ReceiveAsync();
            Assert.False(t.IsCompleted);
            wob.Post(16);
            Assert.Equal(expected: 16, actual: await t);
        }

        [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))]
        [OuterLoop] // stress test with many iterations
        public async Task TestConcurrentPostAndReceiveAsync()
        {
            // Test for race condition where ReceiveAsync could return default value
            // instead of the posted value when Post and ReceiveAsync are called concurrently.
            for (int iteration = 0; iteration < 10_000; iteration++)
            {
                var wob = new WriteOnceBlock<object>(a => a);
                var postedValue = new object();

                Task postTask = Task.Run(() => wob.Post(postedValue));
                Task<object> receiveTask = Task.Run(() => wob.ReceiveAsync());

                await postTask;
                object receivedValue = await receiveTask;

                Assert.NotNull(receivedValue);
                Assert.Same(postedValue, receivedValue);
            }
        }

        [Fact]
        public async Task TestTryReceiveWithFilter()
        {
            var wob = new WriteOnceBlock<int>(null);
            wob.Post(1);

            int item;
            Assert.True(wob.TryReceive(out item));
            Assert.Equal(expected: 1, actual: item);