public void ClientRetriesConnectionInStreamingModeWithNonFatalError()
        {
            var failThenSucceedHandler = Handlers.Sequential(Error503Response, ValidStreamingResponse);

            using (var streamServer = HttpServer.Start(failThenSucceedHandler))
            {
                var config = BasicConfig()
                             .DataSource(Components.StreamingDataSource())
                             .ServiceEndpoints(Components.ServiceEndpoints().Streaming(streamServer.Uri))
                             .StartWaitTime(TimeSpan.FromSeconds(5))
                             .Build();

                using (var client = new LdClient(config))
                {
                    Assert.True(client.Initialized);
                    Assert.Equal(DataSourceState.Valid, client.DataSourceStatusProvider.Status.State);

                    var value = client.BoolVariation(AlwaysTrueFlag.Key, BasicUser, false);
                    Assert.True(value);

                    var request1 = streamServer.Recorder.RequireRequest();
                    var request2 = streamServer.Recorder.RequireRequest();
                    Assert.Equal(BasicSdkKey, request1.Headers.Get("Authorization"));
                    Assert.Equal(BasicSdkKey, request2.Headers.Get("Authorization"));

                    Assert.NotEmpty(LogCapture.GetMessages().Where(
                                        m => m.Level == Logging.LogLevel.Warn && m.Text.Contains("error 503") &&
                                        m.Text.Contains("will retry")));
                    Assert.Empty(LogCapture.GetMessages().Where(m => m.Level == Logging.LogLevel.Error));
                }
            }
        }
        public void ClientFailsToStartInStreamingModeWith401Error()
        {
            using (var streamServer = HttpServer.Start(Error401Response))
            {
                var config = BasicConfig()
                             .DataSource(Components.StreamingDataSource())
                             .ServiceEndpoints(Components.ServiceEndpoints().Streaming(streamServer.Uri))
                             .StartWaitTime(TimeSpan.FromSeconds(5))
                             .Build();

                using (var client = new LdClient(config))
                {
                    Assert.False(client.Initialized);
                    Assert.Equal(DataSourceState.Off, client.DataSourceStatusProvider.Status.State);

                    var value = client.BoolVariation(AlwaysTrueFlag.Key, BasicUser, false);
                    Assert.False(value);

                    var request = streamServer.Recorder.RequireRequest();
                    Assert.Equal(BasicSdkKey, request.Headers.Get("Authorization"));

                    Assert.NotEmpty(LogCapture.GetMessages().Where(
                                        m => m.Level == Logging.LogLevel.Error && m.Text.Contains("error 401") &&
                                        m.Text.Contains("giving up permanently")));
                }
            }
        }
 internal Configuration(ConfigurationBuilder builder)
 {
     BigSegmentsConfigurationFactory = builder._bigSegmentsConfigurationFactory;
     DataSourceFactory           = builder._dataSourceFactory;
     DataStoreFactory            = builder._dataStoreFactory;
     DiagnosticOptOut            = builder._diagnosticOptOut;
     EventProcessorFactory       = builder._eventProcessorFactory;
     HttpConfigurationFactory    = builder._httpConfigurationFactory;
     LoggingConfigurationFactory = builder._loggingConfigurationFactory;
     Offline          = builder._offline;
     SdkKey           = builder._sdkKey;
     ServiceEndpoints = (builder._serviceEndpointsBuilder ?? Components.ServiceEndpoints()).Build();
     StartWaitTime    = builder._startWaitTime;
 }
示例#4
0
        public void CustomPollingDataSourceBaseUri()
        {
            using (var client = new LdClient(
                       BasicConfig()
                       .DataSource(Components.PollingDataSource())
                       .Http(Components.HttpConfiguration().MessageHandler(_stubHandler))
                       .ServiceEndpoints(Components.ServiceEndpoints().Polling(CustomUri))
                       .Build()))
            {
                var req = _stubHandler.Requests.ExpectValue();
                Assert.Equal(CustomUri, BaseUriOf(req.RequestUri));

                Assert.False(LogCapture.HasMessageWithRegex(LogLevel.Error,
                                                            "You have set custom ServiceEndpoints without specifying"));
            }
        }
示例#5
0
        public void CustomEventsBaseUri()
        {
            using (var client = new LdClient(
                       BasicConfig()
                       .Events(Components.SendEvents())
                       .Http(Components.HttpConfiguration().MessageHandler(_stubHandler))
                       .ServiceEndpoints(Components.ServiceEndpoints().Events(CustomUri))
                       .Build()))
            {
                var req = _stubHandler.Requests.ExpectValue();
                Assert.Equal(CustomUri, BaseUriOf(req.RequestUri));

                AssertLogMessageRegex(false, LogLevel.Error,
                                      "You have set custom ServiceEndpoints without specifying");
            }
        }
示例#6
0
        public void ErrorIsLoggedIfANecessaryUriIsNotSetWhenOtherCustomUrisAreSet()
        {
            var logCapture1 = Logs.Capture();

            using (var client = new LdClient(
                       BasicConfig()
                       .DataSource(Components.StreamingDataSource())
                       .Http(Components.HttpConfiguration().MessageHandler(_stubHandler))
                       .Logging(logCapture1)
                       .ServiceEndpoints(Components.ServiceEndpoints().Polling(CustomUri))
                       .Build()))
            {
                Assert.True(logCapture1.HasMessageWithRegex(LogLevel.Error,
                                                            "You have set custom ServiceEndpoints without specifying the Streaming base URI"));
            }

            var logCapture2 = Logs.Capture();

            using (var client = new LdClient(
                       BasicConfig()
                       .DataSource(Components.PollingDataSource())
                       .Http(Components.HttpConfiguration().MessageHandler(_stubHandler))
                       .Logging(logCapture2)
                       .ServiceEndpoints(Components.ServiceEndpoints().Events(CustomUri))
                       .Build()))
            {
                Assert.True(logCapture2.HasMessageWithRegex(LogLevel.Error,
                                                            "You have set custom ServiceEndpoints without specifying the Polling base URI"));
            }

            var logCapture3 = Logs.Capture();

            using (var client = new LdClient(
                       BasicConfig()
                       .Events(Components.SendEvents())
                       .Http(Components.HttpConfiguration().MessageHandler(_stubHandler))
                       .Logging(logCapture3)
                       .ServiceEndpoints(Components.ServiceEndpoints().Streaming(CustomUri))
                       .Build()))
            {
                Assert.True(logCapture3.HasMessageWithRegex(LogLevel.Error,
                                                            "You have set custom ServiceEndpoints without specifying the Events base URI"));
            }
        }
        public void ClientStartsInStreamingMode()
        {
            using (var streamServer = HttpServer.Start(ValidStreamingResponse))
            {
                var config = BasicConfig()
                             .DataSource(Components.StreamingDataSource())
                             .ServiceEndpoints(Components.ServiceEndpoints().Streaming(streamServer.Uri))
                             .StartWaitTime(TimeSpan.FromSeconds(5))
                             .Build();

                using (var client = new LdClient(config))
                {
                    VerifyClientStartedAndHasExpectedData(client, streamServer);

                    Assert.Empty(LogCapture.GetMessages().Where(m => m.Level == Logging.LogLevel.Warn));
                    Assert.Empty(LogCapture.GetMessages().Where(m => m.Level == Logging.LogLevel.Error));
                }
            }
        }
 public void HttpConfigurationIsAppliedToPolling()
 {
     TestHttpUtils.TestWithSpecialHttpConfigurations(
         ValidPollingResponse,
         (targetUri, httpConfig, server) =>
     {
         var config = BasicConfig()
                      .DataSource(Components.PollingDataSource())
                      .Http(httpConfig)
                      .ServiceEndpoints(Components.ServiceEndpoints().Polling(targetUri))
                      .StartWaitTime(TimeSpan.FromSeconds(5))
                      .Build();
         using (var client = new LdClient(config))
         {
             VerifyClientStartedAndHasExpectedData(client, server);
         }
     },
         TestLogger
         );
 }
        public void ClientStartsInPollingMode()
        {
            using (var pollServer = HttpServer.Start(ValidPollingResponse))
            {
                var config = BasicConfig()
                             .DataSource(Components.PollingDataSource())
                             .ServiceEndpoints(Components.ServiceEndpoints().Polling(pollServer.Uri))
                             .StartWaitTime(TimeSpan.FromSeconds(5))
                             .Build();

                using (var client = new LdClient(config))
                {
                    VerifyClientStartedAndHasExpectedData(client, pollServer);

                    Assert.NotEmpty(LogCapture.GetMessages().Where(
                                        m => m.Level == Logging.LogLevel.Warn &&
                                        m.Text.Contains("You should only disable the streaming API if instructed to do so")));
                    Assert.Empty(LogCapture.GetMessages().Where(m => m.Level == Logging.LogLevel.Error));
                }
            }
        }
 public void HttpConfigurationIsAppliedToEvents()
 {
     TestHttpUtils.TestWithSpecialHttpConfigurations(
         EventsAcceptedResponse,
         (targetUri, httpConfig, server) =>
     {
         var config = BasicConfig()
                      .DiagnosticOptOut(true)
                      .Events(Components.SendEvents())
                      .Http(httpConfig)
                      .ServiceEndpoints(Components.ServiceEndpoints().Events(targetUri))
                      .StartWaitTime(TimeSpan.FromSeconds(5))
                      .Build();
         using (var client = new LdClient(config))
         {
             client.Identify(User.WithKey("userkey"));
             client.Flush();
             server.Recorder.RequireRequest();
         }
     },
         TestLogger
         );
 }
        public void EventsAreSentToCorrectEndpoints(
            string baseUriExtraPath,
            string expectedBasePath
            )
        {
            using (var server = HttpServer.Start(EventsAcceptedResponse))
            {
                var baseUri = server.Uri.ToString().TrimEnd('/') + baseUriExtraPath;
                var config  = BasicConfig()
                              .Events(Components.SendEvents())
                              .ServiceEndpoints(Components.ServiceEndpoints().Events(baseUri))
                              .StartWaitTime(TimeSpan.FromSeconds(5))
                              .Build();

                using (var client = new LdClient(config))
                {
                    client.Identify(User.WithKey("userkey"));
                    client.Flush();

                    var request1 = server.Recorder.RequireRequest();
                    var request2 = server.Recorder.RequireRequest();

                    if (request1.Path.EndsWith("diagnostic"))
                    {
                        var temp = request1;
                        request1 = request2;
                        request2 = temp;
                    }

                    Assert.Equal("POST", request1.Method.ToUpper());
                    Assert.Equal(expectedBasePath + "/bulk", request1.Path);

                    Assert.Equal("POST", request2.Method.ToUpper());
                    Assert.Equal(expectedBasePath + "/diagnostic", request2.Path);
                }
            }
        }
        public void TestConfigForServiceEndpoints()
        {
            TestDiagnosticConfig(
                c => c.ServiceEndpoints(Components.ServiceEndpoints().RelayProxy("http://custom"))
                .Http(Components.HttpConfiguration().MessageHandler(StubMessageHandler.EmptyStreamingResponse())),
                null,
                ExpectedConfigProps.Base()
                .Set("customBaseURI", false)     // this is the polling base URI, not relevant in streaming mode
                .Set("customStreamURI", true)
                .Set("customEventsURI", true)
                );

            TestDiagnosticConfig(
                c => c.ServiceEndpoints(Components.ServiceEndpoints().RelayProxy("http://custom"))
                .DataSource(Components.PollingDataSource())
                .Http(Components.HttpConfiguration().MessageHandler(StubMessageHandler.EmptyPollingResponse())),
                null,
                ExpectedConfigProps.Base()
                .WithPollingDefaults()
                .Set("customBaseURI", true)
                .Set("customEventsURI", true)
                );

            TestDiagnosticConfig(
                c => c.ServiceEndpoints(Components.ServiceEndpoints()
                                        .Streaming("http://custom-streaming")
                                        .Polling("http://custom-polling")
                                        .Events("http://custom-events"))
                .Http(Components.HttpConfiguration().MessageHandler(StubMessageHandler.EmptyStreamingResponse())),
                null,
                ExpectedConfigProps.Base()
                .Set("customBaseURI", false)     // this is the polling base URI, not relevant in streaming mode
                .Set("customStreamURI", true)
                .Set("customEventsURI", true)
                );

            TestDiagnosticConfig(
                c => c.DataSource(
#pragma warning disable CS0618  // using deprecated symbol
                    Components.StreamingDataSource()
                    .BaseUri(new Uri("http://custom"))
#pragma warning restore CS0618
                    )
                .Http(Components.HttpConfiguration().MessageHandler(StubMessageHandler.EmptyStreamingResponse())),
                null,
                ExpectedConfigProps.Base()
                .Set("customStreamURI", true)
                );

            TestDiagnosticConfig(
                c => c.DataSource(
#pragma warning disable CS0618  // using deprecated symbol
                    Components.PollingDataSource().BaseUri(new Uri("http://custom"))
#pragma warning restore CS0618
                    )
                .Http(Components.HttpConfiguration().MessageHandler(StubMessageHandler.EmptyPollingResponse())),
                null,
                ExpectedConfigProps.Base()
                .WithPollingDefaults()
                .Set("customBaseURI", true)
                );
        }