public void AllowsProxySettingsReconfiguration() { using (IRollbar logger = RollbarFactory.CreateNew().Configure(this._loggerConfig)) { Assert.AreNotSame(this._loggerConfig, logger.Config); logger.Configure(this._loggerConfig); Assert.AreNotSame(this._loggerConfig, logger.Config); int errorCount = 0; logger.AsBlockingLogger(TimeSpan.FromSeconds(3)).Info("test 1"); this.ExpectedCommunicationEventsTotal++; Assert.AreEqual(0, errorCount); RollbarConfig newConfig = new RollbarConfig("seed"); newConfig.Reconfigure(this._loggerConfig); Assert.AreNotSame(this._loggerConfig, newConfig); logger.Configure(newConfig); logger.AsBlockingLogger(TimeSpan.FromSeconds(3)).Info("test 2"); this.ExpectedCommunicationEventsTotal++; Assert.AreEqual(0, errorCount); newConfig.ProxyAddress = "www.fakeproxy.com"; newConfig.ProxyUsername = "******"; newConfig.ProxyPassword = "******"; logger.Configure(newConfig); Assert.IsFalse(string.IsNullOrEmpty(logger.Config.ProxyAddress)); Assert.IsFalse(string.IsNullOrEmpty(logger.Config.ProxyUsername)); Assert.IsFalse(string.IsNullOrEmpty(logger.Config.ProxyPassword)); try { // the fake proxy settings will cause a timeout exception here: this.ExpectedCommunicationErrorsTotal++; logger.AsBlockingLogger(TimeSpan.FromSeconds(3)).Info("test 3 with fake proxy"); } catch { errorCount++; } Assert.AreEqual(1, errorCount); newConfig.ProxyAddress = null; newConfig.ProxyUsername = null; newConfig.ProxyPassword = null; logger.Configure(newConfig); Assert.IsTrue(string.IsNullOrEmpty(logger.Config.ProxyAddress)); Assert.IsTrue(string.IsNullOrEmpty(logger.Config.ProxyUsername)); Assert.IsTrue(string.IsNullOrEmpty(logger.Config.ProxyPassword)); try { // the fake proxy settings are gone, so, next call is expected to succeed: this.ExpectedCommunicationEventsTotal++; logger.AsBlockingLogger(TimeSpan.FromSeconds(15)).Info("test 4"); } catch { errorCount++; } Assert.AreEqual(1, errorCount); } }
public void BasicTest() { MessagePackage package = new MessagePackage($"{nameof(RollbarClientFixture)}.BasicTest"); // [DO]: let's send a payload with default config settings (including default value for the PayloadPostTimeout) // [EXPECT]: under all the good networking conditions sending a payload should succeed RollbarConfig config = new RollbarConfig(); config.Reconfigure(this._loggerConfig); using (var logger = (RollbarLogger)RollbarFactory.CreateNew(config)) { var client = new RollbarClient(logger); var bundle = new PayloadBundle(logger, package, ErrorLevel.Info); client.EnsureHttpContentToSend(bundle); var response = client.PostAsJson(bundle); Assert.IsNotNull(response); Assert.AreEqual(0, response.Error); Assert.AreEqual(0, bundle.Exceptions.Count); } // [DO]: let's send a payload using unreasonably short PayloadPostTimeout // [EXPECT]: even under all the good networking conditions sending a payload should not succeed config.PayloadPostTimeout = TimeSpan.FromMilliseconds(10); // too short using (var logger = (RollbarLogger)RollbarFactory.CreateNew(config)) { var client = new RollbarClient(logger); var bundle = new PayloadBundle(logger, package, ErrorLevel.Info); client.EnsureHttpContentToSend(bundle); var response = client.PostAsJson(bundle); Assert.IsNull(response); Assert.AreEqual(1, bundle.Exceptions.Count); //Assert.AreEqual("While PostAsJson(PayloadBundle payloadBundle)...", bundle.Exceptions.First().Message); //Assert.IsTrue(bundle.Exceptions.First().InnerException.Message.StartsWith("One or more errors occurred")); //Assert.AreEqual("A task was canceled.", bundle.Exceptions.First().InnerException.InnerException.Message); } // [DO]: let's send a payload using reasonably long PayloadPostTimeout // [EXPECT]: even under all the good networking conditions sending a payload should succeed config.PayloadPostTimeout = TimeSpan.FromMilliseconds(1000); // long enough using (var logger = (RollbarLogger)RollbarFactory.CreateNew(config)) { var client = new RollbarClient(logger); var bundle = new PayloadBundle(logger, package, ErrorLevel.Info); client.EnsureHttpContentToSend(bundle); var response = client.PostAsJson(bundle); Assert.IsNotNull(response); Assert.AreEqual(0, response.Error); Assert.AreEqual(0, bundle.Exceptions.Count); } }
public void TestAppenderReconfiguration() { Person[] expectedPersons = new Person[] { null, new Person("Person1"), new Person("Person2"), }; RollbarAppender appender = new RollbarAppender( RollbarUnitTestSettings.AccessToken, RollbarUnitTestSettings.Environment, TimeSpan.FromSeconds(3) ); string repositoryName = typeof(RollbarAppenderFixture).Name; var repository = LoggerManager.CreateRepository(repositoryName); string loggerName = typeof(RollbarAppenderFixture).Name; BasicConfigurator.Configure(repository, appender); ILog log = LogManager.GetLogger(repositoryName, loggerName); log.Info("Via log4net"); RollbarConfig newConfig = new RollbarConfig(); newConfig.Reconfigure(appender.RollbarConfig); newConfig.Person = expectedPersons[1]; appender.RollbarConfig.Reconfigure(newConfig); log.Info("Via log4net"); newConfig = new RollbarConfig(); newConfig.Reconfigure(appender.RollbarConfig); newConfig.Person = expectedPersons[2]; newConfig.ScrubFields = new string[] { "log4net:UserName", "log4net:HostName", "log4net:Identity", }; appender.RollbarConfig.Reconfigure(newConfig); log.Info("Via log4net"); Assert.IsFalse(this._rollbarCommEvents[0].Payload.Contains("Person")); Assert.IsTrue(this._rollbarCommEvents[1].Payload.Contains(expectedPersons[1].Id)); Assert.IsTrue(this._rollbarCommEvents[2].Payload.Contains(expectedPersons[2].Id)); }
public void RateLimitConfigSettingOverridesServerHeadersBasedReportingRateTest() { const int totalTestPayloads = 10; const int localReportingRate = 30; using (IRollbar rollbar = this.ProvideDisposableRollbar()) { // using Rollbar API service enforced reporting rate: Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i < totalTestPayloads; i++) { rollbar.AsBlockingLogger(TimeSpan.FromSeconds(3)).Critical("RateLimitConfigSettingOverridesServerHeadersBasedReportingRateTest"); } sw.Stop(); TimeSpan serverRateDuration = sw.Elapsed; // reconfigure with locally defined reporting rate: RollbarConfig rollbarConfig = new RollbarConfig(); rollbarConfig.Reconfigure(rollbar.Config); Assert.IsFalse(rollbarConfig.MaxReportsPerMinute.HasValue); rollbarConfig.MaxReportsPerMinute = localReportingRate; Assert.IsTrue(rollbarConfig.MaxReportsPerMinute.HasValue); Assert.AreEqual(localReportingRate, rollbarConfig.MaxReportsPerMinute.Value); rollbar.Config.Reconfigure(rollbarConfig); Assert.IsTrue(rollbar.Config.MaxReportsPerMinute.HasValue); Assert.AreEqual(localReportingRate, rollbar.Config.MaxReportsPerMinute.Value); // using local config defined reporting rate: sw.Restart(); for (int i = 0; i < totalTestPayloads; i++) { rollbar.AsBlockingLogger(TimeSpan.FromSeconds(3)).Critical("RateLimitConfigSettingOverridesServerHeadersBasedReportingRateTest"); } sw.Stop(); TimeSpan localRateDuration = sw.Elapsed; Assert.IsTrue(2 < (localRateDuration.TotalMilliseconds / serverRateDuration.TotalMilliseconds), "This is good enough confirmation of locally defined rate in action..."); } this.IncrementCount <CommunicationEventArgs>(2 * totalTestPayloads); }
public void AllowsProxySettingsReconfiguration() { this.Reset(); using (IRollbar logger = this.ProvideDisposableRollbar()) { IRollbarConfig initialConfig = logger.Config; Assert.AreSame(initialConfig, logger.Config); logger.Configure(initialConfig); Assert.AreSame(initialConfig, logger.Config); int errorCount = 0; logger.AsBlockingLogger(TimeSpan.FromSeconds(3)).Info("test 1"); this.IncrementCount <CommunicationEventArgs>(); Assert.AreEqual(0, errorCount); RollbarConfig newConfig = new RollbarConfig("seed"); newConfig.Reconfigure(initialConfig); Assert.AreNotSame(initialConfig, newConfig); logger.Configure(newConfig); logger.AsBlockingLogger(TimeSpan.FromSeconds(3)).Info("test 2"); this.IncrementCount <CommunicationEventArgs>(); Assert.AreEqual(0, errorCount); newConfig.ProxyAddress = "www.fakeproxy.com"; newConfig.ProxyUsername = "******"; newConfig.ProxyPassword = "******"; logger.Configure(newConfig); Assert.IsFalse(string.IsNullOrEmpty(logger.Config.ProxyAddress)); Assert.IsFalse(string.IsNullOrEmpty(logger.Config.ProxyUsername)); Assert.IsFalse(string.IsNullOrEmpty(logger.Config.ProxyPassword)); try { // the fake proxy settings will cause a timeout exception here: this.IncrementCount <CommunicationErrorEventArgs>(); logger.AsBlockingLogger(TimeSpan.FromSeconds(3)).Info("test 3 with fake proxy"); } catch { errorCount++; } Assert.AreEqual(1, errorCount); newConfig.ProxyAddress = null; newConfig.ProxyUsername = null; newConfig.ProxyPassword = null; logger.Configure(newConfig); Assert.IsTrue(string.IsNullOrEmpty(logger.Config.ProxyAddress)); Assert.IsTrue(string.IsNullOrEmpty(logger.Config.ProxyUsername)); Assert.IsTrue(string.IsNullOrEmpty(logger.Config.ProxyPassword)); try { // the fake proxy settings are gone, so, next call is expected to succeed: this.IncrementCount <CommunicationEventArgs>(); logger.AsBlockingLogger(TimeSpan.FromSeconds(15)).Info("test 4"); } catch { errorCount++; } Assert.AreEqual(1, errorCount); } }
public void TestAppenderReconfiguration() { Person[] expectedPersons = new Person[] { null, new Person("Person1"), new Person("Person2"), }; RollbarAppender appender = new RollbarAppender( RollbarUnitTestSettings.AccessToken, RollbarUnitTestSettings.Environment, TimeSpan.FromSeconds(3) ); string repositoryName = typeof(RollbarAppenderFixture).Name; var repository = LoggerManager.CreateRepository(repositoryName); string loggerName = typeof(RollbarAppenderFixture).Name; BasicConfigurator.Configure(repository, appender); ILog log = LogManager.GetLogger(repositoryName, loggerName); log.Info("Via log4net"); RollbarConfig newConfig = new RollbarConfig(); newConfig.Reconfigure(appender.RollbarConfig); newConfig.Person = expectedPersons[1]; appender.RollbarConfig.Reconfigure(newConfig); log.Info("Via log4net"); newConfig = new RollbarConfig(); newConfig.Reconfigure(appender.RollbarConfig); newConfig.Person = expectedPersons[2]; newConfig.ScrubFields = new string[] { "log4net:UserName", "log4net:HostName", "log4net:Identity", }; appender.RollbarConfig.Reconfigure(newConfig); log.Info("Via log4net"); // wait until all the payloads are processed and transmitted Thread.Sleep(TimeSpan.FromSeconds(5)); Assert.IsTrue(this._rollbarCommEvents.Count == 3 || this._rollbarCommErrorEvents.Count == 3, "Either comm successes or errors are captured..."); if (this._rollbarCommErrorEvents.Count == 3) { //this scenario happens on the CI server (Azure Pipelines): foreach (var commErrorEvent in this._rollbarCommErrorEvents) { Assert.IsTrue( commErrorEvent.Error.Message.Contains( "Preliminary ConnectivityMonitor detected offline status!"), "Matching error message." ); } Assert.IsFalse(this._rollbarCommErrorEvents[0].Payload.Contains("\"person\":{\"id\":"), "checking this._rollbarCommErrorEvents[0].Payload"); Assert.IsTrue(this._rollbarCommErrorEvents[1].Payload.Contains(expectedPersons[1].Id), "checking this._rollbarCommErrorEvents[1].Payload"); Assert.IsTrue(this._rollbarCommErrorEvents[2].Payload.Contains(expectedPersons[2].Id), "checking this._rollbarCommErrorEvents[2].Payload"); } else { Assert.IsFalse(this._rollbarCommEvents[0].Payload.Contains("\"person\":{\"id\":"), "checking this._rollbarCommEvents[0].Payload"); Assert.IsTrue(this._rollbarCommEvents[1].Payload.Contains(expectedPersons[1].Id), "checking this._rollbarCommEvents[1].Payload"); Assert.IsTrue(this._rollbarCommEvents[2].Payload.Contains(expectedPersons[2].Id), "checking this._rollbarCommEvents[2].Payload"); } }