コード例 #1
0
            public void CanBeSetToZeroToDisableSendingOfTransmissions()
            {
                var sender = new TransmissionSender();

                sender.Capacity = 0;
                Assert.AreEqual(0, sender.Capacity);
            }
コード例 #2
0
            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);
            }
コード例 #3
0
 public void AllowsNewTransmissionsToBeSentAsPreviousTransmissionsAreCompleted()
 {
     var sender = new TransmissionSender { Capacity = 1 };
     sender.Enqueue(() => new StubTransmission());
     Thread.Sleep(50);
     Assert.True(sender.Enqueue(() => new StubTransmission()));
 }
コード例 #4
0
        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);
            }
        }
コード例 #5
0
            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);
            }
コード例 #6
0
            public void DoesNotCountNullTransmissionsReturnedFromEmptyBufferAgainstMaxNumber()
            {
                var sender = new TransmissionSender { Capacity = 1 };
                sender.Enqueue(() => null);

                Transmission transmission2 = new StubTransmission();
                Assert.True(sender.Enqueue(() => transmission2));
            }
コード例 #7
0
            public void ReturnsTrueWhenNewTransmissionDoesNotExceedMaxNumber()
            {
                var sender = new TransmissionSender {
                    Capacity = 1
                };
                bool result = sender.Enqueue(() => new StubTransmission());

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

                Assert.IsFalse(transmissionSent);
            }
コード例 #9
0
            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);
            }
コード例 #10
0
            public void AllowsNewTransmissionsToBeSentAsPreviousTransmissionsAreCompleted()
            {
                var sender = new TransmissionSender {
                    Capacity = 1
                };

                sender.Enqueue(() => new StubTransmission());
                Thread.Sleep(50);
                Assert.IsTrue(sender.Enqueue(() => new StubTransmission()));
            }
コード例 #11
0
            public void StartsSendingTransmissionAndReturnsImmediatelyToUnblockCallingThread()
            {
                var transmissionCanFinishSending = new ManualResetEventSlim();
                var transmission = new StubTransmission { OnSend = () => transmissionCanFinishSending.Wait() };
                var sender = new TransmissionSender();

                sender.Enqueue(() => transmission);

                transmissionCanFinishSending.Set();
            }
コード例 #12
0
            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));
            }
コード例 #13
0
 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));
 }
コード例 #14
0
 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);
 }
コード例 #15
0
            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();
            }
コード例 #16
0
            public void DoesNotCountNullTransmissionsReturnedFromEmptyBufferAgainstMaxNumber()
            {
                var sender = new TransmissionSender {
                    Capacity = 1
                };

                sender.Enqueue(() => null);

                Transmission transmission2 = new StubTransmission();

                Assert.IsTrue(sender.Enqueue(() => transmission2));
            }
コード例 #17
0
            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);
            }
コード例 #18
0
            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));
            }
コード例 #19
0
            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);
            }
コード例 #20
0
            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);
            }
コード例 #21
0
            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);
            }
コード例 #22
0
            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);
            }
コード例 #23
0
            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());
            }
コード例 #24
0
            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);
            }
コード例 #25
0
            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);
            }
コード例 #26
0
        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);
            }
        }
コード例 #27
0
 public void DefaultValueIsAppropriateForMostApps()
 {
     var sender = new TransmissionSender();
     Assert.Equal(3, sender.Capacity);
 }
コード例 #28
0
            public void WaitForPreviousTransmissionsToCompleteReturnsSuccessWithNoInFlightTransmission()
            {
                var sender = new TransmissionSender();

                Assert.AreEqual(TaskStatus.RanToCompletion,
                                sender.WaitForPreviousTransmissionsToComplete(default).Result);
コード例 #29
0
 public void ReturnsFalseWhenTransmissionGetterReturnedNullIndicatingEmptyBuffer()
 {
     var sender = new TransmissionSender();
     Assert.False(sender.Enqueue(() => null));
 }
コード例 #30
0
 public void ReturnsTrueWhenNewTransmissionDoesNotExceedMaxNumber()
 {
     var sender = new TransmissionSender { Capacity = 1 };
     bool result = sender.Enqueue(() => new StubTransmission());
     Assert.True(result);
 }
コード例 #31
0
            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);
            }
コード例 #32
0
            public void ThrowsArgumentOutOfRangeExceptionWhenNewValueLessThanZero()
            {
                var sender = new TransmissionSender();

                AssertEx.Throws <ArgumentOutOfRangeException>(() => sender.Capacity = -1);
            }
コード例 #33
0
 public void ThrowsArgumentOutOfRangeExceptionWhenNewValueLessThanZero()
 {
     var sender = new TransmissionSender();
     Assert.Throws<ArgumentOutOfRangeException>(() => sender.Capacity = -1);
 }
コード例 #34
0
 public void ReturnsFalseWhenNewTransmissionExceedsMaxNumber()
 {
     var sender = new TransmissionSender { Capacity = 0 };
     bool transmissionSent = sender.Enqueue(() => new StubTransmission());
     Assert.False(transmissionSent);
 }
コード例 #35
0
            public void DefaultValueIsAppropriateForMostApps()
            {
                var sender = new TransmissionSender();

                Assert.AreEqual(10, sender.Capacity);
            }
コード例 #36
0
 public void CanBeSetToZeroToDisableSendingOfTransmissions()
 {
     var sender = new TransmissionSender();
     sender.Capacity = 0;
     Assert.Equal(0, sender.Capacity);
 }
コード例 #37
0
            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);
            }
コード例 #38
0
            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);
            }
コード例 #39
0
            public void ReturnsFalseWhenTransmissionGetterReturnedNullIndicatingEmptyBuffer()
            {
                var sender = new TransmissionSender();

                Assert.IsFalse(sender.Enqueue(() => null));
            }
コード例 #40
0
            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);
            }