From fc778eb9789f50634d48fb9ad127f211b3fcfcc7 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Thu, 13 Apr 2017 08:40:05 -0600 Subject: [PATCH] Update ListProxyTest.WeakToWeak so it works in Release mode (#866) * Update ListProxyTest.WeakToWeak so it works in Release mode * Disable CS0414 for test --- Xamarin.Forms.Core.UnitTests/ListProxyTests.cs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Xamarin.Forms.Core.UnitTests/ListProxyTests.cs b/Xamarin.Forms.Core.UnitTests/ListProxyTests.cs index 2d9cda4..cd94e71 100644 --- a/Xamarin.Forms.Core.UnitTests/ListProxyTests.cs +++ b/Xamarin.Forms.Core.UnitTests/ListProxyTests.cs @@ -423,29 +423,31 @@ namespace Xamarin.Forms.Core.UnitTests } } + // 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, INotifyCollectionChanged @@ -467,7 +469,7 @@ namespace Xamarin.Forms.Core.UnitTests { bool invoked = false; var me = new object(); - + Console.WriteLine($"Handler count is {handlers.Count}"); foreach (var handler in handlers.ToList()) { if (handler.IsActive) @@ -477,6 +479,7 @@ namespace Xamarin.Forms.Core.UnitTests } else { + Console.WriteLine($"Handler is inactive"); handlers.Remove(handler); } } -- 2.7.4