Skip to content

Commit

Permalink
Merge pull request #225 from Microsoft/daduffy/SwitchToConcurrentDict…
Browse files Browse the repository at this point in the history
…ionary

Switched to using ConcurrentDictionary, deleted SnapshottingDictionary, and fixed unit tests.
  • Loading branch information
abaranch committed Apr 28, 2016
2 parents c29cbc2 + 3ff314b commit bd9ee21
Show file tree
Hide file tree
Showing 22 changed files with 149 additions and 634 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,241 +4,150 @@
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Linq;

using Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.VisualStudio.TestTools.UnitTesting;

/// <summary>
/// Tests the rich payload event source tracking.
/// </summary>
[TestClass]

public class RichPayloadEventSourceTest
{
/// <summary>
/// Tests tracking request telemetry.
/// </summary>
[TestMethod]
public void RichPayloadEventSourceRequestSentTest()
{
var client = new TelemetryClient();
client.InstrumentationKey = Guid.NewGuid().ToString();

using (var listener = new Microsoft.ApplicationInsights.TestFramework.TestEventListener())
{
listener.EnableEvents(RichPayloadEventSource.Log.EventSourceInternal, EventLevel.Verbose, RichPayloadEventSource.Keywords.Requests);

var item = new RequestTelemetry("TestRequest", DateTimeOffset.Now, TimeSpan.FromMilliseconds(10), "200", true);
item.Context.Properties.Add("property1", "value1");
item.Context.User.Id = "testUserId";
item.Context.Operation.Id = Guid.NewGuid().ToString();

client.TrackRequest(item);

var actualEvent = listener.Messages.FirstOrDefault();

Assert.IsNotNull(actualEvent);
Assert.AreEqual(client.InstrumentationKey, actualEvent.Payload[0]);

object[] tags = actualEvent.Payload[1] as object[];
Assert.AreEqual("ai.user.id", ((Dictionary<string, object>)(tags[0]))["Key"]);
Assert.AreEqual("testUserId", ((Dictionary<string, object>)(tags[0]))["Value"]);

Assert.AreEqual("ai.operation.id", ((Dictionary<string, object>)(tags[1]))["Key"]);
Assert.AreEqual(item.Context.Operation.Id, ((Dictionary<string, object>)(tags[1]))["Value"]);

Assert.IsNotNull(actualEvent.Payload[2]);
}
this.DoTracking(
RichPayloadEventSource.Keywords.Requests,
new RequestTelemetry("TestRequest", DateTimeOffset.Now, TimeSpan.FromMilliseconds(10), "200", true),
(client, item) => { client.TrackRequest((RequestTelemetry)item); });
}

/// <summary>
/// Tests tracking trace telemetry.
/// </summary>
[TestMethod]
public void RichPayloadEventSourceTraceSentTest()
{
var client = new TelemetryClient();
client.InstrumentationKey = Guid.NewGuid().ToString();

using (var listener = new Microsoft.ApplicationInsights.TestFramework.TestEventListener())
{
listener.EnableEvents(RichPayloadEventSource.Log.EventSourceInternal, EventLevel.Verbose, RichPayloadEventSource.Keywords.Traces);

var item = new TraceTelemetry("TestTrace", SeverityLevel.Information);
item.Context.Properties.Add("property1", "value1");
item.Context.User.Id = "testUserId";
item.Context.Operation.Id = Guid.NewGuid().ToString();

client.TrackTrace(item);

var actualEvent = listener.Messages.FirstOrDefault();

Assert.IsNotNull(actualEvent);
Assert.AreEqual(client.InstrumentationKey, actualEvent.Payload[0]);

object[] tags = actualEvent.Payload[1] as object[];
Assert.AreEqual("ai.user.id", ((Dictionary<string, object>)(tags[0]))["Key"]);
Assert.AreEqual("testUserId", ((Dictionary<string, object>)(tags[0]))["Value"]);

Assert.AreEqual("ai.operation.id", ((Dictionary<string, object>)(tags[1]))["Key"]);
Assert.AreEqual(item.Context.Operation.Id, ((Dictionary<string, object>)(tags[1]))["Value"]);

Assert.IsNotNull(actualEvent.Payload[2]);
}
this.DoTracking(
RichPayloadEventSource.Keywords.Traces,
new TraceTelemetry("TestTrace", SeverityLevel.Information),
(client, item) => { client.TrackTrace((TraceTelemetry)item); });
}

/// <summary>
/// Tests tracking event telemetry.
/// </summary>
[TestMethod]
public void RichPayloadEventSourceEventSentTest()
{
var client = new TelemetryClient();
client.InstrumentationKey = Guid.NewGuid().ToString();

using (var listener = new Microsoft.ApplicationInsights.TestFramework.TestEventListener())
{
listener.EnableEvents(RichPayloadEventSource.Log.EventSourceInternal, EventLevel.Verbose, RichPayloadEventSource.Keywords.Events);

var item = new EventTelemetry("TestEvent");
item.Context.Properties.Add("property1", "value1");
item.Context.User.Id = "testUserId";
item.Context.Operation.Id = Guid.NewGuid().ToString();

client.TrackEvent(item);

var actualEvent = listener.Messages.FirstOrDefault();

Assert.IsNotNull(actualEvent);
Assert.AreEqual(client.InstrumentationKey, actualEvent.Payload[0]);

object[] tags = actualEvent.Payload[1] as object[];
Assert.AreEqual("ai.user.id", ((Dictionary<string, object>)(tags[0]))["Key"]);
Assert.AreEqual("testUserId", ((Dictionary<string, object>)(tags[0]))["Value"]);

Assert.AreEqual("ai.operation.id", ((Dictionary<string, object>)(tags[1]))["Key"]);
Assert.AreEqual(item.Context.Operation.Id, ((Dictionary<string, object>)(tags[1]))["Value"]);

Assert.IsNotNull(actualEvent.Payload[2]);
}
this.DoTracking(
RichPayloadEventSource.Keywords.Events,
new EventTelemetry("TestEvent"),
(client, item) => { client.TrackEvent((EventTelemetry)item); });
}

/// <summary>
/// Tests tracking exception telemetry.
/// </summary>
[TestMethod]
public void RichPayloadEventSourceExceptionSentTest()
{
var client = new TelemetryClient();
client.InstrumentationKey = Guid.NewGuid().ToString();

using (var listener = new Microsoft.ApplicationInsights.TestFramework.TestEventListener())
{
listener.EnableEvents(RichPayloadEventSource.Log.EventSourceInternal, EventLevel.Verbose, RichPayloadEventSource.Keywords.Exceptions);

var item = new ExceptionTelemetry(new SystemException("Test"));
item.Context.Properties.Add("property1", "value1");
item.Context.User.Id = "testUserId";
item.Context.Operation.Id = Guid.NewGuid().ToString();

client.TrackException(item);

var actualEvent = listener.Messages.FirstOrDefault();

Assert.IsNotNull(actualEvent);
Assert.AreEqual(client.InstrumentationKey, actualEvent.Payload[0]);

object[] tags = actualEvent.Payload[1] as object[];
Assert.AreEqual("ai.user.id", ((Dictionary<string, object>)(tags[0]))["Key"]);
Assert.AreEqual("testUserId", ((Dictionary<string, object>)(tags[0]))["Value"]);

Assert.AreEqual("ai.operation.id", ((Dictionary<string, object>)(tags[1]))["Key"]);
Assert.AreEqual(item.Context.Operation.Id, ((Dictionary<string, object>)(tags[1]))["Value"]);

Assert.IsNotNull(actualEvent.Payload[2]);
}
this.DoTracking(
RichPayloadEventSource.Keywords.Exceptions,
new ExceptionTelemetry(new SystemException("Test")),
(client, item) => { client.TrackException((ExceptionTelemetry)item); });
}

/// <summary>
/// Tests tracking metric telemetry.
/// </summary>
[TestMethod]
public void RichPayloadEventSourceMetricSentTest()
{
var client = new TelemetryClient();
client.InstrumentationKey = Guid.NewGuid().ToString();

using (var listener = new Microsoft.ApplicationInsights.TestFramework.TestEventListener())
{
listener.EnableEvents(RichPayloadEventSource.Log.EventSourceInternal, EventLevel.Verbose, RichPayloadEventSource.Keywords.Metrics);

var item = new MetricTelemetry("TestMetric", 1);
item.Context.Properties.Add("property1", "value1");
item.Context.User.Id = "testUserId";
item.Context.Operation.Id = Guid.NewGuid().ToString();

client.TrackMetric(item);

var actualEvent = listener.Messages.FirstOrDefault();

Assert.IsNotNull(actualEvent);
Assert.AreEqual(client.InstrumentationKey, actualEvent.Payload[0]);

object[] tags = actualEvent.Payload[1] as object[];
Assert.AreEqual("ai.user.id", ((Dictionary<string, object>)(tags[0]))["Key"]);
Assert.AreEqual("testUserId", ((Dictionary<string, object>)(tags[0]))["Value"]);

Assert.AreEqual("ai.operation.id", ((Dictionary<string, object>)(tags[1]))["Key"]);
Assert.AreEqual(item.Context.Operation.Id, ((Dictionary<string, object>)(tags[1]))["Value"]);

Assert.IsNotNull(actualEvent.Payload[2]);
}
this.DoTracking(
RichPayloadEventSource.Keywords.Metrics,
new MetricTelemetry("TestMetric", 1),
(client, item) => { client.TrackMetric((MetricTelemetry)item); });
}

/// <summary>
/// Tests tracking dependency telemetry.
/// </summary>
[TestMethod]
public void RichPayloadEventSourceDependencySentTest()
{
var client = new TelemetryClient();
client.InstrumentationKey = Guid.NewGuid().ToString();

using (var listener = new Microsoft.ApplicationInsights.TestFramework.TestEventListener())
{
listener.EnableEvents(RichPayloadEventSource.Log.EventSourceInternal, EventLevel.Verbose, RichPayloadEventSource.Keywords.Dependencies);

var item = new DependencyTelemetry("TestDependency", "TestCommand", DateTimeOffset.Now, TimeSpan.Zero, true);
item.Context.Properties.Add("property1", "value1");
item.Context.User.Id = "testUserId";
item.Context.Operation.Id = Guid.NewGuid().ToString();

client.TrackDependency(item);

var actualEvent = listener.Messages.FirstOrDefault();

Assert.IsNotNull(actualEvent);
Assert.AreEqual(client.InstrumentationKey, actualEvent.Payload[0]);

object[] tags = actualEvent.Payload[1] as object[];
Assert.AreEqual("ai.user.id", ((Dictionary<string, object>)(tags[0]))["Key"]);
Assert.AreEqual("testUserId", ((Dictionary<string, object>)(tags[0]))["Value"]);

Assert.AreEqual("ai.operation.id", ((Dictionary<string, object>)(tags[1]))["Key"]);
Assert.AreEqual(item.Context.Operation.Id, ((Dictionary<string, object>)(tags[1]))["Value"]);

Assert.IsNotNull(actualEvent.Payload[2]);
}
this.DoTracking(
RichPayloadEventSource.Keywords.Dependencies,
new DependencyTelemetry("TestDependency", "TestCommand", DateTimeOffset.Now, TimeSpan.Zero, true),
(client, item) => { client.TrackDependency((DependencyTelemetry)item); });
}

/// <summary>
/// Tests tracking page view telemetry.
/// </summary>
[TestMethod]
public void RichPayloadEventSourcePageViewSentTest()
{
this.DoTracking(
RichPayloadEventSource.Keywords.PageViews,
new PageViewTelemetry("TestPage"),
(client, item) => { client.TrackPageView((PageViewTelemetry)item); });
}

/// <summary>
/// Helper method to setup shared context and call the desired tracking for testing.
/// </summary>
/// <param name="keywords">The event keywords to enable.</param>
/// <param name="item">The telemetry item to track.</param>
/// <param name="track">The tracking callback to execute.</param>
private void DoTracking(EventKeywords keywords, ITelemetry item, Action<TelemetryClient, ITelemetry> track)
{
var client = new TelemetryClient();
client.InstrumentationKey = Guid.NewGuid().ToString();

using (var listener = new Microsoft.ApplicationInsights.TestFramework.TestEventListener())
{
listener.EnableEvents(RichPayloadEventSource.Log.EventSourceInternal, EventLevel.Verbose, RichPayloadEventSource.Keywords.PageViews);
listener.EnableEvents(RichPayloadEventSource.Log.EventSourceInternal, EventLevel.Verbose, keywords);

var item = new PageViewTelemetry("TestPage");
item.Context.Properties.Add("property1", "value1");
item.Context.User.Id = "testUserId";
item.Context.Operation.Id = Guid.NewGuid().ToString();

client.TrackPageView(item);
track(client, item);

var actualEvent = listener.Messages.FirstOrDefault();

Assert.IsNotNull(actualEvent);
Assert.AreEqual(client.InstrumentationKey, actualEvent.Payload[0]);

int keysFound = 0;
object[] tags = actualEvent.Payload[1] as object[];
Assert.AreEqual("ai.user.id", ((Dictionary<string, object>)(tags[0]))["Key"]);
Assert.AreEqual("testUserId", ((Dictionary<string, object>)(tags[0]))["Value"]);

Assert.AreEqual("ai.operation.id", ((Dictionary<string, object>)(tags[1]))["Key"]);
Assert.AreEqual(item.Context.Operation.Id, ((Dictionary<string, object>)(tags[1]))["Value"]);

foreach (object tagObject in tags)
{
Dictionary<string, object> tag = (Dictionary<string, object>)tagObject;
Assert.IsNotNull(tag);
string key = (string)tag["Key"];
object value = tag["Value"];
if (!string.IsNullOrWhiteSpace(key))
{
if (key == "ai.user.id")
{
Assert.AreEqual("testUserId", value);
++keysFound;
}
else if (key == "ai.operation.id")
{
Assert.AreEqual(item.Context.Operation.Id, value);
++keysFound;
}
}
}

Assert.AreEqual(2, keysFound);
Assert.IsNotNull(actualEvent.Payload[2]);
}
}
Expand Down
1 change: 0 additions & 1 deletion Test/CoreSDK.Test/Shared/Core.Shared.Tests.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
<Compile Include="$(MSBuildThisFileDirectory)Extensibility\Implementation\SessionContextTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensibility\Implementation\SeverityLevelExtensionsTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensibility\Implementation\SnapshottingCollectionTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensibility\Implementation\SnapshottingDictionaryTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensibility\Implementation\SnapshottingListTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensibility\Implementation\TagsTest.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Extensibility\Implementation\TaskTimerTest.cs" />
Expand Down
15 changes: 9 additions & 6 deletions Test/CoreSDK.Test/Shared/DataContracts/EventTelemetryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,17 @@ public void SanitizeWillTrimAppropriateFields()
Assert.Equal(new string('Z', Property.MaxEventNameLength), telemetry.Name);

Assert.Equal(2, telemetry.Properties.Count);
Assert.Equal(new string('X', Property.MaxDictionaryNameLength), telemetry.Properties.Keys.ToArray()[0]);
Assert.Equal(new string('X', Property.MaxValueLength), telemetry.Properties.Values.ToArray()[0]);
Assert.Equal(new string('X', Property.MaxDictionaryNameLength - 3) + "001", telemetry.Properties.Keys.ToArray()[1]);
Assert.Equal(new string('X', Property.MaxValueLength), telemetry.Properties.Values.ToArray()[1]);
string[] keys = telemetry.Properties.Keys.OrderBy(s => s).ToArray();
string[] values = telemetry.Properties.Values.OrderBy(s => s).ToArray();
Assert.Equal(new string('X', Property.MaxDictionaryNameLength), keys[1]);
Assert.Equal(new string('X', Property.MaxValueLength), values[1]);
Assert.Equal(new string('X', Property.MaxDictionaryNameLength - 3) + "001", keys[0]);
Assert.Equal(new string('X', Property.MaxValueLength), values[0]);

Assert.Equal(2, telemetry.Metrics.Count);
Assert.Equal(new string('Y', Property.MaxDictionaryNameLength), telemetry.Metrics.Keys.ToArray()[0]);
Assert.Equal(new string('Y', Property.MaxDictionaryNameLength - 3) + "001", telemetry.Metrics.Keys.ToArray()[1]);
keys = telemetry.Metrics.Keys.OrderBy(s => s).ToArray();
Assert.Equal(new string('Y', Property.MaxDictionaryNameLength), keys[1]);
Assert.Equal(new string('Y', Property.MaxDictionaryNameLength - 3) + "001", keys[0]);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,9 @@ public void SanitizeWillTrimMetricsNameAndValueInExceptionTelemetry()
((ITelemetry)telemetry).Sanitize();

Assert.Equal(2, telemetry.Metrics.Count);
Assert.Equal(new string('Y', Property.MaxDictionaryNameLength), telemetry.Metrics.Keys.ToArray()[0]);
Assert.Equal(new string('Y', Property.MaxDictionaryNameLength - 3) + "001", telemetry.Metrics.Keys.ToArray()[1]);
string[] keys = telemetry.Metrics.Keys.OrderBy(s => s).ToArray();
Assert.Equal(new string('Y', Property.MaxDictionaryNameLength), keys[1]);
Assert.Equal(new string('Y', Property.MaxDictionaryNameLength - 3) + "001", keys[0]);
}

[TestMethod]
Expand Down
Loading

0 comments on commit bd9ee21

Please sign in to comment.