Example #1
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);
                    }
                }
            }
        }