public void AmbientSettingInt()
        {
            // use a local override in case we ran another test that left the global or local settings set set to something else on this thread
            BasicAmbientSettingsSet settings = new BasicAmbientSettingsSet(nameof(AmbientSettingInt));

            using (ScopedLocalServiceOverride <IAmbientSettingsSet> localOverrideTest = new ScopedLocalServiceOverride <IAmbientSettingsSet>(settings))
            {
                AmbientSetting <int> value;
                value = new AmbientSetting <int>("int-setting", "", s => string.IsNullOrEmpty(s) ? 0 : Int32.Parse(s));
                Assert.AreEqual(0, value.Value);
                value = new AmbientSetting <int>("int-setting-2", "", s => Int32.Parse(s), "1");
                Assert.AreEqual(1, value.Value);
                value = new AmbientSetting <int>("int-setting-3", "", s => Int32.Parse(s), "1");
                Assert.AreEqual(1, value.Value);

                // test changing the setting without an event listener
                settings.ChangeSetting("int-setting-3", "5");
                // test changing the setting to the same value without an event listener
                settings.ChangeSetting("int-setting-3", "5");
                // test changing the setting to null so we fall through to the global settings set
                settings.ChangeSetting("int-setting-3", null);
                int settingValue = value.Value;
                Assert.AreEqual(1, value.Value);
            }
        }
        public async Task LoggerSettings()
        {
            BasicAmbientSettingsSet settingsSet = new BasicAmbientSettingsSet("LoggerSettingsTest");

            settingsSet.ChangeSetting(nameof(BasicAmbientLogger) + "-LogLevel", AmbientLogLevel.Error.ToString());
            settingsSet.ChangeSetting(nameof(BasicAmbientLogger) + "-TypeFilter", "AllowedLoggerType");
            settingsSet.ChangeSetting(nameof(BasicAmbientLogger) + "-CategoryFilter", "AllowedCategory");
            using (ScopedLocalServiceOverride <IAmbientSettingsSet> o = new ScopedLocalServiceOverride <IAmbientSettingsSet>(settingsSet))
            {
                AmbientLogger <AllowedLoggerType> logger = new AmbientLogger <AllowedLoggerType>();
                logger.Log(new ApplicationException());

                AmbientLogger <TestLogger> testlogger = new AmbientLogger <TestLogger>();
                testlogger.Log(new ApplicationException());
                testlogger.Log(new ApplicationException(), "category", AmbientLogLevel.Information);
                testlogger.Log("test message");
                testlogger.Log(() => "test message");
                testlogger.Log("test message", "category", AmbientLogLevel.Information);
                testlogger.Log("test message", "AllowedCategory", AmbientLogLevel.Information);
                testlogger.Log("Exception during test", new ApplicationException());
                testlogger.Log(() => "Exception during test", new ApplicationException());
                testlogger.Log("Exception during test", new ApplicationException(), "category", AmbientLogLevel.Information);
                testlogger.Log("Exception during test", new ApplicationException(), "AllowedCategory", AmbientLogLevel.Information);
                testlogger.Log("Exception during test", new ApplicationException(), "category", AmbientLogLevel.Information);
                testlogger.Log("Exception during test", new ApplicationException(), "AllowedCategory", AmbientLogLevel.Information);

                if (_Logger.Local != null)
                {
                    await _Logger.Local.Flush();
                }
            }
        }
 public void ServiceProfilerNoListener()
 {
     using (ScopedLocalServiceOverride <IAmbientServiceProfiler> o = new ScopedLocalServiceOverride <IAmbientServiceProfiler>(new BasicAmbientServiceProfiler()))
         using (AmbientClock.Pause())
         {
             _ServiceProfiler.Local?.SwitchSystem(nameof(ServiceProfilerNull));
         }
 }
        public void AmbientServiceProfilerCoordinatorSettingsNonCurrent()
        {
            string system1 = "DynamoDB/Table:My-table/Partition:342644/Result:Success";
            string system2 = "S3/Bucket:My-bucket/Prefix:abcdefg/Result:Retry";
            string system3 = "SQL/Database:My-database/Table:User/Result:Failed";
            BasicAmbientSettingsSet settingsSet = new BasicAmbientSettingsSet(nameof(AmbientServiceProfilerCoordinatorSettingsNonCurrent));

            settingsSet.ChangeSetting(nameof(AmbientServiceProfilerCoordinator) + "-DefaultSystemGroupTransform", "(?:([^:/]+)(?:(/Database:[^:/]*)|(/Bucket:[^:/]*)|(/Result:[^:/]*)|(?:/[^/]*))*)");
            using (ScopedLocalServiceOverride <IAmbientSettingsSet> o = new ScopedLocalServiceOverride <IAmbientSettingsSet>(settingsSet))
                using (ScopedLocalServiceOverride <IAmbientServiceProfiler> p = new ScopedLocalServiceOverride <IAmbientServiceProfiler>(new BasicAmbientServiceProfiler()))
                    using (AmbientClock.Pause())
                        using (AmbientServiceProfilerCoordinator coordinator = new AmbientServiceProfilerCoordinator())
                            using (IAmbientServiceProfile scopeProfile = coordinator.CreateCallContextProfiler(nameof(ServiceProfilerBasic)))
                            {
                                _ServiceProfiler.Local?.SwitchSystem(system1);
                                AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(5));

                                _ServiceProfiler.Local?.SwitchSystem(system2);
                                AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(200));

                                _ServiceProfiler.Local?.SwitchSystem(system3);
                                AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(3000));

                                _ServiceProfiler.Local?.SwitchSystem(system1);
                                AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(5));

                                _ServiceProfiler.Local?.SwitchSystem("noreport");

                                if (scopeProfile != null)
                                {
                                    foreach (AmbientServiceProfilerAccumulator stats in scopeProfile.ProfilerStatistics)
                                    {
                                        if (stats.Group.StartsWith("DynamoDB"))
                                        {
                                            Assert.AreEqual("DynamoDB/Result:Success", stats.Group);
                                            Assert.AreEqual(TimeSpan.FromMilliseconds(10), stats.TimeUsed);
                                            Assert.AreEqual(2, stats.ExecutionCount);
                                        }
                                        else if (stats.Group.StartsWith("S3"))
                                        {
                                            Assert.AreEqual("S3/Bucket:My-bucket/Result:Retry", stats.Group);
                                            Assert.AreEqual(TimeSpan.FromMilliseconds(200), stats.TimeUsed);
                                            Assert.AreEqual(1, stats.ExecutionCount);
                                        }
                                        else if (stats.Group.StartsWith("SQL"))
                                        {
                                            Assert.AreEqual("SQL/Database:My-database/Result:Failed", stats.Group);
                                            Assert.AreEqual(TimeSpan.FromMilliseconds(3000), stats.TimeUsed);
                                            Assert.AreEqual(1, stats.ExecutionCount);
                                        }
                                    }
                                }
                            }
        }
        public async Task StatusAuditorHistory()
        {
            BasicAmbientSettingsSet settings = new BasicAmbientSettingsSet(nameof(StatusAuditorHistory));

            settings.ChangeSetting(nameof(StatusChecker) + "-HistoryRetentionMinutes", "5");
            settings.ChangeSetting(nameof(StatusChecker) + "-HistoryRetentionEntries", "10");
            using (ScopedLocalServiceOverride <IAmbientSettingsSet> localOverrideTest = new ScopedLocalServiceOverride <IAmbientSettingsSet>(settings))

                using (AmbientClock.Pause())
                {
                    using (StatusAuditorTest test = new StatusAuditorTest(nameof(StatusAuditorTest)))
                    {
                        StatusResults results = await test.GetStatus();

                        Assert.AreEqual("StatusAuditorTest", test.TargetSystem);
                        AmbientClock.SkipAhead(TimeSpan.FromSeconds(30)); // since we're rotating the status rating, the frequency will vary because of that
                        Assert.IsTrue(test.History.Count() <= 10);
                        Assert.IsTrue(test.History.First().Time >= AmbientClock.UtcNow.AddMinutes(-5));
                        AmbientClock.SkipAhead(TimeSpan.FromSeconds(30));
                        Assert.IsTrue(test.History.Count() <= 10);
                        Assert.IsTrue(test.History.First().Time >= AmbientClock.UtcNow.AddMinutes(-5));
                        AmbientClock.SkipAhead(TimeSpan.FromSeconds(30));
                        Assert.IsTrue(test.History.Count() <= 10);
                        Assert.IsTrue(test.History.First().Time >= AmbientClock.UtcNow.AddMinutes(-5));
                        AmbientClock.SkipAhead(TimeSpan.FromSeconds(30));
                        Assert.IsTrue(test.History.Count() <= 10);
                        Assert.IsTrue(test.History.First().Time >= AmbientClock.UtcNow.AddMinutes(-5));
                        AmbientClock.SkipAhead(TimeSpan.FromSeconds(30));
                        Assert.IsTrue(test.History.Count() <= 10);
                        Assert.IsTrue(test.History.First().Time >= AmbientClock.UtcNow.AddMinutes(-5));
                        AmbientClock.SkipAhead(TimeSpan.FromSeconds(30));
                        Assert.IsTrue(test.History.Count() <= 10);
                        Assert.IsTrue(test.History.First().Time >= AmbientClock.UtcNow.AddMinutes(-5));
                        AmbientClock.SkipAhead(TimeSpan.FromSeconds(30));
                        Assert.IsTrue(test.History.Count() <= 10);
                        Assert.IsTrue(test.History.First().Time >= AmbientClock.UtcNow.AddMinutes(-5));
                        AmbientClock.SkipAhead(TimeSpan.FromSeconds(30)); // this one starts kicking out history items due to count (this was determined by simply trying it out, but that part doesn't matter for this test)
                        Assert.IsTrue(test.History.Count() <= 10);
                        Assert.IsTrue(test.History.First().Time >= AmbientClock.UtcNow.AddMinutes(-5));
                        AmbientClock.SkipAhead(TimeSpan.FromSeconds(30));
                        Assert.IsTrue(test.History.Count() <= 10);
                        Assert.IsTrue(test.History.First().Time >= AmbientClock.UtcNow.AddMinutes(-5));
                        AmbientClock.SkipAhead(TimeSpan.FromSeconds(30));
                        Assert.IsTrue(test.History.Count() <= 10);
                        Assert.IsTrue(test.History.First().Time >= AmbientClock.UtcNow.AddMinutes(-5));
                        AmbientClock.SkipAhead(TimeSpan.FromSeconds(30)); // this one should start kicking history items out due to time
                        Assert.IsTrue(test.History.Count() <= 10);
                        Assert.IsTrue(test.History.First().Time >= AmbientClock.UtcNow.AddMinutes(-5));
                        AmbientClock.SkipAhead(TimeSpan.FromSeconds(30));
                        Assert.IsTrue(test.History.Count() <= 10);
                        Assert.IsTrue(test.History.First().Time >= AmbientClock.UtcNow.AddMinutes(-5));
                    }
                }
        }
 public void ServiceProfilerNull()
 {
     using (ScopedLocalServiceOverride <IAmbientServiceProfiler> o = new ScopedLocalServiceOverride <IAmbientServiceProfiler>(null))
         using (AmbientServiceProfilerCoordinator coordinator = new AmbientServiceProfilerCoordinator())
             using (IAmbientServiceProfile processProfile = coordinator.CreateProcessProfiler(nameof(ServiceProfilerNull)))
                 using (IDisposable timeWindowProfile = coordinator.CreateTimeWindowProfiler(nameof(ServiceProfilerNull), TimeSpan.FromMilliseconds(100), p => Task.CompletedTask))
                     using (IAmbientServiceProfile scopeProfile = coordinator.CreateCallContextProfiler(nameof(ServiceProfilerNull)))
                         using (AmbientClock.Pause())
                         {
                             _ServiceProfiler.Local?.SwitchSystem(nameof(ServiceProfilerNull));
                         }
 }
Exemple #7
0
        public void LocalServiceOverride()
        {
            ILocalOverrideTest oldGlobal        = _LocalOverrideTest.Global;
            ILocalOverrideTest oldLocalOverride = _LocalOverrideTest.Override;

            using (ScopedLocalServiceOverride <ILocalOverrideTest> o = new ScopedLocalServiceOverride <ILocalOverrideTest>(null))
            {
                Assert.IsNull(_LocalOverrideTest.Local);
                Assert.AreEqual(oldGlobal, o.OldGlobal);
                Assert.AreEqual(oldLocalOverride, o.OldOverride);
            }
        }
 public void ServiceProfilerNullOnWindowComplete()
 {
     using (ScopedLocalServiceOverride <IAmbientServiceProfiler> o = new ScopedLocalServiceOverride <IAmbientServiceProfiler>(new BasicAmbientServiceProfiler()))
         using (AmbientServiceProfilerCoordinator coordinator = new AmbientServiceProfilerCoordinator())
         {
             Assert.ThrowsException <ArgumentNullException>(
                 () =>
             {
                 using (IDisposable timeWindowProfile = coordinator.CreateTimeWindowProfiler(nameof(ServiceProfilerNull), TimeSpan.FromMilliseconds(100), null !))
                 {
                 }
             });
         }
 }
 public void NoAmbientSetting()
 {
     using (ScopedLocalServiceOverride <IAmbientSettingsSet> localOverrideTest = new ScopedLocalServiceOverride <IAmbientSettingsSet>(null))
     {
         AmbientSetting <int> value;
         value = new AmbientSetting <int>("int-setting", "", s => string.IsNullOrEmpty(s) ? 0 : Int32.Parse(s));
         Assert.AreEqual(0, value.Value);
         value = new AmbientSetting <int>("int-setting-2", "", s => Int32.Parse(s), "1");
         Assert.AreEqual(1, value.Value);
         value = new AmbientSetting <int>("int-setting-4", "", s => Int32.Parse(s), "-1");
         Assert.AreEqual(-1, value.Value);
         SettingsSetSetting <int> settingsSetSetting = new SettingsSetSetting <int>(_SettingsSet, nameof(NoAmbientSetting) + "4", "", s => Int32.Parse(s), "4");
         Assert.AreEqual(4, settingsSetSetting.Value);
     }
 }
        public void GetCancellationToken()
        {
            IAmbientProgress progress = AmbientProgressService.GlobalProgress;

            progress?.ResetCancellation(); // make a new cancellation in case the source was canceled in this execution context during a previous test
            CancellationToken token = progress?.CancellationToken ?? default(CancellationToken);

            Assert.IsNotNull(token);

            using (ScopedLocalServiceOverride <IAmbientProgressService> LocalServiceOverride = new ScopedLocalServiceOverride <IAmbientProgressService>(null))
            {
                IAmbientProgress  noProgress = AmbientProgressService.Progress;
                CancellationToken cancel     = noProgress?.CancellationToken ?? default(CancellationToken);
            }
        }
        public void ServiceProfilerCloseSampleWithRepeat()
        {
            using (ScopedLocalServiceOverride <IAmbientServiceProfiler> o = new ScopedLocalServiceOverride <IAmbientServiceProfiler>(new BasicAmbientServiceProfiler()))
                using (AmbientClock.Pause())
                    using (AmbientServiceProfilerCoordinator coordinator = new AmbientServiceProfilerCoordinator())
                        using (IDisposable timeWindowProfile = coordinator.CreateTimeWindowProfiler(nameof(ServiceProfilerCloseSampleWithRepeat), TimeSpan.FromMilliseconds(100), p => Task.CompletedTask))
                        {
                            AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(100)); // this should trigger the first window and it should have only the default "" entry

                            _ServiceProfiler.Local?.SwitchSystem(nameof(ServiceProfilerCloseSampleWithRepeat) + "1");
                            AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(10));
                            _ServiceProfiler.Local?.SwitchSystem(null);
                            AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(10));
                            _ServiceProfiler.Local?.SwitchSystem("ServiceProfilerCloseSampleWithRepeat1");
                            AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(80)); // this should trigger the second window, which should close with an augmentation of the first ServiceProfilerCloseSampleWithRepeat1 entry
                            _ServiceProfiler.Local?.SwitchSystem(null);
                        }
        }
        public void AmbientSettingsWithSetName()
        {
            string settingName            = nameof(AmbientSettingsWithSetName) + "-int-setting";
            AmbientSetting <int> setting3 = new AmbientSetting <int>(settingName, "", s => Int32.Parse(s), "1");

            Assert.AreEqual(1, setting3.Value);
            Assert.IsNotNull(setting3.GetValueWithSetName().Item2);
            IMutableAmbientSettingsSet settingsSet = _SettingsSet.Global as IMutableAmbientSettingsSet;

            if (settingsSet != null)
            {
                settingsSet.ChangeSetting(settingName, "2");
                Assert.AreEqual(2, setting3.Value);
                Assert.AreEqual(BasicAmbientSettingsSet.DefaultSetName, setting3.GetValueWithSetName().Item2);
            }
            // use a local override in case we ran another test that left the global or local settings set set to something else on this thread
            BasicAmbientSettingsSet settings = new BasicAmbientSettingsSet(nameof(AmbientSettingsWithSetName));

            using (ScopedLocalServiceOverride <IAmbientSettingsSet> localOverrideTest = new ScopedLocalServiceOverride <IAmbientSettingsSet>(settings))
            {
                AmbientSetting <int> setting;
                setting = new AmbientSetting <int>(nameof(AmbientSettingsWithSetName) + "-int-setting-1", "", s => string.IsNullOrEmpty(s) ? 0 : Int32.Parse(s));
                Assert.AreEqual(0, setting.Value);
                Assert.IsNotNull(setting.GetValueWithSetName().Item2);
                setting = new AmbientSetting <int>(nameof(AmbientSettingsWithSetName) + "-int-setting-2", "", s => Int32.Parse(s), "1");
                settings.ChangeSetting(nameof(AmbientSettingsWithSetName) + "-int-setting-2", "1");
                Assert.AreEqual(1, setting.Value);
                Assert.AreEqual(nameof(AmbientSettingsWithSetName), setting.GetValueWithSetName().Item2);
                setting = new AmbientSetting <int>(settingName, "", s => Int32.Parse(s), "1");
                settings.ChangeSetting(settingName, "3");
                Assert.AreEqual(3, setting3.Value);
                Assert.AreEqual(nameof(AmbientSettingsWithSetName), setting3.GetValueWithSetName().Item2);

                // test changing the setting to null so we fall through to the global settings set
                settings.ChangeSetting(settingName, null);
                int settingValue = setting.Value;
                Assert.AreEqual(1, setting.Value);
                Assert.IsNotNull(setting3.GetValueWithSetName().Item2);
            }
        }
Exemple #13
0
        public async Task CacheNone()
        {
            using (ScopedLocalServiceOverride <IAmbientCache> localCache = new ScopedLocalServiceOverride <IAmbientCache>(null))
            {
                TestCache ret;
                AmbientCache <TestCache> cache = new AmbientCache <TestCache>();
                await cache.Store(true, "Test1", this);

                ret = await cache.Retrieve <TestCache>("Test1");

                Assert.IsNull(ret);
                await cache.Remove <TestCache>(true, "Test1");

                ret = await cache.Retrieve <TestCache>("Test1", null);

                Assert.IsNull(ret);
                await cache.Clear();

                ret = await cache.Retrieve <TestCache>("Test1", null);

                Assert.IsNull(ret);
            }
        }
Exemple #14
0
        public async Task CacheAmbient()
        {
            AmbientSettingsOverride localSettingsSet = new AmbientSettingsOverride(TestCacheSettingsDictionary, nameof(CacheAmbient));

            using (new ScopedLocalServiceOverride <IAmbientSettingsSet>(localSettingsSet))
            {
                IAmbientCache localOverride = new BasicAmbientCache();
                using (ScopedLocalServiceOverride <IAmbientCache> localCache = new ScopedLocalServiceOverride <IAmbientCache>(localOverride))
                {
                    TestCache ret;
                    AmbientCache <TestCache> cache = new AmbientCache <TestCache>();
                    await cache.Store(true, "Test1", this);

                    await cache.Store(true, "Test1", this);

                    ret = await cache.Retrieve <TestCache>("Test1", null);

                    Assert.AreEqual(this, ret);
                    await cache.Remove <TestCache>(true, "Test1");

                    ret = await cache.Retrieve <TestCache>("Test1", null);

                    Assert.IsNull(ret);
                    await cache.Store(true, "Test2", this, null, DateTime.MinValue);

                    ret = await cache.Retrieve <TestCache>("Test2", null);

                    Assert.AreEqual(this, ret);
                    await Eject(cache, 2);

                    ret = await cache.Retrieve <TestCache>("Test2", null);

                    Assert.IsNull(ret);
                    await cache.Store(true, "Test3", this, TimeSpan.FromMinutes(-1));

                    ret = await cache.Retrieve <TestCache>("Test3", null);

                    Assert.IsNull(ret);
                    await cache.Store(true, "Test4", this, TimeSpan.FromMinutes(10), DateTime.UtcNow.AddMinutes(11));

                    ret = await cache.Retrieve <TestCache>("Test4", null);

                    Assert.AreEqual(this, ret);
                    await cache.Store(true, "Test5", this, TimeSpan.FromMinutes(10), DateTime.Now.AddMinutes(11));

                    ret = await cache.Retrieve <TestCache>("Test5", null);

                    Assert.AreEqual(this, ret);
                    await cache.Store(true, "Test6", this, TimeSpan.FromMinutes(60), DateTime.UtcNow.AddMinutes(10));

                    ret = await cache.Retrieve <TestCache>("Test6", null);

                    Assert.AreEqual(this, ret);
                    ret = await cache.Retrieve <TestCache>("Test6", TimeSpan.FromMinutes(10));

                    Assert.AreEqual(this, ret);
                    await Eject(cache, 50);

                    await cache.Clear();

                    ret = await cache.Retrieve <TestCache>("Test6", null);

                    Assert.IsNull(ret);
                }
            }
        }
        public void AmbientServiceProfilerCoordinatorOverrideGroupTransform()
        {
            string system1Start   = "DynamoDB/Table:My-table/Partition:342644";
            string system1End     = "DynamoDB/Table:My-table/Partition:342644/Result:Success";
            string system2        = "S3/Bucket:My-bucket/Prefix:abcdefg/Result:Retry";
            string system3        = "SQL/Database:My-database/Table:User/Result:Failed";
            string groupTransform = "(?:([^:/]+)(?:(/Database:[^:/]*)|(/Bucket:[^:/]*)|(/Result:[^:/]*)|(?:/[^/]*))*)";
            IAmbientServiceProfile timeWindowProfile = null;

            using (AmbientClock.Pause())
                using (ScopedLocalServiceOverride <IAmbientServiceProfiler> o = new ScopedLocalServiceOverride <IAmbientServiceProfiler>(new BasicAmbientServiceProfiler()))
                    using (AmbientServiceProfilerCoordinator coordinator = new AmbientServiceProfilerCoordinator())
                        using (IAmbientServiceProfile processProfile = coordinator.CreateProcessProfiler(nameof(AmbientServiceProfilerCoordinatorOverrideGroupTransform), groupTransform))
                            using (IDisposable timeWindowProfiler = coordinator.CreateTimeWindowProfiler(nameof(AmbientServiceProfilerCoordinatorOverrideGroupTransform), TimeSpan.FromMilliseconds(10000), p => { timeWindowProfile = p; return(Task.CompletedTask); }, groupTransform))
                                using (IAmbientServiceProfile scopeProfile = coordinator.CreateCallContextProfiler(nameof(AmbientServiceProfilerCoordinatorOverrideGroupTransform), groupTransform))
                                {
                                    _ServiceProfiler.Local?.SwitchSystem(system1Start);
                                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(5));

                                    _ServiceProfiler.Local?.SwitchSystem(system2, system1End);
                                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(200));

                                    _ServiceProfiler.Local?.SwitchSystem(system3);
                                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(3000));

                                    _ServiceProfiler.Local?.SwitchSystem(system1End);
                                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(5));

                                    _ServiceProfiler.Local?.SwitchSystem("noreport");

                                    if (scopeProfile != null)
                                    {
                                        foreach (AmbientServiceProfilerAccumulator stats in scopeProfile.ProfilerStatistics)
                                        {
                                            if (stats.Group.StartsWith("DynamoDB"))
                                            {
                                                Assert.AreEqual("DynamoDB/Result:Success", stats.Group);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(10), stats.TimeUsed);
                                                Assert.AreEqual(2, stats.ExecutionCount);
                                            }
                                            else if (stats.Group.StartsWith("S3"))
                                            {
                                                Assert.AreEqual("S3/Bucket:My-bucket/Result:Retry", stats.Group);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(200), stats.TimeUsed);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                            }
                                            else if (stats.Group.StartsWith("SQL"))
                                            {
                                                Assert.AreEqual("SQL/Database:My-database/Result:Failed", stats.Group);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(3000), stats.TimeUsed);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                            }
                                        }
                                    }
                                    if (processProfile != null)
                                    {
                                        foreach (AmbientServiceProfilerAccumulator stats in processProfile.ProfilerStatistics)
                                        {
                                            if (stats.Group.StartsWith("DynamoDB"))
                                            {
                                                Assert.AreEqual("DynamoDB/Result:Success", stats.Group);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(10), stats.TimeUsed);
                                                Assert.AreEqual(2, stats.ExecutionCount);
                                            }
                                            else if (stats.Group.StartsWith("S3"))
                                            {
                                                Assert.AreEqual("S3/Bucket:My-bucket/Result:Retry", stats.Group);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(200), stats.TimeUsed);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                            }
                                            else if (stats.Group.StartsWith("SQL"))
                                            {
                                                Assert.AreEqual("SQL/Database:My-database/Result:Failed", stats.Group);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(3000), stats.TimeUsed);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                            }
                                        }
                                    }
                                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(10000));
                                    if (timeWindowProfile != null)
                                    {
                                        foreach (AmbientServiceProfilerAccumulator stats in timeWindowProfile.ProfilerStatistics)
                                        {
                                            if (stats.Group.StartsWith("DynamoDB"))
                                            {
                                                Assert.AreEqual("DynamoDB/Result:Success", stats.Group);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(10), stats.TimeUsed);
                                                Assert.AreEqual(2, stats.ExecutionCount);
                                            }
                                            else if (stats.Group.StartsWith("S3"))
                                            {
                                                Assert.AreEqual("S3/Bucket:My-bucket/Result:Retry", stats.Group);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(200), stats.TimeUsed);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                            }
                                            else if (stats.Group.StartsWith("SQL"))
                                            {
                                                Assert.AreEqual("SQL/Database:My-database/Result:Failed", stats.Group);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(3000), stats.TimeUsed);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                            }
                                        }
                                    }
                                }
        }
Exemple #16
0
        public async Task CacheExpiration()
        {
            IAmbientCache localOverride = new BasicAmbientCache();

            using (AmbientClock.Pause())
                using (ScopedLocalServiceOverride <IAmbientCache> localCache = new ScopedLocalServiceOverride <IAmbientCache>(localOverride))
                {
                    string    keyName1 = nameof(CacheExpiration) + "1";
                    string    keyName2 = nameof(CacheExpiration) + "2";
                    string    keyName3 = nameof(CacheExpiration) + "3";
                    string    keyName4 = nameof(CacheExpiration) + "4";
                    string    keyName5 = nameof(CacheExpiration) + "5";
                    string    keyName6 = nameof(CacheExpiration) + "6";
                    string    keyName7 = nameof(CacheExpiration) + "7";
                    TestCache ret;
                    AmbientCache <TestCache> cache = new AmbientCache <TestCache>();
                    await cache.Store(true, keyName1, this, TimeSpan.FromMilliseconds(50));

                    await cache.Store(true, keyName1, this, TimeSpan.FromMilliseconds(51));

                    await cache.Store(true, keyName2, this);

                    await cache.Store(true, keyName2, this);

                    await cache.Store(true, keyName3, this, TimeSpan.FromMilliseconds(-51)); // this should never get cached because the time span is negative

                    await cache.Store(true, keyName3, this, TimeSpan.FromMilliseconds(-50)); // this should never get cached because the time span is negative

                    await cache.Store(true, keyName4, this);

                    await cache.Store(true, keyName4, this);

                    await cache.Store(true, keyName5, this, TimeSpan.FromMilliseconds(50));

                    await cache.Store(true, keyName5, this, TimeSpan.FromMilliseconds(50));

                    await cache.Store(true, keyName6, this, TimeSpan.FromMilliseconds(1000));

                    await cache.Store(true, keyName6, this, TimeSpan.FromMilliseconds(1000));

                    await cache.Store(true, keyName7, this, TimeSpan.FromMilliseconds(75));

                    await cache.Store(true, keyName7, this, TimeSpan.FromMilliseconds(1000));

                    ret = await cache.Retrieve <TestCache>(keyName1);

                    Assert.IsNotNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName2);

                    Assert.IsNotNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName3);

                    Assert.IsNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName4);

                    Assert.IsNotNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName5);

                    Assert.IsNotNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName6);

                    Assert.IsNotNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName7);

                    Assert.IsNotNull(ret);
                    await Eject(cache, 1); // this should eject 1 because it's the LRU timed and 2 because it's the LRU untimed

                    ret = await cache.Retrieve <TestCache>(keyName1);

                    Assert.IsNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName2);

                    Assert.IsNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName4);

                    Assert.IsNotNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName5);

                    Assert.IsNotNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName6);

                    Assert.IsNotNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName7);

                    Assert.IsNotNull(ret);
                    await Eject(cache, 1); // this should eject 5 because it's the LRU timed and 4 because it's the LRU untimed

                    ret = await cache.Retrieve <TestCache>(keyName4);

                    Assert.IsNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName5);

                    Assert.IsNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName6);

                    Assert.IsNotNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName7);

                    Assert.IsNotNull(ret);
                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(100));
                    await Eject(cache, 1); // this should eject 6 because it's the LRU timed but not 7 because only the first entry is expired, and not untimed LRU

                    ret = await cache.Retrieve <TestCache>(keyName6);

                    Assert.IsNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName7);

                    Assert.IsNotNull(ret);
                }
        }
Exemple #17
0
        public async Task CacheSkipAndEmptyEject()
        {
            AmbientSettingsOverride localSettingsSet = new AmbientSettingsOverride(AllowEmptyCacheSettingsDictionary, nameof(CacheAmbient));

            using (AmbientClock.Pause())
                using (new ScopedLocalServiceOverride <IAmbientSettingsSet>(localSettingsSet))
                {
                    IAmbientCache localOverride = new BasicAmbientCache();
                    using (ScopedLocalServiceOverride <IAmbientCache> localCache = new ScopedLocalServiceOverride <IAmbientCache>(localOverride))
                    {
                        string keyName1 = nameof(CacheExpiration) + "1";
                        string keyName2 = nameof(CacheExpiration) + "2";
                        string keyName3 = nameof(CacheExpiration) + "3";
                        //string keyName4 = nameof(CacheExpiration) + "4";
                        //string keyName5 = nameof(CacheExpiration) + "5";
                        //string keyName6 = nameof(CacheExpiration) + "6";
                        //string keyName7 = nameof(CacheExpiration) + "7";
                        TestCache ret;
                        AmbientCache <TestCache> cache = new AmbientCache <TestCache>();
                        await cache.Store(true, keyName1, this, TimeSpan.FromMilliseconds(100));

                        await cache.Store(true, keyName2, this, TimeSpan.FromMilliseconds(50));

                        await cache.Store(true, keyName3, this, TimeSpan.FromMilliseconds(100));

                        ret = await cache.Retrieve <TestCache>(keyName1);

                        Assert.IsNotNull(ret);
                        ret = await cache.Retrieve <TestCache>(keyName2, TimeSpan.FromMilliseconds(100));

                        Assert.IsNotNull(ret);
                        ret = await cache.Retrieve <TestCache>(keyName3);

                        Assert.IsNotNull(ret);
                        AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(50));
                        await Eject(cache, 1); // this should eject 1 because it's the LRU timed, and the first timed entry for 2 because that's expired, but 2 should remain with a refreshed entry

                        ret = await cache.Retrieve <TestCache>(keyName1);

                        Assert.IsNull(ret);
                        ret = await cache.Retrieve <TestCache>(keyName2, TimeSpan.FromMilliseconds(100));

                        Assert.IsNotNull(ret);
                        ret = await cache.Retrieve <TestCache>(keyName3);

                        Assert.IsNotNull(ret);
                        await Eject(cache, 1); // this should skip 2 because it's bee refershed again and eject 3 because it's the LRU timed

                        ret = await cache.Retrieve <TestCache>(keyName1);

                        Assert.IsNull(ret);
                        ret = await cache.Retrieve <TestCache>(keyName2);

                        Assert.IsNotNull(ret);
                        ret = await cache.Retrieve <TestCache>(keyName3);

                        Assert.IsNull(ret);

                        // change key2 to be untimed
                        await cache.Store(true, keyName2, this);
                        await Eject(cache, 1); // this should skip over the timed entry for 2 but then eject it because it is untimed
                    }
                }
        }
        public void ServiceProfilerBasic()
        {
            using (ScopedLocalServiceOverride <IAmbientServiceProfiler> o = new ScopedLocalServiceOverride <IAmbientServiceProfiler>(new BasicAmbientServiceProfiler()))
                using (AmbientClock.Pause())
                    using (AmbientServiceProfilerCoordinator coordinator = new AmbientServiceProfilerCoordinator())
                        using (IAmbientServiceProfile processProfile = coordinator.CreateProcessProfiler(nameof(ServiceProfilerBasic)))
                            using (IDisposable timeWindowProfile = coordinator.CreateTimeWindowProfiler(nameof(ServiceProfilerBasic), TimeSpan.FromMilliseconds(100), p => Task.CompletedTask))
                                using (IAmbientServiceProfile scopeProfile = coordinator.CreateCallContextProfiler(nameof(ServiceProfilerBasic)))
                                {
                                    _ServiceProfiler.Local?.SwitchSystem("ServiceProfilerBasic1");
                                    Assert.AreEqual(nameof(ServiceProfilerBasic), processProfile?.ScopeName);
                                    if (processProfile != null)
                                    {
                                        foreach (AmbientServiceProfilerAccumulator stats in processProfile.ProfilerStatistics)
                                        {
                                            if (string.IsNullOrEmpty(stats.Group))
                                            {
                                                Assert.AreEqual("", stats.Group);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                                Assert.AreEqual(0, stats.TotalStopwatchTicksUsed);
                                            }
                                            else
                                            {
                                                Assert.AreEqual("ServiceProfilerBasic1", stats.Group);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                                Assert.AreEqual(0, stats.TotalStopwatchTicksUsed);
                                            }
                                        }
                                    }
                                    Assert.AreEqual(nameof(ServiceProfilerBasic), scopeProfile?.ScopeName);
                                    if (scopeProfile != null)
                                    {
                                        foreach (AmbientServiceProfilerAccumulator stats in scopeProfile.ProfilerStatistics)
                                        {
                                            if (string.IsNullOrEmpty(stats.Group))
                                            {
                                                Assert.AreEqual("", stats.Group);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                                Assert.AreEqual(0, stats.TotalStopwatchTicksUsed);
                                            }
                                            else
                                            {
                                                Assert.AreEqual("ServiceProfilerBasic1", stats.Group);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                                Assert.AreEqual(0, stats.TotalStopwatchTicksUsed);
                                            }
                                        }
                                    }

                                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(100));

                                    _ServiceProfiler.Local?.SwitchSystem(null);

                                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(113));

                                    _ServiceProfiler.Local?.SwitchSystem("ServiceProfilerBasic2");

                                    if (scopeProfile != null)
                                    {
                                        foreach (AmbientServiceProfilerAccumulator stats in scopeProfile.ProfilerStatistics)
                                        {
                                            if (string.IsNullOrEmpty(stats.Group))
                                            {
                                                Assert.AreEqual("", stats.Group);
                                                Assert.AreEqual(2, stats.ExecutionCount);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(113), stats.TimeUsed);
                                            }
                                            else if (stats.Group.EndsWith("1"))
                                            {
                                                Assert.AreEqual("ServiceProfilerBasic1", stats.Group);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(100), stats.TimeUsed);
                                            }
                                            else if (stats.Group.EndsWith("2"))
                                            {
                                                Assert.AreEqual("ServiceProfilerBasic2", stats.Group);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(0), stats.TimeUsed);
                                            }
                                        }
                                    }

                                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(100));

                                    _ServiceProfiler.Local?.SwitchSystem(null);

                                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(113));

                                    _ServiceProfiler.Local?.SwitchSystem("ServiceProfilerBasic3");

                                    if (scopeProfile != null)
                                    {
                                        foreach (AmbientServiceProfilerAccumulator stats in scopeProfile.ProfilerStatistics)
                                        {
                                            if (string.IsNullOrEmpty(stats.Group))
                                            {
                                                Assert.AreEqual("", stats.Group);
                                                Assert.AreEqual(3, stats.ExecutionCount);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(226), stats.TimeUsed);
                                            }
                                            else if (stats.Group.EndsWith("1"))
                                            {
                                                Assert.AreEqual("ServiceProfilerBasic1", stats.Group);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(100), stats.TimeUsed);
                                            }
                                            else if (stats.Group.EndsWith("2"))
                                            {
                                                Assert.AreEqual("ServiceProfilerBasic2", stats.Group);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(100), stats.TimeUsed);
                                            }
                                            else if (stats.Group.EndsWith("3"))
                                            {
                                                Assert.AreEqual("ServiceProfilerBasic3", stats.Group);
                                                Assert.AreEqual(1, stats.ExecutionCount);
                                                Assert.AreEqual(TimeSpan.FromMilliseconds(0), stats.TimeUsed);
                                            }
                                        }
                                    }

                                    _ServiceProfiler.Local?.SwitchSystem(null);
                                }
        }
Exemple #19
0
        public async Task CacheDoubleExpiration()
        {
            IAmbientCache localOverride = new BasicAmbientCache();

            using (AmbientClock.Pause())
                using (ScopedLocalServiceOverride <IAmbientCache> localCache = new ScopedLocalServiceOverride <IAmbientCache>(localOverride))
                {
                    string keyName1 = nameof(CacheDoubleExpiration) + "1";
                    string keyName2 = nameof(CacheDoubleExpiration) + "2";
                    string keyName3 = nameof(CacheDoubleExpiration) + "3";
                    string keyName4 = nameof(CacheDoubleExpiration) + "4";
                    string keyName5 = nameof(CacheDoubleExpiration) + "5";
//                string keyName6 = nameof(CacheDoubleExpiration) + "6";
                    TestCache ret;
                    AmbientCache <TestCache> cache = new AmbientCache <TestCache>();
                    await cache.Store(true, keyName1, this, TimeSpan.FromMilliseconds(51));

                    await cache.Store(true, keyName2, this, TimeSpan.FromMilliseconds(50));

                    await cache.Store(true, keyName3, this, TimeSpan.FromSeconds(50));

                    await cache.Store(true, keyName4, this, TimeSpan.FromSeconds(50));

                    await cache.Store(true, keyName5, this, TimeSpan.FromSeconds(50));

//                await cache.Store(true, keyName6, this, TimeSpan.FromSeconds(50));
                    ret = await cache.Retrieve <TestCache>(keyName2);

                    Assert.IsNotNull(ret);
                    await Eject(cache, 1); // this should eject 1 because it's the LRU item

                    ret = await cache.Retrieve <TestCache>(keyName2);

                    Assert.IsNotNull(ret);
                    AmbientClock.SkipAhead(TimeSpan.FromMilliseconds(100));
                    ret = await cache.Retrieve <TestCache>(keyName1); // this should return null even though we haven't ejected stuff because it's expired

                    Assert.IsNull(ret);
                    await Eject(cache, 2); // this should eject 2 because it's both expired, and 3 because it's the LRU item

                    ret = await cache.Retrieve <TestCache>(keyName1);

                    Assert.IsNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName2);

                    Assert.IsNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName3);

                    Assert.IsNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName4);

                    Assert.IsNotNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName5);

                    Assert.IsNotNull(ret);
                    //ret = await cache.Retrieve<TestCache>(keyName6);
                    //Assert.IsNotNull(ret);
                    await Eject(cache, 1); // this should eject 4, but only because it's the LRU item

                    ret = await cache.Retrieve <TestCache>(keyName4);

                    Assert.IsNull(ret);
                    ret = await cache.Retrieve <TestCache>(keyName5);

                    Assert.IsNotNull(ret);
                    //ret = await cache.Retrieve<TestCache>(keyName6);
                    //Assert.IsNotNull(ret);
                }
        }