public async Task Should_lock_and_call_inner()
        {
            var stream = new MemoryStream();

            var dateFrom = DateTime.Today;
            var dateTo   = dateFrom.AddDays(2);

            var key = "MyKey";

            var releaseToken = Guid.NewGuid().ToString();

            A.CallTo(() => lockGrain.AcquireLockAsync(key))
            .Returns(releaseToken);

            await sut.ReadLogAsync(key, dateFrom, dateTo, stream);

            A.CallTo(() => lockGrain.AcquireLockAsync(key))
            .MustHaveHappened();

            A.CallTo(() => lockGrain.ReleaseLockAsync(releaseToken))
            .MustHaveHappened();

            A.CallTo(() => inner.ReadLogAsync(key, dateFrom, dateTo, stream))
            .MustHaveHappened();
        }
Esempio n. 2
0
        public async Task ReadLogAsync(string key, DateTime from, DateTime to, Stream stream, TimeSpan lockTimeout)
        {
            using (var cts = new CancellationTokenSource(lockTimeout))
            {
                string releaseToken = null;

                while (!cts.IsCancellationRequested)
                {
                    releaseToken = await lockGrain.AcquireLockAsync(key);

                    if (releaseToken != null)
                    {
                        break;
                    }

                    await Task.Delay(2000, cts.Token);
                }

                if (!cts.IsCancellationRequested)
                {
                    try
                    {
                        await inner.ReadLogAsync(key, from, to, stream);
                    }
                    finally
                    {
                        await lockGrain.ReleaseLockAsync(releaseToken);
                    }
                }
                else
                {
                    await stream.WriteAsync(LockedText, 0, LockedText.Length);
                }
            }
        }
        public Task ReadLogAsync(string appId, DateTime from, DateTime to, Stream stream)
        {
            Guard.NotNull(appId);

            return(logStore.ReadLogAsync(appId, from, to, stream));
        }