Exemplo n.º 1
0
        public void OnSuccessIsOnlyCalledOnce()
        {
            const int MaxTries         = 5;
            const int ExpectedFailures = 0;
            const int ExpectedTries    = 1;

            RetryWrapper <bool> dut = new RetryWrapper <bool>(MaxTries, exponentialBackoffBase: 0);

            int actualFailures = 0;

            dut.OnFailure += errorArgs => actualFailures++;

            int actualTries = 0;

            RetryWrapper <bool> .InvocationResult output = dut.InvokeAsync(
                tryCount =>
            {
                actualTries++;
                return(Task.Run(() => new RetryWrapper <bool> .CallbackResult(true)));
            }).Result;

            output.Succeeded.ShouldEqual(true);
            output.Result.ShouldEqual(true);
            actualTries.ShouldEqual(ExpectedTries);
            actualFailures.ShouldEqual(ExpectedFailures);
        }
Exemplo n.º 2
0
        public void WillRetryWhenRequested()
        {
            const int MaxTries         = 5;
            const int ExpectedFailures = 5;
            const int ExpectedTries    = 5;

            RetryWrapper <bool> dut = new RetryWrapper <bool>(MaxTries, exponentialBackoffBase: 0);

            int actualFailures = 0;

            dut.OnFailure += errorArgs => actualFailures++;

            int actualTries = 0;

            RetryWrapper <bool> .InvocationResult output = dut.InvokeAsync(
                tryCount =>
            {
                actualTries++;
                return(Task.Run(() => new RetryWrapper <bool> .CallbackResult(new Exception("Test"), true)));
            }).Result;

            output.Succeeded.ShouldEqual(false);
            output.Result.ShouldEqual(false);
            actualTries.ShouldEqual(ExpectedTries);
            actualFailures.ShouldEqual(ExpectedFailures);
        }
Exemplo n.º 3
0
        public void WillNotRetryForGenericExceptions()
        {
            const int MaxTries = 5;

            RetryWrapper <bool> dut = new RetryWrapper <bool>(MaxTries, exponentialBackoffBase: 0);

            Assert.Throws <AggregateException>(
                () =>
            {
                RetryWrapper <bool> .InvocationResult output = dut.InvokeAsync(tryCount => { throw new Exception(); }).Result;
            });
        }
Exemplo n.º 4
0
        public void WillRetryOnIOException()
        {
            const int ExpectedTries = 5;

            RetryWrapper <bool> dut = new RetryWrapper <bool>(ExpectedTries, exponentialBackoffBase: 0);

            int actualTries = 0;

            RetryWrapper <bool> .InvocationResult output = dut.InvokeAsync(
                tryCount =>
            {
                actualTries++;
                throw new IOException();
            }).Result;

            output.Succeeded.ShouldEqual(false);
            actualTries.ShouldEqual(ExpectedTries);
        }
Exemplo n.º 5
0
        public virtual List <GitObjectSize> QueryForFileSizes(IEnumerable <string> objectIds)
        {
            string objectIdsJson = ToJsonList(objectIds);
            Uri    gvfsEndpoint  = new Uri(this.enlistment.RepoUrl + "/gvfs/sizes");

            EventMetadata metadata      = new EventMetadata();
            int           objectIdCount = objectIds.Count();

            if (objectIdCount > 10)
            {
                metadata.Add("ObjectIdCount", objectIdCount);
            }
            else
            {
                metadata.Add("ObjectIdJson", objectIdsJson);
            }

            this.tracer.RelatedEvent(EventLevel.Informational, "QueryFileSizes", metadata, Keywords.Network);

            RetryWrapper <List <GitObjectSize> > retrier = new RetryWrapper <List <GitObjectSize> >(this.MaxRetries);

            retrier.OnFailure += RetryWrapper <List <GitObjectSize> > .StandardErrorHandler(this.tracer, "QueryFileSizes");

            RetryWrapper <List <GitObjectSize> > .InvocationResult requestTask = retrier.InvokeAsync(
                async tryCount =>
            {
                GitEndPointResponseData response = this.SendRequest(gvfsEndpoint, HttpMethod.Post, objectIdsJson);
                if (response.HasErrors)
                {
                    return(new RetryWrapper <List <GitObjectSize> > .CallbackResult(response.Error, response.ShouldRetry));
                }

                using (Stream objectSizesStream = response.Stream)
                    using (StreamReader reader = new StreamReader(objectSizesStream))
                    {
                        string objectSizesString         = await reader.ReadToEndAsync();
                        List <GitObjectSize> objectSizes = JsonConvert.DeserializeObject <List <GitObjectSize> >(objectSizesString);
                        return(new RetryWrapper <List <GitObjectSize> > .CallbackResult(objectSizes));
                    }
            }).Result;

            return(requestTask.Result ?? new List <GitObjectSize>(0));
        }
Exemplo n.º 6
0
        public void OnFailureIsCalledWhenEventHandlerAttached()
        {
            const int MaxTries         = 5;
            const int ExpectedFailures = 5;

            RetryWrapper <bool> dut = new RetryWrapper <bool>(MaxTries, exponentialBackoffBase: 0);

            int actualFailures = 0;

            dut.OnFailure += errorArgs => actualFailures++;

            RetryWrapper <bool> .InvocationResult output = dut.InvokeAsync(
                tryCount =>
            {
                throw new IOException();
            }).Result;

            output.Succeeded.ShouldEqual(false);
            actualFailures.ShouldEqual(ExpectedFailures);
        }