コード例 #1
0
ファイル: TestAsyncResult.cs プロジェクト: modulexcite/WcfEx
 public void TestResult()
 {
     AsyncResult result;
      // null result
      result = new AsyncResult(null, null);
      Assert.IsFalse(result.IsFaulted);
      result.Complete(null);
      Assert.AreEqual(result.GetResult(), null);
      Assert.AreEqual(result.GetResult<String>(), null);
      Assert.IsTrue(result.IsCompleted);
      Assert.IsFalse(result.IsFaulted);
      // non-null result
      result = new AsyncResult(null, null);
      Assert.IsFalse(result.IsFaulted);
      result.Complete("complete");
      Assert.AreEqual(result.GetResult(), "complete");
      Assert.AreEqual(result.GetResult<String>(), "complete");
      try
      {
     result.GetResult<Int32>();
     Assert.Fail("InvalidCastException expected");
      }
      catch (InvalidCastException) { }
      Assert.IsTrue(result.IsCompleted);
      Assert.IsFalse(result.IsFaulted);
      // exception result
      result = new AsyncResult(null, null);
      Assert.IsFalse(result.IsFaulted);
      result.Complete(null, new Exception("exception"));
      try
      {
     result.GetResult();
     Assert.Fail("Exception expected");
      }
      catch (Exception e)
      {
     Assert.AreEqual(e.Message, "exception");
      }
      Assert.IsTrue(result.IsCompleted);
      Assert.IsTrue(result.IsFaulted);
      // exception with typed result
      result = new AsyncResult(null, null);
      Assert.IsFalse(result.IsFaulted);
      result.Complete("complete", new Exception("exception"));
      try
      {
     result.GetResult<String>();
     Assert.Fail("Exception expected");
      }
      catch (Exception e)
      {
     Assert.AreEqual(e.Message, "exception");
      }
      Assert.IsTrue(result.IsCompleted);
      Assert.IsTrue(result.IsFaulted);
      // exception with invalid typed result
      result = new AsyncResult(null, null);
      Assert.IsFalse(result.IsFaulted);
      result.Complete("complete", new Exception("exception"));
      try
      {
     result.GetResult<Int32>();
     Assert.Fail("Exception expected");
      }
      catch (Exception e)
      {
     Assert.AreEqual(e.Message, "exception");
      }
      Assert.IsTrue(result.IsCompleted);
      Assert.IsTrue(result.IsFaulted);
 }