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 MaxBufferCapacityCanBeSetByPolicy()
        {
            var policy = new StubTransmissionPolicy();

            policy.MaxBufferCapacity = 42;
            Assert.Equal(42, policy.MaxBufferCapacity);
        }
            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 MaxStorageCapacityCanBeSetByPolicy()
        {
            var policy = new StubTransmissionPolicy();

            policy.MaxStorageCapacity = 42;
            Assert.AreEqual(42, policy.MaxStorageCapacity);
        }
            public void InitializesTransmissionPolicies()
            {
                Transmitter policyTransmitter = null;
                var         policy            = new StubTransmissionPolicy();

                policy.OnInitialize = t => policyTransmitter = t;

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

                Assert.Same(transmitter, policyTransmitter);
            }
            public void ReturnsMaximumTransmissionStorageCapacityRegardlessOfPolicyInEffect()
            {
                var storage = new StubTransmissionStorage {
                    Capacity = 42
                };
                var policy = new StubTransmissionPolicy {
                    MaxStorageCapacity = 0
                };
                Transmitter transmitter = CreateTransmitter(storage: storage, policies: new[] { policy });

                policy.Apply();

                Assert.Equal(42, transmitter.MaxStorageCapacity);
            }
            public void DoesNotChangeCurrentStorageCapacityWhenOverridingPolicyIsInEffect()
            {
                var storage = new StubTransmissionStorage();
                var policy  = new StubTransmissionPolicy {
                    MaxStorageCapacity = 0
                };
                Transmitter transmitter = CreateTransmitter(storage: storage, policies: new[] { policy });

                policy.Apply();

                transmitter.MaxStorageCapacity = 42;

                Assert.Equal(0, storage.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 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 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 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 async Task AppliesTransmistionPoliciesIfTheyNeverBeenAppliedBefore()
            {
                var senderPolicy = new StubTransmissionPolicy {
                    MaxSenderCapacity = 0
                };
                var sender = new StubTransmissionSender {
                    Capacity = 1
                };
                Transmitter transmitter = CreateTransmitter(sender: sender, policies: new[] { senderPolicy });

                var result = await transmitter.FlushAsync(new StubTransmission(), default);

                Assert.AreEqual(senderPolicy.MaxSenderCapacity, sender.Capacity);
                Assert.IsTrue(result);
            }
            public async Task DoesNotApplyPolicesIfTheyAlreadyApplied()
            {
                var senderPolicy = new StubTransmissionPolicy {
                    MaxSenderCapacity = 0
                };
                var         sender      = new StubTransmissionSender();
                Transmitter transmitter = CreateTransmitter(sender: sender, policies: new[] { senderPolicy });

                transmitter.ApplyPolicies();
                senderPolicy.MaxSenderCapacity = 2;

                var result = await transmitter.FlushAsync(new StubTransmission(), default);

                Assert.AreNotEqual(senderPolicy.MaxSenderCapacity, sender.Capacity);
                Assert.IsTrue(result);
            }
        public void ApplyInvokesApplyPoliciesAsyncOfTransmitter()
        {
            bool policiesApplied = false;
            var  transmitter     = new StubTransmitter
            {
                OnApplyPolicies = () => { policiesApplied = true; }
            };

            var policy = new StubTransmissionPolicy();

            policy.Initialize(transmitter);

            policy.Apply();

            Assert.True(policiesApplied);
        }
            public void MovesTransmissionsFromStorageToBufferWhenBufferCapacityIsGreaterThanZero()
            {
                var storedTransmission = new StubTransmission();

                var storage = new StubTransmissionStorage();

                storage.Enqueue(() => storedTransmission);

                var buffer = new TransmissionBuffer();

                var policy = new StubTransmissionPolicy();

                policy.MaxBufferCapacity = 0;

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

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

                Transmission bufferedTransmission = buffer.Dequeue();

                Assert.Same(storedTransmission, bufferedTransmission);
            }
            public void MovesTransmissionsFromBufferToStorageWhenBufferCapacityIsZero()
            {
                var storage = new StubTransmissionStorage {
                    Capacity = 1
                };

                var bufferedTransmission = new StubTransmission();
                var buffer = new TransmissionBuffer();

                storage.Enqueue(() => bufferedTransmission);

                var policy = new StubTransmissionPolicy();

                policy.MaxBufferCapacity = 1;

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

                policy.MaxBufferCapacity = 0;
                policy.Apply();

                Transmission storedTransmission = storage.Dequeue();

                Assert.Same(bufferedTransmission, storedTransmission);
            }
            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 ApplyThrowsInvalidOperationExceptionWhenPolicyWasNotInitializedToPreventUsageErrors()
        {
            var policy = new StubTransmissionPolicy();

            Assert.Throws <InvalidOperationException>(() => policy.Apply());
        }
        public void MaxStorageCapacityIsNullByDefaultToIndicateThatPolicyIsNotApplicable()
        {
            var policy = new StubTransmissionPolicy();

            Assert.Null(policy.MaxStorageCapacity);
        }
        public void InitializeThrowsArgumentNullExceptionWhenTransmitterIsNullToPreventUsageErrors()
        {
            var policy = new StubTransmissionPolicy();

            Assert.Throws <ArgumentNullException>(() => policy.Initialize(null));
        }