Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[repo/PersistentStorage] Prepare to .NET9 #2264

Merged
merged 8 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ public IEnumerable<PersistentBlob> GetBlobs()
{
try
{
return this.OnGetBlobs() ?? Enumerable.Empty<PersistentBlob>();
return this.OnGetBlobs() ?? [];
}
catch (Exception ex)
{
PersistentStorageAbstractionsEventSource.Log.PersistentStorageAbstractionsException(nameof(PersistentBlobProvider), "Failed to get all the blobs", ex);
return Enumerable.Empty<PersistentBlob>();
return [];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ internal static long CalculateFolderSize(string path)
long directorySize = 0;
try
{
foreach (string file in Directory.EnumerateFiles(path))
foreach (var file in Directory.EnumerateFiles(path))
{
if (File.Exists(file))
{
FileInfo fileInfo = new FileInfo(file);
var fileInfo = new FileInfo(file);
directorySize += fileInfo.Length;
}
}

foreach (string dir in Directory.GetDirectories(path))
foreach (var dir in Directory.GetDirectories(path))
{
directorySize += CalculateFolderSize(dir);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected override bool OnTryWrite(byte[] buffer, int leasePeriodMilliseconds =
{
Guard.ThrowIfNull(buffer);

string path = this.FullPath + ".tmp";
var path = this.FullPath + ".tmp";

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected override IEnumerable<PersistentBlob> OnGetBlobs()

foreach (var file in Directory.EnumerateFiles(this.DirectoryPath, "*.blob", SearchOption.TopDirectoryOnly).OrderByDescending(f => f))
{
DateTime fileDateTime = PersistentStorageHelper.GetDateTimeFromBlobName(file);
var fileDateTime = PersistentStorageHelper.GetDateTimeFromBlobName(file);
if (fileDateTime > retentionDeadline)
{
yield return new FileBlob(file, this.directorySizeTracker);
Expand Down Expand Up @@ -173,7 +173,7 @@ private void OnMaintenanceEvent(object? source, ElapsedEventArgs e)

private bool CheckStorageSize()
{
if (!this.directorySizeTracker.IsSpaceAvailable(out long size))
if (!this.directorySizeTracker.IsSpaceAvailable(out var size))
{
// TODO: check accuracy of size reporting.
PersistentStorageEventSource.Log.PersistentStorageWarning(
Expand All @@ -197,14 +197,7 @@ private bool CheckStorageSize()
var blobFilePath = Path.Combine(this.DirectoryPath, PersistentStorageHelper.GetUniqueFileName(".blob"));
var blob = new FileBlob(blobFilePath, this.directorySizeTracker);

if (blob.TryWrite(buffer, leasePeriodMilliseconds))
{
return blob;
}
else
{
return null;
}
return blob.TryWrite(buffer, leasePeriodMilliseconds) ? blob : null;
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal static void RemoveExpiredBlob(DateTime retentionDeadline, string filePa
{
if (filePath.EndsWith(".blob", StringComparison.OrdinalIgnoreCase))
{
DateTime fileDateTime = GetDateTimeFromBlobName(filePath);
var fileDateTime = GetDateTimeFromBlobName(filePath);
if (fileDateTime < retentionDeadline)
{
try
Expand All @@ -30,11 +30,11 @@ internal static void RemoveExpiredBlob(DateTime retentionDeadline, string filePa

internal static bool RemoveExpiredLease(DateTime leaseDeadline, string filePath)
{
bool success = false;
var success = false;

if (filePath.EndsWith(".lock", StringComparison.OrdinalIgnoreCase))
{
DateTime fileDateTime = GetDateTimeFromLeaseName(filePath);
var fileDateTime = GetDateTimeFromLeaseName(filePath);
if (fileDateTime < leaseDeadline)
{
var newFilePath = filePath.Substring(0, filePath.LastIndexOf('@'));
Expand All @@ -55,11 +55,11 @@ internal static bool RemoveExpiredLease(DateTime leaseDeadline, string filePath)

internal static bool RemoveTimedOutTmpFiles(DateTime timeoutDeadline, string filePath)
{
bool success = false;
var success = false;

if (filePath.EndsWith(".tmp", StringComparison.OrdinalIgnoreCase))
{
DateTime fileDateTime = GetDateTimeFromBlobName(filePath);
var fileDateTime = GetDateTimeFromBlobName(filePath);
if (fileDateTime < timeoutDeadline)
{
try
Expand Down Expand Up @@ -144,7 +144,7 @@ internal static DateTime GetDateTimeFromLeaseName(string filePath)
{
var fileName = Path.GetFileNameWithoutExtension(filePath);
var startIndex = fileName.LastIndexOf('@') + 1;
var time = fileName.Substring(startIndex, fileName.Length - startIndex);
var time = fileName.Substring(startIndex);
DateTime.TryParseExact(time, "yyyy-MM-ddTHHmmss.fffffffZ", CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateTime);
return dateTime.ToUniversalTime();
}
Expand Down
32 changes: 15 additions & 17 deletions test/Shared/EventSourceTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal static class EventSourceTestHelper
{
public static void MethodsAreImplementedConsistentlyWithTheirAttributes(EventSource eventSource)
{
foreach (MethodInfo publicMethod in GetEventMethods(eventSource))
foreach (var publicMethod in GetEventMethods(eventSource))
{
VerifyMethodImplementation(eventSource, publicMethod);
}
Expand All @@ -25,7 +25,7 @@ private static void VerifyMethodImplementation(EventSource eventSource, MethodIn
listener.EnableEvents(eventSource, EventLevel.Verbose, EventKeywords.All);
try
{
object[] eventArguments = GenerateEventArguments(eventMethod);
var eventArguments = GenerateEventArguments(eventMethod);
eventMethod.Invoke(eventSource, eventArguments);

var actualEvent = listener.Messages.First(q => q.EventName == eventMethod.Name);
Expand Down Expand Up @@ -54,9 +54,9 @@ private static void VerifyMethodImplementation(EventSource eventSource, MethodIn

private static object[] GenerateEventArguments(MethodInfo eventMethod)
{
ParameterInfo[] parameters = eventMethod.GetParameters();
var parameters = eventMethod.GetParameters();
var arguments = new object[parameters.Length];
for (int i = 0; i < parameters.Length; i++)
for (var i = 0; i < parameters.Length; i++)
{
arguments[i] = GenerateArgument(parameters[i]);
}
Expand All @@ -71,37 +71,35 @@ private static object GenerateArgument(ParameterInfo parameter)
return "Test String";
}

if (parameter.ParameterType.IsValueType)
{
return Activator.CreateInstance(parameter.ParameterType)
?? throw new NotSupportedException($"Could not create an instance of the '{parameter.ParameterType}' type.");
}

throw new NotSupportedException("Complex types are not supported");
return parameter.ParameterType.IsValueType
? Activator.CreateInstance(parameter.ParameterType)
?? throw new NotSupportedException(
$"Could not create an instance of the '{parameter.ParameterType}' type.")
: throw new NotSupportedException("Complex types are not supported");
}

private static void VerifyEventId(MethodInfo eventMethod, EventWrittenEventArgs actualEvent)
{
int expectedEventId = GetEventAttribute(eventMethod).EventId;
var expectedEventId = GetEventAttribute(eventMethod).EventId;
AssertEqual(nameof(VerifyEventId), expectedEventId, actualEvent.EventId);
}

private static void VerifyEventLevel(MethodInfo eventMethod, EventWrittenEventArgs actualEvent)
{
EventLevel expectedLevel = GetEventAttribute(eventMethod).Level;
var expectedLevel = GetEventAttribute(eventMethod).Level;
AssertEqual(nameof(VerifyEventLevel), expectedLevel, actualEvent.Level);
}

private static void VerifyEventMessage(MethodInfo eventMethod, EventWrittenEventArgs actualEvent, object[] eventArguments)
{
string expectedMessage = eventArguments.Length == 0
var expectedMessage = eventArguments.Length == 0
? GetEventAttribute(eventMethod).Message!
: string.Format(CultureInfo.InvariantCulture, GetEventAttribute(eventMethod).Message!, eventArguments);

string actualMessage = string.Format(
var actualMessage = string.Format(
CultureInfo.InvariantCulture,
actualEvent.Message!,
actualEvent.Payload?.ToArray() ?? Array.Empty<object>());
actualEvent.Payload?.ToArray() ?? []);

AssertEqual(nameof(VerifyEventMessage), expectedMessage, actualMessage);
}
Expand All @@ -128,7 +126,7 @@ private static EventAttribute GetEventAttribute(MethodInfo eventMethod)

private static IEnumerable<MethodInfo> GetEventMethods(EventSource eventSource)
{
MethodInfo[] methods = eventSource.GetType().GetMethods();
var methods = eventSource.GetType().GetMethods();
return methods.Where(m => m.GetCustomAttributes(typeof(EventAttribute), false).Length != 0);
}
}
2 changes: 1 addition & 1 deletion test/Shared/TestEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)
protected override void OnEventSourceCreated(EventSource eventSource)
{
// Check for null because this method is called by the base class constructor before we can initialize it
Action<EventSource>? callback = this.OnOnEventSourceCreated;
var callback = this.OnOnEventSourceCreated;
callback?.Invoke(eventSource);
}
}
Loading