Skip to content

Commit fea4f09

Browse files
TypeCache returns unexpected data after multiple insertions and deletions (#121)
* feat: get rid of type cache entirely * tests: ensure multiple inserts and removals do not throw --------- Co-authored-by: Yury Tsai <y.tsai@smartcat.ai>
1 parent 39f3ade commit fea4f09

File tree

4 files changed

+71
-57
lines changed

4 files changed

+71
-57
lines changed

Tests/YDotNet.Tests.Unit/XmlElements/InsertElementTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using NUnit.Framework;
22
using YDotNet.Document;
3+
using YDotNet.Document.Types.XmlElements;
34

45
namespace YDotNet.Tests.Unit.XmlElements;
56

@@ -55,4 +56,41 @@ public void InsertMultipleElements()
5556

5657
transaction.Commit();
5758
}
59+
60+
[Test]
61+
public void InsertAndRemoveMultipleTimes()
62+
{
63+
// Arrange
64+
var doc = new Doc();
65+
var xmlFragment = doc.XmlFragment("xml-fragment");
66+
XmlElement xmlElement;
67+
using (var transaction = doc.WriteTransaction())
68+
{
69+
xmlElement = xmlFragment.InsertElement(transaction, index: 0, "xml-element");
70+
}
71+
72+
// Act
73+
for (var i = 0; i < 100; i++)
74+
{
75+
// Assert
76+
Assert.DoesNotThrow(() =>
77+
{
78+
using (var transaction = doc.WriteTransaction())
79+
{
80+
for (uint j = 0; j < 100; j++)
81+
{
82+
xmlElement
83+
.InsertElement(transaction, index: j, "paragraph")
84+
.InsertText(transaction, index: 0)
85+
.Insert(transaction, index: 0, "Hello world");
86+
}
87+
}
88+
89+
using (var transaction = doc.WriteTransaction())
90+
{
91+
xmlElement.RemoveRange(transaction, index: 0, length: 100);
92+
}
93+
});
94+
}
95+
}
5896
}

Tests/YDotNet.Tests.Unit/XmlFragments/InsertElementTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,36 @@ public void InsertMultipleElements()
4747

4848
transaction.Commit();
4949
}
50+
51+
[Test]
52+
public void InsertAndRemoveMultipleTimes()
53+
{
54+
// Arrange
55+
var doc = new Doc();
56+
var xmlFragment = doc.XmlFragment("xml-fragment");
57+
58+
// Act
59+
for (var i = 0; i < 100; i++)
60+
{
61+
// Assert
62+
Assert.DoesNotThrow(() =>
63+
{
64+
using (var transaction = doc.WriteTransaction())
65+
{
66+
for (uint j = 0; j < 100; j++)
67+
{
68+
xmlFragment
69+
.InsertElement(transaction, index: j, "paragraph")
70+
.InsertText(transaction, index: 0)
71+
.Insert(transaction, index: 0, "Hello world");
72+
}
73+
}
74+
75+
using (var transaction = doc.WriteTransaction())
76+
{
77+
xmlFragment.RemoveRange(transaction, index: 0, length: 100);
78+
}
79+
});
80+
}
81+
}
5082
}

YDotNet/Document/Doc.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public class Doc : UnmanagedResource
3939
private readonly EventSubscriber<UpdateEvent> onUpdateV1;
4040
private readonly EventSubscriber<UpdateEvent> onUpdateV2;
4141
private readonly Doc? parent;
42-
private readonly TypeCache typeCache = new();
4342
private int openTransactions;
4443

4544
/// <summary>
@@ -542,7 +541,7 @@ private T GetOrAdd<T>(nint handle, Func<nint, Doc, T> factory)
542541
{
543542
var doc = GetRootDoc();
544543

545-
return doc.typeCache.GetOrAdd(handle, h => factory(h, doc));
544+
return factory(handle, doc);
546545
}
547546

548547
private void ThrowIfOpenTransaction()

YDotNet/Infrastructure/TypeCache.cs

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)