Skip to content

Commit

Permalink
[repo/AspNet] Prepare to .NET9 (#2282)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kielek authored Oct 31, 2024
1 parent 1c1f8d2 commit 6b7ad12
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Has_Started_Returns_Correctly()
{
var context = HttpContextHelper.GetFakeHttpContext();

bool result = ActivityHelper.HasStarted(context, out Activity? aspNetActivity);
var result = ActivityHelper.HasStarted(context, out var aspNetActivity);

Assert.False(result);
Assert.Null(aspNetActivity);
Expand All @@ -42,7 +42,7 @@ public void Has_Started_Returns_Correctly()
Assert.True(result);
Assert.Null(aspNetActivity);

Activity activity = new Activity(TestActivityName);
var activity = new Activity(TestActivityName);
context.Items[ActivityHelper.ContextKey] = new ActivityHelper.ContextHolder(activity);

result = ActivityHelper.HasStarted(context, out aspNetActivity);
Expand Down Expand Up @@ -90,7 +90,7 @@ public async Task Can_Restore_Baggage()
};

var context = HttpContextHelper.GetFakeHttpContext(headers: requestHeaders);
using var rootActivity = ActivityHelper.StartAspNetActivity(new CompositeTextMapPropagator(new TextMapPropagator[] { new TraceContextPropagator(), new BaggagePropagator() }), context, null)!;
using var rootActivity = ActivityHelper.StartAspNetActivity(new CompositeTextMapPropagator([new TraceContextPropagator(), new BaggagePropagator()]), context, null)!;

rootActivity.AddTag("k1", "v1");
rootActivity.AddTag("k2", "v2");
Expand Down Expand Up @@ -237,7 +237,7 @@ public async Task Can_Stop_Root_Activity_If_It_Is_Broken()
using var root = ActivityHelper.StartAspNetActivity(this.noopTextMapPropagator, context, null)!;
new Activity("child").Start();

for (int i = 0; i < 2; i++)
for (var i = 0; i < 2; i++)
{
await Task.Run(() =>
{
Expand Down Expand Up @@ -266,7 +266,7 @@ public void Stop_Root_Activity_With_129_Nesting_Depth()
var context = HttpContextHelper.GetFakeHttpContext();
using var root = ActivityHelper.StartAspNetActivity(this.noopTextMapPropagator, context, null)!;

for (int i = 0; i < 129; i++)
for (var i = 0; i < 129; i++)
{
new Activity("child" + i).Start();
}
Expand Down Expand Up @@ -368,7 +368,7 @@ public void Can_Create_RootActivity_From_W3C_Traceparent_With_Baggage()
};

var context = HttpContextHelper.GetFakeHttpContext(headers: requestHeaders);
using var rootActivity = ActivityHelper.StartAspNetActivity(new CompositeTextMapPropagator(new TextMapPropagator[] { new TraceContextPropagator(), new BaggagePropagator() }), context, null);
using var rootActivity = ActivityHelper.StartAspNetActivity(new CompositeTextMapPropagator([new TraceContextPropagator(), new BaggagePropagator()]), context, null);

Assert.NotNull(rootActivity);
Assert.Equal(ActivityIdFormat.W3C, rootActivity.IdFormat);
Expand Down Expand Up @@ -416,11 +416,11 @@ public void Can_Create_RootActivity_And_Saved_In_HttContext()
public void Fire_Exception_Events()
#pragma warning restore CA1030 // Use events where appropriate
{
int callbacksFired = 0;
var callbacksFired = 0;

var context = HttpContextHelper.GetFakeHttpContext();

Activity activity = new Activity(TestActivityName);
var activity = new Activity(TestActivityName);

ActivityHelper.WriteActivityException(activity, context, new InvalidOperationException(), (a, c, e) => { callbacksFired++; });

Expand Down Expand Up @@ -480,12 +480,7 @@ private void EnableListener(Action<Activity>? onStarted = null, Action<Activity>
ActivityStopped = (a) => onStopped?.Invoke(a),
Sample = (ref ActivityCreationOptions<ActivityContext> options) =>
{
if (onSample != null)
{
return onSample(options.Parent);
}
return ActivitySamplingResult.AllDataAndRecorded;
return onSample?.Invoke(options.Parent) ?? ActivitySamplingResult.AllDataAndRecorded;
},
};

Expand All @@ -494,7 +489,7 @@ private void EnableListener(Action<Activity>? onStarted = null, Action<Activity>

private class TestHttpRequest : HttpRequestBase
{
private readonly NameValueCollection headers = new();
private readonly NameValueCollection headers = [];

public override NameValueCollection Headers => this.headers;

Expand Down Expand Up @@ -537,7 +532,7 @@ private class TestHttpContext : HttpContextBase
public TestHttpContext(Exception? error = null)
{
this.Server = new TestHttpServerUtility(this);
this.items = new Hashtable();
this.items = [];
this.Error = error;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,16 @@ private class SimpleWorkerRequestWithHeaders : SimpleWorkerRequest
public SimpleWorkerRequestWithHeaders(string page, string query, TextWriter output, IDictionary<string, string>? headers)
: base(page, query, output)
{
if (headers != null)
{
this.headers = headers;
}
else
{
this.headers = new Dictionary<string, string>();
}
this.headers = headers ?? new Dictionary<string, string>();
}

public override string[][] GetUnknownRequestHeaders()
{
List<string[]> result = new List<string[]>();
List<string[]> result = [];

foreach (var header in this.headers)
{
result.Add(new string[] { header.Key, header.Value });
result.Add([header.Key, header.Value]);
}

var baseResult = base.GetUnknownRequestHeaders();
Expand All @@ -58,29 +51,19 @@ public override string[][] GetUnknownRequestHeaders()
result.AddRange(baseResult);
}

return result.ToArray();
return [.. result];
}

public override string GetUnknownRequestHeader(string name)
{
if (this.headers.TryGetValue(name, out var value))
{
return value;
}

return base.GetUnknownRequestHeader(name);
return this.headers.TryGetValue(name, out var value) ? value : base.GetUnknownRequestHeader(name);
}

public override string GetKnownRequestHeader(int index)
{
var name = GetKnownRequestHeaderName(index);

if (this.headers.TryGetValue(name, out var value))
{
return value;
}

return base.GetKnownRequestHeader(index);
return this.headers.TryGetValue(name, out var value) ? value : base.GetKnownRequestHeader(index);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,6 @@ private XDocument ApplyInstallTransformation(string originalConfiguration, strin
return this.ApplyTransformation(originalConfiguration, resourceName);
}

private XDocument ApplyUninstallTransformation(string originalConfiguration, string resourceName)
{
return this.ApplyTransformation(originalConfiguration, resourceName);
}

private void VerifyTransformation(string expectedConfigContent, XDocument transformedWebConfig)
{
Assert.True(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void AspNetMetricTagsAreCollectedSuccessfully(
}

Assert.Equal(
expected: new List<double> { 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10, double.PositiveInfinity },
expected: [0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10, double.PositiveInfinity],
actual: histogramBounds);

void ExpectTag<T>(T? expected, string tagName)
Expand Down

0 comments on commit 6b7ad12

Please sign in to comment.