Skip to content

Commit

Permalink
More unit test tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
tippmar-nr committed Oct 18, 2024
1 parent ef1293a commit 17348aa
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// SPDX-License-Identifier: Apache-2.0

using System;
using System.Collections.Generic;
using System.IO;
using NewRelic.Agent.Core.Configuration;
using NewRelic.Agent.Core.SharedInterfaces;
using NUnit.Framework;
using Telerik.JustMock;
using Telerik.JustMock.Helpers;

namespace NewRelic.Agent.Core.Config
{
Expand Down Expand Up @@ -35,6 +37,7 @@ public void TestDefaultBootstrapConfiguration()
Assert.That(config.ServerlessModeEnabled, Is.False);
Assert.That(config.ServerlessFunctionName, Is.Null);
Assert.That(config.ServerlessFunctionVersion, Is.Null);
Assert.That(config.ModernGCSamplerEnabled, Is.False);
});
}

Expand Down Expand Up @@ -152,6 +155,54 @@ public void DoesNotThrowWhenExceptionOccursWhileReadingAppSettings()
Assert.That(config.AgentEnabled, Is.True);
}

[Test]
public void ModernGCSamplerEnabledDisabledByDefault()
{
var config = CreateBootstrapConfiguration();

Assert.That(config.ModernGCSamplerEnabled, Is.False);
}
[Test]
public void ModernGCSamplerEnabledViaLocalConfig()
{
_localConfiguration.modernGCSamplerEnabled = true;

var config = CreateBootstrapConfiguration();

Assert.Multiple(() =>
{
Assert.That(config.ModernGCSamplerEnabled, Is.True);
});
}
[Test]
public void ModernGCSamplerEnabledViaEnvironmentVariable()
{
_originalEnvironment = ConfigLoaderHelpers.EnvironmentVariableProxy;
try
{

var environmentMock = Mock.Create<IEnvironment>();
Mock.Arrange(() => environmentMock.GetEnvironmentVariable(Arg.IsAny<string>())).Returns(MockGetEnvironmentVar);
ConfigLoaderHelpers.EnvironmentVariableProxy = environmentMock;

_localConfiguration.modernGCSamplerEnabled = false;

SetEnvironmentVar("NEW_RELIC_MODERN_GC_SAMPLER_ENABLED", "1");

var config = CreateBootstrapConfiguration();

Assert.Multiple(() =>
{
Assert.That(config.ModernGCSamplerEnabled, Is.True);
});

}
finally
{
ConfigLoaderHelpers.EnvironmentVariableProxy = _originalEnvironment;
}
}

private BootstrapConfiguration CreateBootstrapConfiguration()
{
return new BootstrapConfiguration(_localConfiguration, TestFileName, _ => _webConfigValueWithProvenance, _configurationManagerStatic, new ProcessStatic(), Directory.Exists, Path.GetFullPath);
Expand All @@ -163,5 +214,21 @@ private BootstrapConfiguration CreateBootstrapConfiguration()
private IConfigurationManagerStatic _configurationManagerStatic;
private const string TestWebConfigProvenance = "web.config";
private const string TestAppSettingProvenance = "app setting";

private IEnvironment _originalEnvironment;
private Dictionary<string, string> _envVars = new Dictionary<string, string>();
private void SetEnvironmentVar(string name, string value)
{
_envVars[name] = value;
}

private void ClearEnvironmentVars() => _envVars.Clear();

private string MockGetEnvironmentVar(string name)
{
if (_envVars.TryGetValue(name, out var value)) return value;
return null;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public void Transform_ShouldBuildAndRecordMetrics()
MetricTestHelpers.CompareCountMetric(generatedMetrics, MetricNames.GetGCMetricName(GCSampleType.POHCollectionCount), sample.GCCollectionCounts[4]);
});
}

[Test]
public void Transform_ShouldRecordZeroMetric_WhenCurrentValueIsLessThanPreviousValue()
{
Expand Down Expand Up @@ -144,5 +145,44 @@ private ImmutableGCSample CreateSampleWithCollectionCounts(int[] collectionCount
);
}

[Test]
public void Transform_ShouldRecordZeroMetric_WhenCurrentAllocatedMemoryIsLessThanPreviousAllocatedMemory()
{
// Arrange
var previousSample = CreateSampleWithAllocatedBytes(2048L);
var currentSample = CreateSampleWithAllocatedBytes(1024L);

var generatedMetrics = new Dictionary<string, MetricDataWireModel>();

Mock.Arrange(() => _metricAggregator.Collect(Arg.IsAny<MetricWireModel>())).DoInstead<MetricWireModel>(m => generatedMetrics.Add(m.MetricNameModel.Name, m.DataModel));

// Act
_transformer.Transform(previousSample);

generatedMetrics.Clear();

_transformer.Transform(currentSample);

// Assert
Assert.Multiple(() =>
{
Assert.That(generatedMetrics, Has.Count.EqualTo(18));
MetricTestHelpers.CompareMetric(generatedMetrics, MetricNames.GetGCMetricName(GCSampleType.TotalAllocatedMemory), 0);
});
}

private ImmutableGCSample CreateSampleWithAllocatedBytes(long allocatedBytes)
{
return new ImmutableGCSample(
lastSampleTime: System.DateTime.UtcNow.AddMinutes(-1),
currentSampleTime: System.DateTime.UtcNow,
totalMemoryBytes: 1024L,
totalAllocatedBytes: allocatedBytes,
totalCommittedBytes: 4096L,
heapSizesBytes: [100, 200, 300, 400, 500],
rawCollectionCounts: [5, 4, 3, 2, 1],
fragmentationSizesBytes: [10, 20, 30, 40, 50]
);
}
}
}

0 comments on commit 17348aa

Please sign in to comment.