Esempio n. 1
0
        public void RetryConnectionErrorTestNetStandard20()
        {
            Stopwatch watch = new Stopwatch();

            // Set invalid host address and make timeout to 1s
            var config = new RudderConfig().SetAsync(false);

            config.SetHost("https://fake.rudder-server.com");
            config.SetTimeout(new TimeSpan(0, 0, 1));
            config.SetMaxRetryTime(new TimeSpan(0, 0, 10));
            RudderAnalytics.Initialize(Constants.WRITE_KEY, config);

            // Calculate working time for Identiy message with invalid host address
            watch.Start();
            Actions.Identify(RudderAnalytics.Client);
            watch.Stop();

            Assert.AreEqual(1, RudderAnalytics.Client.Statistics.Submitted);
            Assert.AreEqual(0, RudderAnalytics.Client.Statistics.Succeeded);
            Assert.AreEqual(1, RudderAnalytics.Client.Statistics.Failed);

            // Handling Identify message will take more than 10s even though the timeout is 1s.
            // That's because it retries submit when it's failed.
            Assert.AreEqual(true, watch.ElapsedMilliseconds > 10000);
        }
Esempio n. 2
0
        private static void OnExecute(string writeKey)
        {
            var config = new RudderConfig()
                         .SetMaxQueueSize(100000)
                         .SetHost("https://hosted.rudderlabs.com")
                         .SetMaxBatchSize(40);

            //.SetRequestCompression(true);

            RudderAnalytics.Initialize(writeKey, config);

            Logger.Handlers += Utils.LoggerOnHandlers;

            for (var i = 0; i < UserJourneys; i++)
            {
                Utils.DoJourney();
            }

            // sending all pendant messages
            RudderAnalytics.Client.Flush();

            Utils.PrintSummary();

            RudderAnalytics.Client.Dispose();
        }
Esempio n. 3
0
 /// <summary>
 /// Initialized the default RudderStack client with your API writeKey.
 /// </summary>
 /// <param name="writeKey"></param>
 public static void Initialize(string writeKey, RudderConfig config)
 {
     lock (padlock)
     {
         if (Client == null)
         {
             Client = new RudderClient(writeKey, config);
         }
     }
 }
Esempio n. 4
0
        public static void AddAnalytics(this IServiceCollection services, string writeKey, RudderConfig config = null)
        {
            RudderConfig configuration;

            if (config == null)
            {
                configuration = new RudderConfig();
            }
            else
            {
                configuration = config;
            }

            var client = new RudderClient(writeKey, configuration);

            services.AddSingleton <IRudderAnalyticsClient>(client);
        }
        public void TestInicializationOfClientWithServicesCollectionAndConfig()
        {
            var writeKey    = "writeKey";
            var threadCount = 10;
            var config      = new RudderConfig {
                Threads = threadCount
            };

            var services = new ServiceCollection();

            services.AddAnalytics(writeKey, config);

            var provider          = services.BuildServiceProvider();
            var analyticsInstance = provider.GetRequiredService(typeof(IRudderAnalyticsClient));

            Assert.IsNotNull(analyticsInstance);

            var client = analyticsInstance as RudderClient;

            Assert.AreEqual("writeKey", client.WriteKey);
            Assert.AreEqual(threadCount, client.Config.Threads);
        }
Esempio n. 6
0
        public void RetryServerErrorWithDefaultMaxRetryTimeTestNetStandard20()
        {
            Stopwatch watch          = new Stopwatch();
            string    DummyServerUrl = "http://localhost:8181";

            using (var DummyServer = new WebServer(DummyServerUrl))
            {
                // Set invalid host address and make timeout to 1s
                var config = new RudderConfig().SetAsync(false);
                config.SetHost(DummyServerUrl);
                config.SetTimeout(new TimeSpan(0, 0, 1));
                RudderAnalytics.Initialize(Constants.WRITE_KEY, config);

                var TestCases = new RetryErrorTestCase[]
                {
                    // The errors (500 > code >= 400) doesn't require retry
                    new RetryErrorTestCase()
                    {
                        ErrorMessage  = "Server Gone",
                        ResponseCode  = HttpStatusCode.Gone,
                        ShouldRetry   = false,
                        Timeout       = 10000,
                        BaseActionUrl = "/ServerGone"
                    },
                    // 429 error requires retry
                    new RetryErrorTestCase()
                    {
                        ErrorMessage  = "Too many requests",
                        ResponseCode  = (HttpStatusCode)429,
                        ShouldRetry   = true,
                        Timeout       = 10000,
                        BaseActionUrl = "/TooManyRequests"
                    },
                    // Server errors require retry
                    new RetryErrorTestCase()
                    {
                        ErrorMessage  = "Bad Gateway",
                        ResponseCode  = HttpStatusCode.BadGateway,
                        ShouldRetry   = true,
                        Timeout       = 10000,
                        BaseActionUrl = "/BadGateWay"
                    }
                };

                foreach (var testCase in TestCases)
                {
                    // Setup Action module which returns error code
                    var actionModule = new ActionModule(testCase.BaseActionUrl, HttpVerbs.Any, (ctx) =>
                    {
                        return(ctx.SendStandardHtmlAsync((int)testCase.ResponseCode));
                    });
                    DummyServer.WithModule(actionModule);
                }

                DummyServer.RunAsync();

                foreach (var testCase in TestCases)
                {
                    RudderAnalytics.Client.Config.SetHost(DummyServerUrl + testCase.BaseActionUrl);
                    // Calculate working time for Identiy message with invalid host address
                    watch.Reset();
                    watch.Start();
                    Actions.Identify(RudderAnalytics.Client);
                    watch.Stop();

                    Assert.AreEqual(0, RudderAnalytics.Client.Statistics.Succeeded);

                    // Handling Identify message will less than 10s because the server returns GONE message.
                    // That's because it retries submit when it's failed.
                    if (testCase.ShouldRetry)
                    {
                        Assert.IsTrue(watch.ElapsedMilliseconds > testCase.Timeout);
                    }
                    else
                    {
                        Assert.IsFalse(watch.ElapsedMilliseconds > testCase.Timeout);
                    }
                }
            }
        }
        public void RetryServerErrorWithDefaultMaxRetryTimeTestNet35()
        {
            Stopwatch watch = new Stopwatch();

            string DummyServerUrl = "http://localhost:9696";

            using (var DummyServer = new WebServer(DummyServerUrl))
            {
                DummyServer.RunAsync();

                // Set invalid host address and make timeout to 1s
                var config = new RudderConfig().SetAsync(false);
                config.SetHost(DummyServerUrl);
                config.SetTimeout(new TimeSpan(0, 0, 1));
                RudderAnalytics.Initialize(Constants.WRITE_KEY, config);

                var TestCases = new RetryErrorTestCase[]
                {
                    // The errors (500 > code >= 400) doesn't require retry
                    new RetryErrorTestCase()
                    {
                        ErrorMessage = "Server Gone",
                        ResponseCode = HttpStatusCode.Gone,
                        ShouldRetry  = false,
                        Timeout      = 10000
                    },
                    // 429 error requires retry
                    new RetryErrorTestCase()
                    {
                        ErrorMessage = "Too many requests",
                        ResponseCode = (HttpStatusCode)429,
                        ShouldRetry  = true,
                        Timeout      = 10000
                    },
                    // Server errors require retry
                    new RetryErrorTestCase()
                    {
                        ErrorMessage = "Bad Gateway",
                        ResponseCode = HttpStatusCode.BadGateway,
                        ShouldRetry  = true,
                        Timeout      = 10000
                    }
                };

                foreach (var testCase in TestCases)
                {
                    // Setup fallback module which returns error code
                    DummyServer.RequestHandler = ((req, res) =>
                    {
                        string pageData = "{ ErrorMessage: '" + testCase.ErrorMessage + "' }";
                        byte[] data = Encoding.UTF8.GetBytes(pageData);

                        res.StatusCode = (int)testCase.ResponseCode;
                        res.ContentType = "application/json";
                        res.ContentEncoding = Encoding.UTF8;
                        res.ContentLength64 = data.LongLength;

                        res.OutputStream.Write(data, 0, data.Length);
                        res.Close();
                    });

                    // Calculate working time for Identiy message with invalid host address
                    watch.Start();
                    Actions.Identify(RudderAnalytics.Client);
                    watch.Stop();

                    DummyServer.RequestHandler = null;

                    Assert.AreEqual(0, RudderAnalytics.Client.Statistics.Succeeded);

                    // Handling Identify message will less than 10s because the server returns GONE message.
                    // That's because it retries submit when it's failed.
                    if (testCase.ShouldRetry)
                    {
                        Assert.IsTrue(watch.ElapsedMilliseconds > testCase.Timeout);
                    }
                    else
                    {
                        Assert.IsFalse(watch.ElapsedMilliseconds > testCase.Timeout);
                    }
                }
            }
        }
Esempio n. 8
0
 public void Init()
 {
     _config = new RudderConfig();
 }