public void ThrowIfDisposedOrImmutable_Fault_Abort ()
		{
			bool opening = false;
			bool opened = false;
			bool closing = false;
			bool closed = false;
			bool faulted = false;

			CommunicationObjectPoker co = new CommunicationObjectPoker ();

			co.Opening += delegate (object sender, EventArgs e) {
				opening = true;
				Assert.AreEqual (CommunicationState.Opening, co.State, "Opening/State");
				Assert.Throws<ObjectDisposedException> (delegate {
					co._ThrowIfDisposedOrImmutable ();
				}, "ThrowIfDisposedOrImmutable/Opening");
			};
			co.Opened += delegate (object sender, EventArgs e) {
				opened = true;
				Assert.AreEqual (CommunicationState.Opened, co.State, "Opened/State");
				Assert.Throws<ObjectDisposedException> (delegate {
					co._ThrowIfDisposedOrImmutable ();
				}, "ThrowIfDisposedOrImmutable/Opened");
			};
			co.Closing += delegate (object sender, EventArgs e) {
				closing = true;
				Assert.AreEqual (CommunicationState.Closing, co.State, "Closing/State");
				Assert.Throws<CommunicationObjectAbortedException> (delegate {
					co._ThrowIfDisposedOrImmutable ();
				}, "ThrowIfDisposedOrImmutable/Closing");
			};
			co.Closed += delegate (object sender, EventArgs e) {
				closed = true;
				Assert.AreEqual (CommunicationState.Closed, co.State, "Closed/State");
				Assert.Throws<CommunicationObjectAbortedException> (delegate {
					co._ThrowIfDisposedOrImmutable ();
				}, "ThrowIfDisposedOrImmutable/Closed");
			};
			co.Faulted += delegate (object sender, EventArgs e) {
				faulted = true;
				Assert.AreEqual (CommunicationState.Faulted, co.State, "Faulted/State");
				Assert.Throws<CommunicationObjectFaultedException> (delegate {
					co._ThrowIfDisposedOrImmutable ();
				}, "ThrowIfDisposedOrImmutable/Faulted");
			};

			Assert.AreEqual (CommunicationState.Created, co.State, "Created");
			co._ThrowIfDisposedOrImmutable ();

			co._FaultNoAssert ();

			co.OnAbortState = CommunicationState.Closing;
			co.Abort ();

			// ensure all states were tested
			Assert.IsFalse (opening, "opening");
			Assert.IsFalse (opened, "opened");
			Assert.IsTrue (closing, "closing");
			Assert.IsTrue (closed, "closing");
			Assert.IsTrue (faulted, "faulted");
		}
		public void Create_Fault_Open_Abort_Close ()
		{
			int opening = 0;
			int opened = 0;
			int closing = 0;
			int closed = 0;
			int faulted = 0;

			CommunicationObjectPoker co = new CommunicationObjectPoker ();
			co.Faulted += delegate (object sender, EventArgs e) {
				faulted++;
				Assert.AreEqual (CommunicationState.Faulted, co.State, "State/Faulted");
				Assert.AreSame (co, sender, "sender");
				Assert.AreSame (EventArgs.Empty, e, "e");
			};
			co.Opening += delegate (object sender, EventArgs e) {
				opening++;
				Assert.AreEqual (CommunicationState.Opening, co.State, "Opening/State");
				Assert.AreSame (co, sender, "Opening/sender");
				Assert.AreSame (EventArgs.Empty, e, "Opening/e");
			};
			co.Opened += delegate (object sender, EventArgs e) {
				opened++;
				Assert.AreEqual (CommunicationState.Opened, co.State, "Opened/State");
				Assert.AreSame (co, sender, "Opened/sender");
				Assert.AreSame (EventArgs.Empty, e, "Opened/e");
			};
			co.Closing += delegate (object sender, EventArgs e) {
				closing++;
				Assert.AreEqual (CommunicationState.Closing, co.State, "Closing/State");
				Assert.AreSame (co, sender, "Closing/sender");
				Assert.AreSame (EventArgs.Empty, e, "Closing/e");
			};
			co.Closed += delegate (object sender, EventArgs e) {
				closed++;
				Assert.AreEqual (CommunicationState.Closed, co.State, "Closed/State");
				Assert.AreSame (co, sender, "Closed/sender");
				Assert.AreSame (EventArgs.Empty, e, "Closed/e");
			};

			Assert.AreEqual (CommunicationState.Created, co.State, "State/before");

			co._Fault ();
			Assert.AreEqual (0, opening, "opening");
			Assert.AreEqual (0, opened, "opened");
			Assert.AreEqual (0, closing, "closing");
			Assert.AreEqual (0, closed, "closed");
			Assert.AreEqual (1, faulted, "faulted");

			Assert.AreEqual (CommunicationState.Faulted, co.State, "State/after");
			Assert.IsFalse (co.Disposed, "IsDisposed");

			// 2nd fault does not throw a CommunicationObjectFaultedException
			// nor does it raise Faulted again
			co._Fault ();
			Assert.AreEqual (1, faulted, "faulted(same)");

			Assert.IsFalse (co.DefaultCloseTimeoutCalled, "DefaultCloseTimeoutCalled");
			Assert.IsFalse (co.DefaultOpenTimeoutCalled, "DefaultOpenTimeoutCalled");
			Assert.IsFalse (co.OnAbortCalled, "OnAbortCalled");

			Assert.IsFalse (co.OnBeginCloseCalled, "OnBeginCloseCalled");
			Assert.IsFalse (co.OnCloseCalled, "OnCloseCalled");
			Assert.IsFalse (co.OnEndCloseCalled, "OnEndCloseCalled");

			Assert.IsFalse (co.OnBeginOpenCalled, "OnBeginOpenCalled");
			Assert.IsFalse (co.OnOpenCalled, "OnOpenCalled");
			Assert.IsFalse (co.OnEndOpenCalled, "OnEndCloseCalled");

			Assert.Throws<CommunicationObjectFaultedException> (delegate {
				co.Open ();
			}, "Open");
			Assert.AreEqual (0, opening, "opening-b");
			Assert.AreEqual (0, opened, "opened-b");

			co.OnAbortState = CommunicationState.Closing;
			// Abort does not throw a CommunicationObjectFaultedException
			co.Abort ();
			Assert.AreEqual (1, closing, "closing-b");
			Assert.AreEqual (1, closed, "closed-b");

			co.Close ();
			Assert.AreEqual (1, closing, "closing-c");
			Assert.AreEqual (1, closed, "closed-c");
		}
		public void Create_Open_Open ()
		{
			int opening = 0;
			int opened = 0;

			CommunicationObjectPoker co = new CommunicationObjectPoker ();
			co.Opening += delegate (object sender, EventArgs e) {
				opening++;
				Assert.AreEqual (CommunicationState.Opening, co.State, "Opening/State");
				Assert.AreSame (co, sender, "Opening/sender");
				Assert.AreSame (EventArgs.Empty, e, "Opening/e");
			};
			co.Opened += delegate (object sender, EventArgs e) {
				opened++;
				Assert.AreEqual (CommunicationState.Opened, co.State, "Opened/State");
				Assert.AreSame (co, sender, "Opened/sender");
				Assert.AreSame (EventArgs.Empty, e, "Opened/e");
			};

			co.Open ();
			Assert.AreEqual (1, opening, "opening");
			Assert.AreEqual (1, opened, "opened");
			Assert.AreEqual (CommunicationState.Opened, co.State, "State/after/open");
			Assert.IsFalse (co.Disposed, "IsDisposed/open");

			Assert.Throws<InvalidOperationException> (delegate {
				co.Open ();
			}, "Open/2");
			Assert.AreEqual (1, opening, "opening-b");
			Assert.AreEqual (1, opened, "openedg-b");
			Assert.AreEqual (CommunicationState.Opened, co.State, "State/after/openg-b");
			Assert.IsFalse (co.Disposed, "IsDisposed/openg-b");
		}
		public void Create_Open_Close_Abort ()
		{
			int opening = 0;
			int opened = 0;
			int closing = 0;
			int closed = 0;

			CommunicationObjectPoker co = new CommunicationObjectPoker ();
			co.Opening += delegate (object sender, EventArgs e) {
				opening++;
				Assert.AreEqual (CommunicationState.Opening, co.State, "Opening/State");
				Assert.AreSame (co, sender, "Opening/sender");
				Assert.AreSame (EventArgs.Empty, e, "Opening/e");
			};
			co.Opened += delegate (object sender, EventArgs e) {
				opened++;
				Assert.AreEqual (CommunicationState.Opened, co.State, "Opened/State");
				Assert.AreSame (co, sender, "Opened/sender");
				Assert.AreSame (EventArgs.Empty, e, "Opened/e");
			};
			co.Closing += delegate (object sender, EventArgs e) {
				closing++;
				Assert.AreEqual (CommunicationState.Closing, co.State, "Closing/State");
				Assert.AreSame (co, sender, "Closing/sender");
				Assert.AreSame (EventArgs.Empty, e, "Closing/e");
			};
			co.Closed += delegate (object sender, EventArgs e) {
				closed++;
				Assert.AreEqual (CommunicationState.Closed, co.State, "Closed/State");
				Assert.AreSame (co, sender, "Closed/sender");
				Assert.AreSame (EventArgs.Empty, e, "Closed/e");
			};
			Assert.AreEqual (CommunicationState.Created, co.State, "State/before");

			co.Open ();
			Assert.AreEqual (1, opening, "opening");
			Assert.AreEqual (1, opened, "opened");
			Assert.AreEqual (0, closing, "closing");
			Assert.AreEqual (0, closed, "closed");

			Assert.AreEqual (CommunicationState.Opened, co.State, "State/after/open");
			Assert.IsFalse (co.Disposed, "IsDisposed/open");

			Assert.IsFalse (co.DefaultCloseTimeoutCalled, "DefaultCloseTimeoutCalled/open");
			Assert.IsTrue (co.DefaultOpenTimeoutCalled, "DefaultOpenTimeoutCalled/open");
			Assert.IsFalse (co.OnAbortCalled, "OnAbortCalled/open");

			Assert.IsFalse (co.OnBeginCloseCalled, "OnBeginCloseCalled/open");
			Assert.IsFalse (co.OnCloseCalled, "OnCloseCalled/open");
			Assert.IsFalse (co.OnEndCloseCalled, "OnEndCloseCalled/open");

			Assert.IsFalse (co.OnBeginOpenCalled, "OnBeginOpenCalled/open");
			Assert.IsTrue (co.OnOpenCalled, "OnOpenCalled/open");
			Assert.IsFalse (co.OnEndOpenCalled, "OnEndCloseCalled/open");

			co.Close ();
			Assert.AreEqual (1, opening, "opening-b");
			Assert.AreEqual (1, opened, "opened-b");
			Assert.AreEqual (1, closing, "closing-b");
			Assert.AreEqual (1, closed, "closed-b");

			Assert.AreEqual (CommunicationState.Closed, co.State, "State/close");
			Assert.IsTrue (co.Disposed, "IsDisposed/close");

			Assert.IsTrue (co.DefaultCloseTimeoutCalled, "DefaultCloseTimeoutCalled/close");
			Assert.IsTrue (co.DefaultOpenTimeoutCalled, "DefaultOpenTimeoutCalled/close");
			Assert.IsFalse (co.OnAbortCalled, "OnAbortCalled/close");

			Assert.IsFalse (co.OnBeginCloseCalled, "OnBeginCloseCalled/close");
			Assert.IsTrue (co.OnCloseCalled, "OnCloseCalled/close");
			Assert.IsFalse (co.OnEndCloseCalled, "OnEndCloseCalled/close");

			Assert.IsFalse (co.OnBeginOpenCalled, "OnBeginOpenCalled/close");
			Assert.IsTrue (co.OnOpenCalled, "OnOpenCalled/close");
			Assert.IsFalse (co.OnEndOpenCalled, "OnEndCloseCalled/close");

			co.Abort ();
			Assert.AreEqual (1, opening, "opening-c");
			Assert.AreEqual (1, opened, "opened-c");
			Assert.AreEqual (1, closing, "closing-c");
			Assert.AreEqual (1, closed, "closed-c");

			Assert.IsFalse (co.OnAbortCalled, "OnAbortCalled/abort");
			Assert.AreEqual (CommunicationState.Closed, co.State, "State/abort");
		}
		public void Create_Fault_Abort ()
		{
			int opening = 0;
			int opened = 0;
			int closing = 0;
			int closed = 0;

			CommunicationObjectPoker co = new CommunicationObjectPoker ();
			co.Opening += delegate (object sender, EventArgs e) {
				opening++;
				Assert.AreEqual (CommunicationState.Opening, co.State, "Opening/State");
				Assert.AreSame (co, sender, "Opening/sender");
				Assert.AreSame (EventArgs.Empty, e, "Opening/e");
			};
			co.Opened += delegate (object sender, EventArgs e) {
				opened++;
				Assert.AreEqual (CommunicationState.Opened, co.State, "Opened/State");
				Assert.AreSame (co, sender, "Opened/sender");
				Assert.AreSame (EventArgs.Empty, e, "Opened/e");
			};
			co.Closing += delegate (object sender, EventArgs e) {
				closing++;
				Assert.AreEqual (CommunicationState.Closing, co.State, "Closing/State");
				Assert.AreSame (co, sender, "Closing/sender");
				Assert.AreSame (EventArgs.Empty, e, "Closing/e");

				Assert.IsFalse (co.Disposed, "Closing/Disposed");
				// note: IsDisposed is false but we still throw! 
				// but this match MSDN docs about ThrowIfDisposed
				Assert.Throws<CommunicationObjectAbortedException> (delegate {
					co._ThrowIfDisposed ();
				}, "Closing/ThrowIfDisposed");
				Assert.Throws<CommunicationObjectAbortedException> (delegate {
					co._ThrowIfDisposedOrImmutable ();
				}, "Closing/ThrowIfDisposedOrImmutable");
				Assert.Throws<CommunicationObjectAbortedException> (delegate {
					co._ThrowIfDisposedOrNotOpen ();
				}, "Closing/ThrowIfDisposedOrNotOpen");
			};
			co.Closed += delegate (object sender, EventArgs e) {
				closed++;
				Assert.AreEqual (CommunicationState.Closed, co.State, "Closed/State");
				Assert.AreSame (co, sender, "Closed/sender");
				Assert.AreSame (EventArgs.Empty, e, "Closed/e");

				Assert.IsTrue (co.Disposed, "Closed/Disposed");
				Assert.Throws<CommunicationObjectAbortedException> (delegate {
					co._ThrowIfDisposed ();
				}, "Closed/ThrowIfDisposed");
				Assert.Throws<CommunicationObjectAbortedException> (delegate {
					co._ThrowIfDisposedOrImmutable ();
				}, "Closed/ThrowIfDisposedOrImmutable");
				Assert.Throws<CommunicationObjectAbortedException> (delegate {
					co._ThrowIfDisposedOrNotOpen ();
				}, "Closed/ThrowIfDisposedOrNotOpen");
			};
			Assert.AreEqual (CommunicationState.Created, co.State, "State/before");

			co.OnAbortState = CommunicationState.Closing;
			co._Fault ();
			Assert.AreEqual (0, opening, "opening");
			Assert.AreEqual (0, opened, "opened");
			Assert.AreEqual (0, closing, "closing");
			Assert.AreEqual (0, closed, "closed");

			Assert.AreEqual (CommunicationState.Faulted, co.State, "State/after");
			Assert.IsFalse (co.Disposed, "IsDisposed");

			Assert.IsFalse (co.DefaultCloseTimeoutCalled, "DefaultCloseTimeoutCalled");
			Assert.IsFalse (co.DefaultOpenTimeoutCalled, "DefaultOpenTimeoutCalled");
			Assert.IsFalse (co.OnAbortCalled, "OnAbortCalled");

			Assert.IsFalse (co.OnBeginCloseCalled, "OnBeginCloseCalled");
			Assert.IsFalse (co.OnCloseCalled, "OnCloseCalled");
			Assert.IsFalse (co.OnEndCloseCalled, "OnEndCloseCalled");

			Assert.IsFalse (co.OnBeginOpenCalled, "OnBeginOpenCalled");
			Assert.IsFalse (co.OnOpenCalled, "OnOpenCalled");
			Assert.IsFalse (co.OnEndOpenCalled, "OnEndCloseCalled");

			co.Abort ();
			Assert.AreEqual (0, opening, "opening-b");
			Assert.AreEqual (0, opened, "opened-b");
			Assert.AreEqual (1, closing, "closing-c");
			Assert.AreEqual (1, closed, "closed-c");
		}
		public void Create_Close_Fault ()
		{
			bool faulted = false;

			CommunicationObjectPoker co = new CommunicationObjectPoker ();
			co.Faulted += delegate (object sender, EventArgs e) {
				faulted = true; // won't be hit
			};
			co.Open ();
			co.Close (); // real Close, not an implicit Abort since Open was called
			co._FaultNoAssert (); // don't check State since it won't be Faulted
			Assert.AreEqual (CommunicationState.Closed, co.State, "State/Fault");
			Assert.IsFalse (faulted, "Faulted");
		}
		public void Create_Abort ()
		{
			int closing = 0;
			int closed = 0;

			CommunicationObjectPoker co = new CommunicationObjectPoker ();
			co.Closing += delegate (object sender, EventArgs e) {
				closing++;
				Assert.AreEqual (CommunicationState.Closing, co.State, "Closing/State");
				Assert.AreSame (co, sender, "Closing/sender");
				Assert.AreSame (EventArgs.Empty, e, "Closing/e");

				Assert.IsFalse (co.Disposed, "Closing/Disposed");
				Assert.Throws<CommunicationObjectAbortedException> (delegate {
					co._ThrowIfDisposed ();
				}, "Closing/ThrowIfDisposed");
				Assert.Throws<CommunicationObjectAbortedException> (delegate {
					co._ThrowIfDisposedOrImmutable ();
				}, "Closing/ThrowIfDisposedOrImmutable");
				Assert.Throws<CommunicationObjectAbortedException> (delegate {
					co._ThrowIfDisposedOrNotOpen ();
				}, "Closing/ThrowIfDisposedOrNotOpen");
			};
			co.Closed += delegate (object sender, EventArgs e) {
				closed++;
				Assert.AreEqual (CommunicationState.Closed, co.State, "Closed/State");
				Assert.AreSame (co, sender, "Closed/sender");
				Assert.AreSame (EventArgs.Empty, e, "Closed/e");

				Assert.IsTrue (co.Disposed, "Closed/Disposed");
				Assert.Throws<CommunicationObjectAbortedException> (delegate {
					co._ThrowIfDisposed ();
				}, "Closed/ThrowIfDisposed");
				Assert.Throws<CommunicationObjectAbortedException> (delegate {
					co._ThrowIfDisposedOrImmutable ();
				}, "Closed/ThrowIfDisposedOrImmutable");
				Assert.Throws<CommunicationObjectAbortedException> (delegate {
					co._ThrowIfDisposedOrNotOpen ();
				}, "Closed/ThrowIfDisposedOrNotOpen");
			};
			Assert.AreEqual (CommunicationState.Created, co.State, "State/before");

			co.OnAbortState = CommunicationState.Closing;
			co.Abort ();
			Assert.AreEqual (1, closing, "closing");
			Assert.AreEqual (1, closed, "closed");

			Assert.AreEqual (CommunicationState.Closed, co.State, "State/after");
			Assert.IsTrue (co.Disposed, "IsDisposed");

			Assert.IsFalse (co.DefaultCloseTimeoutCalled, "DefaultCloseTimeoutCalled");
			Assert.IsFalse (co.DefaultOpenTimeoutCalled, "DefaultOpenTimeoutCalled");
			Assert.IsTrue (co.OnAbortCalled, "OnAbortCalled");

			Assert.IsFalse (co.OnBeginCloseCalled, "OnBeginCloseCalled");
			Assert.IsFalse (co.OnCloseCalled, "OnCloseCalled");
			Assert.IsFalse (co.OnEndCloseCalled, "OnEndCloseCalled");

			Assert.IsFalse (co.OnBeginOpenCalled, "OnBeginOpenCalled");
			Assert.IsFalse (co.OnOpenCalled, "OnOpenCalled");
			Assert.IsFalse (co.OnEndOpenCalled, "OnEndCloseCalled");
		}
		public void Constructor ()
		{
			CommunicationObjectPoker co = new CommunicationObjectPoker ();
			Assert.AreEqual (typeof (object), co._ThisLock.GetType (), "ThisLock/default");

			co = new CommunicationObjectPoker (null);
			Assert.IsNull (co._ThisLock, "ThisLock/null");

			co = new CommunicationObjectPoker (String.Empty);
			Assert.AreSame (String.Empty, co._ThisLock, "ThisLock/weak");
		}
		public void AbortWhileClosing ()
		{
			int closing = 0;
			int closed = 0;
			CommunicationObjectPoker co = new CommunicationObjectPoker ();
			co.Closing += delegate (object sender, EventArgs e) {
				closing++;
				co.OnAbortState = CommunicationState.Closing;
				// ensure we're not repeating OnClosing
				co.Abort ();
			};
			co.Closed += delegate (object sender, EventArgs e) {
				closed++;
				co.Abort ();
			};
			co.Close ();

			Assert.AreEqual (1, closing, "closing");
			Assert.AreEqual (1, closed, "closed");
		}
		public void AbortWhileAborting ()
		{
			int closing = 0;
			int closed = 0;
			CommunicationObjectPoker co = new CommunicationObjectPoker ();
			co.Closing += delegate (object sender, EventArgs e) {
				closing++;
				co.OnAbortState = CommunicationState.Closing;
				co.Abort ();
			};
			co.Closed += delegate (object sender, EventArgs e) {
				closed++;
				co.Abort ();
			};
			co.OnAbortState = CommunicationState.Created;
			// Abort will call Closing, which can call Abort...
			co.Abort ();

			Assert.AreEqual (1, closing, "closing");
			Assert.AreEqual (1, closed, "closed");
		}