|
1 | | -using Cysharp.Threading; |
| 1 | +using Cysharp.Threading; |
2 | 2 |
|
3 | 3 | namespace LogicLooper.Test; |
4 | 4 |
|
@@ -55,12 +55,172 @@ public void GetLooper() |
55 | 55 | Assert.Equal(pool.Loopers[0], pool.GetLooper()); |
56 | 56 | } |
57 | 57 |
|
| 58 | + [Fact] |
| 59 | + public void PooledLogicLooper_Dispose_Noop() |
| 60 | + { |
| 61 | + var looperFactory = new FakeLogicLooperPoolLooperFactory(); |
| 62 | + using var pool = new LogicLooperPool(60, 4, RoundRobinLogicLooperPoolBalancer.Instance, looperFactory); |
| 63 | + var pooled1 = pool.GetLooper(); |
| 64 | + var pooled2 = pool.GetLooper(); |
| 65 | + |
| 66 | + Assert.Equal(4, looperFactory.CreatedLoopers.Count); |
| 67 | + Assert.False(looperFactory.CreatedLoopers[0].IsDisposed); |
| 68 | + Assert.False(looperFactory.CreatedLoopers[1].IsDisposed); |
| 69 | + Assert.False(looperFactory.CreatedLoopers[2].IsDisposed); |
| 70 | + Assert.False(looperFactory.CreatedLoopers[3].IsDisposed); |
| 71 | + |
| 72 | + pooled1.Dispose(); |
| 73 | + pooled2.Dispose(); |
| 74 | + |
| 75 | + Assert.False(looperFactory.CreatedLoopers[0].IsDisposed); |
| 76 | + Assert.False(looperFactory.CreatedLoopers[1].IsDisposed); |
| 77 | + Assert.False(looperFactory.CreatedLoopers[2].IsDisposed); |
| 78 | + Assert.False(looperFactory.CreatedLoopers[3].IsDisposed); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + public void PooledLogicLooper_DisposeFromLooperPool() |
| 83 | + { |
| 84 | + var looperFactory = new FakeLogicLooperPoolLooperFactory(); |
| 85 | + var pool = new LogicLooperPool(60, 4, RoundRobinLogicLooperPoolBalancer.Instance, looperFactory); |
| 86 | + |
| 87 | + Assert.Equal(4, looperFactory.CreatedLoopers.Count); |
| 88 | + Assert.False(looperFactory.CreatedLoopers[0].IsDisposed); |
| 89 | + Assert.False(looperFactory.CreatedLoopers[1].IsDisposed); |
| 90 | + Assert.False(looperFactory.CreatedLoopers[2].IsDisposed); |
| 91 | + Assert.False(looperFactory.CreatedLoopers[3].IsDisposed); |
| 92 | + |
| 93 | + pool.Dispose(); |
| 94 | + |
| 95 | + Assert.True(looperFactory.CreatedLoopers[0].IsDisposed); |
| 96 | + Assert.True(looperFactory.CreatedLoopers[1].IsDisposed); |
| 97 | + Assert.True(looperFactory.CreatedLoopers[2].IsDisposed); |
| 98 | + Assert.True(looperFactory.CreatedLoopers[3].IsDisposed); |
| 99 | + } |
| 100 | + |
| 101 | + [Fact] |
| 102 | + public async Task PooledLogicLooper_ShutdownAsync_NotSupported() |
| 103 | + { |
| 104 | + var looperFactory = new FakeLogicLooperPoolLooperFactory(); |
| 105 | + using var pool = new LogicLooperPool(60, 4, RoundRobinLogicLooperPoolBalancer.Instance, looperFactory); |
| 106 | + var pooled1 = pool.GetLooper(); |
| 107 | + var pooled2 = pool.GetLooper(); |
| 108 | + |
| 109 | + Assert.Equal(4, looperFactory.CreatedLoopers.Count); |
| 110 | + Assert.False(looperFactory.CreatedLoopers[0].IsShutdownRequested); |
| 111 | + Assert.False(looperFactory.CreatedLoopers[1].IsShutdownRequested); |
| 112 | + Assert.False(looperFactory.CreatedLoopers[2].IsShutdownRequested); |
| 113 | + Assert.False(looperFactory.CreatedLoopers[3].IsShutdownRequested); |
| 114 | + |
| 115 | + await Assert.ThrowsAsync<NotSupportedException>(() => pooled1.ShutdownAsync(TimeSpan.FromSeconds(1))); |
| 116 | + await Assert.ThrowsAsync<NotSupportedException>(() => pooled2.ShutdownAsync(TimeSpan.FromSeconds(1))); |
| 117 | + Assert.False(looperFactory.CreatedLoopers[0].IsShutdownRequested); |
| 118 | + Assert.False(looperFactory.CreatedLoopers[1].IsShutdownRequested); |
| 119 | + Assert.False(looperFactory.CreatedLoopers[2].IsShutdownRequested); |
| 120 | + Assert.False(looperFactory.CreatedLoopers[3].IsShutdownRequested); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public async Task PooledLogicLooper_ShutdownAsyncFromLooperPool() |
| 125 | + { |
| 126 | + var looperFactory = new FakeLogicLooperPoolLooperFactory(); |
| 127 | + using var pool = new LogicLooperPool(60, 4, RoundRobinLogicLooperPoolBalancer.Instance, looperFactory); |
| 128 | + var pooled1 = pool.GetLooper(); |
| 129 | + var pooled2 = pool.GetLooper(); |
| 130 | + |
| 131 | + Assert.Equal(4, looperFactory.CreatedLoopers.Count); |
| 132 | + Assert.False(looperFactory.CreatedLoopers[0].IsShutdownRequested); |
| 133 | + Assert.False(looperFactory.CreatedLoopers[1].IsShutdownRequested); |
| 134 | + Assert.False(looperFactory.CreatedLoopers[2].IsShutdownRequested); |
| 135 | + Assert.False(looperFactory.CreatedLoopers[3].IsShutdownRequested); |
| 136 | + |
| 137 | + await pool.ShutdownAsync(TimeSpan.FromSeconds(1)); |
| 138 | + Assert.True(looperFactory.CreatedLoopers[0].IsShutdownRequested); |
| 139 | + Assert.True(looperFactory.CreatedLoopers[1].IsShutdownRequested); |
| 140 | + Assert.True(looperFactory.CreatedLoopers[2].IsShutdownRequested); |
| 141 | + Assert.True(looperFactory.CreatedLoopers[3].IsShutdownRequested); |
| 142 | + } |
| 143 | + |
58 | 144 | class FakeSequentialLogicLooperPoolBalancer : ILogicLooperPoolBalancer |
59 | 145 | { |
60 | 146 | private int _count; |
61 | | - public Cysharp.Threading.LogicLooper GetPooledLooper(Cysharp.Threading.LogicLooper[] pooledLoopers) |
| 147 | + public Cysharp.Threading.ILogicLooper GetPooledLooper(Cysharp.Threading.ILogicLooper[] pooledLoopers) |
62 | 148 | { |
63 | 149 | return pooledLoopers[_count++ % pooledLoopers.Length]; |
64 | 150 | } |
65 | 151 | } |
| 152 | + |
| 153 | + class FakeLogicLooperPoolLooperFactory : ILogicLooperPoolLooperFactory |
| 154 | + { |
| 155 | + public List<LogicLooper> CreatedLoopers { get; } = new(); |
| 156 | + |
| 157 | + public ILogicLooper Create(TimeSpan targetFrameTime) |
| 158 | + { |
| 159 | + var looper = new LogicLooper(); |
| 160 | + CreatedLoopers.Add(looper); |
| 161 | + return looper; |
| 162 | + } |
| 163 | + |
| 164 | + public class LogicLooper : ILogicLooper |
| 165 | + { |
| 166 | + public bool IsDisposed { get; set; } |
| 167 | + public bool IsShutdownRequested { get; set; } |
| 168 | + |
| 169 | + public void Dispose() |
| 170 | + { |
| 171 | + IsDisposed = true; |
| 172 | + } |
| 173 | + |
| 174 | + public int Id { get; } |
| 175 | + public int ApproximatelyRunningActions { get; } |
| 176 | + public TimeSpan LastProcessingDuration { get; } |
| 177 | + public double TargetFrameRate { get; } |
| 178 | + public long CurrentFrame { get; } |
| 179 | + public Task RegisterActionAsync(LogicLooperActionDelegate loopAction) |
| 180 | + { |
| 181 | + throw new NotImplementedException(); |
| 182 | + } |
| 183 | + |
| 184 | + public Task RegisterActionAsync(LogicLooperActionDelegate loopAction, LooperActionOptions options) |
| 185 | + { |
| 186 | + throw new NotImplementedException(); |
| 187 | + } |
| 188 | + |
| 189 | + public Task RegisterActionAsync<TState>(LogicLooperActionWithStateDelegate<TState> loopAction, TState state) |
| 190 | + { |
| 191 | + throw new NotImplementedException(); |
| 192 | + } |
| 193 | + |
| 194 | + public Task RegisterActionAsync<TState>(LogicLooperActionWithStateDelegate<TState> loopAction, TState state, LooperActionOptions options) |
| 195 | + { |
| 196 | + throw new NotImplementedException(); |
| 197 | + } |
| 198 | + |
| 199 | + public Task RegisterActionAsync(LogicLooperAsyncActionDelegate loopAction) |
| 200 | + { |
| 201 | + throw new NotImplementedException(); |
| 202 | + } |
| 203 | + |
| 204 | + public Task RegisterActionAsync(LogicLooperAsyncActionDelegate loopAction, LooperActionOptions options) |
| 205 | + { |
| 206 | + throw new NotImplementedException(); |
| 207 | + } |
| 208 | + |
| 209 | + public Task RegisterActionAsync<TState>(LogicLooperAsyncActionWithStateDelegate<TState> loopAction, TState state) |
| 210 | + { |
| 211 | + throw new NotImplementedException(); |
| 212 | + } |
| 213 | + |
| 214 | + public Task RegisterActionAsync<TState>(LogicLooperAsyncActionWithStateDelegate<TState> loopAction, TState state, LooperActionOptions options) |
| 215 | + { |
| 216 | + throw new NotImplementedException(); |
| 217 | + } |
| 218 | + |
| 219 | + public Task ShutdownAsync(TimeSpan shutdownDelay) |
| 220 | + { |
| 221 | + IsShutdownRequested = true; |
| 222 | + return Task.CompletedTask; |
| 223 | + } |
| 224 | + } |
| 225 | + } |
66 | 226 | } |
0 commit comments