Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Update ListProxyTest.WeakToWeak so it works in Release mode (#866)
Browse files Browse the repository at this point in the history
* Update ListProxyTest.WeakToWeak so it works in Release mode

* Disable CS0414 for test
  • Loading branch information
hartez authored and rmarinho committed Apr 13, 2017
1 parent 811b9bc commit fc778eb
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions Xamarin.Forms.Core.UnitTests/ListProxyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,29 +423,31 @@ IEnumerator IEnumerable.GetEnumerator ()
}
}

// Need a member to keep this reference around, otherwise it gets optimized
// out early in Release mode during the WeakToWeak test
#pragma warning disable 0414 // Never accessed, it's just here to prevent GC
ListProxy _proxyForWeakToWeakTest;
#pragma warning restore 0414

[Test]
[Ignore()]
//TODO: Need to figure why this is failing on release
public void WeakToWeak()
{
WeakCollectionChangedList list = new WeakCollectionChangedList();
var proxy = new ListProxy(list);
_proxyForWeakToWeakTest = new ListProxy(list);

Assert.True(list.AddObject());
Assert.True(list.AddObject(), "GC hasn't run");

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Assert.IsTrue(list.AddObject());
Assert.IsTrue(list.AddObject(), "GC run, but proxy should still hold a reference");

proxy = null;
_proxyForWeakToWeakTest = null;

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();

Assert.IsFalse(list.AddObject());
Assert.IsFalse(list.AddObject(), "Proxy is gone and GC has run");
}

public class WeakCollectionChangedList : List<object>, INotifyCollectionChanged
Expand All @@ -467,7 +469,7 @@ public bool AddObject()
{
bool invoked = false;
var me = new object();

Console.WriteLine($"Handler count is {handlers.Count}");
foreach (var handler in handlers.ToList())
{
if (handler.IsActive)
Expand All @@ -477,6 +479,7 @@ public bool AddObject()
}
else
{
Console.WriteLine($"Handler is inactive");
handlers.Remove(handler);
}
}
Expand Down

0 comments on commit fc778eb

Please sign in to comment.