예제 #1
0
        public void SynchronizingString_ThatIsNotReferenceEqual_Test()
        {
            var sourceGameWorld = new NotifyPropertyChangedTestGameWorld {
                RandomIntProperty = 5
            };

            // Concatenating 1 to make sure the string is a new object
            sourceGameWorld.Players.Add("player1", new NotifyPropertyChangedTestPlayer {
                Name = "sameString" + 1, Health = 100, Level = 30
            });

            var SourceSynchronizerRoot = new SourceSynchronizerRoot(sourceGameWorld);

            var TargetSynchronizerRoot = new TargetSynchronizerRoot <NotifyPropertyChangedTestGameWorld>(SourceSynchronizerRoot.WriteFullAndDispose());

            sourceGameWorld.Players.Add("player2", new NotifyPropertyChangedTestPlayer {
                Name = "sameString" + 1, Health = 44, Level = 1337
            });
            SynchronizationPacket writeChangesAndDispose = SourceSynchronizerRoot.WriteChangesAndDispose();

            TargetSynchronizerRoot.Read(writeChangesAndDispose.SetTick(TimeSpan.Zero));

            NotifyPropertyChangedTestGameWorld targetGameWorld = TargetSynchronizerRoot.Reference;

            AssertExtension.AssertCloneEqual(sourceGameWorld, targetGameWorld);
        }
        public void AddingItems_AfterClear_ShouldSynchronizeItems()
        {
            var sourceGameWorld = new NotifyPropertyChangedTestGameWorld {
                RandomIntProperty = 5
            };

            var sourceSynchronizerRoot = new SourceSynchronizerRoot(sourceGameWorld);

            sourceGameWorld.Players.Clear();

            var targetSynchronizerRoot = new TargetSynchronizerRoot <NotifyPropertyChangedTestGameWorld>(sourceSynchronizerRoot.WriteFullAndDispose());

            sourceGameWorld.Players.Add("player1", new NotifyPropertyChangedTestPlayer {
                Name = "player1", Health = 100, Level = 30
            });
            sourceGameWorld.Players.Add("player2", new NotifyPropertyChangedTestPlayer {
                Name = "player2", Health = 44, Level = 1337
            });

            targetSynchronizerRoot.Read(sourceSynchronizerRoot.WriteChangesAndDispose().SetTick(TimeSpan.FromMilliseconds(10)));

            NotifyPropertyChangedTestGameWorld targetGameWorld = targetSynchronizerRoot.Reference;

            AssertExtension.AssertCloneEqual(sourceGameWorld, targetGameWorld);
        }
        public void AddingItems_AfterClear_ShouldSynchronizeItems()
        {
            var hashSetTestObject = new NotifyPropertyChangedHashSetTestObject {
                RandomIntProperty = 5
            };

            var SourceSynchronizerRoot = new SourceSynchronizerRoot(hashSetTestObject);

            hashSetTestObject.Players.Clear();

            var TargetSynchronizerRoot = new TargetSynchronizerRoot <NotifyPropertyChangedHashSetTestObject>(SourceSynchronizerRoot.WriteFullAndDispose());

            hashSetTestObject.Players.Add(new NotifyPropertyChangedTestPlayer {
                Name = "player1", Health = 100, Level = 30
            });
            hashSetTestObject.Players.Add(new NotifyPropertyChangedTestPlayer {
                Name = "player2", Health = 44, Level = 1337
            });

            TargetSynchronizerRoot.Read(SourceSynchronizerRoot.WriteChangesAndDispose().SetTick(TimeSpan.FromMilliseconds(10)));

            NotifyPropertyChangedHashSetTestObject targetGameWorld = TargetSynchronizerRoot.Reference;

            AssertExtension.AssertCloneEqual(hashSetTestObject, targetGameWorld);
        }
예제 #4
0
        private static void RunMonoSyncTest(int entityCount)
        {
            Console.WriteLine();
            Console.WriteLine($"Benchmark with {entityCount} entities");

            // Initialization
            Console.Write("Initializing: ");
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            var world          = new MonoSyncWorld();
            var syncSourceRoot = new SourceSynchronizerRoot(world);

            using (world.Entities.BeginMassUpdate())
            {
                for (int i = 0; i < entityCount; i++)
                {
                    world.Entities.Add(i, new Entity());
                }
            }
            stopwatch.Stop();
            Console.WriteLine(stopwatch.ElapsedMilliseconds + "MS");
            stopwatch.Reset();

            // Full write
            Console.Write("Full write: ");
            stopwatch.Start();


            WriteSession fullWriteSession = syncSourceRoot.BeginWrite();
            var          syncTargetRoot   = new TargetSynchronizerRoot <MonoSyncWorld>(fullWriteSession.WriteFull());

            fullWriteSession.Dispose();

            stopwatch.Stop();
            Console.WriteLine(stopwatch.ElapsedMilliseconds + "MS");
            stopwatch.Reset();

            int changes = entityCount / 10;

            Console.Write($"{changes} changes write: ");
            stopwatch.Start();

            for (int i = 0; i < changes; i++)
            {
                world.Entities[i].XPos = 2;
            }
            using (WriteSession writeSession = syncSourceRoot.BeginWrite())
            {
                var data = writeSession.WriteChanges().SetTick(TimeSpan.Zero);
                syncTargetRoot.Read(data);
            }
            stopwatch.Stop();
            Console.WriteLine(stopwatch.ElapsedMilliseconds + "MS");
        }
        public void Synchronizing_MarkedParameterizedConstructor_InvokesConstructorWithParameters()
        {
            var sourceConstructorMock = new NotifyPropertyChangedSynchronizeConstructorMock();

            var SourceSynchronizerRoot = new SourceSynchronizerRoot(sourceConstructorMock);

            var TargetSynchronizerRoot =
                new TargetSynchronizerRoot <NotifyPropertyChangedSynchronizeConstructorMock>(SourceSynchronizerRoot.WriteFullAndDispose());

            TargetSynchronizerRoot.Read(SourceSynchronizerRoot.WriteChangesAndDispose().SetTick(TimeSpan.Zero));

            NotifyPropertyChangedSynchronizeConstructorMock targetConstructorMock = TargetSynchronizerRoot.Reference;

            Assert.True(targetConstructorMock.SyncConstructorCalled);
        }
        public void Changing_ConstructorProperty_Synchronizes()
        {
            var getterOnlyConstructorMockSource = new ConstructedPropertyChangeSynchronizationMock(5f);

            var SourceSynchronizerRoot = new SourceSynchronizerRoot(getterOnlyConstructorMockSource);
            var TargetSynchronizerRoot = new TargetSynchronizerRoot <ConstructedPropertyChangeSynchronizationMock>(SourceSynchronizerRoot.WriteFullAndDispose());

            ConstructedPropertyChangeSynchronizationMock getterOnlyConstructorMockRoot = TargetSynchronizerRoot.Reference;

            Assert.Equal(getterOnlyConstructorMockSource.Accessor, getterOnlyConstructorMockRoot.Accessor);

            TargetSynchronizerRoot.Read(SourceSynchronizerRoot.WriteChangesAndDispose().SetTick(TimeSpan.Zero));

            Assert.Equal(getterOnlyConstructorMockSource.Accessor, getterOnlyConstructorMockRoot.Accessor);
        }
예제 #7
0
        public void Synchronizing_ValueWithLowerTickThanDirtyTick_ShouldNotBeSet()
        {
            var sourceObject = new NotifyPropertyChangedLatestTickMock {
                Value = 5
            };
            var SourceSynchronizerRoot = new SourceSynchronizerRoot(sourceObject);

            var TargetSynchronizerRoot = new TargetSynchronizerRoot <NotifyPropertyChangedLatestTickMock>(SourceSynchronizerRoot.WriteFullAndDispose());
            var targetObject           = TargetSynchronizerRoot.Reference;

            Assert.Equal(5, targetObject.Value);

            TargetSynchronizerRoot.Clock.OwnTick = TimeSpan.FromMilliseconds(10);
            targetObject.Value = 7;
            sourceObject.Value = 6;

            TargetSynchronizerRoot.Read(SourceSynchronizerRoot.WriteChangesAndDispose().SetTick(TimeSpan.FromMilliseconds(9)));

            Assert.Equal(7, targetObject.Value);

            TargetSynchronizerRoot.Read(SourceSynchronizerRoot.WriteChangesAndDispose().SetTick(TimeSpan.FromMilliseconds(11)));

            Assert.Equal(6, targetObject.Value);
        }
        public void Synchronizing_RollsBackTargetChangesPriorToSourceTick()
        {
            var sourceDictionary = new ObservableDictionary <int, string>();

            var sourceSynchronizerRoot = new SourceSynchronizerRoot(sourceDictionary);

            var targetSynchronizerRoot = new TargetSynchronizerRoot <ObservableDictionary <int, string> >(
                sourceSynchronizerRoot.WriteFullAndDispose());

            ObservableDictionary <int, string> targetDictionary = targetSynchronizerRoot.Reference;

            targetDictionary.Add(1, "2");

            targetSynchronizerRoot.Clock.OwnTick = TimeSpan.FromMilliseconds(5);

            //Set tick older than client tick
            byte[] changes = sourceSynchronizerRoot.WriteChangesAndDispose().SetTick(TimeSpan.FromMilliseconds(5));
            targetSynchronizerRoot.Read(changes);

            // Recently added item should be rolled back
            Assert.Empty(targetDictionary);
        }
        public void Synchronizing_RollsBackTargetChangesPriorToSourceTick()
        {
            var source = new ObservableHashSet <string>();
            var SourceSynchronizerRoot = new SourceSynchronizerRoot(source);

            byte[] writeFullAndDispose    = SourceSynchronizerRoot.WriteFullAndDispose();
            var    TargetSynchronizerRoot = new TargetSynchronizerRoot <ObservableHashSet <string> >(
                writeFullAndDispose);

            ObservableHashSet <string> target = TargetSynchronizerRoot.Reference;

            target.Add("2");

            TargetSynchronizerRoot.Clock.OwnTick = TimeSpan.FromMilliseconds(5);

            //Set tick older than client tick
            byte[] changes = SourceSynchronizerRoot.WriteChangesAndDispose().SetTick(TimeSpan.FromMilliseconds(6));
            TargetSynchronizerRoot.Read(changes);

            // Recently added item should be rolled back
            Assert.Empty(target);
        }