public void SessionRemovedOnGameRemoved()
        {
            // Arrange
            var inactiveSessions = new List <Session>();
            var activeSessions   = new List <Session>();

            var sessionTracker = new SessionTracker(new NullLogger <SessionTracker>(), DefaultTimeoutCheckInterval, DefaultTimeoutCheckInterval);
            var removeGame     = Helpers.GetOrCreateGame("SessionRemovedOnGameRemoved_Remove");
            var keepGame       = Helpers.GetOrCreateGame("SessionRemovedOnGameRemoved_Keep");

            for (var i = 0; i < 5; i++)
            {
                var user    = Helpers.GetOrCreateUser($"SessionRemovedOnGameRemoved_ShouldRemove_{i}");
                var session = sessionTracker.StartSession(removeGame.Id, user.Id);
                inactiveSessions.Add(session);
            }

            for (var i = 0; i < 5; i++)
            {
                var user    = Helpers.GetOrCreateUser($"SessionRemovedOnGameRemoved_ShouldNotRemove_{i}");
                var session = sessionTracker.StartSession(keepGame.Id, user.Id);
                activeSessions.Add(session);
            }

            // Act
            ControllerLocator.GameController.Delete(removeGame.Id);

            // Assert
            inactiveSessions.ForEach(s => Assert.False(sessionTracker.IsActive(s.Id)));

            activeSessions.ForEach(s => Assert.True(sessionTracker.IsActive(s.Id)));
        }
        public void CanStartSession()
        {
            // Arrange
            var sessionTracker = new SessionTracker(new NullLogger <SessionTracker>(), DefaultTimeoutCheckInterval, DefaultTimeoutCheckInterval);
            var game           = Helpers.GetOrCreateGame("CanStartSession");

            var user = Helpers.GetOrCreateUser($"CanStartSession");

            // Act
            var session = sessionTracker.StartSession(game.Id, user.Id);

            // Assert
            Assert.True(sessionTracker.IsActive(session.Id));
        }
        public async Task CanRemoveTimedOut()
        {
            // Arrange
            var timeoutSeconds   = 5;
            var inactiveSessions = new List <Session>();
            var activeSessions   = new List <Session>();

            var sessionTracker = new SessionTracker(new NullLogger <SessionTracker>(), new TimeSpan(0, 0, 0, timeoutSeconds), DefaultTimeoutCheckInterval);
            var game           = Helpers.GetOrCreateGame("CanRemoveTimedOut");

            for (var i = 0; i < 5; i++)
            {
                var user    = Helpers.GetOrCreateUser($"CanRemoveTimedOut_ShouldRemove_{i}");
                var session = sessionTracker.StartSession(game.Id, user.Id);
                inactiveSessions.Add(session);
            }

            await Task.Delay(timeoutSeconds * 1000);

            for (var i = 0; i < 5; i++)
            {
                var user    = Helpers.GetOrCreateUser($"CanRemoveTimedOut_ShouldNotRemove_{i}");
                var session = sessionTracker.StartSession(game.Id, user.Id);
                activeSessions.Add(session);
            }

            // Act
            sessionTracker
            .GetType()
            .GetMethod("RemoveTimedOut", BindingFlags.Instance | BindingFlags.NonPublic)
            .Invoke(sessionTracker, null);

            // Assert
            inactiveSessions.ForEach(s => Assert.False(sessionTracker.IsActive(s.Id)));

            activeSessions.ForEach(s => Assert.True(sessionTracker.IsActive(s.Id)));
        }
 public void OnAuthorization(AuthorizationFilterContext context)
 {
     if (context.ActionDescriptor.GetCustomMethodAttribute <AllowWithoutSession>() == null)
     {
         if (!context.HttpContext.Request.Headers.TryGetSessionId(out var sessionId))
         {
             throw new InvalidSessionException("No \"SessionId\" set in the token's claims.");
         }
         if (!_sessionTracker.IsActive(sessionId))
         {
             throw new InvalidSessionException($"Session with id \"{sessionId}\" is not active.");
         }
         _sessionTracker.SetLastActive(sessionId, DateTime.UtcNow);
     }
 }
        public async Task InactiveGetRemoved()
        {
            // Arrange
            var timeoutMilliseconds = 100;
            var game           = Helpers.GetOrCreateGame("InactiveGetRemoved");
            var sessions       = new List <Session>();
            var sessionTracker = new SessionTracker(new NullLogger <SessionTracker>(), new TimeSpan(0, 0, 0, 0, timeoutMilliseconds), new TimeSpan(0, 0, 0, 0, timeoutMilliseconds));

            for (var i = 0; i < 100; i++)
            {
                var user    = Helpers.GetOrCreateUser($"CanRemoveTimedOut_ShouldRemove_{i}");
                var session = sessionTracker.StartSession(game.Id, user.Id);
                sessions.Add(session);
            }

            // Act
            await Task.Delay(timeoutMilliseconds * 2);

            // Assert
            sessions.ForEach(s => Assert.False(sessionTracker.IsActive(s.Id)));
        }