public MyForm() { InitializeComponent(); this.myDisposable_ = new MyDisposable("Goodbye, World"); }
private static async Task TestErrorDetectionWithMultipleThreads(Func <Task> newErrorTask) { var d = new MyDisposable(); using (d) { Task t1 = TaskV2.Run(newErrorTask); Task t2 = null, t3 = null; try { Assert.False(d.DEBUG_ThrownExceptionDetectedInCurrentContext()); t2 = TaskV2.Run(newErrorTask); Assert.False(d.DEBUG_ThrownExceptionDetectedInCurrentContext()); t3 = TaskV2.Run(newErrorTask); Assert.False(d.DEBUG_ThrownExceptionDetectedInCurrentContext()); await Task.WhenAll(t1, t2, t3); } catch (Exception) { Assert.True(d.DEBUG_ThrownExceptionDetectedInCurrentContext()); } Assert.False(d.DEBUG_ThrownExceptionDetectedInCurrentContext()); Assert.True(t1.IsCompleted); Assert.True(t2.IsCompleted); Assert.True(t3.IsCompleted); Assert.True(t1.IsFaulted); Assert.True(t2.IsFaulted); Assert.True(t3.IsFaulted); } Assert.False(d.DEBUG_ThrownExceptionDetectedInCurrentContext()); Assert.False(d.exceptionWasDetected); }
public void GetSharedInstance_ExistingWeakRefIsNull_AnotherThreadCreatesInstanceWhileOurFactoryRuns_ReturnsExistingInstanceAndDisposesNewInstance() { // Arrange WeakReference <MyDisposable> wr = null; MyDisposable instanceThatWillBeCreatedFirst = new MyDisposable(); MyDisposable instanceThatWillBeCreatedSecond = new MyDisposable(); // Act var retVal = WeakReferenceHelpers.GetSharedInstance(ref wr, () => { // mimic another thread creating the instance while our factory is being invoked WeakReferenceHelpers.GetSharedInstance(ref wr, () => instanceThatWillBeCreatedFirst); return(instanceThatWillBeCreatedSecond); }); // Assert MyDisposable target; Assert.NotNull(wr); Assert.True(wr.TryGetTarget(out target)); Assert.Same(instanceThatWillBeCreatedFirst, target); Assert.Same(instanceThatWillBeCreatedFirst, retVal); Assert.False(instanceThatWillBeCreatedFirst.HasBeenDisposed); Assert.True(instanceThatWillBeCreatedSecond.HasBeenDisposed); }
public void TestAssertWhenDisposed() { MyDisposable o = new MyDisposable(); o.Dispose(); o.TestAssert(); }
public void TestDisposedEvent() { MyDisposable o = new MyDisposable(); bool disposed = false; o.Disposed += delegate { disposed = true; }; o.Dispose(); Assert.IsTrue(disposed, "Disposed event failed."); }
public static IEnumerable<int> Test (int a) { MyDisposable d; using (d = new MyDisposable ()) { yield return a; yield return d.ID; } }
static void Test() { var disp = new MyDisposable(); using var _ = new MyDisposable(); Console.WriteLine("tako"); }
public static Task Demo() { Console.WriteLine("Disposable ========================"); // checking 'using' combination with async operations using var disposable = new MyDisposable(); return(MyMethod(disposable)); // This needs 'await' to avoid premature dispose }
public void RetrieveNonExistingSingleton() { using (var singleton = new MyDisposable()) { SingletonsContainer.Add(singleton); Assert.IsNull(SingletonsContainer.Item("bla")); } }
public void RetrieveSingleton() { using (var singleton = new MyDisposable()) { SingletonsContainer.Add(singleton); Assert.AreSame(singleton, SingletonsContainer.Item(typeof(MyDisposable).FullName)); } }
public void AddingTwiceThrowsException() { using (var singleton = new MyDisposable()) { SingletonsContainer.Add(singleton); Assert.Throws(typeof(ArgumentException), () => SingletonsContainer.Add(singleton)); } }
public MyForm() { InitializeComponent(); this.myDisposable_ = new MyDisposable("Goodbye, World"); this.components.Add(new Disposer(this.OnDispose)); }
public void GetExistingWithKey() { using (var existingSingleton = new MyDisposable()) { SingletonsContainer.Add("foo", existingSingleton); var singleton = SingletonsContainer.Get <MyDisposable>("foo"); Assert.AreSame(existingSingleton, singleton); } }
public void GetExistingWithCreateFunc() { using (var existingSingleton = new MyDisposable()) { SingletonsContainer.Add("foo", existingSingleton); var singleton = SingletonsContainer.Get("foo", () => new MyDisposable()); Assert.AreSame(existingSingleton, singleton); } }
public void RemoveNonExistingSingleton() { using (var singleton = new MyDisposable()) { SingletonsContainer.Add("foo", singleton); Assert.IsFalse(SingletonsContainer.Remove("bar")); Assert.AreSame(singleton, SingletonsContainer.Item("foo")); } }
static void Main() { Console.WriteLine("* Fun with Dispose *"); MyDisposable obj = new MyDisposable(); obj.DoSomething(); obj.Dispose(); }
public void RemoveSingleton() { using (var singleton = new MyDisposable()) { SingletonsContainer.Add("foo", singleton); Assert.IsTrue(SingletonsContainer.Remove("foo")); Assert.IsFalse(singleton.DisposeCalled); Assert.IsNull(SingletonsContainer.Item("foo")); } }
public static IEnumerable <int> Test(int a) { MyDisposable d; using (d = new MyDisposable()) { yield return(a); yield return(d.ID); } }
public void DisposeWith_NotifyDisposed() { var disposable = new MyDisposable(); var component = new MyNotifyDisposed(); disposable.DisposeWith(component); Assert.IsFalse(disposable.IsDisposed); component.Dispose(); Assert.IsTrue(disposable.IsDisposed); }
static void DeclaracionesUsing() { Console.WriteLine("[ 6 ] Declaraciones Using .........."); using (var myDisposable = new MyDisposable()) { EmpleadoPosicion empleado = new EmpleadoPosicion("A2", "Humberto Esparza Lopez", 23, 2800, "Recursos Humanos", "Loreto"); Console.WriteLine($"Rfc: {empleado.Rfc} Nombre: {empleado.Nombre} Edad: {empleado.Edad}"); } }
public void TestRemoveDisposedEvent() { MyDisposable o = new MyDisposable(); bool disposed = false; EventHandler handler = delegate { disposed = true; }; o.Disposed += handler; o.Disposed -= handler; o.Dispose(); Assert.IsFalse(disposed, "Disposed fired?"); }
public void testUsing() { AssertFalse(MyDisposable.IsCalled); using (var myDisp = new MyDisposable()) { AssertNotNull(myDisp); } AssertTrue(MyDisposable.IsCalled); }
public void Disposes_results() { var plan = new PlanBuilder(); var aDisposable = new MyDisposable(); var create = plan.Add("Create", () => aDisposable); plan.Add("Use", (MyDisposable _) => { }, create); plan.Build().Invoke(); Check.That(() => aDisposable.IsDisposed); }
public void Should_dispose_all_services_on_disposal() { // Setup var md = new MyDisposable(); _sut.Add <IMyDisposable>(md); // Execute _sut.Dispose(); // Verify Assert.AreEqual(true, md.IsDisposed); }
public static void TestSetDisposableSingletonTwice() { TypeMap.Register.Singleton(() => new MyDisposable()); MyDisposable md = New <MyDisposable>(); Assert.That(md.IsDisposed, Is.False); TypeMap.Register.Singleton(() => new MyDisposable()); Assert.That(md.IsDisposed, Is.True); }
public static void Main() { var d = new MyDisposable(); using (d) { // Here we could do stuff. } var sb = new StringBuilder(); sb.AppendLine("Disposed: " + d.Disposed); Console.WriteLine(sb.ToString()); }
public void DisposeElements_disposes_elements() { var lbs = new ListBehaviorSubject <MyDisposable>(); var d1 = new MyDisposable(); var d2 = new MyDisposable(); var d3 = new MyDisposable(); var d4 = new MyDisposable(); lbs.OnAdd(d1); lbs.OnAdd(d2); lbs.OnAdd(d3); var list = new List <MyDisposable>(); var sub = lbs.DisposeElements().Subscribe(change => change.Apply(list)); CollectionAssert.AreEqual(new[] { d1, d2, d3 }, list); Assert.AreEqual(0, d1.DisposeCount); Assert.AreEqual(0, d2.DisposeCount); Assert.AreEqual(0, d3.DisposeCount); Assert.AreEqual(0, d4.DisposeCount); lbs.OnRemove(1); CollectionAssert.AreEqual(new [] { d1, d3 }, list); Assert.AreEqual(0, d1.DisposeCount); Assert.AreEqual(1, d2.DisposeCount); Assert.AreEqual(0, d3.DisposeCount); Assert.AreEqual(0, d4.DisposeCount); lbs.OnReplace(1, d4); CollectionAssert.AreEqual(new [] { d1, d4 }, list); Assert.AreEqual(0, d1.DisposeCount); Assert.AreEqual(1, d2.DisposeCount); Assert.AreEqual(1, d3.DisposeCount); Assert.AreEqual(0, d4.DisposeCount); lbs.OnClear(); CollectionAssert.AreEqual(new MyDisposable[] { }, list); Assert.AreEqual(1, d1.DisposeCount); Assert.AreEqual(1, d2.DisposeCount); Assert.AreEqual(1, d3.DisposeCount); Assert.AreEqual(1, d4.DisposeCount); sub.Dispose(); }
public void TestDisposeOnFinalize() { MyDisposable o = new MyDisposable(); bool disposed = false; o.Disposed += delegate { disposed = true; }; o = null; GC.Collect(0, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); Assert.IsTrue(disposed, "Disposed event failed."); }
public void SingletonProperlyDisposedAutoKey() { using (var singleton = new MyDisposable()) { SingletonsContainer.Add(singleton); Assert.IsFalse(singleton.DisposeCalled); // Simulate application exit SingletonsContainer.Release(); Assert.IsTrue(singleton.DisposeCalled); } }
public void TestDisposedOnce() { MyDisposable o = new MyDisposable(); using (o) { Assert.AreEqual(0, o._disposedCount); o.Dispose(); Assert.AreEqual(1, o._disposedCount); o.Dispose(); Assert.AreEqual(1, o._disposedCount); } Assert.AreEqual(1, o._disposedCount); }
public void DisposeSingletons_ProperlyDisposed() { using (var singleton = new MyDisposable()) { SingletonsContainer.Add(singleton.GetType().FullName, singleton); Assert.IsFalse(singleton.DisposeCalled); // Simulate application exit SingletonsContainer.Release(); Assert.IsTrue(singleton.DisposeCalled); } }
public void SingletonProperlyDisposed() { using (var singleton = new MyDisposable()) { SingletonsContainer.Add(singleton.GetType().FullName, singleton); Assert.IsFalse(singleton.DisposeCalled); // Simulate application exit SingletonsContainer.Release(); Assert.IsTrue(singleton.DisposeCalled); } }
public static void MethodOne() { Console.WriteLine("Method One"); using (MyDisposable disposable = new MyDisposable()) { try { throw new Exception(); } catch (Exception ex) { Console.WriteLine("In catch"); } } }
public void GetSharedInstance_ExistingWeakRefIsNull_CreatesNew() { // Arrange WeakReference<MyDisposable> wr = null; MyDisposable newInstance = new MyDisposable(); // Act var retVal = WeakReferenceHelpers.GetSharedInstance(ref wr, () => newInstance); // Assert MyDisposable target; Assert.NotNull(wr); Assert.True(wr.TryGetTarget(out target)); Assert.Same(newInstance, target); Assert.Same(newInstance, retVal); Assert.False(newInstance.HasBeenDisposed); }
public void TestMethod1() { MyDisposable target = null; try { target = new MyDisposable(); } finally { if (target != null) { target.Dispose(); } } using (MyDisposable target2 = new MyDisposable()) { } }
public void GetSharedInstance_ExistingWeakRefIsNull_AnotherThreadCreatesInstanceWhileOurFactoryRuns_ReturnsExistingInstanceAndDisposesNewInstance() { // Arrange WeakReference<MyDisposable> wr = null; MyDisposable instanceThatWillBeCreatedFirst = new MyDisposable(); MyDisposable instanceThatWillBeCreatedSecond = new MyDisposable(); // Act var retVal = WeakReferenceHelpers.GetSharedInstance(ref wr, () => { // mimic another thread creating the instance while our factory is being invoked WeakReferenceHelpers.GetSharedInstance(ref wr, () => instanceThatWillBeCreatedFirst); return instanceThatWillBeCreatedSecond; }); // Assert MyDisposable target; Assert.NotNull(wr); Assert.True(wr.TryGetTarget(out target)); Assert.Same(instanceThatWillBeCreatedFirst, target); Assert.Same(instanceThatWillBeCreatedFirst, retVal); Assert.False(instanceThatWillBeCreatedFirst.HasBeenDisposed); Assert.True(instanceThatWillBeCreatedSecond.HasBeenDisposed); }
public void Using1() { var d = default(MyDisposable); var xs = EnumerableEx.Using(() => d = new MyDisposable(), d_ => new[] { 1 }); Assert.IsNull(d); var d1 = default(MyDisposable); xs.ForEach(_ => { d1 = d; Assert.IsNotNull(d1); Assert.IsFalse(d1.Done); }); Assert.IsTrue(d1.Done); var d2 = default(MyDisposable); xs.ForEach(_ => { d2 = d; Assert.IsNotNull(d2); Assert.IsFalse(d2.Done); }); Assert.IsTrue(d2.Done); Assert.AreNotSame(d1, d2); }
public void TestAssertBeforeDispose() { MyDisposable o = new MyDisposable(); o.TestAssert(); }
public void Using2() { var d = default(MyDisposable); var xs = EnumerableEx.Using(() => d = new MyDisposable(), d_ => EnumerableEx.Throw<int>(new MyException())); Assert.Null(d); AssertThrows<MyException>(() => xs.ForEach(_ => { })); Assert.True(d.Done); }
public void GetExistingWithKey() { using (var existingSingleton = new MyDisposable()) { SingletonsContainer.Add("foo", existingSingleton); var singleton = SingletonsContainer.Get<MyDisposable>("foo"); Assert.AreSame(existingSingleton, singleton); } }
public void Using3() { var d = default(MyDisposable); var xs = EnumerableEx.Using<int, MyDisposable>(() => d = new MyDisposable(), d_ => { throw new MyException(); }); Assert.IsNull(d); AssertThrows<MyException>(() => xs.ForEach(_ => { })); Assert.IsTrue(d.Done); }