private static StubTransmissionSender CreateSender(ICollection<Transmission> enqueuedTransmissions)
        {
            var sender = new StubTransmissionSender();
            sender.OnEnqueue = getTransmission =>
            {
                enqueuedTransmissions.Add(getTransmission());
                return false;
            };

            return sender;
        }
            public void DoesNotApplyPolicesIfTheyAlreadyApplied()
            {
                var senderPolicy = new StubTransmissionPolicy { MaxSenderCapacity = 0 };
                var sender = new StubTransmissionSender();
                Transmitter transmitter = CreateTransmitter(sender: sender, policies: new[] { senderPolicy });
                transmitter.ApplyPolicies();
                senderPolicy.MaxSenderCapacity = 2;

                transmitter.Enqueue(new StubTransmission());

                Assert.NotEqual(senderPolicy.MaxSenderCapacity, sender.Capacity);
            }
            public void AppliesTransmistionPoliciesIfTheyNeverBeenAppliedBefore()
            {
                var senderPolicy = new StubTransmissionPolicy { MaxSenderCapacity = 0 };
                var sender = new StubTransmissionSender { Capacity = 1 };
                Transmitter transmitter = CreateTransmitter(sender: sender, policies: new[] { senderPolicy });

                transmitter.Enqueue(new StubTransmission());

                Assert.Equal(senderPolicy.MaxSenderCapacity, sender.Capacity);
            }
            public void StoresTransmissionWhenBufferIsFull()
            {
                Transmission storedTransmission = null;
                var storage = new StubTransmissionStorage
                {
                    OnEnqueue = transmission =>
                    {
                        storedTransmission = transmission;
                        return false;
                    }
                };

                var sender = new StubTransmissionSender { OnEnqueue = t => false };
                var buffer = new StubTransmissionBuffer { OnEnqueue = t => false };
                Transmitter transmitter = CreateTransmitter(sender: sender, buffer: buffer, storage: storage);

                var enqueuedTransmission = new StubTransmission();
                transmitter.Enqueue(enqueuedTransmission);

                Assert.Same(enqueuedTransmission, storedTransmission);
            }
            public void BuffersTransmissionWhenSenderIsFull()
            {
                var sender = new StubTransmissionSender { OnEnqueue = t => false };

                Transmission bufferedTransmission = null;
                var buffer = new StubTransmissionBuffer
                {
                    OnEnqueue = getTransmission =>
                    {
                        bufferedTransmission = getTransmission();
                        return bufferedTransmission != null;
                    },
                };

                Transmitter transmitter = CreateTransmitter(sender: sender, buffer: buffer);

                var transmission = new StubTransmission();
                transmitter.Enqueue(transmission);

                Assert.Same(transmission, bufferedTransmission);
            }
            public void PassesTransmissionToSenderAndReturnsTrue()
            {
                Transmission sentTransmission = null;
                var sender = new StubTransmissionSender
                {
                    OnEnqueue = getTransmission =>
                    {
                        sentTransmission = getTransmission();
                        return sentTransmission != null;
                    },
                };

                Transmitter transmitter = CreateTransmitter(sender: sender);

                var transmission = new StubTransmission();
                transmitter.Enqueue(transmission);

                Assert.Same(transmission, sentTransmission);
            }
            public void ReturnsMaximumTransmissionSenderCapacityRegardlessOfPolicyInEffect()
            {
                var sender = new StubTransmissionSender { Capacity = 42 };
                var policy = new StubTransmissionPolicy { MaxSenderCapacity = 0 };
                Transmitter transmitter = CreateTransmitter(sender: sender, policies: new[] { policy });

                policy.Apply();

                Assert.Equal(42, transmitter.MaxSenderCapacity);
            }
            public void LogsUnhandledAsyncExceptionsToPreventThemFromCrashingApplication()
            {
                var exception = new Exception(Guid.NewGuid().ToString());
                var sender = new StubTransmissionSender { OnEnqueue = getTransmissionAsync => { throw exception; } };
                Transmitter transmitter = CreateTransmitter(sender: sender);
                using (var listener = new TestEventListener())
                {
                    const long AllKeywords = -1;
                    listener.EnableEvents(TelemetryChannelEventSource.Log, EventLevel.Warning, (EventKeywords)AllKeywords);

                    sender.OnTransmissionSent(new TransmissionProcessedEventArgs(new StubTransmission()));

                    EventWrittenEventArgs message = listener.Messages.First();
                    Assert.Contains(exception.Message, (string)message.Payload[0], StringComparison.Ordinal);
                }
            }
            public void RestoresOriginalComponentCapacityWhenPolicyIsNoLongerApplicable()
            {
                var sender = new StubTransmissionSender { Capacity = 1 };
                var buffer = new StubTransmissionBuffer { Capacity = 10 };
                var storage = new StubTransmissionStorage { Capacity = 100 };
                var policy = new StubTransmissionPolicy()
                {
                    MaxSenderCapacity = 0,
                    MaxBufferCapacity = 0,
                    MaxStorageCapacity = 0,
                };

                Transmitter transmitter = CreateTransmitter(sender, buffer, storage, new[] { policy });

                policy.MaxSenderCapacity = null;
                policy.MaxBufferCapacity = null;
                policy.MaxStorageCapacity = null;
                policy.Apply();

                Assert.Equal(1, sender.Capacity);
                Assert.Equal(10, buffer.Capacity);
                Assert.Equal(100, storage.Capacity);
            }
            public void DoesNotChangeComponentCapacityIfNoneOfPoliciesAreApplicable()
            {
                var sender = new StubTransmissionSender { Capacity = 1 };
                var buffer = new StubTransmissionBuffer { Capacity = 10 };
                var storage = new StubTransmissionStorage { Capacity = 100 };
                var policies = new[] { new StubTransmissionPolicy() };

                Transmitter transmitter = CreateTransmitter(sender, buffer, storage, policies: policies);
                    
                Assert.Equal(1, sender.Capacity);
                Assert.Equal(10, buffer.Capacity);
                Assert.Equal(100, storage.Capacity);
            }
            public void SetsSenderCapacityToMinValueReturnedByTransmissionPolicies()
            {
                var sender = new StubTransmissionSender();
                var policies = new[]
                {
                    new StubTransmissionPolicy { MaxSenderCapacity = 4 },
                    new StubTransmissionPolicy { MaxSenderCapacity = 2 },
                };

                Transmitter transmitter = CreateTransmitter(sender: sender, policies: policies);
                transmitter.ApplyPolicies();

                Assert.Equal(2, sender.Capacity);
            }
            public void DoesNotChangeCurrentSenderCapacityWhenOverridingPolicyIsInEffect()
            {
                var sender = new StubTransmissionSender();
                var policy = new StubTransmissionPolicy { MaxSenderCapacity = 0 };
                Transmitter transmitter = CreateTransmitter(sender: sender, policies: new[] { policy });
                policy.Apply();

                transmitter.MaxSenderCapacity = 42;

                Assert.Equal(0, sender.Capacity);
            }
            public void ChangesCurrentTransmissionSenderCapacityImmediatelyWhenNoOverridingPoliciesAreInEffect()
            {
                var sender = new StubTransmissionSender();
                Transmitter transmitter = CreateTransmitter(sender: sender);
                transmitter.ApplyPolicies();

                transmitter.MaxSenderCapacity = 42;

                Assert.Equal(42, sender.Capacity);
            }
            public void MovesOldestTransmissionFromBufferToSender()
            {
                Transmission sentTransmission = null;
                var sender = new StubTransmissionSender();
                sender.OnEnqueue = getTransmission =>
                {
                    sentTransmission = getTransmission();
                    return false;
                };

                Transmission bufferedTransmission = new StubTransmission();
                var buffer = new TransmissionBuffer();
                buffer.Enqueue(() => bufferedTransmission);

                Transmitter transmitter = CreateTransmitter(sender: sender, buffer: buffer);

                sender.OnTransmissionSent(new TransmissionProcessedEventArgs(new StubTransmission()));

                Assert.Same(bufferedTransmission, sentTransmission);
            }
            public void EmptiesBufferIfCapacityIsZero()
            {
                //// We set capacity to 0 and clear the cache when DC responds with 439.

                var buffer = new StubTransmissionBuffer();
                buffer.Enqueue(() => new StubTransmission());
                var storage = new StubTransmissionStorage();
                storage.Enqueue(() => new StubTransmission());
                var sender = new StubTransmissionSender();
                sender.Enqueue(() => new StubTransmission());

                var policy = new StubTransmissionPolicy();
                policy.MaxStorageCapacity = 0;

                Transmitter transmitter = CreateTransmitter(sender, buffer, storage, new[] { policy });

                policy.Apply();

                Assert.Equal(0, storage.Queue.Count);
            }
            public void RaisesTransmissionSentWhenTransmissionFinishesSending()
            {
                var sender = new StubTransmissionSender();
                Transmitter queue = CreateTransmitter(sender: sender);

                object eventSender = null;
                TransmissionProcessedEventArgs eventArgs = null;
                queue.TransmissionSent += (s, e) =>
                {
                    eventSender = s;
                    eventArgs = e;
                };

                var transmission = new StubTransmission();
                var exception = new Exception();
                sender.OnTransmissionSent(new TransmissionProcessedEventArgs(transmission, exception));

                Assert.Same(queue, eventSender);
                Assert.Same(transmission, eventArgs.Transmission);
                Assert.Same(exception, eventArgs.Exception);
            }
            public void MovesTransmissionsFromBufferToSenderWhenSenderCapacityIsGreaterThanZero()
            {
                var bufferedTransmission = new StubTransmission();
                var buffer = new TransmissionBuffer();
                buffer.Enqueue(() => bufferedTransmission);

                Transmission sentTransmission = null;
                var sender = new StubTransmissionSender();
                sender.OnEnqueue = getTransmission =>
                {
                    sentTransmission = getTransmission();
                    return false;
                };

                var policy = new StubTransmissionPolicy { MaxSenderCapacity = 0 };

                Transmitter transmitter = CreateTransmitter(sender: sender, buffer: buffer, policies: new[] { policy });

                policy.MaxSenderCapacity = 1;
                policy.Apply();

                Assert.Same(bufferedTransmission, sentTransmission);
            }
            public void MovesOldestTransmissionFromStorageToBuffer()
            {
                var previouslyStoredTransmissions = new List<Transmission> { new StubTransmission(), new StubTransmission() };
                int storageIndex = 0;
                var storage = new StubTransmissionStorage
                {
                    OnDequeue = () =>
                    {
                        if (storageIndex < previouslyStoredTransmissions.Count)
                        {
                            return previouslyStoredTransmissions[storageIndex++];
                        }

                        return null;
                    }
                };

                var newlyBufferedTransmissions = new List<Transmission>();
                var buffer = new StubTransmissionBuffer
                {
                    OnEnqueue = getTransmission =>
                    {
                        var transmission = getTransmission();
                        if (transmission != null)
                        {
                            newlyBufferedTransmissions.Add(transmission);
                            return true;
                        }

                        return false;
                    }
                };

                var sender = new StubTransmissionSender();

                Transmitter queue = CreateTransmitter(sender: sender, buffer: buffer, storage: storage);

                buffer.OnTransmissionDequeued(new TransmissionProcessedEventArgs(null));

                Assert.Equal(previouslyStoredTransmissions, newlyBufferedTransmissions);
            }
 public void ReturnsCurrentTransmissionSenderCapacityByDefault()
 {
     var sender = new StubTransmissionSender { Capacity = 42 };
     Transmitter transmitter = CreateTransmitter(sender: sender);
     Assert.Equal(42, transmitter.MaxSenderCapacity);
 }