My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
Changes to /src/MajorityVoting.Tests/WhenMajorityTest.cs
000000000000 vs. 59de0a154f85 Compare: vs.  Format:
Revision 59de0a154f85
Go to: 
Project members, sign in to write a code review
/src/MajorityVoting.Tests/WhenMajorityTest.cs /src/MajorityVoting.Tests/WhenMajorityTest.cs   59de0a154f85
  1 // Copyright 2011 Jon Skeet
  2 //
  3 // Licensed under the Apache License, Version 2.0 (the "License");
  4 // you may not use this file except in compliance with the License.
  5 // You may obtain a copy of the License at
  6 //
  7 // http://www.apache.org/licenses/LICENSE-2.0
  8 //
  9 // Unless required by applicable law or agreed to in writing, software
  10 // distributed under the License is distributed on an "AS IS" BASIS,
  11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12 // See the License for the specific language governing permissions and
  13 // limitations under the License.
  14
  15 using System;
  16 using System.Threading;
  17 using System.Threading.Tasks;
  18 using NUnit.Framework;
  19
  20 namespace Eduasync
  21 {
  22 [TestFixture]
  23 public class WhenMajorityTest
  24 {
  25 [Test]
  26 public void SimpleSuccess()
  27 {
  28 var timeMachine = new TimeMachine();
  29 // All three tasks return the same value.
  30 var task1 = timeMachine.AddSuccessTask(1, 10);
  31 var task2 = timeMachine.AddSuccessTask(2, 10);
  32 var task3 = timeMachine.AddSuccessTask(3, 10);
  33
  34 var resultTask = MoreTaskEx.WhenMajority(task1, task2, task3);
  35 Assert.IsFalse(resultTask.IsCompleted);
  36
  37 // Only one result so far - no consensus
  38 timeMachine.AdvanceTo(1);
  39 Assert.IsFalse(resultTask.IsCompleted);
  40
  41 // Second result gives a majority
  42 timeMachine.AdvanceTo(2);
  43 Assert.AreEqual(TaskStatus.RanToCompletion, resultTask.Status);
  44 Assert.AreEqual(10, resultTask.Result);
  45 }
  46
  47 [Test]
  48 public void MajorityWithSomeDisagreement()
  49 {
  50 var timeMachine = new TimeMachine();
  51 // Second task gives a different result
  52 var task1 = timeMachine.AddSuccessTask(1, 10);
  53 var task2 = timeMachine.AddSuccessTask(2, 20);
  54 var task3 = timeMachine.AddSuccessTask(3, 10);
  55
  56 var resultTask = MoreTaskEx.WhenMajority(task1, task2, task3);
  57 Assert.IsFalse(resultTask.IsCompleted);
  58
  59 // Only one result so far - no consensus
  60 timeMachine.AdvanceTo(1);
  61 Assert.IsFalse(resultTask.IsCompleted);
  62
  63 // Two results so far disagree
  64 timeMachine.AdvanceTo(2);
  65 Assert.IsFalse(resultTask.IsCompleted);
  66
  67 // Third result gives majority verdict
  68 timeMachine.AdvanceTo(3);
  69 Assert.AreEqual(TaskStatus.RanToCompletion, resultTask.Status);
  70 Assert.AreEqual(10, resultTask.Result);
  71 }
  72
  73 [Test]
  74 public void MajorityWithFailureTask()
  75 {
  76 var timeMachine = new TimeMachine();
  77 // Second task gives a different result
  78 var task1 = timeMachine.AddSuccessTask(1, 10);
  79 var task2 = timeMachine.AddFaultingTask<int>(2, new Exception("Bang!"));
  80 var task3 = timeMachine.AddSuccessTask(3, 10);
  81
  82 var resultTask = MoreTaskEx.WhenMajority(task1, task2, task3);
  83 Assert.IsFalse(resultTask.IsCompleted);
  84
  85 // Only one result so far - no consensus
  86 timeMachine.AdvanceTo(1);
  87 Assert.IsFalse(resultTask.IsCompleted);
  88
  89 // Second result is a failure
  90 timeMachine.AdvanceTo(2);
  91 Assert.IsFalse(resultTask.IsCompleted);
  92
  93 // Third result gives majority verdict
  94 timeMachine.AdvanceTo(3);
  95 Assert.AreEqual(TaskStatus.RanToCompletion, resultTask.Status);
  96 Assert.AreEqual(10, resultTask.Result);
  97 }
  98
  99 [Test]
  100 public void EarlyFailure()
  101 {
  102 var timeMachine = new TimeMachine();
  103 // Second task gives a different result
  104 var task1 = timeMachine.AddCancelTask<int>(1);
  105 var task2 = timeMachine.AddFaultingTask<int>(2, new Exception("Bang 2!"));
  106 var task3 = timeMachine.AddSuccessTask(3, 10);
  107 var resultTask = MoreTaskEx.WhenMajority(task1, task2, task3);
  108 Assert.IsFalse(resultTask.IsCompleted);
  109
  110 // First result is a cancellation
  111 timeMachine.AdvanceTo(1);
  112 Assert.IsFalse(resultTask.IsCompleted);
  113
  114 // Second result is a failure. We now can't have a successful majority
  115 timeMachine.AdvanceTo(2);
  116 Assert.AreEqual(TaskStatus.Faulted, resultTask.Status);
  117 }
  118
  119 [Test]
  120 public void NoMajority()
  121 {
  122 var timeMachine = new TimeMachine();
  123 // Second task gives a different result
  124 var task1 = timeMachine.AddSuccessTask(1, 10);
  125 var task2 = timeMachine.AddSuccessTask(2, 20);
  126 var task3 = timeMachine.AddSuccessTask(3, 30);
  127
  128 var resultTask = MoreTaskEx.WhenMajority(task1, task2, task3);
  129 Assert.IsFalse(resultTask.IsCompleted);
  130
  131 // Only one result so far - no consensus
  132 timeMachine.AdvanceTo(1);
  133 Assert.IsFalse(resultTask.IsCompleted);
  134
  135 // Two results so far disagree
  136 timeMachine.AdvanceTo(2);
  137 Assert.IsFalse(resultTask.IsCompleted);
  138
  139 // Third result is different again
  140 timeMachine.AdvanceTo(3);
  141 Assert.AreEqual(TaskStatus.Faulted, resultTask.Status);
  142 }
  143 }
  144 }
Powered by Google Project Hosting