Skip to content

Commit

Permalink
ADD : identifiergenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
Poltuu committed Oct 22, 2019
1 parent 21d1ffb commit 20fa223
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 14 deletions.
6 changes: 3 additions & 3 deletions KeySmith.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public async Task SimpleScenario()
{
var connection = ConfigurationHelper.GetNewConnection();
var library = new ScriptLibrary(connection);
var service = new LockService(library);
var service = new LockService(library, new Internals.Locks.IdentifierGenerator());

var root = Guid.NewGuid().ToString().Substring(0, 8);
var key = new Key(root, "name", TimeSpan.FromSeconds(1));
Expand Down Expand Up @@ -49,7 +49,7 @@ public async Task CancelScenario()
{
var connection = ConfigurationHelper.GetNewConnection();
var library = new ScriptLibrary(connection);
var service = new LockService(library);
var service = new LockService(library, new Internals.Locks.IdentifierGenerator());

var root = Guid.NewGuid().ToString().Substring(0, 8);
var key = new Key(root, "name", TimeSpan.FromSeconds(2));
Expand Down Expand Up @@ -84,7 +84,7 @@ public async Task ConcurrencyScenario()
{
var connection = ConfigurationHelper.GetNewConnection();
var library = new ScriptLibrary(connection);
var service = new LockService(library);
var service = new LockService(library, new Internals.Locks.IdentifierGenerator());

var root = Guid.NewGuid().ToString().Substring(0, 8);
var key = new Key(root, "name", TimeSpan.FromSeconds(5));
Expand Down
12 changes: 6 additions & 6 deletions KeySmith.Tests/LockProtectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class LockProtectorTests
public void EnsureDisposeWorks()
{
var library = new Mock<IScriptLibrary>();
using var context = new LockState(new Key("", "", TimeSpan.FromSeconds(1)), CancellationToken.None);
using var context = new LockState(new Key("", "", TimeSpan.FromSeconds(1)), "id", CancellationToken.None);

using (var protector = new LockProtector(library.Object, context))
{
Expand All @@ -28,7 +28,7 @@ public void EnsureDisposeWorks()
public async Task EnsureNoDeadLockAsyncShouldStop()
{
var library = new Mock<IScriptLibrary>();
using var context = new LockState(new Key("", "", TimeSpan.FromSeconds(1)), CancellationToken.None);
using var context = new LockState(new Key("", "", TimeSpan.FromSeconds(1)), "id2", CancellationToken.None);
using var protector = new LockProtector(library.Object, context, false);

context.SetDone();
Expand All @@ -41,7 +41,7 @@ public async Task EnsureNoDeadLockAsyncShouldStop()
public async Task EnsureNoDeadLockAsyncShouldStop2()
{
var library = new Mock<IScriptLibrary>();
using var context = new LockState(new Key("", "", TimeSpan.FromSeconds(1)), CancellationToken.None);
using var context = new LockState(new Key("", "", TimeSpan.FromSeconds(1)), "id3", CancellationToken.None);
using var protector = new LockProtector(library.Object, context, false);

context.SetDone(new Exception(""));
Expand All @@ -58,7 +58,7 @@ public async Task EnsureNoDeadLockAsyncShouldStop2()
public async Task CheckSituationIsCorrectlyInterpreted(int value, object state)
{
var library = new Mock<IScriptLibrary>();
using var context = new LockState(new Key("", "", TimeSpan.FromMilliseconds(100)), CancellationToken.None);
using var context = new LockState(new Key("", "", TimeSpan.FromMilliseconds(100)), "id4", CancellationToken.None);
switch (state)
{
case State.WithKey:
Expand Down Expand Up @@ -94,7 +94,7 @@ public async Task CheckSituationIsCorrectlyInterpreted(int value, object state)
public async Task CheckSituationIsCorrectlyInterpretedFailCase1()
{
var library = new Mock<IScriptLibrary>();
using var context = new LockState(new Key("", "", TimeSpan.FromSeconds(1)), CancellationToken.None);
using var context = new LockState(new Key("", "", TimeSpan.FromSeconds(1)), "id5", CancellationToken.None);
using var protector = new LockProtector(library.Object, context, false);
library.Setup(l => l.GetKeySituation(It.IsAny<LockLuaParameters>())).ReturnsAsync(() => throw new ApplicationException(""));

Expand All @@ -108,7 +108,7 @@ public async Task CheckSituationIsCorrectlyInterpretedFailCase1()
public async Task CheckSituationIsCorrectlyInterpretedFailCase2()
{
var library = new Mock<IScriptLibrary>();
using var context = new LockState(new Key("", "", TimeSpan.FromSeconds(1)), CancellationToken.None);
using var context = new LockState(new Key("", "", TimeSpan.FromSeconds(1)), "id6", CancellationToken.None);
using var protector = new LockProtector(library.Object, context, false);
library.Setup(l => l.GetKeySituation(It.IsAny<LockLuaParameters>())).ReturnsAsync(() => 4);

Expand Down
32 changes: 32 additions & 0 deletions KeySmith/Internals/Locks/IdentifierGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Security.Cryptography;
using System.Text;

namespace KeySmith.Internals.Locks
{
class IdentifierGenerator : IDisposable
{
private static readonly char[] _chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_".ToCharArray();

private readonly RNGCryptoServiceProvider _rngCryptoServiceProvider;

public IdentifierGenerator()
{
_rngCryptoServiceProvider = new RNGCryptoServiceProvider();
}

public void Dispose() => _rngCryptoServiceProvider?.Dispose();

public string GetUniqueKey(int size)
{
var data = new byte[size];
_rngCryptoServiceProvider.GetBytes(data);
var result = new StringBuilder(size);
foreach (var b in data)
{
result.Append(_chars[b % _chars.Length]);
}
return result.ToString();
}
}
}
3 changes: 1 addition & 2 deletions KeySmith/Internals/Locks/LockState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ class LockState : IDisposable

private readonly object _stateLocker = new object();

public LockState(Key key, CancellationToken cancellationToken) : this(key, GetUniqueKey(12), cancellationToken) { }
internal LockState(Key key, string identifier, CancellationToken cancellationToken)
public LockState(Key key, string identifier, CancellationToken cancellationToken)
{
Key = key;
Identifier = identifier;
Expand Down
6 changes: 4 additions & 2 deletions KeySmith/LockService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ namespace KeySmith
internal class LockService : ILockService
{
private readonly IScriptLibrary _scriptLibrary;
private readonly IdentifierGenerator _identifierGenerator;

public LockService(IScriptLibrary scriptLibrary)
public LockService(IScriptLibrary scriptLibrary, IdentifierGenerator identifierGenerator)
{
_scriptLibrary = scriptLibrary ?? throw new ArgumentNullException(nameof(scriptLibrary));
_identifierGenerator = identifierGenerator ?? throw new ArgumentNullException(nameof(identifierGenerator));
}

public Task LockAsync(Key key, Func<CancellationToken, Task> callback, CancellationToken cancellationToken)
Expand All @@ -25,7 +27,7 @@ public async Task<T> LockAsync<T>(Key key, Func<CancellationToken, Task<T>> call
throw new ArgumentNullException(nameof(callback));
}

using var state = new LockState(key, cancellationToken);
using var state = new LockState(key, _identifierGenerator.GetUniqueKey(15), cancellationToken);
try
{
await _scriptLibrary.SubscribeAsync(state).ConfigureAwait(false);
Expand Down
4 changes: 3 additions & 1 deletion KeySmith/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using KeySmith.Internals.Scripts;
using KeySmith.Internals.Locks;
using KeySmith.Internals.Scripts;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -70,6 +71,7 @@ public static IServiceCollection AddKeySmithWithoutConnectionMultiplexer(this IS
services.TryAddSingleton<IMemoLockService, MemoLockService>();

return services
.AddSingleton<IdentifierGenerator>()
.AddSingleton<IScriptLibrary, ScriptLibrary>()
.AddSingleton<IMemoScriptLibrary, MemoScriptLibrary>();
}
Expand Down

0 comments on commit 20fa223

Please sign in to comment.