Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
esmadau committed Oct 13, 2023
1 parent f8b515a commit 8beffdb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
// -------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Health.Dicom.Core.Features.Diagnostic;
using Microsoft.Health.Dicom.Core.Features.Model;
Expand All @@ -18,48 +16,10 @@ namespace Microsoft.Health.Dicom.Core.UnitTests.Features.Diagnostic;

public class LogForwarderExtensionsTests
{
public LogForwarderExtensionsTests()
{
}

public class MockTelemetryChannel : ITelemetryChannel
{
public IList<ITelemetry> Items { get; private set; } = new List<ITelemetry>();

public void Send(ITelemetry item)
{
Items.Add(item);
}

public void Flush()
{
throw new NotImplementedException();
}

public bool? DeveloperMode { get; set; }
public string EndpointAddress { get; set; }

public void Dispose()
{
throw new NotImplementedException();
}
}

[Fact]
public void GivenClientUsingForwardTelemetry_ExpectForwardLogFlagIsSetWithNoAdditionalProperties()
{
MockTelemetryChannel channel = new MockTelemetryChannel();

TelemetryConfiguration configuration = new TelemetryConfiguration
{
TelemetryChannel = channel,
#pragma warning disable CS0618 // Type or member is obsolete
InstrumentationKey = Guid.NewGuid().ToString()
#pragma warning restore CS0618 // Type or member is obsolete
};
configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());

TelemetryClient telemetryClient = new TelemetryClient(configuration);
(TelemetryClient telemetryClient, var channel) = CreateTelemetryClientWithChannel();
telemetryClient.ForwardLogTrace("A message");

Assert.Single(channel.Items);
Expand All @@ -72,18 +32,7 @@ public void GivenClientUsingForwardTelemetry_ExpectForwardLogFlagIsSetWithNoAddi
[Fact]
public void GivenClientUsingForwardTelemetryWithIdentifier_ExpectForwardLogFlagIsSetWithAdditionalProperties()
{
MockTelemetryChannel channel = new MockTelemetryChannel();

TelemetryConfiguration configuration = new TelemetryConfiguration
{
TelemetryChannel = channel,
#pragma warning disable CS0618 // Type or member is obsolete
InstrumentationKey = Guid.NewGuid().ToString()
#pragma warning restore CS0618 // Type or member is obsolete
};
configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());

TelemetryClient telemetryClient = new TelemetryClient(configuration);
(TelemetryClient telemetryClient, var channel) = CreateTelemetryClientWithChannel();

var expectedIdentifier = new InstanceIdentifier(TestUidGenerator.Generate(), TestUidGenerator.Generate(),
TestUidGenerator.Generate(), Partition.Default);
Expand All @@ -107,18 +56,7 @@ public void GivenClientUsingForwardTelemetryWithIdentifier_ExpectForwardLogFlagI
[Fact]
public void GivenClientUsingForwardTelemetry_whenForwardOperationLogTraceWithSizeLimit_ExpectForwardLogFlagIsSet()
{
MockTelemetryChannel channel = new MockTelemetryChannel();

TelemetryConfiguration configuration = new TelemetryConfiguration
{
TelemetryChannel = channel,
#pragma warning disable CS0618 // Type or member is obsolete
InstrumentationKey = Guid.NewGuid().ToString()
#pragma warning restore CS0618 // Type or member is obsolete
};
configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());

TelemetryClient telemetryClient = new TelemetryClient(configuration);
(TelemetryClient telemetryClient, var channel) = CreateTelemetryClientWithChannel();

var operationId = Guid.NewGuid().ToString();
var input = "input";
Expand All @@ -135,21 +73,9 @@ public void GivenClientUsingForwardTelemetry_whenForwardOperationLogTraceWithSiz
}

[Fact]
public void
GivenClientUsingForwardTelemetry_whenForwardOperationLogTraceWithSizeLimitExceeded_ExpectForwardLogFlagIsSetAndMultipleTelemetriesEmitted()
public void GivenClientUsingForwardTelemetry_whenForwardOperationLogTraceWithSizeLimitExceeded_ExpectForwardLogFlagIsSetAndMultipleTelemetriesEmitted()
{
MockTelemetryChannel channel = new MockTelemetryChannel();

TelemetryConfiguration configuration = new TelemetryConfiguration
{
TelemetryChannel = channel,
#pragma warning disable CS0618 // Type or member is obsolete
InstrumentationKey = Guid.NewGuid().ToString()
#pragma warning restore CS0618 // Type or member is obsolete
};
configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());

TelemetryClient telemetryClient = new TelemetryClient(configuration);
(TelemetryClient telemetryClient, var channel) = CreateTelemetryClientWithChannel();

var operationId = Guid.NewGuid().ToString();
var expectedFirstItemInput = "a".PadRight(32 * 1024); // split occurs at 32 kb
Expand All @@ -174,4 +100,20 @@ public void
Assert.Equal(expectedSecondItemInput, secondItem.Context.Properties["dicomAdditionalInformation_input"]);
#pragma warning restore CS0618 // Type or member is obsolete
}

private static (TelemetryClient, MockTelemetryChannel) CreateTelemetryClientWithChannel()
{
MockTelemetryChannel channel = new MockTelemetryChannel();

TelemetryConfiguration configuration = new TelemetryConfiguration
{
TelemetryChannel = channel,
#pragma warning disable CS0618 // Type or member is obsolete
InstrumentationKey = Guid.NewGuid().ToString()
#pragma warning restore CS0618 // Type or member is obsolete
};
configuration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());

return (new TelemetryClient(configuration), channel);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// -------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// -------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using Microsoft.ApplicationInsights.Channel;

namespace Microsoft.Health.Dicom.Core.UnitTests.Features.Diagnostic;

internal class MockTelemetryChannel : ITelemetryChannel
{
public IList<ITelemetry> Items { get; private set; } = new List<ITelemetry>();

public void Send(ITelemetry item)
{
Items.Add(item);
}

public void Flush()
{
throw new NotImplementedException();
}

public bool? DeveloperMode { get; set; }
public string EndpointAddress { get; set; }

public void Dispose()
{
throw new NotImplementedException();
}
}

0 comments on commit 8beffdb

Please sign in to comment.