Ejemplo n.º 1
0
        public void TestYMapEventExceptionsShouldCompleteTransaction()
        {
            var doc = new YDoc();
            var map = doc.GetMap("map");

            bool updateCalled               = false;
            bool throwingObserverCalled     = false;
            bool throwingDeepObserverCalled = false;

            doc.UpdateV2 += (s, e) =>
            {
                updateCalled = true;
            };

            void throwingObserver(object sender, YEventArgs args)
            {
                throwingObserverCalled = true;
                throw new Exception("Failure");
            }

            void throwingDeepObserver(object sender, YDeepEventArgs args)
            {
                throwingDeepObserverCalled = true;
                throw new Exception("Deep failure");
            }

            map.EventHandler     += throwingObserver;
            map.DeepEventHandler += throwingDeepObserver;

            Assert.ThrowsException <Exception>(() =>
            {
                map.Set("y", "2");
            });

            Assert.IsTrue(updateCalled);
            Assert.IsTrue(throwingObserverCalled);
            Assert.IsTrue(throwingDeepObserverCalled);

            // Check if it works again.
            updateCalled               = false;
            throwingObserverCalled     = false;
            throwingDeepObserverCalled = false;

            Assert.ThrowsException <Exception>(() =>
            {
                map.Set("z", "3");
            });

            Assert.IsTrue(updateCalled);
            Assert.IsTrue(throwingObserverCalled);
            Assert.IsTrue(throwingDeepObserverCalled);

            Assert.AreEqual("3", map.Get("z"));
        }
Ejemplo n.º 2
0
        public void TestRestoreLeftItem()
        {
            var doc = new YDoc(new YDocOptions {
                Gc = false
            });

            doc.GetArray("array").Insert(0, new[] { "item1" });
            doc.GetMap("map").Set("test", 1);
            doc.GetArray("array").Insert(0, new[] { "item0" });

            var snap = doc.CreateSnapshot();

            doc.GetArray("array").Delete(1);
            var docRestored = snap.RestoreDocument(doc);

            CollectionAssert.AreEqual(new[] { "item0", "item1" }, (ICollection)docRestored.GetArray("array").ToArray());
            CollectionAssert.AreEqual(new[] { "item0" }, (ICollection)doc.GetArray("array").ToArray());
        }
Ejemplo n.º 3
0
        public void TestSubdoc()
        {
            var doc = new YDoc();

            doc.Load();

            {
                List <List <string> > events = null;
                doc.SubdocsChanged += (s, e) =>
                {
                    events = new List <List <string> >();
                    events.Add(new List <string>(e.Added.Select(d => d.Guid)));
                    events.Add(new List <string>(e.Removed.Select(d => d.Guid)));
                    events.Add(new List <string>(e.Loaded.Select(d => d.Guid)));
                };

                var subdocs = doc.GetMap("mysubdocs");
                var docA    = new YDoc(new YDocOptions {
                    Guid = "a"
                });
                docA.Load();
                subdocs.Set("a", docA);
                CollectionAssert.AreEqual(new[] { "a" }, events[0]);
                CollectionAssert.AreEqual(new object[] { }, events[1]);
                CollectionAssert.AreEqual(new[] { "a" }, events[2]);
                events = null;

                (subdocs.Get("a") as YDoc).Load();
                Assert.IsNull(events);
                events = null;

                (subdocs.Get("a") as YDoc).Destroy();
                CollectionAssert.AreEqual(new[] { "a" }, events[0]);
                CollectionAssert.AreEqual(new[] { "a" }, events[1]);
                CollectionAssert.AreEqual(new object[] { }, events[2]);
                events = null;

                (subdocs.Get("a") as YDoc).Load();
                CollectionAssert.AreEqual(new object[] { }, events[0]);
                CollectionAssert.AreEqual(new object[] { }, events[1]);
                CollectionAssert.AreEqual(new[] { "a" }, events[2]);
                events = null;

                subdocs.Set("b", new YDoc(new YDocOptions {
                    Guid = "a"
                }));
                CollectionAssert.AreEqual(new[] { "a" }, events[0]);
                CollectionAssert.AreEqual(new object[] { }, events[1]);
                CollectionAssert.AreEqual(new object[] { }, events[2]);
                events = null;

                (subdocs.Get("b") as YDoc).Load();
                CollectionAssert.AreEqual(new object[] { }, events[0]);
                CollectionAssert.AreEqual(new object[] { }, events[1]);
                CollectionAssert.AreEqual(new[] { "a" }, events[2]);
                events = null;

                var docC = new YDoc(new YDocOptions {
                    Guid = "c"
                });
                docC.Load();
                subdocs.Set("c", docC);
                CollectionAssert.AreEqual(new[] { "c" }, events[0]);
                CollectionAssert.AreEqual(new object[] { }, events[1]);
                CollectionAssert.AreEqual(new[] { "c" }, events[2]);
                events = null;

                var guids = doc.GetSubdocGuids().ToList();
                guids.Sort();
                CollectionAssert.AreEqual(new[] { "a", "c" }, guids);
            }

            var doc2 = new YDoc();

            {
                Assert.AreEqual(0, doc2.GetSubdocGuids().Count());

                List <List <string> > events = null;
                doc2.SubdocsChanged += (s, e) =>
                {
                    events = new List <List <string> >();
                    events.Add(new List <string>(e.Added.Select(d => d.Guid)));
                    events.Add(new List <string>(e.Removed.Select(d => d.Guid)));
                    events.Add(new List <string>(e.Loaded.Select(d => d.Guid)));
                };

                doc2.ApplyUpdateV2(doc.EncodeStateAsUpdateV2());
                CollectionAssert.AreEqual(new[] { "a", "a", "c" }, events[0]);
                CollectionAssert.AreEqual(new object[] { }, events[1]);
                CollectionAssert.AreEqual(new object[] { }, events[2]);
                events = null;

                (doc2.GetMap("mysubdocs").Get("a") as YDoc).Load();
                CollectionAssert.AreEqual(new object[] { }, events[0]);
                CollectionAssert.AreEqual(new object[] { }, events[1]);
                CollectionAssert.AreEqual(new[] { "a" }, events[2]);
                events = null;

                var guids = doc2.GetSubdocGuids().ToList();
                guids.Sort();
                CollectionAssert.AreEqual(new[] { "a", "c" }, guids);

                doc2.GetMap("mysubdocs").Delete("a");
                CollectionAssert.AreEqual(new object[] { }, events[0]);
                CollectionAssert.AreEqual(new[] { "a" }, events[1]);
                CollectionAssert.AreEqual(new object[] { }, events[2]);
                events = null;

                guids = doc2.GetSubdocGuids().ToList();
                guids.Sort();
                CollectionAssert.AreEqual(new[] { "a", "c" }, guids);
            }
        }