Exemplo n.º 1
0
        public void Finalization_Any_Collect()
        {
            WeakReference <ProxyObjectBase> weakReference = null;

            new Action(() =>
            {
                var proxy     = new MockProxyObject();
                weakReference = new WeakReference <ProxyObjectBase>(proxy, true);
            })();

            GC.Collect(); //first finalization (mark as finalized)
            GC.WaitForPendingFinalizers();

            new Action(() =>
            {
                weakReference.TryGetTarget(out var target);
                Assert.IsTrue(ProxyObjectBase.FinalizeRequested.ContainsKey(target.DtoGuid), "Object did not save itself!");
                target.FinalizeProxy();
            })();

            GC.Collect(); //first finalization (mark as finalized)
            GC.WaitForPendingFinalizers();
            GC.Collect(); //collect finalization
            GC.WaitForPendingFinalizers();

            Assert.IsTrue(ProxyObjectBase.FinalizeRequested.Count == 0, "There should be no Finalization Requests at this point!");
            Assert.IsFalse(weakReference.TryGetTarget(out _), "GetTarget should not return anything!");
        }
        public void ResolveReference_ReferenceResurrected_ReturnObject()
        {
            WeakReference <ProxyObjectBase> weakReference = null;
            Guid guid = Guid.Empty;

            new Action(() =>
            {
                var mockObject = new MockProxyObject {
                    DtoGuid = Guid.NewGuid()
                };
                weakReference = new WeakReference <ProxyObjectBase>(mockObject, true);
                _knownDtos.Add(mockObject.DtoGuid, new WeakReference <ProxyObjectBase>(mockObject));
                guid = new Guid(mockObject.DtoGuid.ToString());
            })();

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

            if (ProxyObjectBase.FinalizeRequested.Count < 1)
            {
                Assert.Fail("Object did not finalize or finalized instantly");
            }

            new Action(() =>
            {
                weakReference.TryGetTarget(out var target);
                var proxy = _clientReferenceResolver.ResolveReference(this, target.DtoGuid.ToString());
                Assert.AreEqual(target, proxy, "Proxy did not resurrected properly");
            })();
        }
Exemplo n.º 3
0
 public void Initialize()
 {
     _mockProxy = new MockProxyObject {
         DtoGuid = Guid.NewGuid()
     };
     _mockBase = new PrivateObject(_mockProxy, new PrivateType(typeof(ProxyObjectBase)));
 }
        public void Initialize()
        {
            _mockObject = new MockProxyObject {
                DtoGuid = Guid.NewGuid()
            };
            _clientReferenceResolver = new ReferenceResolver();

            var po = new PrivateObject(_clientReferenceResolver);

            _knownDtos = ((Dictionary <Guid, WeakReference <ProxyObjectBase> >)po.GetField("_knownDtos"));
        }
Exemplo n.º 5
0
        public void Finalization_Any_Resurrected()
        {
            WeakReference <ProxyObjectBase> weakReference = null;

            new Action(() =>
            {
                var proxy     = new MockProxyObject();
                weakReference = new WeakReference <ProxyObjectBase>(proxy, true);
            })();

            GC.Collect(); //first finalization (mark as finalized)
            GC.WaitForPendingFinalizers();

            new Action(() =>
            {
                weakReference.TryGetTarget(out var target);
                Assert.IsTrue(ProxyObjectBase.FinalizeRequested.ContainsKey(target.DtoGuid), "Object did not save itself!");
            })();

            GC.Collect(); //first finalization (mark as finalized)
            GC.WaitForPendingFinalizers();
            GC.Collect(); //collect finalization
            GC.WaitForPendingFinalizers();

            new Action(() =>
            {
                weakReference.TryGetTarget(out var target);
                target.Resurrect();
            })();

            GC.Collect(); //first finalization (mark as finalized)
            GC.WaitForPendingFinalizers();
            GC.Collect(); //collect finalization
            GC.WaitForPendingFinalizers();

            Assert.IsTrue(ProxyObjectBase.FinalizeRequested.Count == 0, "There should be no Finalization Requests at this point!");
            Assert.IsTrue(weakReference.TryGetTarget(out var final), "GetTarget should not return anything!");
            PrivateObject priv = new PrivateObject(final, new PrivateType(typeof(ProxyObjectBase)));

            Assert.IsFalse((bool)priv.GetField("_finalizedRequested"), "Object resurrected, but did not reset finalization properties!");
        }
Exemplo n.º 6
0
        public void FinalizerTest()
        {
            WeakReference <ProxyObjectBase> weakRefernce = null;
            Guid checkGuid = Guid.Empty;

            new Action(() =>
            {
                var mockProxy = new MockProxyObject {
                    DtoGuid = Guid.NewGuid()
                };
                checkGuid    = mockProxy.DtoGuid;
                weakRefernce = new WeakReference <ProxyObjectBase>(mockProxy, true);
            })();

            GC.Collect(); //first collect (save)
            GC.WaitForPendingFinalizers();

            new Action(() =>
            {
                weakRefernce.TryGetTarget(out var target);
                var priv = new PrivateObject(target, new PrivateType(typeof(ProxyObjectBase)));

                Assert.IsTrue(ProxyObjectBase.FinalizeRequested.ContainsKey(target.DtoGuid), "Object did not save itself!");
                Assert.IsTrue((bool)priv.GetField("_finalizeRequested"), "Object not prepared for collection!");

                ProxyObjectBase.FinalizeRequested.TryRemove(target.DtoGuid, out _);
            })();

            GC.Collect(); //first collect (mark as finalized)
            GC.WaitForPendingFinalizers();
            GC.Collect(); //second collect (collection)
            GC.WaitForPendingFinalizers();

            Assert.IsFalse(ProxyObjectBase.FinalizeRequested.ContainsKey(checkGuid), "Object did not collect...");
            Assert.IsFalse(weakRefernce.TryGetTarget(out _), "Object is still alive...");
        }