Ejemplo n.º 1
0
        public async Task ErrorHandler_ShouldThrowError_For429()
        {
            RiotClientSettings settings = RiotClient.DefaultSettings();

            settings.ThrowOnError             = true;
            settings.RetryOnRateLimitExceeded = false;

            var            eventCount = 0;
            TestRiotClient client     = new TestRiotClient(settings);
            await client.WaitForRetryAfterDelay(); // in case a previous test maxed out the limit

            client.MessageHandler.RespondWithStatus((HttpStatusCode)429);
            client.ConnectionFailed  += (s, e) => { Assert.Fail("Connection failed"); };
            client.RateLimitExceeded += (s, e) => { ++eventCount; };
            client.RequestTimedOut   += (s, e) => { Assert.Fail("Request timed out"); };
            client.ResourceNotFound  += (s, e) => { Assert.Fail("Not found"); };
            client.ResponseError     += (s, e) => { Assert.Fail("Response contained an error"); };
            client.ServerError       += (s, e) => { Assert.Fail("Response contained a server error"); };

            Task <ShardStatus> task = client.GetShardDataAsync();

            Assert.That((AsyncTestDelegate)(() => task), Throws.InstanceOf <RateLimitExceededException>());
            try
            {
                ShardStatus result = await task;
            }
            catch (Exception ex) when(!(ex is AssertionException))
            {
            }
            Assert.That(eventCount, Is.EqualTo(1), "Event was raised wrong number of times.");
        }
Ejemplo n.º 2
0
        public async Task ErrorHandler_ShouldSuppressRetry_ForTimeout_WithEventHandler()
        {
            RiotClientSettings settings = RiotClient.DefaultSettings();

            settings.ThrowOnError       = true;
            settings.RetryOnTimeout     = true;
            settings.MaxRequestAttempts = 2;

            var            eventCount = 0;
            TestRiotClient client     = new TestRiotClient(settings);

            client.Client.Timeout = TimeSpan.FromMilliseconds(1);

            client.MessageHandler.Delay(500);
            client.ConnectionFailed += (s, e) => { Assert.Fail("Connection failed"); };
            client.RequestTimedOut  += (s, e) =>
            {
                ++eventCount;
                e.Retry = false;
            };
            client.ResourceNotFound += (s, e) => { Assert.Fail("Not found"); };
            client.ResponseError    += (s, e) => { Assert.Fail("Response contained an error"); };
            client.ServerError      += (s, e) => { Assert.Fail("Response contained a server error"); };

            try
            {
                ShardStatus result = await client.GetShardDataAsync();
            }
            catch (Exception ex) when(!(ex is AssertionException))
            {
            }
            Assert.That(eventCount, Is.EqualTo(1), "Event was raised wrong number of times.");
        }
Ejemplo n.º 3
0
        public async Task ErrorHandler_ShouldRetry_ForTimeout_IfCancelled()
        {
            RiotClientSettings settings = RiotClient.DefaultSettings();

            settings.ThrowOnError       = true;
            settings.RetryOnTimeout     = true;
            settings.MaxRequestAttempts = 2;

            var         eventCount = 0;
            IRiotClient client     = new RiotClient(settings, PlatformId.NA1);

            client.ConnectionFailed += (s, e) => { Assert.Fail("Connection failed"); };
            client.RequestTimedOut  += (s, e) => { ++eventCount; };
            client.ResourceNotFound += (s, e) => { Assert.Fail("Not found"); };
            client.ResponseError    += (s, e) => { Assert.Fail("Response contained an error"); };
            client.ServerError      += (s, e) => { Assert.Fail("Response contained a server error"); };

            var cts = new CancellationTokenSource();
            Task <ShardStatus> task = client.GetShardDataAsync(token: cts.Token);

            cts.Cancel();

            try
            {
                ShardStatus result = await task;
            }
            catch (Exception ex) when(!(ex is AssertionException))
            {
            }
            Assert.That(eventCount, Is.EqualTo(1), "Event was raised wrong number of times.");
        }
Ejemplo n.º 4
0
        public async Task ErrorHandler_ShouldReturnNull_ForTimeout()
        {
            RiotClientSettings settings = RiotClient.DefaultSettings();

            settings.RetryOnTimeout = false;
            settings.ThrowOnError   = false;

            var         eventCount = 0;
            IRiotClient client     = new RiotClient(settings, PlatformId.NA1);

            client.ConnectionFailed += (s, e) => { Assert.Fail("Connection failed"); };
            client.RequestTimedOut  += (s, e) => { ++eventCount; };
            client.ResourceNotFound += (s, e) => { Assert.Fail("Not found"); };
            client.ResponseError    += (s, e) => { Assert.Fail("Response contained an error"); };
            client.ServerError      += (s, e) => { Assert.Fail("Response contained a server error"); };

            var cts = new CancellationTokenSource();
            Task <ShardStatus> task = client.GetShardDataAsync(token: cts.Token);

            cts.Cancel();

            ShardStatus result = await task;

            Assert.That(result, Is.Null);
            Assert.That(eventCount, Is.EqualTo(1), "Event was raised wrong number of times.");
        }
Ejemplo n.º 5
0
        public async Task ErrorHandler_ShouldSuppressRetry_ForConnectionFailure()
        {
            RiotClientSettings settings = RiotClient.DefaultSettings();

            settings.ThrowOnError             = true;
            settings.RetryOnConnectionFailure = true;
            settings.MaxRequestAttempts       = 2;

            var            eventCount = 0;
            TestRiotClient client     = new TestRiotClient(settings);
            await client.WaitForRetryAfterDelay(); // in case a previous test maxed out the limit

            client.MessageHandler.FailConnection();
            client.ConnectionFailed += (s, e) =>
            {
                ++eventCount;
                e.Retry = false;
            };
            client.RequestTimedOut  += (s, e) => { Assert.Fail("Request timed out"); };
            client.ResourceNotFound += (s, e) => { Assert.Fail("Not found"); };
            client.ResponseError    += (s, e) => { Assert.Fail("Response contained an error"); };
            client.ServerError      += (s, e) => { Assert.Fail("Response contained a server error"); };

            try
            {
                ShardStatus result = await client.GetShardDataAsync();
            }
            catch (Exception ex) when(!(ex is AssertionException))
            {
            }
            Assert.That(eventCount, Is.EqualTo(1), "Event was raised wrong number of times.");
        }
Ejemplo n.º 6
0
        public async Task ErrorHandler_ShouldReturnNull_For400()
        {
            RiotClientSettings settings = RiotClient.DefaultSettings();

            settings.ThrowOnError = false;

            var            eventCount = 0;
            TestRiotClient client     = new TestRiotClient(settings);
            await client.WaitForRetryAfterDelay(); // in case a previous test maxed out the limit

            client.MessageHandler.RespondWithStatus(HttpStatusCode.BadRequest);
            client.ConnectionFailed += (s, e) => { Assert.Fail("Connection failed"); };
            client.RequestTimedOut  += (s, e) => { Assert.Fail("Request timed out"); };
            client.ResourceNotFound += (s, e) => { Assert.Fail("Not found"); };
            client.ResponseError    += (s, e) => { ++eventCount; };
            client.ServerError      += (s, e) => { Assert.Fail("Response contained a server error"); };

            ShardStatus result = await client.GetShardDataAsync();

            Assert.That(result, Is.Null);
            Assert.That(eventCount, Is.EqualTo(1), "Event was raised wrong number of times.");
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Creates a new <see cref="TestRiotClient"/> instance.
 /// </summary>
 /// <param name="settings">The settings to use.</param>
 public TestRiotClient(RiotClientSettings settings)
     : base(settings, Models.PlatformId.NA1)
 {
 }