示例#1
0
        public async Task ShouldCallLockMthodWithTheSameToken()
        {
            var key      = Utility.NewPassword();
            var timeSpan = TimeSpan.FromSeconds(10);
            await lockService.Lock(key, timeSpan);

            Mock.Get(lockService).Verify(p => p.Lock(key, key, timeSpan), Times.Once);
        }
示例#2
0
 public static Func <CancellationToken, Tuple <Action, CancellationToken> > ToFunc(this ILockService service, String key)
 {
     return(token =>
     {
         var @lock = service.Lock(key, token);
         return new Tuple <Action, CancellationToken>(@lock.Dispose, @lock.Token);
     });
 }
示例#3
0
 public ActionResult LockItem([FromBody] LockRequest lockRequest)
 {
     try
     {
         _lockService.Lock(lockRequest.ItemId, lockRequest.UserId);
         return(Ok());
     }
     catch (Exception e)
     {
         return(BadRequest(e.GetBaseException().Message));
     }
 }
示例#4
0
        private async Task <TopoMojo.Api.Data.Gamespace> _Register(GamespaceRegistration request, User actor)
        {
            string playerId = request.Players.FirstOrDefault()?.SubjectId ?? actor.Id;

            var gamespace = await _store.LoadActiveByContext(
                request.ResourceId,
                playerId
                );

            if (gamespace is Data.Gamespace)
            {
                return(gamespace);
            }

            if (!await _store.IsBelowGamespaceLimit(actor.Id, actor.GamespaceLimit))
            {
                throw new ClientGamespaceLimitReached();
            }

            string lockKey = $"{playerId}{request.ResourceId}";

            var ctx = await LoadContext(request);

            if (!await _locker.Lock(lockKey))
            {
                throw new ResourceIsLocked();
            }

            try
            {
                await Create(ctx, actor);
            }
            finally
            {
                await _locker.Unlock(lockKey);
            }

            return(ctx.Gamespace);
        }
 public static async Task <bool> GlobalRunOnce(this ILockService lockService, string key, Action action)
 {
     if (action == null)
     {
         throw new ArgumentNullException(nameof(action));
     }
     if (await lockService.Lock(key, TimeSpan.MaxValue))
     {
         try
         {
             action();
             return(true);
         }
         finally
         {
             await lockService.UnLock(key);
         }
     }
     return(false);
 }
 public static Task <bool> Lock(this ILockService lockService, string key, TimeSpan timeSpan)
 {
     return(lockService.Lock(key, key, timeSpan));
 }
示例#7
0
        public async Task CanLockSimpleTypes()
        {
            var key = Utility.NewPassword(16);
            await lockService.Lock(key + "string", "", TimeSpan.FromSeconds(2));

            await lockService.Lock(key + "long", 1L, TimeSpan.FromSeconds(2));

            await lockService.Lock(key + "dateTime", DateTime.Now, TimeSpan.FromSeconds(2));
        }