public void ReturnsMaximumTransmissionBufferCapacityRegardlessOfPolicyInEffect()
            {
                var buffer = new StubTransmissionBuffer { Capacity = 42 };
                var policy = new StubTransmissionPolicy { MaxBufferCapacity = 0 };
                Transmitter transmitter = CreateTransmitter(buffer: buffer, policies: new[] { policy });

                policy.Apply();

                Assert.Equal(42, transmitter.MaxBufferCapacity);
            }
 public void ReturnsCurrentTransmissionBufferCapacityByDefault()
 {
     var buffer = new StubTransmissionBuffer { Capacity = 42 };
     Transmitter transmitter = CreateTransmitter(buffer: buffer);
     Assert.Equal(42, transmitter.MaxBufferCapacity);
 }
            public void LogsUnhandledAsyncExceptionsToPreventThemFromCrashingApplication()
            {
                var exception = new Exception(Guid.NewGuid().ToString());
                var buffer = new StubTransmissionBuffer 
                { 
                    OnEnqueue = getTransmissionAsync => 
                    { 
                        throw exception; 
                    } 
                };

                Transmitter transmitter = CreateTransmitter(buffer: buffer);

                using (var listener = new TestEventListener())
                {
                    const long AllKeywords = -1;
                    listener.EnableEvents(TelemetryChannelEventSource.Log, EventLevel.Warning, (EventKeywords)AllKeywords);

                    buffer.OnTransmissionDequeued(new TransmissionProcessedEventArgs(new StubTransmission()));

                    EventWrittenEventArgs message = listener.Messages.First();
                    Assert.Contains(exception.Message, (string)message.Payload[0], StringComparison.Ordinal);
                }
            }
            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 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 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 DoesNotMoveTransmissionsFromStorageToSenderWhenBufferIsNotEmptyToPreserveQueueOrder()
            {
                var storedTransmission = new StubTransmission();
                var storage = new StubTransmissionStorage();
                storage.Enqueue(() => storedTransmission);
                var buffer = new StubTransmissionBuffer { OnGetSize = () => 1 };

                var sentTransmissions = new List<Transmission>();
                StubTransmissionSender sender = CreateSender(sentTransmissions);
                sender.OnGetCapacity = () => 1;

                Transmitter transmitter = CreateTransmitter(sender, buffer, storage);
                transmitter.ApplyPolicies();

                Assert.DoesNotContain(storedTransmission, sentTransmissions);                
            }
            public void MovesTransmissionsFromStorageToSenderToAvoidWaitingUntilBufferIsFullBeforeSendingStarts()
            {
                var storedTransmission = new StubTransmission();
                var storage = new StubTransmissionStorage();
                storage.Enqueue(() => storedTransmission);
                var buffer = new StubTransmissionBuffer();

                var sentTransmissions = new List<Transmission>();
                StubTransmissionSender sender = CreateSender(sentTransmissions);
                sender.OnGetCapacity = () => 1;

                Transmitter transmitter = CreateTransmitter(sender, buffer, storage);
                transmitter.ApplyPolicies();

                Assert.Contains(storedTransmission, sentTransmissions);
            }
            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 SetsBufferCapacityToMinValueReturnedByTransmissionPolicies()
            {
                var buffer = new StubTransmissionBuffer();
                var policies = new[]
                {
                    new StubTransmissionPolicy { MaxBufferCapacity = 4 },
                    new StubTransmissionPolicy { MaxBufferCapacity = 2 },
                };

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

                Assert.Equal(2, buffer.Capacity);
            }
            public void DoesNotChangeCurrentBufferCapacityWhenOverridingPolicyIsInEffect()
            {
                var buffer = new StubTransmissionBuffer();
                var policy = new StubTransmissionPolicy { MaxBufferCapacity = 0 };
                Transmitter transmitter = CreateTransmitter(buffer: buffer, policies: new[] { policy });
                policy.Apply();

                transmitter.MaxBufferCapacity = 42;

                Assert.Equal(0, buffer.Capacity);
            }
            public void ChangesCurrentBufferCapacityImmediatelyWhenNoOverridingPoliciesAreInEffect()
            {
                var buffer = new StubTransmissionBuffer();
                Transmitter transmitter = CreateTransmitter(buffer: buffer);
                transmitter.ApplyPolicies();

                transmitter.MaxBufferCapacity = 42;

                Assert.Equal(42, buffer.Capacity);
            }