예제 #1
0
        public virtual async Task CanSetMinMaxExpirationAsync()
        {
            var cache = GetCacheClient();

            if (cache == null)
            {
                return;
            }

            using (cache) {
                await cache.RemoveAllAsync();

                using (TestSystemClock.Install()) {
                    var now = DateTime.UtcNow;
                    TestSystemClock.SetFrozenTime(now);

                    var expires = DateTime.MaxValue - now.AddDays(1);
                    Assert.True(await cache.SetAsync("test1", 1, expires));
                    Assert.False(await cache.SetAsync("test2", 1, DateTime.MinValue));
                    Assert.True(await cache.SetAsync("test3", 1, DateTime.MaxValue));
                    Assert.True(await cache.SetAsync("test4", 1, DateTime.MaxValue - now.AddDays(-1)));

                    Assert.Equal(1, (await cache.GetAsync <int>("test1")).Value);
                    Assert.InRange((await cache.GetExpirationAsync("test1")).Value, expires.Subtract(TimeSpan.FromSeconds(10)), expires);

                    Assert.False(await cache.ExistsAsync("test2"));
                    Assert.Equal(1, (await cache.GetAsync <int>("test3")).Value);
                    Assert.False((await cache.GetExpirationAsync("test3")).HasValue);
                    Assert.Equal(1, (await cache.GetAsync <int>("test4")).Value);
                    Assert.False((await cache.GetExpirationAsync("test4")).HasValue);
                }
            }
        }
        public async Task MaintainOnlyOldIndexesWithPartialAliasesAsync()
        {
            using (TestSystemClock.Install()) {
                TestSystemClock.SetFrozenTime(SystemClock.UtcNow.EndOfYear());

                var index = new MonthlyEmployeeIndex(_configuration, 1)
                {
                    MaxIndexAge = SystemClock.UtcNow.EndOfMonth() - SystemClock.UtcNow.SubtractMonths(12).StartOfMonth()
                };

                await index.EnsureIndexAsync(SystemClock.UtcNow.SubtractMonths(11));

                await index.EnsureIndexAsync(SystemClock.UtcNow.SubtractMonths(12));

                var existsResponse = await _client.Indices.ExistsAsync(index.GetIndex(SystemClock.UtcNow.SubtractMonths(12)));

                _logger.LogRequest(existsResponse);
                Assert.True(existsResponse.ApiCall.Success);
                Assert.True(existsResponse.Exists);

                index.MaxIndexAge = SystemClock.UtcNow.EndOfMonth() - SystemClock.UtcNow.StartOfMonth();
                await DeleteAliasesAsync(index.GetVersionedIndex(SystemClock.UtcNow.SubtractMonths(12)));

                await index.MaintainAsync();

                existsResponse = await _client.Indices.ExistsAsync(index.GetIndex(SystemClock.UtcNow.SubtractMonths(12)));

                _logger.LogRequest(existsResponse);
                Assert.True(existsResponse.ApiCall.Success);
                Assert.False(existsResponse.Exists);
            }
        }
예제 #3
0
 public EventIndexTests(ITestOutputHelper output) : base(output)
 {
     TestSystemClock.SetFrozenTime(new DateTime(2015, 2, 13, 0, 0, 0, DateTimeKind.Utc));
     _repository = GetService <IEventRepository>();
     _validator  = GetService <PersistentEventQueryValidator>();
     CreateEventsAsync().GetAwaiter().GetResult();
 }
        public async Task GetDateOffsetAggregationsAsync(DateTime utcNow)
        {
            using (TestSystemClock.Install()) {
                TestSystemClock.SetFrozenTime(utcNow);

                var today = SystemClock.OffsetNow.Floor(TimeSpan.FromMilliseconds(1));

                await _employeeRepository.AddAsync(new List <Employee> {
                    EmployeeGenerator.Generate(nextReview: today.SubtractDays(2)),
                    EmployeeGenerator.Generate(nextReview: today.SubtractDays(1)),
                    EmployeeGenerator.Generate(nextReview: today)
                }, o => o.ImmediateConsistency());

                const string aggregations = "min:nextReview max:nextReview date:nextReview";
                var          result       = await _employeeRepository.CountAsync(q => q.AggregationsExpression(aggregations));

                Assert.Equal(3, result.Total);
                Assert.Equal(3, result.Aggregations.Count);

                // Dates are always returned in utc.
                AssertEqual(DateTime.SpecifyKind(today.UtcDateTime.SubtractDays(2), DateTimeKind.Utc), result.Aggregations.Min <DateTime>("min_nextReview")?.Value);
                AssertEqual(DateTime.SpecifyKind(today.UtcDateTime, DateTimeKind.Utc), result.Aggregations.Max <DateTime>("max_nextReview")?.Value);

                var dateHistogramAgg = result.Aggregations.DateHistogram("date_nextReview");
                Assert.Equal(3, dateHistogramAgg.Buckets.Count);
                var oldestDate = DateTime.SpecifyKind(today.UtcDateTime.Date.SubtractDays(2), DateTimeKind.Utc);
                foreach (var bucket in dateHistogramAgg.Buckets)
                {
                    AssertEqual(oldestDate, bucket.Date);
                    Assert.Equal(1, bucket.Total);
                    oldestDate = oldestDate.AddDays(1);
                }
            }
        }
예제 #5
0
    public EventStackFilterTests(ITestOutputHelper output, AppWebHostFactory factory) : base(output, factory)
    {
        TestSystemClock.SetFrozenTime(new DateTime(2015, 2, 13, 0, 0, 0, DateTimeKind.Utc));
        _stackRepository = GetService <IStackRepository>();
        _eventRepository = GetService <IEventRepository>();

        Log.SetLogLevel <EventRepository>(LogLevel.Trace);
        Log.SetLogLevel <StackRepository>(LogLevel.Trace);
    }
예제 #6
0
 public void CanGetTime()
 {
     using (TestSystemClock.Install()) {
         var now = DateTime.UtcNow;
         TestSystemClock.SetFrozenTime(now);
         Assert.Equal(now, SystemClock.UtcNow);
         Assert.Equal(now.ToLocalTime(), SystemClock.Now);
         Assert.Equal(now, SystemClock.OffsetUtcNow);
         Assert.Equal(now.ToLocalTime(), SystemClock.OffsetNow);
         Assert.Equal(DateTimeOffset.Now.Offset, SystemClock.TimeZoneOffset);
     }
 }
예제 #7
0
        public void CanSetLocalFixedTime()
        {
            using (TestSystemClock.Install()) {
                var now    = DateTime.Now;
                var utcNow = now.ToUniversalTime();
                TestSystemClock.SetFrozenTime(now);

                Assert.Equal(now, SystemClock.Now);
                Assert.Equal(now, SystemClock.OffsetNow);
                Assert.Equal(utcNow, SystemClock.UtcNow);
                Assert.Equal(utcNow, SystemClock.OffsetUtcNow);
                Assert.Equal(DateTimeOffset.Now.Offset, SystemClock.TimeZoneOffset);
            }
        }
예제 #8
0
        public void CanSetTimeZone()
        {
            using (TestSystemClock.Install()) {
                var utcNow = DateTime.UtcNow;
                var now    = new DateTime(utcNow.AddHours(1).Ticks, DateTimeKind.Local);
                TestSystemClock.SetFrozenTime(utcNow);
                TestSystemClock.SetTimeZoneOffset(TimeSpan.FromHours(1));

                Assert.Equal(utcNow, SystemClock.UtcNow);
                Assert.Equal(utcNow, SystemClock.OffsetUtcNow);
                Assert.Equal(now, SystemClock.Now);
                Assert.Equal(new DateTimeOffset(now.Ticks, TimeSpan.FromHours(1)), SystemClock.OffsetNow);
                Assert.Equal(TimeSpan.FromHours(1), SystemClock.TimeZoneOffset);
            }
        }
예제 #9
0
        public async Task CanRunJobsWithIntervalBetweenFailingJob()
        {
            using (TestSystemClock.Install()) {
                var time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

                TestSystemClock.SetFrozenTime(time);
                TestSystemClock.UseFakeSleep();

                var job      = new FailingJob(Log);
                var interval = TimeSpan.FromHours(.75);

                await job.RunContinuousAsync(iterationLimit : 2, interval : interval);

                Assert.Equal(2, job.RunCount);
                Assert.Equal(interval, (SystemClock.UtcNow - time));
            }
        }
        public async Task CanRoundTripById()
        {
            using var _ = TestSystemClock.Install();
            TestSystemClock.SetFrozenTime(new DateTime(2020, 6, 16, 20, 0, 0, DateTimeKind.Local));

            Assert.Equal(0, await _dailyRepository.CountAsync());

            var utcNow   = SystemClock.UtcNow;
            var logEvent = await _dailyRepository.AddAsync(LogEventGenerator.Generate(createdUtc: utcNow, date: utcNow.SubtractDays(1)), o => o.ImmediateConsistency());

            Assert.NotNull(logEvent?.Id);

            var ev = await _dailyRepository.GetByIdAsync(logEvent.Id);

            Assert.NotNull(ev);
            Assert.Equal(ev.Date, ObjectId.Parse(ev.Id).CreationTime);
            Assert.NotEqual(ev.Date, ev.CreatedUtc);
        }
        public async Task MonthlyIndexMaxAgeAsync(DateTime utcNow)
        {
            using (TestSystemClock.Install()) {
                TestSystemClock.SetFrozenTime(utcNow);

                var index = new MonthlyEmployeeIndex(_configuration, 1)
                {
                    MaxIndexAge = SystemClock.UtcNow.EndOfMonth() - SystemClock.UtcNow.StartOfMonth()
                };
                await index.DeleteAsync();

                using (new DisposableAction(() => index.DeleteAsync().GetAwaiter().GetResult())) {
                    await index.ConfigureAsync();

                    await index.EnsureIndexAsync(utcNow);

                    var existsResponse = await _client.Indices.ExistsAsync(index.GetIndex(utcNow));

                    _logger.LogRequest(existsResponse);
                    Assert.True(existsResponse.ApiCall.Success);
                    Assert.True(existsResponse.Exists);

                    await index.EnsureIndexAsync(utcNow.Subtract(index.MaxIndexAge.GetValueOrDefault()));

                    existsResponse = await _client.Indices.ExistsAsync(index.GetIndex(utcNow.Subtract(index.MaxIndexAge.GetValueOrDefault())));

                    _logger.LogRequest(existsResponse);
                    Assert.True(existsResponse.ApiCall.Success);
                    Assert.True(existsResponse.Exists);

                    var endOfTwoMonthsAgo = utcNow.SubtractMonths(2).EndOfMonth();
                    if (utcNow - endOfTwoMonthsAgo >= index.MaxIndexAge.GetValueOrDefault())
                    {
                        await Assert.ThrowsAsync <ArgumentException>(async() => await index.EnsureIndexAsync(endOfTwoMonthsAgo));

                        existsResponse = await _client.Indices.ExistsAsync(index.GetIndex(endOfTwoMonthsAgo));

                        _logger.LogRequest(existsResponse);
                        Assert.True(existsResponse.ApiCall.Success);
                        Assert.False(existsResponse.Exists);
                    }
                }
            }
        }
        public async Task DailyIndexMaxAgeAsync(DateTime utcNow)
        {
            using (TestSystemClock.Install()) {
                TestSystemClock.SetFrozenTime(utcNow);

                var index = new DailyEmployeeIndex(_configuration, 1)
                {
                    MaxIndexAge = TimeSpan.FromDays(1)
                };
                await index.DeleteAsync();

                using (new DisposableAction(() => index.DeleteAsync().GetAwaiter().GetResult())) {
                    await index.ConfigureAsync();

                    await index.EnsureIndexAsync(utcNow);

                    var existsResponse = await _client.Indices.ExistsAsync(index.GetIndex(utcNow));

                    _logger.LogRequest(existsResponse);
                    Assert.True(existsResponse.ApiCall.Success);
                    Assert.True(existsResponse.Exists);

                    await index.EnsureIndexAsync(utcNow.SubtractDays(1));

                    existsResponse = await _client.Indices.ExistsAsync(index.GetIndex(utcNow.SubtractDays(1)));

                    _logger.LogRequest(existsResponse);
                    Assert.True(existsResponse.ApiCall.Success);
                    Assert.True(existsResponse.Exists);

                    await Assert.ThrowsAsync <ArgumentException>(async() => await index.EnsureIndexAsync(utcNow.SubtractDays(2)));

                    existsResponse = await _client.Indices.ExistsAsync(index.GetIndex(utcNow.SubtractDays(2)));

                    _logger.LogRequest(existsResponse);
                    Assert.True(existsResponse.ApiCall.Success);
                    Assert.False(existsResponse.Exists);
                }
            }
        }
        public async Task CanCreateMonthlyAliasesAsync(DateTime utcNow)
        {
            using (TestSystemClock.Install()) {
                TestSystemClock.SetFrozenTime(utcNow);

                var index = new MonthlyEmployeeIndex(_configuration, 1);
                await index.DeleteAsync();

                using (new DisposableAction(() => index.DeleteAsync().GetAwaiter().GetResult())) {
                    await index.ConfigureAsync();

                    IEmployeeRepository repository = new EmployeeRepository(index);

                    for (int i = 0; i < 4; i++)
                    {
                        var employee = await repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow.SubtractMonths(i)));

                        Assert.NotNull(employee?.Id);

                        Assert.Equal(1, await index.GetCurrentVersionAsync());
                        var existsResponse = await _client.Indices.ExistsAsync(index.GetIndex(employee.CreatedUtc));

                        _logger.LogRequest(existsResponse);
                        Assert.True(existsResponse.ApiCall.Success);
                        Assert.True(existsResponse.Exists);

                        var aliasesResponse = await _client.Indices.GetAliasAsync(index.GetIndex(employee.CreatedUtc));

                        _logger.LogRequest(aliasesResponse);
                        Assert.True(aliasesResponse.IsValid);
                        Assert.Equal(1, aliasesResponse.Indices.Count);

                        var aliases = aliasesResponse.Indices.Values.Single().Aliases.Select(s => s.Key).ToList();
                        aliases.Sort();

                        Assert.Equal(GetExpectedEmployeeMonthlyAliases(index, utcNow, employee.CreatedUtc), String.Join(", ", aliases));
                    }
                }
            }
        }
예제 #14
0
 public MoreEventIndexTests(ITestOutputHelper output, AppWebHostFactory factory) : base(output, factory)
 {
     TestSystemClock.SetFrozenTime(new DateTime(2015, 2, 13, 0, 0, 0, DateTimeKind.Utc));
     _repository = GetService <IEventRepository>();
     _validator  = GetService <PersistentEventQueryValidator>();
 }
        public async Task DailyAliasMaxAgeAsync(DateTime utcNow)
        {
            using (TestSystemClock.Install()) {
                TestSystemClock.SetFrozenTime(utcNow);
                var index = new DailyEmployeeIndex(_configuration, 1)
                {
                    MaxIndexAge = TimeSpan.FromDays(45)
                };

                await index.DeleteAsync();

                using (new DisposableAction(() => index.DeleteAsync().GetAwaiter().GetResult())) {
                    await index.ConfigureAsync();

                    IEmployeeRepository version1Repository = new EmployeeRepository(index);

                    var employee = await version1Repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow), o => o.ImmediateConsistency());

                    Assert.NotNull(employee?.Id);

                    var existsResponse = await _client.Indices.ExistsAsync(index.GetIndex(employee.CreatedUtc));

                    _logger.LogRequest(existsResponse);
                    Assert.True(existsResponse.ApiCall.Success);
                    Assert.True(existsResponse.Exists);

                    var aliasesResponse = await _client.Indices.GetAliasAsync(index.GetIndex(employee.CreatedUtc));

                    _logger.LogRequest(aliasesResponse);
                    Assert.True(aliasesResponse.IsValid);
                    Assert.Equal(1, aliasesResponse.Indices.Count);
                    var aliases = aliasesResponse.Indices.Values.Single().Aliases.Select(s => s.Key).ToList();
                    aliases.Sort();
                    Assert.Equal(GetExpectedEmployeeDailyAliases(index, utcNow, employee.CreatedUtc), String.Join(", ", aliases));

                    employee = await version1Repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow.SubtractDays(2)), o => o.ImmediateConsistency());

                    Assert.NotNull(employee?.Id);

                    existsResponse = await _client.Indices.ExistsAsync(index.GetIndex(employee.CreatedUtc));

                    _logger.LogRequest(existsResponse);
                    Assert.True(existsResponse.ApiCall.Success);
                    Assert.True(existsResponse.Exists);

                    aliasesResponse = await _client.Indices.GetAliasAsync(index.GetIndex(employee.CreatedUtc));

                    _logger.LogRequest(aliasesResponse);
                    Assert.True(aliasesResponse.IsValid);
                    Assert.Equal(1, aliasesResponse.Indices.Count);
                    aliases = aliasesResponse.Indices.Values.Single().Aliases.Select(s => s.Key).ToList();
                    aliases.Sort();
                    Assert.Equal(GetExpectedEmployeeDailyAliases(index, utcNow, employee.CreatedUtc), String.Join(", ", aliases));

                    employee = await version1Repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow.SubtractDays(35)), o => o.ImmediateConsistency());

                    Assert.NotNull(employee?.Id);

                    existsResponse = await _client.Indices.ExistsAsync(index.GetIndex(employee.CreatedUtc));

                    _logger.LogRequest(existsResponse);
                    Assert.True(existsResponse.ApiCall.Success);
                    Assert.True(existsResponse.Exists);

                    aliasesResponse = await _client.Indices.GetAliasAsync(index.GetIndex(employee.CreatedUtc));

                    _logger.LogRequest(aliasesResponse);
                    Assert.True(aliasesResponse.IsValid);
                    Assert.Equal(1, aliasesResponse.Indices.Count);
                    aliases = aliasesResponse.Indices.Values.Single().Aliases.Select(s => s.Key).ToList();
                    aliases.Sort();
                    Assert.Equal(GetExpectedEmployeeDailyAliases(index, utcNow, employee.CreatedUtc), String.Join(", ", aliases));
                }
            }
        }
        public async Task MaintainWillCreateAliasesOnTimeSeriesIndexAsync()
        {
            using (TestSystemClock.Install()) {
                TestSystemClock.SetFrozenTime(SystemClock.UtcNow);
                var version1Index = new DailyEmployeeIndex(_configuration, 1);
                await version1Index.DeleteAsync();

                var version2Index = new DailyEmployeeIndex(_configuration, 2);
                await version2Index.DeleteAsync();

                // Indexes don't exist yet so the current version will be the index version.
                Assert.Equal(1, await version1Index.GetCurrentVersionAsync());
                Assert.Equal(2, await version2Index.GetCurrentVersionAsync());

                using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) {
                    await version1Index.ConfigureAsync();

                    await version1Index.EnsureIndexAsync(SystemClock.UtcNow);

                    Assert.True((await _client.Indices.ExistsAsync(version1Index.GetVersionedIndex(SystemClock.UtcNow))).Exists);
                    Assert.Equal(1, await version1Index.GetCurrentVersionAsync());

                    // delete all aliases
                    await _configuration.Cache.RemoveAllAsync();
                    await DeleteAliasesAsync(version1Index.GetVersionedIndex(SystemClock.UtcNow));

                    using (new DisposableAction(() => version2Index.DeleteAsync().GetAwaiter().GetResult())) {
                        await version2Index.ConfigureAsync();

                        await version2Index.EnsureIndexAsync(SystemClock.UtcNow);

                        Assert.True((await _client.Indices.ExistsAsync(version2Index.GetVersionedIndex(SystemClock.UtcNow))).Exists);
                        Assert.Equal(2, await version2Index.GetCurrentVersionAsync());

                        // delete all aliases
                        await _configuration.Cache.RemoveAllAsync();
                        await DeleteAliasesAsync(version2Index.GetVersionedIndex(SystemClock.UtcNow));

                        await _client.Indices.RefreshAsync(Indices.All);

                        var aliasesResponse = await _client.Indices.GetAliasAsync($"{version1Index.GetVersionedIndex(SystemClock.UtcNow)},{version2Index.GetVersionedIndex(SystemClock.UtcNow)}");

                        Assert.Empty(aliasesResponse.Indices.Values.SelectMany(i => i.Aliases));

                        // Indexes exist but no alias so the oldest index version will be used.
                        Assert.Equal(1, await version1Index.GetCurrentVersionAsync());
                        Assert.Equal(1, await version2Index.GetCurrentVersionAsync());

                        await version1Index.MaintainAsync();

                        aliasesResponse = await _client.Indices.GetAliasAsync(version1Index.GetVersionedIndex(SystemClock.UtcNow));

                        Assert.Equal(version1Index.Aliases.Count + 1, aliasesResponse.Indices.Single().Value.Aliases.Count);
                        aliasesResponse = await _client.Indices.GetAliasAsync(version2Index.GetVersionedIndex(SystemClock.UtcNow));

                        Assert.Equal(0, aliasesResponse.Indices.Single().Value.Aliases.Count);

                        Assert.Equal(1, await version1Index.GetCurrentVersionAsync());
                        Assert.Equal(1, await version2Index.GetCurrentVersionAsync());
                    }
                }
            }
        }