-
Notifications
You must be signed in to change notification settings - Fork 264
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
base: main
Are you sure you want to change the base?
Conversation
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.
There was a problem hiding this 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. |
There was a problem hiding this comment.
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 FileOperation
s.
/// </summary> | ||
[NonSerialized] | ||
#endif | ||
private readonly List<Subscription> subscriptions = []; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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>(); |
There was a problem hiding this comment.
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>(); |
There was a problem hiding this comment.
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>(); |
There was a problem hiding this comment.
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).
[Test] | ||
public void Events_FileSystemOperationEventArgs_ShouldNotBeSerializable() | ||
{ |
There was a problem hiding this comment.
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.
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