Skip to content

feat: Add support for event-driven behaviours in MockFileSystem. #1313

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mistial-dev
Copy link

Added unit tests including various event handling scenarios, such as tracking file operations, validating timestamps, monitoring access patterns, simulating controlled failures, and profiling performance metrics.

Potential resolution for #805

Added unit tests including various event handling scenarios, such as tracking file operations, validating timestamps, monitoring access patterns, simulating controlled failures, and profiling performance metrics.
Copy link
Member

@vbreuss vbreuss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate your contribution, but as it seems to be quite heavily AI-generated, it will take time to make a thorough review. I expect it to take multiple iterations, so please be patient!

public enum FileOperation
{
/// <summary>
/// File or directory creation operation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would help to have a list of operations on the file system in the XML-Comment, that trigger the corresponding FileOperations.

/// </summary>
[NonSerialized]
#endif
private readonly List<Subscription> subscriptions = [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use a ConcurrentDictionary (or another thread-safe collection), to avoid using the LockObject...

/// Tracks the current version of active subscriptions, allowing for the detection of changes
/// such as additions or removals of subscription handlers.
/// </summary>
private volatile int subscriptionVersion;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why you need the subscriptionVersion, as you only ever increment it, but never read it. Or did I overlook something?

}

var args = new FileSystemOperationEventArgs(path, operation, resourceType, phase);
Subscription[] currentSubscriptions;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For thread-safe collections you should be able to directly iterate over the subscriptions and can avoid introducing a temporary copy of the collection.

/// <summary>
/// Gets a value indicating whether this subscription has been disposed.
/// </summary>
public bool IsDisposed
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you remove the Subscription from the collection, it seems unnecessary to provide a flag and check for it.
What are your reasons for doing so?


using (fileSystem.Events.Subscribe(args => Interlocked.Increment(ref eventCount)))
{
const int operationCount = 100;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100 parallel tasks seems a bit excessive for a unit test. Wouldn't e.g. 10 be sufficient?

{
var fileSystem = new MockFileSystem(new MockFileSystemOptions { EnableEvents = true });
var eventCount = 0;
var exceptions = new List<Exception>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use a thread-safe collection, you can avoid locking on it (see line 588).

public void Events_SubscriptionDisposal_DuringEventFiring_ShouldNotFail()
{
var fileSystem = new MockFileSystem(new MockFileSystemOptions { EnableEvents = true });
var exceptions = new List<Exception>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use a thread-safe collection, you can avoid locking on it (see line 620).

public void Events_ConcurrentSubscriptionAndUnsubscription_ShouldBeThreadSafe()
{
var fileSystem = new MockFileSystem(new MockFileSystemOptions { EnableEvents = true });
var exceptions = new List<Exception>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use a thread-safe collection, you can avoid locking on it (see line 694 and 714).

Comment on lines +792 to +794
[Test]
public void Events_FileSystemOperationEventArgs_ShouldNotBeSerializable()
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need a test to verify, that a class is NOT serializable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants