public void GetWhichChanged_NeitherHasEvent_Neither()
        {
            Stamp first  = new Stamp();
            Stamp second = first.Fork();

            Assert.AreEqual(ChangeOccurredIn.Neither, ChangeEvaluator.GetWhichChanged(first, second));
        }
        public void Fork_NewStamp_NotIsLeaf()
        {
            var target   = new Stamp();
            var newStamp = target.Fork();

            Assert.IsFalse(newStamp.Id.IsLeaf);
        }
        public void GetConflictingItems_ChangesHaveNoConflicts_NoConflictsInSyncResult()
        {
            // Create non conflicting items
            Guid id          = Guid.NewGuid();
            var  serverStamp = new Stamp();
            var  clientStamp = serverStamp.Fork();
            var  serverItem  = new TestItem {
                Id = id, Stamp = serverStamp
            };
            var clientItem = new TestItem {
                Id = id, Stamp = clientStamp
            };

            // Arrange server repository to return server item
            var repository = Mocking.MockAndBind <IRepository <TestItem, Guid> >(_container);

            Mock.Arrange(() => repository.Find(id)).Returns(serverItem);

            // Arrange client sync request
            ISyncRequest <Guid> syncRequest = new SyncRequest <TestItem, Guid> {
                new SyncItem <Guid>(clientItem)
            };

            var target = new SyncResult <TestItem, Guid>(_container);

            target.GetConflictingItems(syncRequest);

            Assert.IsFalse(target.ConflictingItems.Contains(serverItem));
        }
        public void Fork_NewStamp_Id0Id0()
        {
            var target   = new Stamp();
            var newStamp = target.Fork();

            Assert.AreEqual(0, target.Id.Value);
            Assert.AreEqual(0, newStamp.Id.Value);
        }
        public void Fork_NewStamp_Event0OnBoth()
        {
            var target   = new Stamp();
            var newStamp = target.Fork();

            Assert.AreEqual(0, target.Event.Value);
            Assert.AreEqual(0, newStamp.Event.Value);
        }
        public void GetWhichChanged_SecondHasEvent_SecondChanged()
        {
            Stamp first  = new Stamp();
            Stamp second = first.Fork();

            second.CreateEvent();

            Assert.AreEqual(ChangeOccurredIn.SecondObject, ChangeEvaluator.GetWhichChanged(first, second));
        }
        public void GetWhichChanged_FirstHasTwoEvents_FirstChanged()
        {
            Stamp first  = new Stamp();
            Stamp second = first.Fork();

            first.CreateEvent();
            first.CreateEvent();

            Assert.AreEqual(ChangeOccurredIn.FirstObject, ChangeEvaluator.GetWhichChanged(first, second));
        }
        public void GetWhichChanged_FirstHasEventThenJoinedToSecond_Neither()
        {
            Stamp first  = new Stamp();
            Stamp second = first.Fork();

            first.CreateEvent();
            second.Join(first);

            Assert.AreEqual(ChangeOccurredIn.Neither, ChangeEvaluator.GetWhichChanged(first, second));
        }
        public void GetWhichChanged_BothHaveEvent_Both()
        {
            Stamp first  = new Stamp();
            Stamp second = first.Fork();

            first.CreateEvent();
            second.CreateEvent();

            Assert.AreEqual(ChangeOccurredIn.Both, ChangeEvaluator.GetWhichChanged(first, second));
        }
        public void Fork_EventValue1_BothWithEventValue1()
        {
            var target = new Stamp();

            target.CreateEvent();
            var newStamp = target.Fork();

            Assert.AreEqual(1, target.Event.Value);
            Assert.AreEqual(1, newStamp.Event.Value);
        }
        public void GetWhichChanged_BothChangedButJoinedBetweenEvents_Neither()
        {
            Stamp first  = new Stamp();
            Stamp second = first.Fork();

            first.CreateEvent();
            second.Join(first.Peek());
            second.CreateEvent();
            first.Join(second.Peek());

            Assert.AreEqual(ChangeOccurredIn.Neither, ChangeEvaluator.GetWhichChanged(first, second));
        }
Exemple #12
0
        public void Itc2008Example()
        {
            // Start with seed stamp
            Stamp seed = new Stamp();

            seed.Should().Be(new Stamp(1, 0));

            // Fork seed stamp to create stamps for process A and B
            (Stamp a1, Stamp b1) = seed.Fork();
            a1.Should().Be(new Stamp(new Id.Node(1, 0), 0));
            b1.Should().Be(new Stamp(new Id.Node(0, 1), 0));

            // Process A suffers an event
            Stamp a2 = a1.Event();

            a2.Should().Be(new Stamp(new Id.Node(1, 0), new Event.Node(0, 1, 0)));

            // Process A forks to create stamp for new process C (dynamic number of participants)
            (Stamp a3, Stamp c1) = a2.Fork();
            a3.Should().Be(new Stamp(new Id.Node(new Id.Node(1, 0), 0), new Event.Node(0, 1, 0)));
            c1.Should().Be(new Stamp(new Id.Node(new Id.Node(0, 1), 0), new Event.Node(0, 1, 0)));

            // Process A suffers an event
            Stamp a4 = a3.Event();

            a4.Should().Be(new Stamp(new Id.Node(new Id.Node(1, 0), 0), new Event.Node(0, new Event.Node(1, 1, 0), 0)));

            // Process B suffers an event
            Stamp b2 = b1.Event();

            b2.Should().Be(new Stamp(new Id.Node(0, 1), new Event.Node(0, 0, 1)));

            // Process B suffers an event
            Stamp b3 = b2.Event();

            b3.Should().Be(new Stamp(new Id.Node(0, 1), new Event.Node(0, 0, 2)));

            // Process B and C synchronize (join and fork)
            (Stamp c3, Stamp b4) = b3.Sync(c1);
            c3.Should().Be(new Stamp(new Id.Node(new Id.Node(0, 1), 0), new Event.Node(1, 0, 1)));
            b4.Should().Be(new Stamp(new Id.Node(0, 1), new Event.Node(1, 0, 1)));

            // Process C retires and joins with process A
            Stamp a5 = a4.Join(c3);

            a5.Should().Be(new Stamp(new Id.Node(1, 0), new Event.Node(1, new Event.Node(0, 1, 0), 1)));

            // Process A suffers an event (inflates and simplifies to single integer event)
            Stamp a6 = a5.Event();

            a6.Should().Be(new Stamp(new Id.Node(1, 0), 2));
        }
Exemple #13
0
        public void OrderingOfEventsExample()
        {
            //         ^            ^ time ↑
            //         |            |
            // evt p3  *            |
            //         |            |
            // rcv p2  *---+    +---* q2 rcv
            //         |    \  /    |
            //         |     \/     |
            //         |     /\     |
            //         |    /  \    |
            // rcv p1  *---+    +---* q1 snd
            //         |            |

            Stamp seed = new Stamp();

            (Stamp, Stamp)fork = seed.Fork();
            Process p = new Process("p", fork.Item1);
            Process q = new Process("q", fork.Item2);

            Message msgp1 = p.Send("p1");
            Stamp   p1    = p.CurrentStamp;

            Console.WriteLine($"p1:{p1} after send");

            Message msgq1 = q.Send("q1");
            Stamp   q1    = q.CurrentStamp;

            Console.WriteLine($"q1:{q1} after send");

            Stamp p2 = p.Receive(msgq1);

            Console.WriteLine($"p2:{p2} after receive");

            Stamp q2 = q.Receive(msgp1);

            Console.WriteLine($"q1:{q1} after receive");

            Stamp p3 = p.Increment();

            Console.WriteLine($"p3:{p3} after event");

            p1.Leq(p2).Should().BeTrue();
            p1.Leq(p3).Should().BeTrue();
            p2.Leq(p3).Should().BeTrue();

            q1.Leq(q2).Should().BeTrue();

            p1.Leq(q2).Should().BeTrue(); // send occurs before receive
            q1.Leq(p2).Should().BeTrue(); // send occurs before receive

            p1.Concurrent(q1).Should().BeTrue();
            p2.Concurrent(q2).Should().BeTrue();
            p3.Concurrent(q2).Should().BeTrue();

            // Stamp implements IComparable<Stamp> so a collection of stamps
            // can be partially ordered using Sort methods. Note this is a
            // partial order (not total order) as CompareTo returns 0
            // for equal and concurrent stamps.

            var arr = new[] { q2, q1, p3, p2, p1 };

            Array.Sort(arr);

            arr.Should().ContainInOrder(p1, p2, p3);
            arr.Should().ContainInOrder(q1, q2);
            arr.Should().ContainInOrder(p1, q2);
            arr.Should().ContainInOrder(q1, p2, p3);
        }