-
Notifications
You must be signed in to change notification settings - Fork 747
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
refactor: Remove reflection usage in Brush #16741
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2495fab
refactor: Remove reflection usage in Brush
dr1rrb acd9283
test: Test the WekEventManager
dr1rrb e99727b
test: Add test for the remove handler while raising
dr1rrb bb0e2ac
perf: Even more perf by using ref to access handlers list
dr1rrb 153a52c
chore: Fix build on .net7
dr1rrb d3ed372
chore: typo
dr1rrb 5c81a9e
fix(build): Exclude test on windows
dr1rrb 65cc5ed
Merge branch 'dev/dr/brushReflection' of https://github.com/unoplatfo…
dr1rrb 3c192bc
chore: Fix net7.0 build
dr1rrb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
161 changes: 161 additions & 0 deletions
161
src/Uno.UI.RuntimeTests/Tests/Uno_UI_Helpers/Given_WeakEventManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
#nullable enable | ||
#if HAS_UNO | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Uno.UI.Helpers; | ||
|
||
namespace Uno.UI.RuntimeTests.Tests.Uno_UI_Helpers; | ||
|
||
[TestClass] | ||
public class Given_WeakEventManager | ||
{ | ||
private sealed class EventPublisher | ||
{ | ||
private readonly WeakEventManager _manager = new(); | ||
|
||
internal event Action Event | ||
{ | ||
add => _manager.AddEventHandler(value); | ||
remove => _manager.RemoveEventHandler(value); | ||
} | ||
|
||
public void RaiseEvent() => _manager.HandleEvent("Event"); | ||
} | ||
|
||
private sealed class EventSubscriber | ||
{ | ||
public void M() { } | ||
} | ||
|
||
[TestMethod] | ||
public void When_ManySubscriptions_Then_DoesNotLeak() | ||
{ | ||
var publisher = new EventPublisher(); | ||
var weakRefs = new List<WeakReference<EventSubscriber>>(); | ||
Subscribe(publisher, weakRefs); | ||
|
||
for (var i = 0; i < 10; i++) | ||
{ | ||
GC.Collect(); | ||
GC.WaitForPendingFinalizers(); | ||
} | ||
|
||
foreach (var x in weakRefs) | ||
{ | ||
Assert.IsFalse(x.TryGetTarget(out _)); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void When_ReEnter_Then_AllHandlersInvokedProperly() | ||
{ | ||
var sut = new WeakEventManager(); | ||
int handler1Count = 0, handler2Count = 0; | ||
|
||
sut.AddEventHandler(Handler1, "Event1"); | ||
sut.AddEventHandler(Handler2, "Event1"); | ||
sut.AddEventHandler(Handler1_2, "Event2"); | ||
sut.AddEventHandler(Handler2_2, "Event2"); | ||
|
||
sut.HandleEvent("Event1"); | ||
|
||
Assert.AreEqual(3, handler1Count); | ||
Assert.AreEqual(3, handler2Count); | ||
|
||
sut.HandleEvent("Event2"); | ||
|
||
Assert.AreEqual(4, handler1Count); | ||
Assert.AreEqual(4, handler2Count); | ||
|
||
void Handler1() | ||
{ | ||
handler1Count++; | ||
sut.HandleEvent("Event2"); | ||
} | ||
|
||
void Handler1_2() | ||
{ | ||
handler1Count++; | ||
} | ||
|
||
void Handler2() | ||
{ | ||
handler2Count++; | ||
sut.HandleEvent("Event2"); | ||
} | ||
|
||
void Handler2_2() | ||
{ | ||
handler2Count++; | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void When_UnSubscribeInHandler() | ||
{ | ||
var sut = new WeakEventManager(); | ||
int handler1Count = 0, handler2Count = 0; | ||
|
||
sut.AddEventHandler(Handler1, "Event1"); | ||
sut.AddEventHandler(Handler2, "Event1"); | ||
|
||
sut.HandleEvent("Event1"); | ||
sut.HandleEvent("Event1"); | ||
|
||
Assert.AreEqual(1, handler1Count); | ||
Assert.AreEqual(2, handler2Count); | ||
|
||
void Handler1() | ||
{ | ||
handler1Count++; | ||
sut.RemoveEventHandler(Handler1, "Event1"); | ||
} | ||
|
||
void Handler2() | ||
{ | ||
handler2Count++; | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void When_UnSubscribeInHandler2() | ||
{ | ||
var pub = new EventPublisher(); | ||
int handler1Count = 0, handler2Count = 0; | ||
|
||
pub.Event += Handler1; | ||
pub.Event += Handler2; | ||
|
||
pub.RaiseEvent(); | ||
pub.RaiseEvent(); | ||
|
||
Assert.AreEqual(1, handler1Count); | ||
Assert.AreEqual(2, handler2Count); | ||
|
||
void Handler1() | ||
{ | ||
handler1Count++; | ||
pub.Event -= Handler1; | ||
} | ||
|
||
void Handler2() | ||
{ | ||
handler2Count++; | ||
} | ||
} | ||
|
||
private void Subscribe(EventPublisher publisher, List<WeakReference<EventSubscriber>> weakRefs) | ||
{ | ||
for (var i = 0; i < 1000; i++) | ||
{ | ||
var subscriber = new EventSubscriber(); | ||
publisher.Event += subscriber.M; | ||
weakRefs.Add(new WeakReference<EventSubscriber>(subscriber)); | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is failing on iOS @dr1rrb