Skip to content

Commit

Permalink
refactor test to include setting/reading EnvVar
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMothra committed May 23, 2023
1 parent 929c43c commit 8e5b162
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
17 changes: 6 additions & 11 deletions src/OpenTelemetry.Api/Internal/HttpSemanticConventionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,17 @@ public static HttpSemanticConvention GetSemanticConventionOptIn()
try
{
var envVarValue = Environment.GetEnvironmentVariable("OTEL_SEMCONV_STABILITY_OPT_IN");
return EvaluateValue(envVarValue);
return envVarValue?.ToLowerInvariant() switch
{
"http" => HttpSemanticConvention.New,
"http/dup" => HttpSemanticConvention.Dupe,
_ => HttpSemanticConvention.Old,
};
}
catch
{
return HttpSemanticConvention.Old;
}
}

internal static HttpSemanticConvention EvaluateValue(string value)
{
return value?.ToLowerInvariant() switch
{
"http" => HttpSemanticConvention.New,
"http/dup" => HttpSemanticConvention.Dupe,
_ => HttpSemanticConvention.Old,
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,31 @@ public void VerifyFlags()
}

[Fact]
public void VerifyEvaluate()
public void VerifyGetSemanticConventionOptIn()
{
Assert.Equal(HttpSemanticConvention.Old, EvaluateValue(null));
Assert.Equal(HttpSemanticConvention.Old, EvaluateValue(string.Empty));
Assert.Equal(HttpSemanticConvention.Old, EvaluateValue("junk"));
Assert.Equal(HttpSemanticConvention.Old, EvaluateValue("none"));
Assert.Equal(HttpSemanticConvention.Old, EvaluateValue("NONE"));
Assert.Equal(HttpSemanticConvention.New, EvaluateValue("http"));
Assert.Equal(HttpSemanticConvention.New, EvaluateValue("HTTP"));
Assert.Equal(HttpSemanticConvention.Dupe, EvaluateValue("http/dup"));
Assert.Equal(HttpSemanticConvention.Dupe, EvaluateValue("HTTP/DUP"));
this.RunTestWithEnvironmentVariable(null, HttpSemanticConvention.Old);
this.RunTestWithEnvironmentVariable(string.Empty, HttpSemanticConvention.Old);
this.RunTestWithEnvironmentVariable("junk", HttpSemanticConvention.Old);
this.RunTestWithEnvironmentVariable("none", HttpSemanticConvention.Old);
this.RunTestWithEnvironmentVariable("NONE", HttpSemanticConvention.Old);
this.RunTestWithEnvironmentVariable("http", HttpSemanticConvention.New);
this.RunTestWithEnvironmentVariable("HTTP", HttpSemanticConvention.New);
this.RunTestWithEnvironmentVariable("http/dup", HttpSemanticConvention.Dupe);
this.RunTestWithEnvironmentVariable("HTTP/DUP", HttpSemanticConvention.Dupe);
}

private void RunTestWithEnvironmentVariable(string value, HttpSemanticConvention expected)
{
try
{
Environment.SetEnvironmentVariable("OTEL_SEMCONV_STABILITY_OPT_IN", value);

Assert.Equal(expected, GetSemanticConventionOptIn());
}
finally
{
Environment.SetEnvironmentVariable("OTEL_SEMCONV_STABILITY_OPT_IN", null);
}
}
}
}

0 comments on commit 8e5b162

Please sign in to comment.