Skip to content

Commit

Permalink
FIX : some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Poltuu committed Oct 16, 2019
1 parent 924bcca commit 23aa8a4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
42 changes: 21 additions & 21 deletions KeySmith.Tests/IntegrationMemoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task SimpleScenario()
var service = provider.GetRequiredService<IMemoLockService>();

var root = Guid.NewGuid().ToString().Substring(0, 8);
var memoKey = new MemoKey(root, "name", TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(500));
var memoKey = new MemoKey(root, "name", TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));

var db = provider.GetRequiredService<ConnectionMultiplexer>().GetDatabase();
await ResetKeys(db, memoKey);
Expand Down Expand Up @@ -49,31 +49,37 @@ public async Task SimpleFailScenario()
var service = provider.GetRequiredService<IMemoLockService>();

var root = Guid.NewGuid().ToString().Substring(0, 8);
var memoKey = new MemoKey(root, "name", TimeSpan.FromSeconds(100), TimeSpan.FromSeconds(500));
var memoKey = new MemoKey(root, "name", TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));

var db = provider.GetRequiredService<ConnectionMultiplexer>().GetDatabase();
await ResetKeys(db, memoKey);
try
{
RedisValue answer = "answer";
var failedFirstTask = service.MemoLockAsync(memoKey, c =>
try
{
throw new NullReferenceException("oups");
}, CancellationToken.None);
var succesfullSecondTask = WaitBeforeStart(service.MemoLockAsync(memoKey, c => Task.FromResult(answer), CancellationToken.None), 100);
await service.MemoLockAsync(memoKey, c => throw new NullReferenceException("oups"), CancellationToken.None);

throw new Exception("Nothing was thrown");
}
catch (NullReferenceException)
{
}
catch (GenerationException)// we actually get it from redis before we had time to clean the setup.
{
}

try
{
Task.WaitAll(failedFirstTask, succesfullSecondTask);
RedisValue answer = "answer";
await service.MemoLockAsync(memoKey, c => Task.FromResult(answer), CancellationToken.None);

throw new Exception("Nothing was thrown");
}
catch (AggregateException e)
catch (GenerationException)
{
Assert.Equal(2, e.InnerExceptions.Count);
Assert.Single(e.InnerExceptions.OfType<NullReferenceException>());
Assert.Single(e.InnerExceptions.OfType<GenerationException>());
}

Assert.False(await db.KeyExistsAsync(memoKey.GetValueKey()));//value is in redis
Assert.False(await db.KeyExistsAsync(memoKey.GetValueKey()));
Assert.True(await db.KeyExistsAsync(memoKey.GetErrorKey()));
}
finally
Expand All @@ -82,12 +88,6 @@ public async Task SimpleFailScenario()
}
}

private async Task WaitBeforeStart(Task t, int ms)
{
await Task.Delay(ms);
await t;
}

[Fact]
public async Task ConcurrencyScenario()
{
Expand All @@ -98,7 +98,7 @@ public async Task ConcurrencyScenario()
var service = provider.GetRequiredService<IMemoLockService>();

var root = Guid.NewGuid().ToString().Substring(0, 8);
var memoKey = new MemoKey(root, "name", TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(500));
var memoKey = new MemoKey(root, "name", TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1));

var db = provider.GetRequiredService<ConnectionMultiplexer>().GetDatabase();

Expand Down Expand Up @@ -131,8 +131,8 @@ public async Task ConcurrencyScenario()
Assert.Equal(42, answer);
}

Assert.True(await db.KeyExistsAsync(memoKey.GetValueKey()));//value is in redis
Assert.False(await db.KeyExistsAsync(memoKey.GetErrorKey()));
Assert.True(await db.KeyExistsAsync(memoKey.GetValueKey()));
}
finally
{
Expand Down
9 changes: 9 additions & 0 deletions KeySmith.Tests/LockContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public void HandlerTests(string message, object initialState)
}
break;
case State.WithKey:
if (context.Identifier == message)
{
Assert.Equal(State.WithKey, context.State);
}
else
{
Assert.Equal(State.Done, context.State);
}
break;
case State.Done:
Assert.Equal(State.Done, context.State);
break;
Expand Down
13 changes: 11 additions & 2 deletions KeySmith/MemoLockService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,16 @@ private async Task<RedisValue> MemoLockWithoutSubscriptionAsync(MemoKey key, Fun

try
{
return await _lockService.LockAsync(key.GetLockKey(), c => LockedCallback(key, generator, c), cancellationToken).ConfigureAwait(false);
return await _lockService.LockAsync(key.GetLockKey(), async c =>
{
//in case we missed the publication
var result = await TryGetCachedValue(key).ConfigureAwait(false);
if (result != null)
{
return result.Value;
}
return await LockedCallback(key, generator, c).ConfigureAwait(false);
}, cancellationToken).ConfigureAwait(false);
}
catch (TaskCanceledException)
{
Expand Down Expand Up @@ -119,7 +128,7 @@ private Action<RedisChannel, RedisValue> GetHandler(CancellationTokenSource sour
}
else
{
task.SetResult(v);
task.TrySetResult(v);
}
try
Expand Down

0 comments on commit 23aa8a4

Please sign in to comment.