private void Test(Action<IProjectCacheHostService, ProjectId, ICachedObjectOwner, ObjectReference> action) { // Putting cacheService.CreateStrongReference in a using statement // creates a temporary local that isn't collected in Debug builds // Wrapping it in a lambda allows it to get collected. var cacheService = new ProjectCacheService(null, int.MaxValue); var projectId = ProjectId.CreateNewId(); var owner = new Owner(); var instance = new ObjectReference(); action(cacheService, projectId, owner, instance); }
private void StopObservingAndWaitForReferenceToGo(ObjectReference observed, int delay = 0) { // stop observing it and let GC reclaim it observed.Strong = null; DateTime start = DateTime.UtcNow; TimeSpan maximumTimeToWait = TimeSpan.FromSeconds(120); while (observed.Weak.IsAlive && (DateTime.UtcNow - start) < maximumTimeToWait) { GC.Collect(); GC.WaitForPendingFinalizers(); } const int TimerPrecision = 30; var actualTimePassed = DateTime.UtcNow - start + TimeSpan.FromMilliseconds(TimerPrecision); Assert.True(observed.Weak.Target == null, string.Format("Target object ({0}) was not collected after {1} ms", observed.Weak.Target, actualTimePassed)); }
private void StopObservingAndWaitForReferenceToGo(ObjectReference observed, int delay = 0, string dumpFileName = null) { // stop observing it and let GC reclaim it observed.Strong = null; DateTime start = DateTime.UtcNow; TimeSpan maximumTimeToWait = TimeSpan.FromSeconds(120); while (observed.Weak.IsAlive && (DateTime.UtcNow - start) < maximumTimeToWait) { GC.Collect(); GC.WaitForPendingFinalizers(); } const int TimerPrecision = 30; var actualTimePassed = DateTime.UtcNow - start + TimeSpan.FromMilliseconds(TimerPrecision); var isTargetCollected = observed.Weak.Target == null; if (!isTargetCollected && !string.IsNullOrEmpty(dumpFileName)) { if (string.Compare(Path.GetExtension(dumpFileName), ".dmp", StringComparison.OrdinalIgnoreCase) != 0) { dumpFileName += ".dmp"; } DumpProcess(dumpFileName); Assert.True(false, $"Target object not collected. Process dump saved to '{dumpFileName}'"); } Assert.True(isTargetCollected, string.Format("Target object ({0}) was not collected after {1} ms", observed.Weak.Target, actualTimePassed)); }
private void Add(WeakList<object> list, ObjectReference<C> value) { value.UseReference(r => list.Add(r)); }
private void Add(WeakList<object> list, ObjectReference value) { list.Add(value.Strong); }