public void TryUpdateContext_AfterDisposeCompletes_ReturnsFalse()
        {
            var    originalContext                = new object();
            var    updatedContext                 = new object();
            object contextPassedToDispose         = null;
            bool   tryUpdateContextDelegateCalled = false;
            var    disposable = new DelegateSingleDisposable <object>(originalContext, context => { contextPassedToDispose = context; });

            disposable.Dispose();
            Assert.False(disposable.TryUpdateContext(context => { tryUpdateContextDelegateCalled = true; return(updatedContext); }));
            Assert.False(tryUpdateContextDelegateCalled);
            Assert.Same(originalContext, contextPassedToDispose);
        }
        public void Dispose_UpdatedContext_ReceivesUpdatedContext()
        {
            var    originalContext        = new object();
            var    updatedContext         = new object();
            object contextPassedToDispose = null;
            object contextPassedToTryUpdateContextDelegate = null;
            var    disposable = new DelegateSingleDisposable <object>(originalContext, context => { contextPassedToDispose = context; });

            Assert.True(disposable.TryUpdateContext(context => { contextPassedToTryUpdateContextDelegate = context; return(updatedContext); }));
            disposable.Dispose();
            Assert.Same(originalContext, contextPassedToTryUpdateContextDelegate);
            Assert.Same(updatedContext, contextPassedToDispose);
        }
        public async Task TryUpdateContext_AfterDisposeStarts_ReturnsFalse()
        {
            var    originalContext                = new object();
            var    updatedContext                 = new object();
            object contextPassedToDispose         = null;
            bool   tryUpdateContextDelegateCalled = false;
            var    ready      = new ManualResetEventSlim();
            var    signal     = new ManualResetEventSlim();
            var    disposable = new DelegateSingleDisposable <object>(originalContext, context =>
            {
                ready.Set();
                signal.Wait();
                contextPassedToDispose = context;
            });
            var task = Task.Run(() => disposable.Dispose());

            ready.Wait();
            Assert.False(disposable.TryUpdateContext(context => { tryUpdateContextDelegateCalled = true; return(updatedContext); }));
            signal.Set();
            await task;

            Assert.False(tryUpdateContextDelegateCalled);
            Assert.Same(originalContext, contextPassedToDispose);
        }
Example #4
0
        public async Task TryUpdateContext_AfterDisposeStarts_ReturnsFalse()
        {
            var    originalContext                = new object();
            var    updatedContext                 = new object();
            object contextPassedToDispose         = null;
            bool   tryUpdateContextDelegateCalled = false;
            var    ready      = new TaskCompletionSource <object>();
            var    signal     = new TaskCompletionSource <object>();
            var    disposable = new DelegateSingleDisposable <object>(originalContext, async context =>
            {
                ready.TrySetResult(null);
                await signal.Task;
                contextPassedToDispose = context;
            });
            var task = Task.Run(async() => await disposable.DisposeAsync());
            await ready.Task;

            Assert.False(disposable.TryUpdateContext(context => { tryUpdateContextDelegateCalled = true; return(updatedContext); }));
            signal.TrySetResult(null);
            await task;

            Assert.False(tryUpdateContextDelegateCalled);
            Assert.Same(originalContext, contextPassedToDispose);
        }