public async Task SingletonConcurrenctCloseOpenTests()
        {
            var createTcs = new TaskCompletionSource <object>();
            var closeTcs  = new TaskCompletionSource <object>();

            var singleton = new SingletonTester(createTcs.Task, closeTcs.Task);

            var creating = singleton.GetOrCreateAsync(TimeSpan.FromDays(1000));
            var closing  = singleton.CloseAsync();

            closeTcs.SetResult(new object());
            await closing;

            createTcs.SetResult(new object());
            await creating;

            try
            {
                await singleton.GetOrCreateAsync(TimeSpan.FromDays(1000));

                Assert.Fail("Singleton should throw an ObjectDisposedException");
            }
            catch (ObjectDisposedException) { }

            var createdObj = GetInternalProperty <object>(singleton, "Value");

            createdObj.Should().BeNull("A closed Singleton shouldn't have a value!");
        }
예제 #2
0
        public async Task SingletonConcurrentCloseOpenTests()
        {
            var createTcs = new TaskCompletionSource <object>();
            var closeTcs  = new TaskCompletionSource <object>();

            var singleton = new SingletonTester(createTcs.Task, closeTcs.Task);

            var creating = singleton.GetOrCreateAsync(TimeSpan.FromSeconds(1));
            var closing  = singleton.CloseAsync();

            closeTcs.SetResult(new object());
            await closing;

            createTcs.SetResult(new object());
            await creating;

            await Assert.ThrowsAsync <ObjectDisposedException>(() => singleton.GetOrCreateAsync(TimeSpan.FromSeconds(1)));

            var createdObj = GetInternalProperty <object>(singleton, "Value");

            Assert.Null(createdObj);
        }