Skip to content

Commit

Permalink
[Tests] Nullable (#5882)
Browse files Browse the repository at this point in the history
Co-authored-by: Piotr Kiełkowicz <[email protected]>
Co-authored-by: Mikel Blanchard <[email protected]>
  • Loading branch information
3 people authored Oct 11, 2024
1 parent 36d5900 commit f3cd0e7
Show file tree
Hide file tree
Showing 38 changed files with 211 additions and 173 deletions.
8 changes: 4 additions & 4 deletions src/Shared/PooledList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace OpenTelemetry.Internal;

internal readonly struct PooledList<T> : IEnumerable<T>, ICollection
{
private static int lastAllocatedSize = 64;
public static int LastAllocatedSize = 64;

private readonly T[] buffer;

Expand All @@ -36,7 +36,7 @@ public ref T this[int index]

public static PooledList<T> Create()
{
return new PooledList<T>(ArrayPool<T>.Shared.Rent(lastAllocatedSize), 0);
return new PooledList<T>(ArrayPool<T>.Shared.Rent(LastAllocatedSize), 0);
}

public static void Add(ref PooledList<T> list, T item)
Expand All @@ -47,10 +47,10 @@ public static void Add(ref PooledList<T> list, T item)

if (list.Count >= buffer.Length)
{
lastAllocatedSize = buffer.Length * 2;
LastAllocatedSize = buffer.Length * 2;
var previousBuffer = buffer;

buffer = ArrayPool<T>.Shared.Rent(lastAllocatedSize);
buffer = ArrayPool<T>.Shared.Rent(LastAllocatedSize);

var span = previousBuffer.AsSpan();
span.CopyTo(buffer);
Expand Down
13 changes: 2 additions & 11 deletions test/OpenTelemetry.Tests/Internal/PooledListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0

using System.Collections;
using System.Reflection;

using Xunit;

Expand Down Expand Up @@ -44,17 +43,9 @@ public void Verify_CreateAddClear()
[Fact]
public void Verify_AllocatedSize()
{
int GetLastAllocatedSize(PooledList<int> pooledList)
{
var value = typeof(PooledList<int>)
.GetField("lastAllocatedSize", BindingFlags.NonPublic | BindingFlags.Static)
.GetValue(pooledList);
return (int)value;
}

var pooledList = PooledList<int>.Create();

var size = GetLastAllocatedSize(pooledList);
var size = PooledList<int>.LastAllocatedSize;
Assert.Equal(64, size);

// The Add() method has a condition to double the size of the buffer
Expand All @@ -65,7 +56,7 @@ int GetLastAllocatedSize(PooledList<int> pooledList)
PooledList<int>.Add(ref pooledList, i);
}

size = GetLastAllocatedSize(pooledList);
size = PooledList<int>.LastAllocatedSize;
Assert.Equal(128, size);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void SelfDiagnosticsConfigParser_TryParseLogLevel()
""FileSize"": 1024,
""LogLevel"": ""Error""
}";
Assert.True(SelfDiagnosticsConfigParser.TryParseLogLevel(configJson, out string logLevelString));
Assert.True(SelfDiagnosticsConfigParser.TryParseLogLevel(configJson, out string? logLevelString));
Assert.Equal("Error", logLevelString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void SelfDiagnosticsEventListener_constructor_Invalid_Input()
// no configRefresher object
Assert.Throws<ArgumentNullException>(() =>
{
_ = new SelfDiagnosticsEventListener(EventLevel.Error, null);
_ = new SelfDiagnosticsEventListener(EventLevel.Error, null!);
});
}

Expand Down
2 changes: 1 addition & 1 deletion test/OpenTelemetry.Tests/Internal/WildcardHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void WildcardRegex_ShouldMatch(string[] patterns, string matchWith, bool
[InlineData("a", false)]
[InlineData("a.*", true)]
[InlineData("a.?", true)]
public void Verify_ContainsWildcard(string pattern, bool expected)
public void Verify_ContainsWildcard(string? pattern, bool expected)
{
Assert.Equal(expected, WildcardHelper.ContainsWildcard(pattern));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,23 @@ public void StateValuesAndScopeBufferingTest()
Assert.NotNull(logRecord.AttributeStorage);
Assert.NotNull(logRecord.ILoggerData.BufferedScopes);

KeyValuePair<string, object> actualState = logRecord.StateValues[0];
KeyValuePair<string, object?> actualState = logRecord.StateValues[0];

Assert.Same("Value", actualState.Key);
Assert.Same("Hello world", actualState.Value);

int scopeCount = 0;
bool foundScope = false;

logRecord.ForEachScope<object>(
logRecord.ForEachScope<object?>(
(s, o) =>
{
foundScope = ReferenceEquals(s.Scope, exportedItems);
scopeCount++;
},
null);

Assert.Equal(1, scopeCount);
Assert.True(foundScope);

processor.Shutdown();
Expand Down
Loading

0 comments on commit f3cd0e7

Please sign in to comment.