public async Task SeamphoreCanTimeout() { semaphore = new RemoteSemaphore(db, 1, TimeSpan.FromSeconds(1)); var s1 = await semaphore.TryGetAsync(); await Task.Delay(1500); var s2 = await semaphore.TryGetAsync(); var len = await db.SortedSetLengthAsync(semaphore.TimeoutKey); Assert.True(s1.IsSuccess); Assert.True(s2.IsSuccess); Assert.Equal(1, len); }
public async Task SemaphoreCanEnterWhenLessMaxCount() { var resList = new List <RemoteSemaphore.SemaphoreTakeResult>(); for (int i = 0; i < 4; i++) { var res = await semaphore.TryGetAsync(); resList.Add(res); } resList.Take(2).ToList().ForEach(v => Assert.True(v.IsSuccess)); resList.TakeLast(2).ToList().ForEach(v => Assert.False(v.IsSuccess)); foreach (var res in resList) { await semaphore.ReleaseAsync(res.SemaphoreId); } }
public async Task CanRefreshWhenNotTimeout() { semaphore = new RemoteSemaphore(db, 2, TimeSpan.FromSeconds(2)); var r1 = await semaphore.TryGetAsync(); await Task.Delay(500); var isRefresh = await semaphore.RefreshAsync(r1.SemaphoreId); await Task.Delay(2200); var isRefresh2 = await semaphore.RefreshAsync(r1.SemaphoreId); Assert.True(isRefresh); Assert.True(isRefresh2); }