static void Main(string[] args) { try { GCSettings.LatencyMode = GCLatencyMode.Batch; GC.TryStartNoGCRegion(150 * 1024 * 1024, false); Console.WriteLine("== Memory consumption =="); Console.WriteLine(); for (var i = 1; i <= 90_000; i++) { var x = LazyProxyGenerator.CreateLazyProxyFor <IService>(() => new Service()); x.Incrementa(); // instance is created here! if (i % 2000 == 0) { Console.WriteLine($"{ i.ToString("0.0,0").PadLeft(10) } >> { (GC.GetTotalMemory(false) / 1024).ToString("0.0,0") } Kb"); } } } finally { Console.ReadLine(); GC.EndNoGCRegion(); } }
public void DisposeNonLoadedLazy() { var svc = new Service(); Assert.AreEqual(false, svc.Disposed); var construiuLazy = false; IService svcProxy = LazyProxyGenerator.CreateLazyProxyFor <IService>(() => { construiuLazy = true; return(svc); }); Assert.IsFalse(construiuLazy); // dispose não deve ser invocado! svcProxy.Dispose(); Assert.IsFalse(construiuLazy); Assert.IsFalse(svc.Disposed); // chama um método para criar o lazy svcProxy.Do(); Assert.IsTrue(construiuLazy); Assert.IsFalse(svc.Disposed); // chama dispose svcProxy.Dispose(); Assert.IsTrue(svc.Disposed); }
public void GenericInterfaceImplementation() { var proxy = LazyProxyGenerator.CreateLazyProxyFor <IGenericInterface <int> >(() => new GenericInterface <int>(() => 10)); var atual = proxy.Cache; Assert.AreEqual(default, atual);
public void GitHubIssue1Test() { var proxy = LazyProxyGenerator.CreateLazyProxyFor <IUsuarioRepository>(() => new UsuarioRepository()); var usr = proxy.GetById(Guid.NewGuid()); Assert.IsNotNull(usr); }
public void PerformanceTest() { for (var i = 1; i <= 1_000_000; i++) { LazyProxyGenerator.CreateLazyProxyFor <IService>(() => new Service()); } Assert.IsTrue(true); }
public void ThrowCorrectNullReferenceException() { var svc = LazyProxyGenerator.CreateLazyProxyFor <IService>(() => null); svc.Do(); }
public void GeneratedDynamicProxyTest() { var proxy = LazyProxyGenerator.CreateLazyProxyFor <IService>(() => new Service(0)); try { proxy.Increments(); // void method; var valor = proxy.GetValue(); Assert.AreEqual(1, valor); // Readonly Property Assert.AreEqual("ReadonlyProperty", proxy.ReadonlyProperty); // Normal property proxy.Key = "Olá"; Assert.AreEqual("Olá", proxy.Key); var result = proxy.IncrementsWith(2, 3, 6, 12); Assert.AreEqual(24, result); // out parameter proxy.GetVAlue(out valor); Assert.AreEqual(valor, 24); // varios out's e com valor de retorno; var boolResult = proxy.GetOneTwoThree(out var one, out var two, out var three); Assert.IsTrue(boolResult); Assert.IsNotNull(one); Assert.IsNotNull(two); Assert.IsNotNull(three); Assert.AreEqual(1, one.GetValue()); Assert.AreEqual(2, two.GetValue()); Assert.AreEqual(3, three.GetValue()); // reference parameter IService metade = null; new Service(0); proxy.Half(ref metade); Assert.AreEqual(12, metade.GetValue()); // varios ref's e com valor de retorno. var zero = new Service(0); IService four = zero, five = zero, siz = zero; boolResult = proxy.GetQuatroCincoSeis(ref four, ref five, ref siz); Assert.IsTrue(boolResult); Assert.IsFalse(ReferenceEquals(four, zero)); Assert.AreEqual(4, four.GetValue()); Assert.IsFalse(ReferenceEquals(five, zero)); Assert.AreEqual(5, five.GetValue()); Assert.IsFalse(ReferenceEquals(siz, zero)); Assert.AreEqual(6, siz.GetValue()); // params result = proxy.IncrementParams(1, 2, 3); Assert.AreEqual(30, result); proxy.WriteonlyProperty = 10; Assert.AreEqual(10, proxy.GetValue()); var objResult = proxy.GetIncrementedObj(); Assert.AreNotEqual(objResult, proxy); Assert.AreEqual(11, objResult.GetValue()); } catch (Exception ex) { var y = ex; throw; } }