|
| 1 | +using AsyncMemoryCache.CreationBehaviors; |
| 2 | +using Microsoft.Extensions.Time.Testing; |
| 3 | +using Nito.AsyncEx; |
| 4 | +using NSubstitute; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using Xunit; |
| 11 | + |
| 12 | +namespace AsyncMemoryCache.Tests |
| 13 | +{ |
| 14 | + public class CacheItemFactoryInvokerTests |
| 15 | + { |
| 16 | + [Fact] |
| 17 | + public void InvokeFactory_ShouldInvokeObjectFactoryAfterDelay() |
| 18 | + { |
| 19 | + using var syncContext = new SingleThreadSynchronizationContext(); |
| 20 | + |
| 21 | + var manualResetEvent = new ManualResetEvent(false); |
| 22 | + |
| 23 | + var factory = () => |
| 24 | + { |
| 25 | + manualResetEvent.Set(); |
| 26 | + return Task.FromResult(Substitute.For<IAsyncDisposable>()); |
| 27 | + }; |
| 28 | + |
| 29 | + var cacheEntity = new CacheEntity<string, IAsyncDisposable>("test", factory, AsyncLazyFlags.None); |
| 30 | + |
| 31 | + var creationTimeProvider = Substitute.For<ICreationTimeProvider>(); |
| 32 | + var timeProvider = new FakeTimeProvider(); |
| 33 | + var expectedCreationTime = timeProvider.GetUtcNow().AddMilliseconds(100); |
| 34 | + creationTimeProvider.GetExpectedCreationTime().Returns(expectedCreationTime); |
| 35 | + |
| 36 | + CacheItemFactoryInvoker.InvokeFactory(cacheEntity, creationTimeProvider, timeProvider); |
| 37 | + |
| 38 | + syncContext.RunOnCurrentThread(); |
| 39 | + |
| 40 | + Assert.False(cacheEntity.ObjectFactory.IsStarted); |
| 41 | + timeProvider.Advance(TimeSpan.FromMilliseconds(99)); |
| 42 | + Assert.False(cacheEntity.ObjectFactory.IsStarted); |
| 43 | + timeProvider.Advance(TimeSpan.FromMilliseconds(100)); |
| 44 | + |
| 45 | + manualResetEvent.WaitOne(); |
| 46 | + Assert.True(cacheEntity.ObjectFactory.IsStarted); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public void InvokeFactory_ShouldInvokeObjectFactoryImmediatelyIfNoDelay() |
| 51 | + { |
| 52 | + using var syncContext = new SingleThreadSynchronizationContext(); |
| 53 | + |
| 54 | + var manualResetEvent = new ManualResetEvent(false); |
| 55 | + |
| 56 | + var factory = () => |
| 57 | + { |
| 58 | + manualResetEvent.Set(); |
| 59 | + return Task.FromResult(Substitute.For<IAsyncDisposable>()); |
| 60 | + }; |
| 61 | + |
| 62 | + var cacheEntity = new CacheEntity<string, IAsyncDisposable>("test", factory, AsyncLazyFlags.None); |
| 63 | + |
| 64 | + var sw = Stopwatch.StartNew(); |
| 65 | + CacheItemFactoryInvoker.InvokeFactory(cacheEntity, CreationTimeProvider.Default, TimeProvider.System); |
| 66 | + |
| 67 | + syncContext.RunOnCurrentThread(); |
| 68 | + |
| 69 | + manualResetEvent.WaitOne(); |
| 70 | + Assert.InRange(sw.ElapsedMilliseconds, 0, 50); |
| 71 | + } |
| 72 | + |
| 73 | + private class SingleThreadSynchronizationContext : SynchronizationContext, IDisposable |
| 74 | + { |
| 75 | + private readonly Queue<(SendOrPostCallback, object?)> _queue = new(); |
| 76 | + private readonly SynchronizationContext? _originalContext; |
| 77 | + |
| 78 | + public SingleThreadSynchronizationContext() |
| 79 | + { |
| 80 | + _originalContext = Current; |
| 81 | + SetSynchronizationContext(this); |
| 82 | + } |
| 83 | + |
| 84 | + public override void Post(SendOrPostCallback d, object? state) |
| 85 | + { |
| 86 | + _queue.Enqueue((d, state)); |
| 87 | + } |
| 88 | + |
| 89 | + public void RunOnCurrentThread() |
| 90 | + { |
| 91 | + while (_queue.Count > 0) |
| 92 | + { |
| 93 | + var (callback, state) = _queue.Dequeue(); |
| 94 | + callback(state); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public void Dispose() |
| 99 | + { |
| 100 | + SetSynchronizationContext(_originalContext); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments