Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e31e193

Browse files
author
Sandy C
committedJan 14, 2020
Add unit test to verify has conflic flag to be cleared after conflict gets resolved
1 parent 25774a1 commit e31e193

File tree

1 file changed

+86
-29
lines changed

1 file changed

+86
-29
lines changed
 

‎src/Couchbase.Lite.Tests.Shared/ReplicationTest.cs

Lines changed: 86 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,40 @@ public void TestConflictResolverReturningBlobFromDifferentDB()
16661666
TestConflictResolverExceptionThrown(blobFromOtherDbResolver, false, true);
16671667
}
16681668

1669+
//CBL-623: Revision flags get cleared while saving resolved document
1670+
[Fact]
1671+
public void TestConflictResolverPreservesFlags()
1672+
{
1673+
//force conflicts and check flags
1674+
CreateReplicationConflict("doc1", true);
1675+
1676+
var config = CreateConfig(false, true, false);
1677+
C4DocumentFlags flags = (C4DocumentFlags)0;
1678+
config.ConflictResolver = new TestConflictResolver((conflict) =>
1679+
{
1680+
unsafe
1681+
{
1682+
flags = conflict.LocalDocument.c4Doc.RawDoc->flags;
1683+
flags.HasFlag(C4DocumentFlags.DocConflicted).Should().BeTrue();
1684+
flags.HasFlag(C4DocumentFlags.DocExists | C4DocumentFlags.DocHasAttachments).Should().BeTrue();
1685+
return conflict.LocalDocument;
1686+
}
1687+
});
1688+
1689+
RunReplication(config, 0, 0);
1690+
1691+
using (var doc = Db.GetDocument("doc1")) {
1692+
doc.GetBlob("blob")?.Content.Should().ContainInOrder(new byte[] { 6, 6, 6 });
1693+
unsafe
1694+
{
1695+
flags = doc.c4Doc.RawDoc->flags;
1696+
}
1697+
}
1698+
1699+
flags.HasFlag(C4DocumentFlags.DocConflicted).Should().BeFalse();
1700+
flags.HasFlag(C4DocumentFlags.DocExists | C4DocumentFlags.DocHasAttachments).Should().BeTrue();
1701+
}
1702+
16691703
private void TestConflictResolverExceptionThrown(TestConflictResolver resolver, bool continueWithWorkingResolver = false, bool withBlob = false)
16701704
{
16711705
CreateReplicationConflict("doc1");
@@ -1750,39 +1784,62 @@ private void TestConflictResolverWins(bool returnRemoteDoc)
17501784
}
17511785
}
17521786

1753-
private void CreateReplicationConflict(string id)
1787+
private void CreateReplicationConflict(string id, bool checkFlags = false)
17541788
{
1755-
var oddByteArray = new byte[] { 1, 3, 5 };
1756-
1757-
using (var doc1 = new MutableDocument(id)) {
1758-
doc1.SetString("name", "Tiger");
1759-
doc1.SetBlob("blob", new Blob("text/plaintext", oddByteArray));
1760-
Db.Save(doc1);
1761-
}
1762-
1763-
using (var doc1 = new MutableDocument(id)) {
1764-
doc1.SetString("name", "Tiger");
1765-
doc1.SetBlob("blob", new Blob("text/plaintext", oddByteArray));
1766-
_otherDB.Save(doc1);
1767-
}
1768-
1769-
// Force a conflict
1770-
using (var doc1a = Db.GetDocument(id))
1771-
using (var doc1aMutable = doc1a.ToMutable()) {
1772-
var evilByteArray = new byte[] { 6, 6, 6 };
1789+
unsafe
1790+
{
1791+
var oddByteArray = new byte[] {1, 3, 5};
1792+
C4DocumentFlags flags = (C4DocumentFlags)0;
1793+
using (var doc1 = new MutableDocument(id))
1794+
{
1795+
doc1.SetString("name", "Tiger");
1796+
doc1.SetBlob("blob", new Blob("text/plaintext", oddByteArray));
1797+
Db.Save(doc1);
1798+
if (checkFlags) {
1799+
flags = doc1.c4Doc.RawDoc->flags;
1800+
flags.HasFlag(C4DocumentFlags.DocExists | C4DocumentFlags.DocHasAttachments).Should().BeTrue();
1801+
}
1802+
}
17731803

1774-
doc1aMutable.SetString("name", "Cat");
1775-
doc1aMutable.SetBlob("blob", new Blob("text/plaintext", evilByteArray));
1776-
Db.Save(doc1aMutable);
1777-
}
1804+
using (var doc1 = new MutableDocument(id))
1805+
{
1806+
doc1.SetString("name", "Tiger");
1807+
doc1.SetBlob("blob", new Blob("text/plaintext", oddByteArray));
1808+
_otherDB.Save(doc1);
1809+
if (checkFlags) {
1810+
flags = doc1.c4Doc.RawDoc->flags;
1811+
flags.HasFlag(C4DocumentFlags.DocExists | C4DocumentFlags.DocHasAttachments).Should().BeTrue();
1812+
}
1813+
}
17781814

1779-
using (var doc1a = _otherDB.GetDocument(id))
1780-
using (var doc1aMutable = doc1a.ToMutable()) {
1781-
var luckyByteArray = new byte[] { 7, 7, 7 };
1815+
// Force a conflict
1816+
using (var doc1a = Db.GetDocument(id))
1817+
using (var doc1aMutable = doc1a.ToMutable())
1818+
{
1819+
var evilByteArray = new byte[] {6, 6, 6};
1820+
1821+
doc1aMutable.SetString("name", "Cat");
1822+
doc1aMutable.SetBlob("blob", new Blob("text/plaintext", evilByteArray));
1823+
Db.Save(doc1aMutable);
1824+
if (checkFlags) {
1825+
flags = doc1aMutable.c4Doc.RawDoc->flags;
1826+
flags.HasFlag(C4DocumentFlags.DocExists | C4DocumentFlags.DocHasAttachments).Should().BeTrue();
1827+
}
1828+
}
17821829

1783-
doc1aMutable.SetString("name", "Lion");
1784-
doc1aMutable.SetBlob("blob", new Blob("text/plaintext", luckyByteArray));
1785-
_otherDB.Save(doc1aMutable);
1830+
using (var doc1a = _otherDB.GetDocument(id))
1831+
using (var doc1aMutable = doc1a.ToMutable())
1832+
{
1833+
var luckyByteArray = new byte[] {7, 7, 7};
1834+
1835+
doc1aMutable.SetString("name", "Lion");
1836+
doc1aMutable.SetBlob("blob", new Blob("text/plaintext", luckyByteArray));
1837+
_otherDB.Save(doc1aMutable);
1838+
if (checkFlags) {
1839+
flags = doc1aMutable.c4Doc.RawDoc->flags;
1840+
flags.HasFlag(C4DocumentFlags.DocExists | C4DocumentFlags.DocHasAttachments).Should().BeTrue();
1841+
}
1842+
}
17861843
}
17871844
}
17881845

0 commit comments

Comments
 (0)
Please sign in to comment.