Skip to content

Commit

Permalink
refactor TransmissionPolicy (#2311)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMothra authored Jun 24, 2021
1 parent 4af30fd commit 066e180
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 73 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Helpers
{
using System;

using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation;
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation.TransmissionPolicy;

internal class StubTransmissionPolicy : TransmissionPolicy
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Channel.Implementation;
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation;
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation.TransmissionPolicy;

internal class StubTransmitter : Transmitter
{
Expand All @@ -16,13 +17,13 @@ internal class StubTransmitter : Transmitter


public StubTransmitter()
: base(new StubTransmissionSender(), new StubTransmissionBuffer(), new StubTransmissionStorage(), Enumerable.Empty<TransmissionPolicy>(), new BackoffLogicManager(TimeSpan.FromMinutes(30)))
: base(new StubTransmissionSender(), new StubTransmissionBuffer(), new StubTransmissionStorage(), TransmissionPolicyCollection.Default, new BackoffLogicManager(TimeSpan.FromMinutes(30)))
{

}

public StubTransmitter(BackoffLogicManager backoffLogicManager)
: base(new StubTransmissionSender(), new StubTransmissionBuffer(), new StubTransmissionStorage(), Enumerable.Empty<TransmissionPolicy>(), backoffLogicManager)
: base(new StubTransmissionSender(), new StubTransmissionBuffer(), new StubTransmissionStorage(), TransmissionPolicyCollection.Default, backoffLogicManager)
{

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation.TransmissionPolicy
{
using System;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights.TestFramework;
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Channel.Helpers;

using TaskEx = System.Threading.Tasks.Task;

public class ApplicationLifecycleTransmissionPolicyTest
{
[TestClass]
[TestCategory("TransmissionPolicy")]
public class HandleApplicationStoppingEvent : ApplicationLifecycleTransmissionPolicyTest
{
[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Net.Http;

namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation.TransmissionPolicy
{
using System;
using System.Diagnostics.Tracing;
Expand All @@ -23,6 +23,7 @@ namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implement
public class ErrorHandlingTransmissionPolicyTest
{
[TestClass]
[TestCategory("TransmissionPolicy")]
[TestCategory("WindowsOnly")] // these tests are flaky on linux builds.
public class HandleTransmissionSentEvent : ErrorHandlingTransmissionPolicyTest
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation.TransmissionPolicy
{
using System;
using System.Diagnostics.Tracing;
Expand All @@ -13,9 +13,11 @@
using TaskEx = System.Threading.Tasks.Task;

[TestClass]
[TestCategory("TransmissionPolicy")]
public class NetworkAvailabilityTransmissionPolicyTest
{
[TestClass]
[TestCategory("TransmissionPolicy")]
public class Class : NetworkAvailabilityTransmissionPolicyTest
{
[TestMethod]
Expand Down Expand Up @@ -44,6 +46,7 @@ public void DisposeUnsubscribesNetworkChangeEvents()
}

[TestClass]
[TestCategory("TransmissionPolicy")]
public class Initialize : NetworkAvailabilityTransmissionPolicyTest
{
[TestMethod]
Expand Down Expand Up @@ -122,6 +125,7 @@ public void HandlesExceptionsThrownByNetworkWhenAddingAddressChangedEventHandler
}

[TestClass]
[TestCategory("TransmissionPolicy")]
public class HandleNetworkStatusChangedEvent : NetworkAvailabilityTransmissionPolicyTest
{
[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Microsoft.ApplicationInsights.WindowsServer.Channel.Implementation
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation.TransmissionPolicy
{
using System;
using System.Collections.Generic;
Expand All @@ -15,6 +15,7 @@
using TaskEx = System.Threading.Tasks.Task;

[TestClass]
[TestCategory("TransmissionPolicy")]
public class PartialSuccessTransmissionPolicyTest
{
[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation.TransmissionPolicy
{
using System;
using System.Diagnostics.Tracing;
Expand All @@ -13,29 +13,28 @@
public class ThrottlingTransmissionPolicyTest
{
[TestClass]
[TestCategory("TransmissionPolicy")]
public class HandleTransmissionSentEvent : ErrorHandlingTransmissionPolicyTest
{
private const int ResponseCodeTooManyRequests = 429;
private const int ResponseCodeTooManyRequestsOverExtendedTime = 439;
private const int ResponseCodePaymentRequired = 402;
private const int ResponseCodeUnsupported = 0;

[TestMethod]
public void AssertTooManyRequestsStopsSending()
{
this.PositiveTest(ResponseCodeTooManyRequests, 0, null, null, false);
this.PositiveTest(ResponseStatusCodes.ResponseCodeTooManyRequests, 0, null, null, false);
}

[TestMethod]
public void AssertTooManyRequestsStopsSendingWithFlushAsyncTask()
{
this.PositiveTest(ResponseCodeTooManyRequests, 0, 0, null, true);
this.PositiveTest(ResponseStatusCodes.ResponseCodeTooManyRequests, 0, 0, null, true);
}

[TestMethod]
public void AssertTooManyRequestsOverExtendedTimeStopsSendingAndCleansCache()
{
this.PositiveTest(ResponseCodeTooManyRequestsOverExtendedTime, 0, 0, 0, false);
this.PositiveTest(ResponseStatusCodes.ResponseCodeTooManyRequestsOverExtendedTime, 0, 0, 0, false);
}

[TestMethod]
Expand Down Expand Up @@ -124,23 +123,22 @@ private void PositiveTest(int responseCode, int? expectedSenderCapacity, int? ex
**/;

var policyApplied = new AutoResetEvent(false);
var transmitter = new StubTransmitter();
transmitter.OnApplyPolicies = () =>
var transmitter = new StubTransmitter
{
policyApplied.Set();
OnApplyPolicies = () => policyApplied.Set(),
};

var policy = new ThrottlingTransmissionPolicy();
policy.Initialize(transmitter);

string statusDescription = null;

transmitter.OnTransmissionSent(
new TransmissionProcessedEventArgs(
new StubTransmission() { IsFlushAsyncInProgress = hasFlushTask }, null, new HttpWebResponseWrapper()
transmission: new StubTransmission() { IsFlushAsyncInProgress = hasFlushTask },
exception: null,
response: new HttpWebResponseWrapper()
{
StatusCode = responseCode,
StatusDescription = statusDescription,
StatusDescription = null,
RetryAfterHeader = retryAfter
}));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation.TransmissionPolicy
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
[TestCategory("TransmissionPolicy")]
public class TransmissionPolicyCollectionTests
{
[TestMethod]
public void VerifyCalcuateMinimums()
{
// Setup
var policies = new TransmissionPolicyCollection(policies: new List<TransmissionPolicy>
{
new MockValueTransmissionPolicy(maxSenderCapacity: null, maxBufferCapacity: null, maxStorageCapacity: null),
new MockValueTransmissionPolicy(maxSenderCapacity: 1, maxBufferCapacity: 2, maxStorageCapacity: 3),
new MockValueTransmissionPolicy(maxSenderCapacity: 101, maxBufferCapacity: 102, maxStorageCapacity: 103),
});

// Act & Verify
Assert.AreEqual(1, policies.CalculateMinimumMaxSenderCapacity());
Assert.AreEqual(2, policies.CalculateMinimumMaxBufferCapacity());
Assert.AreEqual(3, policies.CalculateMinimumMaxStorageCapacity());
}


[TestMethod]
public void VerifyCalcuateMinimums_CanHandleNulls()
{
// Setup
var policies = new TransmissionPolicyCollection(policies: new List<TransmissionPolicy>
{
new MockValueTransmissionPolicy(maxSenderCapacity: null, maxBufferCapacity: null, maxStorageCapacity: null),
});

// Act & Verify
Assert.AreEqual(null, policies.CalculateMinimumMaxSenderCapacity());
Assert.AreEqual(null, policies.CalculateMinimumMaxBufferCapacity());
Assert.AreEqual(null, policies.CalculateMinimumMaxStorageCapacity());
}

private class MockValueTransmissionPolicy : TransmissionPolicy
{
public MockValueTransmissionPolicy(int? maxSenderCapacity, int? maxBufferCapacity, int? maxStorageCapacity)
{
this.MaxSenderCapacity = maxSenderCapacity;
this.MaxBufferCapacity = maxBufferCapacity;
this.MaxStorageCapacity = maxStorageCapacity;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation.TransmissionPolicy
{
using System;
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Helpers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using TaskEx = System.Threading.Tasks.Task;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation.TransmissionPolicy;

public class TransmitterTest
{
Expand All @@ -35,13 +36,13 @@ private static Transmitter CreateTransmitter(
TransmissionSender sender = null,
TransmissionBuffer buffer = null,
TransmissionStorage storage = null,
IEnumerable<TransmissionPolicy> policies = null)
IEnumerable<TransmissionPolicy.TransmissionPolicy> policies = null)
{
return new Transmitter(
sender ?? new StubTransmissionSender(),
buffer ?? new StubTransmissionBuffer(),
storage ?? new StubTransmissionStorage(),
policies);
new TransmissionPolicyCollection(policies));
}

[TestClass]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation.TransmissionPolicy
{
internal class ApplicationLifecycleTransmissionPolicy : TransmissionPolicy
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation.TransmissionPolicy
{
using System;
using System.Globalization;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation.TransmissionPolicy
{
using System;
using System.Net.NetworkInformation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation.TransmissionPolicy
{
using System;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation.TransmissionPolicy
{
using System;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation
namespace Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel.Implementation.TransmissionPolicy
{
using System;

Expand Down
Loading

0 comments on commit 066e180

Please sign in to comment.