public void CanBeSetToZeroToDisableSendingOfTransmissions()
            {
                var sender = new TransmissionSender();

                sender.Capacity = 0;
                Assert.AreEqual(0, sender.Capacity);
            }
            public void IsRaisedWhenTransmissionReturnsPartialSuccessResult()
            {
                var sender = new TransmissionSender();

                var    eventIsRaised = new ManualResetEventSlim();
                object eventSender   = null;
                TransmissionProcessedEventArgs eventArgs = null;

                sender.TransmissionSent += (s, a) =>
                {
                    eventSender = s;
                    eventArgs   = a;
                    eventIsRaised.Set();
                };

                var          wrapper      = new HttpWebResponseWrapper();
                Transmission transmission = new StubTransmission {
                    OnSend = () => wrapper
                };

                sender.Enqueue(() => transmission);

                Assert.IsTrue(eventIsRaised.Wait(50));
                Assert.AreSame(sender, eventSender);
                Assert.AreSame(transmission, eventArgs.Transmission);
                Assert.AreSame(wrapper, eventArgs.Response);
            }
 public void AllowsNewTransmissionsToBeSentAsPreviousTransmissionsAreCompleted()
 {
     var sender = new TransmissionSender { Capacity = 1 };
     sender.Enqueue(() => new StubTransmission());
     Thread.Sleep(50);
     Assert.True(sender.Enqueue(() => new StubTransmission()));
 }
        internal Transmitter(
            TransmissionSender sender                 = null,
            TransmissionBuffer transmissionBuffer     = null,
            TransmissionStorage storage               = null,
            IEnumerable <TransmissionPolicy> policies = null,
            BackoffLogicManager backoffLogicManager   = null)
        {
            this.backoffLogicManager = backoffLogicManager ?? new BackoffLogicManager();
            this.Sender = sender ?? new TransmissionSender();
            this.Sender.TransmissionSent += this.HandleSenderTransmissionSentEvent;
            this.maxSenderCapacity        = this.Sender.Capacity;

            this.Buffer = transmissionBuffer ?? new TransmissionBuffer();
            this.Buffer.TransmissionDequeued += this.HandleBufferTransmissionDequeuedEvent;
            this.maxBufferCapacity            = this.Buffer.Capacity;

            this.Storage            = storage ?? new TransmissionStorage();
            this.maxStorageCapacity = this.Storage.Capacity;

            this.policies = policies ?? Enumerable.Empty <TransmissionPolicy>();
            foreach (TransmissionPolicy policy in this.policies)
            {
                policy.Initialize(this);
            }
        }
            public void IsRaisedWhenTransmissionThrownExceptionWhileSending()
            {
                var sender = new TransmissionSender();

                var    eventIsRaised = new ManualResetEventSlim();
                object eventSender   = null;
                TransmissionProcessedEventArgs eventArgs = null;

                sender.TransmissionSent += (s, a) =>
                {
                    eventSender = s;
                    eventArgs   = a;
                    eventIsRaised.Set();
                };

                var          exception    = new TimeoutException();
                Transmission transmission = new StubTransmission {
                    OnSend = () => { throw exception; }
                };

                sender.Enqueue(() => transmission);

                Assert.IsTrue(eventIsRaised.Wait(5000));
                Assert.AreSame(sender, eventSender);
                Assert.AreSame(transmission, eventArgs.Transmission);
                Assert.AreSame(exception, eventArgs.Exception);
            }
            public void DoesNotCountNullTransmissionsReturnedFromEmptyBufferAgainstMaxNumber()
            {
                var sender = new TransmissionSender { Capacity = 1 };
                sender.Enqueue(() => null);

                Transmission transmission2 = new StubTransmission();
                Assert.True(sender.Enqueue(() => transmission2));
            }
            public void ReturnsTrueWhenNewTransmissionDoesNotExceedMaxNumber()
            {
                var sender = new TransmissionSender {
                    Capacity = 1
                };
                bool result = sender.Enqueue(() => new StubTransmission());

                Assert.IsTrue(result);
            }
            public void ReturnsFalseWhenNewTransmissionExceedsMaxNumber()
            {
                var sender = new TransmissionSender {
                    Capacity = 0
                };
                bool transmissionSent = sender.Enqueue(() => new StubTransmission());

                Assert.IsFalse(transmissionSent);
            }
            public void WaitForPreviousTransmissionsToCompleteCancelationToken()
            {
                var sender = new TransmissionSender();

                Assert.AreEqual(TaskStatus.Canceled,
                                sender.WaitForPreviousTransmissionsToComplete(new CancellationToken(true)).Result);
                Assert.AreEqual(TaskStatus.Canceled,
                                sender.WaitForPreviousTransmissionsToComplete(1, new CancellationToken(true)).Result);
            }
            public void AllowsNewTransmissionsToBeSentAsPreviousTransmissionsAreCompleted()
            {
                var sender = new TransmissionSender {
                    Capacity = 1
                };

                sender.Enqueue(() => new StubTransmission());
                Thread.Sleep(50);
                Assert.IsTrue(sender.Enqueue(() => new StubTransmission()));
            }
            public void StartsSendingTransmissionAndReturnsImmediatelyToUnblockCallingThread()
            {
                var transmissionCanFinishSending = new ManualResetEventSlim();
                var transmission = new StubTransmission { OnSend = () => transmissionCanFinishSending.Wait() };
                var sender = new TransmissionSender();

                sender.Enqueue(() => transmission);

                transmissionCanFinishSending.Set();
            }
            public void DoesNotCountRejectedTransmissionsAgainstMaxNumber()
            {
                var sender = new TransmissionSender { Capacity = 0 };
                Transmission transmission1 = new StubTransmission();
                sender.Enqueue(() => transmission1);

                sender.Capacity = 1;

                Transmission transmission2 = new StubTransmission();
                Assert.True(sender.Enqueue(() => transmission2));
            }
 private static Transmitter CreateTransmitter(
     TransmissionSender sender   = null,
     TransmissionBuffer buffer   = null,
     TransmissionStorage storage = null,
     IEnumerable <TransmissionPolicy> policies = null)
 {
     return(new Transmitter(
                sender ?? new StubTransmissionSender(),
                buffer ?? new StubTransmissionBuffer(),
                storage ?? new StubTransmissionStorage(),
                policies));
 }
 private static Transmitter CreateTransmitter(
     TransmissionSender sender = null, 
     TransmissionBuffer buffer = null, 
     TransmissionStorage storage = null, 
     IEnumerable<TransmissionPolicy> policies = null)
 {
     return new Transmitter(
         sender ?? new StubTransmissionSender(),
         buffer ?? new StubTransmissionBuffer(),
         storage ?? new StubTransmissionStorage(),
         policies);
 }
            public void StartsSendingTransmissionAndReturnsImmediatelyToUnblockCallingThread()
            {
                var transmissionCanFinishSending = new ManualResetEventSlim();
                var transmission = new StubTransmission {
                    OnSend = () => { transmissionCanFinishSending.Wait(); return(null); }
                };
                var sender = new TransmissionSender();

                sender.Enqueue(() => transmission);

                transmissionCanFinishSending.Set();
            }
            public void DoesNotCountNullTransmissionsReturnedFromEmptyBufferAgainstMaxNumber()
            {
                var sender = new TransmissionSender {
                    Capacity = 1
                };

                sender.Enqueue(() => null);

                Transmission transmission2 = new StubTransmission();

                Assert.IsTrue(sender.Enqueue(() => transmission2));
            }
            public void DoesNotCountTransmissionsSentWithExceptionsAgainstMaxNumber()
            {
                var sender = new TransmissionSender { Capacity = 1 };

                Transmission transmission1 = new StubTransmission { OnSend = () => { throw new TimeoutException(); } };
                sender.Enqueue(() => transmission1);
                Thread.Sleep(10);

                Transmission transmission2 = new StubTransmission();
                bool transmission2Sent = sender.Enqueue(() => transmission2);

                Assert.True(transmission2Sent);
            }
            public void DoesNotCountRejectedTransmissionsAgainstMaxNumber()
            {
                var sender = new TransmissionSender {
                    Capacity = 0
                };
                Transmission transmission1 = new StubTransmission();

                sender.Enqueue(() => transmission1);

                sender.Capacity = 1;

                Transmission transmission2 = new StubTransmission();

                Assert.IsTrue(sender.Enqueue(() => transmission2));
            }
            public void DoesNotInvokeTransmissionGetterWhenMaxNumberOfTransmissionsIsExceededToKeepItBuffered()
            {
                bool transmissionGetterInvoked         = false;
                Func <Transmission> transmissionGetter = () =>
                {
                    transmissionGetterInvoked = true;
                    return(new StubTransmission());
                };
                var sender = new TransmissionSender {
                    Capacity = 0
                };

                sender.Enqueue(transmissionGetter);

                Assert.IsFalse(transmissionGetterInvoked);
            }
            public void DoesNotContinueAsyncOperationsOnCapturedSynchronizationContextToImprovePerformance()
            {
                bool postedBack = false;
                using (var context = new StubSynchronizationContext())
                {
                    context.OnPost = (callback, state) =>
                    {
                        postedBack = true;
                        callback(state);
                    };

                    var buffer = new TransmissionSender();
                    buffer.Enqueue(() => new StubTransmission());
                }

                Assert.False(postedBack);
            }
            public void DoesNotContinueAsyncOperationsOnCapturedSynchronizationContextToImprovePerformance()
            {
                bool postedBack = false;

                using (var context = new StubSynchronizationContext())
                {
                    context.OnPost = (callback, state) =>
                    {
                        postedBack = true;
                        callback(state);
                    };

                    var buffer = new TransmissionSender();
                    buffer.Enqueue(() => new StubTransmission());
                }

                Assert.IsFalse(postedBack);
            }
            public void DoesNotCountTransmissionsSentWithExceptionsAgainstMaxNumber()
            {
                var sender = new TransmissionSender {
                    Capacity = 1
                };

                Transmission transmission1 = new StubTransmission {
                    OnSend = () => { throw new TimeoutException(); }
                };

                sender.Enqueue(() => transmission1);
                Thread.Sleep(10);

                Transmission transmission2     = new StubTransmission();
                bool         transmission2Sent = sender.Enqueue(() => transmission2);

                Assert.IsTrue(transmission2Sent);
            }
            public void IsRaisedWhenTransmissionIsThrottledLocallyWithByteArray()
            {
                var sender = new TransmissionSender();

                sender.ApplyThrottle = true;

                var eventIsRaised = new ManualResetEventSlim();
                var firedCount    = 0;
                var eventArgs     = new List <Implementation.TransmissionProcessedEventArgs>();

                sender.TransmissionSent += (s, a) =>
                {
                    firedCount++;
                    eventArgs.Add(a);
                    if (firedCount == 2)
                    {
                        eventIsRaised.Set();
                    }
                };

                var telemetryItems = new List <ITelemetry>();

                for (var i = 0; i < sender.ThrottleLimit + 10; i++)
                {
                    telemetryItems.Add(new DataContracts.EventTelemetry());
                }

                var          wrapper      = new HttpWebResponseWrapper();
                Transmission transmission = new StubTransmission(JsonSerializer.Serialize(telemetryItems, false))
                {
                    OnSend = () => wrapper
                };

                sender.Enqueue(() => transmission);

                Assert.IsTrue(eventIsRaised.Wait(50));
                Assert.AreEqual(2, firedCount);
                Assert.AreEqual(429, eventArgs[0].Response.StatusCode);
                Assert.AreEqual("Internally Throttled", eventArgs[0].Response.StatusDescription);
                Assert.AreSame(wrapper, eventArgs[1].Response);
                Assert.AreEqual(sender.ThrottleLimit, ((StubTransmission)eventArgs[0].Transmission).CountOfItems());
                Assert.AreEqual(10, ((StubTransmission)eventArgs[1].Transmission).CountOfItems());
            }
            public void FlushAsyncTransmissionWithThrottle()
            {
                var sender = new TransmissionSender();

                sender.ApplyThrottle = true;

                var eventIsRaised = new ManualResetEventSlim();
                var firedCount    = 0;
                var eventArgs     = new List <Implementation.TransmissionProcessedEventArgs>();

                sender.TransmissionSent += (s, a) =>
                {
                    firedCount++;
                    eventArgs.Add(a);
                    if (firedCount == 2)
                    {
                        eventIsRaised.Set();
                    }
                };

                var telemetryItems = new List <ITelemetry>();

                for (var i = 0; i < sender.ThrottleLimit + 10; i++)
                {
                    telemetryItems.Add(new DataContracts.EventTelemetry());
                }

                var          wrapper      = new HttpWebResponseWrapper();
                Transmission transmission = new StubTransmission(telemetryItems)
                {
                    OnSend = () => wrapper
                };

                transmission.IsFlushAsyncInProgress = true;
                sender.Enqueue(() => transmission);

                Assert.IsTrue(eventIsRaised.Wait(50));
                // Both accepted and rejected transmission has flush task
                Assert.IsTrue(eventArgs[0].Transmission.IsFlushAsyncInProgress);
                Assert.IsTrue(eventArgs[1].Transmission.IsFlushAsyncInProgress);
            }
            public void IsRaisedWhenTransmissionIsSentSuccessfully()
            {
                var sender = new TransmissionSender();

                var    eventIsRaised = new ManualResetEventSlim();
                object eventSender   = null;
                TransmissionProcessedEventArgs eventArgs = null;

                sender.TransmissionSent += (s, a) =>
                {
                    eventSender = s;
                    eventArgs   = a;
                    eventIsRaised.Set();
                };

                Transmission transmission = new StubTransmission();

                sender.Enqueue(() => transmission);

                Assert.IsTrue(eventIsRaised.Wait(50));
                Assert.AreSame(sender, eventSender);
                Assert.AreSame(transmission, eventArgs.Transmission);
            }
        internal Transmitter(
            TransmissionSender sender = null, 
            TransmissionBuffer transmissionBuffer = null, 
            TransmissionStorage storage = null, 
            IEnumerable<TransmissionPolicy> policies = null)
        { 
            this.Sender = sender ?? new TransmissionSender();
            this.Sender.TransmissionSent += this.HandleSenderTransmissionSentEvent;
            this.maxSenderCapacity = this.Sender.Capacity;

            this.Buffer = transmissionBuffer ?? new TransmissionBuffer();
            this.Buffer.TransmissionDequeued += this.HandleBufferTransmissionDequeuedEvent;
            this.maxBufferCapacity = this.Buffer.Capacity;

            this.Storage = storage ?? new TransmissionStorage();
            this.maxStorageCapacity = this.Storage.Capacity;

            this.policies = policies ?? Enumerable.Empty<TransmissionPolicy>();
            foreach (TransmissionPolicy policy in this.policies)
            {
                policy.Initialize(this);
            }
        }
 public void DefaultValueIsAppropriateForMostApps()
 {
     var sender = new TransmissionSender();
     Assert.Equal(3, sender.Capacity);
 }
            public void WaitForPreviousTransmissionsToCompleteReturnsSuccessWithNoInFlightTransmission()
            {
                var sender = new TransmissionSender();

                Assert.AreEqual(TaskStatus.RanToCompletion,
                                sender.WaitForPreviousTransmissionsToComplete(default).Result);
 public void ReturnsFalseWhenTransmissionGetterReturnedNullIndicatingEmptyBuffer()
 {
     var sender = new TransmissionSender();
     Assert.False(sender.Enqueue(() => null));
 }
 public void ReturnsTrueWhenNewTransmissionDoesNotExceedMaxNumber()
 {
     var sender = new TransmissionSender { Capacity = 1 };
     bool result = sender.Enqueue(() => new StubTransmission());
     Assert.True(result);
 }
            public void IsRaisedWhenTransmissionIsSentSuccessfully()
            {
                var sender = new TransmissionSender();

                var eventIsRaised = new ManualResetEventSlim();
                object eventSender = null;
                TransmissionProcessedEventArgs eventArgs = null;
                sender.TransmissionSent += (s, a) =>
                {
                    eventSender = s;
                    eventArgs = a;
                    eventIsRaised.Set();
                };

                Transmission transmission = new StubTransmission();
                sender.Enqueue(() => transmission);

                Assert.True(eventIsRaised.Wait(50));
                Assert.Same(sender, eventSender);
                Assert.Same(transmission, eventArgs.Transmission);
            }
            public void ThrowsArgumentOutOfRangeExceptionWhenNewValueLessThanZero()
            {
                var sender = new TransmissionSender();

                AssertEx.Throws <ArgumentOutOfRangeException>(() => sender.Capacity = -1);
            }
 public void ThrowsArgumentOutOfRangeExceptionWhenNewValueLessThanZero()
 {
     var sender = new TransmissionSender();
     Assert.Throws<ArgumentOutOfRangeException>(() => sender.Capacity = -1);
 }
 public void ReturnsFalseWhenNewTransmissionExceedsMaxNumber()
 {
     var sender = new TransmissionSender { Capacity = 0 };
     bool transmissionSent = sender.Enqueue(() => new StubTransmission());
     Assert.False(transmissionSent);
 }
            public void DefaultValueIsAppropriateForMostApps()
            {
                var sender = new TransmissionSender();

                Assert.AreEqual(10, sender.Capacity);
            }
 public void CanBeSetToZeroToDisableSendingOfTransmissions()
 {
     var sender = new TransmissionSender();
     sender.Capacity = 0;
     Assert.Equal(0, sender.Capacity);
 }
            public void IsRaisedWhenTransmissionThrownExceptionWhileSending()
            {
                var sender = new TransmissionSender();

                var eventIsRaised = new ManualResetEventSlim();
                object eventSender = null;
                TransmissionProcessedEventArgs eventArgs = null;
                sender.TransmissionSent += (s, a) =>
                {
                    eventSender = s;
                    eventArgs = a;
                    eventIsRaised.Set();
                };

                var exception = new TimeoutException();
                Transmission transmission = new StubTransmission { OnSend = () => { throw exception; } };
                sender.Enqueue(() => transmission);

                Assert.True(eventIsRaised.Wait(5000));
                Assert.Same(sender, eventSender);
                Assert.Same(transmission, eventArgs.Transmission);
                Assert.Same(exception, eventArgs.Exception);
            }
            public void DoesNotInvokeTransmissionGetterWhenMaxNumberOfTransmissionsIsExceededToKeepItBuffered()
            {
                bool transmissionGetterInvoked = false;
                Func<Transmission> transmissionGetter = () =>
                {
                    transmissionGetterInvoked = true;
                    return new StubTransmission();
                };
                var sender = new TransmissionSender { Capacity = 0 };

                sender.Enqueue(transmissionGetter);

                Assert.False(transmissionGetterInvoked);
            }
            public void ReturnsFalseWhenTransmissionGetterReturnedNullIndicatingEmptyBuffer()
            {
                var sender = new TransmissionSender();

                Assert.IsFalse(sender.Enqueue(() => null));
            }
            public void IsRaisedWhenTransmissionReturnsPartialSuccessResult()
            {
                var sender = new TransmissionSender();

                var eventIsRaised = new ManualResetEventSlim();
                object eventSender = null;
                TransmissionProcessedEventArgs eventArgs = null;
                sender.TransmissionSent += (s, a) =>
                {
                    eventSender = s;
                    eventArgs = a;
                    eventIsRaised.Set();
                };

                var wrapper = new HttpWebResponseWrapper();
                Transmission transmission = new StubTransmission { OnSend = () => wrapper };
                sender.Enqueue(() => transmission);

                Assert.True(eventIsRaised.Wait(50));
                Assert.Same(sender, eventSender);
                Assert.Same(transmission, eventArgs.Transmission);
                Assert.Same(wrapper, eventArgs.Response);
            }