public async Task CloseInactiveSessions(int defaultInactivePeriodInMinutes, bool willCloseSession, int?sessionHeartbeatUpdatedAgoInSeconds, bool heartbeatClosesSession)
        {
            await ResetAsync();

            const string userId = "*****@*****.**";
            var          ev     = GenerateEvent(DateTimeOffset.Now.SubtractMinutes(5), userId);

            var context = await _pipeline.RunAsync(ev);

            Assert.False(context.HasError, context.ErrorMessage);
            Assert.False(context.IsCancelled);
            Assert.True(context.IsProcessed);

            await _client.RefreshAsync();

            var events = await _eventRepository.GetAllAsync();

            Assert.Equal(2, events.Total);
            Assert.Equal(1, events.Documents.Where(e => !String.IsNullOrEmpty(e.GetSessionId())).Select(e => e.GetSessionId()).Distinct().Count());
            var sessionStart = events.Documents.First(e => e.IsSessionStart());

            Assert.Equal(0, sessionStart.Value);
            Assert.False(sessionStart.HasSessionEndTime());

            var utcNow = DateTime.UtcNow;

            if (sessionHeartbeatUpdatedAgoInSeconds.HasValue)
            {
                await _cacheClient.SetAsync($"project:{sessionStart.ProjectId}:heartbeat:{userId.ToSHA1()}", utcNow.SubtractSeconds(sessionHeartbeatUpdatedAgoInSeconds.Value));

                if (heartbeatClosesSession)
                {
                    await _cacheClient.SetAsync($"project:{sessionStart.ProjectId}:heartbeat:{userId.ToSHA1()}-close", true);
                }
            }

            _job.DefaultInactivePeriod = TimeSpan.FromMinutes(defaultInactivePeriodInMinutes);
            Assert.Equal(JobResult.Success, await _job.RunAsync());
            await _client.RefreshAsync();

            events = await _eventRepository.GetAllAsync();

            Assert.Equal(2, events.Total);

            sessionStart = events.Documents.First(e => e.IsSessionStart());
            decimal sessionStartDuration = (decimal)(sessionHeartbeatUpdatedAgoInSeconds.HasValue ? (utcNow.SubtractSeconds(sessionHeartbeatUpdatedAgoInSeconds.Value) - sessionStart.Date.UtcDateTime).TotalSeconds : 0);

            if (willCloseSession)
            {
                Assert.Equal(sessionStartDuration, sessionStart.Value);
                Assert.True(sessionStart.HasSessionEndTime());
            }
            else
            {
                Assert.Equal(sessionStartDuration, sessionStart.Value);
                Assert.False(sessionStart.HasSessionEndTime());
            }
        }
Beispiel #2
0
        public async Task CloseDuplicateIdentitySessions()
        {
            const string userId = "*****@*****.**";
            var          event1 = GenerateEvent(SystemClock.OffsetNow.SubtractMinutes(5), userId);
            var          event2 = GenerateEvent(SystemClock.OffsetNow.SubtractMinutes(5), userId, sessionId: "123456789");

            var contexts = await _pipeline.RunAsync(new [] { event1, event2 }, OrganizationData.GenerateSampleOrganization(_billingManager, _plans), ProjectData.GenerateSampleProject());

            Assert.True(contexts.All(c => !c.HasError));
            Assert.True(contexts.All(c => !c.IsCancelled));
            Assert.True(contexts.All(c => c.IsProcessed));

            await _configuration.Client.RefreshAsync(Indices.All);

            var events = await _eventRepository.GetAllAsync();

            Assert.Equal(4, events.Total);
            Assert.Equal(2, events.Documents.Where(e => !String.IsNullOrEmpty(e.GetSessionId())).Select(e => e.GetSessionId()).Distinct().Count());
            var sessionStarts = events.Documents.Where(e => e.IsSessionStart()).ToList();

            Assert.Equal(0, sessionStarts.Sum(e => e.Value));
            Assert.DoesNotContain(sessionStarts, e => e.HasSessionEndTime());

            var utcNow = SystemClock.UtcNow;
            await _cache.SetAsync($"Project:{sessionStarts.First().ProjectId}:heartbeat:{userId.ToSHA1()}", utcNow.SubtractMinutes(1));

            _job.DefaultInactivePeriod = TimeSpan.FromMinutes(3);
            Assert.Equal(JobResult.Success, await _job.RunAsync());
            await _configuration.Client.RefreshAsync(Indices.All);

            events = await _eventRepository.GetAllAsync();

            Assert.Equal(4, events.Total);

            sessionStarts = events.Documents.Where(e => e.IsSessionStart()).ToList();
            Assert.Equal(2, sessionStarts.Count);
            Assert.Equal(1, sessionStarts.Count(e => !e.HasSessionEndTime()));
            Assert.Equal(1, sessionStarts.Count(e => e.HasSessionEndTime()));
        }
Beispiel #3
0
        public async Task CloseInactiveSessions(int defaultInactivePeriodInMinutes, bool willCloseSession)
        {
            await ResetAsync();

            var ev = GenerateEvent(DateTimeOffset.Now.SubtractMinutes(5), "*****@*****.**");

            var context = await _pipeline.RunAsync(ev);

            Assert.False(context.HasError, context.ErrorMessage);
            Assert.False(context.IsCancelled);
            Assert.True(context.IsProcessed);

            await _client.RefreshAsync();

            var events = await _eventRepository.GetAllAsync();

            Assert.Equal(2, events.Total);
            Assert.Equal(1, events.Documents.Where(e => !String.IsNullOrEmpty(e.GetSessionId())).Select(e => e.GetSessionId()).Distinct().Count());
            var sessionStart = events.Documents.First(e => e.IsSessionStart());

            Assert.Equal(0, sessionStart.Value);
            Assert.False(sessionStart.HasSessionEndTime());

            _job.DefaultInactivePeriod = TimeSpan.FromMinutes(defaultInactivePeriodInMinutes);
            Assert.Equal(JobResult.Success, await _job.RunAsync());
            await _client.RefreshAsync();

            events = await _eventRepository.GetAllAsync();

            Assert.Equal(2, events.Total);

            sessionStart = events.Documents.First(e => e.IsSessionStart());
            if (willCloseSession)
            {
                Assert.Equal(0, sessionStart.Value);
                Assert.True(sessionStart.HasSessionEndTime());
            }
            else
            {
                Assert.Equal(0, sessionStart.Value);
                Assert.False(sessionStart.HasSessionEndTime());
            }
        }