示例#1
0
        public void RepStorage_CanWriteAndRead()
        {
            Assert.AreEqual(0, Storage.GetItems().Count());

            // create an item and export
            ContentItem root = CreateOneItem <TestItem>(0, "root", null);

            Storage.ExportItem(root);

            // check export worked
            Assert.AreEqual(1, Storage.GetItems().Count());
            var ri = Storage.GetItems().Single();

            Assert.AreEqual(root.ID, ri.ID);

            // check timestamps are accurate
            Assert.That(ri.PublishedDateEquals(root.Published.Value));

            // check import works
            var import = Storage.SyncItem(ri);

            Assert.AreEqual(0, import.Attachments.Count);
            Assert.AreEqual(0, import.Errors.Count);
            Assert.AreEqual(0, import.FailedAttachments.Count);
            Assert.AreEqual(0, import.UnresolvedLinks.Count);
            Assert.AreEqual(1, import.ReadItems.Count);
            //     Assert.That(root == import.RootItem); // use overloaded equality
            //     Assert.That(import.RootItem == import.ReadItems.Single()); // use overloaded equality

            // check delete works
            Storage.DeleteItem(ri);
            Assert.AreEqual(0, Storage.GetItems().Count());

            Storage.ExportItem(root);
            var files = Storage.GetItems();

            Assert.AreEqual(1, files.Count());

            // check delete by ID works
            Storage.DeleteItem(root.ID);
            Assert.AreEqual(0, Storage.GetItems().Count());
        }
        // sync local with remote item
        private void SyncOneItem(ReplicatedItem replicatedItem, ConcurrentContentList items)
        {
            var local = items.Get(replicatedItem.ID); // get local match for item

            if (local != null && local.Published.HasValue)
            {
                if (IsSlave)
                // import if newer than local
                {
                    if (replicatedItem.IsNewerThan(local.Published.Value) || !ExistsLocal(local))
                    {
                        _journal.LogItem("import {0} published {1} vs. {2} update",
                                         replicatedItem.ID, replicatedItem.PublishedDateUtc,
                                         local.Published.Value.ToUniversalTime());
                        ImportItem(replicatedItem);
                    }
                }
                if (IsMaster)
                {
                    if (replicatedItem.IsOlderThan(local.Published.Value))
                    {
                        _journal.LogItem("export {0} published {1} vs. {2} update",
                                         replicatedItem.ID, replicatedItem.PublishedDateUtc,
                                         local.Published.Value.ToUniversalTime());
                        _repstore.ExportItem(local);
                    }
                }

                // TODO check version is bigger than replaced version
                items.Remove(local);
            }
            else
            {
                if (IsSlave) // TODO and newer published date
                {
                    _journal.LogItem("import {0} published {1} create",
                                     replicatedItem.ID, replicatedItem.PublishedDateUtc);
                    ImportItem(replicatedItem);
                }
                if (IsMaster)
                {
                    // item is not published anymore -> delete replicated item
                    // TODO signalling - check edge cases, e.g. non-published items that should stay
                    _journal.LogItem("DELETE item {0} published {1}",
                                     replicatedItem.ID, replicatedItem.PublishedDateUtc);
                    _repstore.DeleteItem(replicatedItem);
                }
            }
            replicatedItem.Processed = true;
        }