Skip to content

Commit 21066ee

Browse files
authored
Add tests for send caching (#660)
1 parent 9d5ff85 commit 21066ee

File tree

5 files changed

+102
-5
lines changed

5 files changed

+102
-5
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using FluentAssertions;
2+
using NUnit.Framework;
3+
using Speckle.Connectors.Common.Caching;
4+
using Speckle.Sdk.Models;
5+
using Speckle.Sdk.Serialisation;
6+
7+
namespace Speckle.Connectors.Common.Tests.Caching;
8+
9+
public class NullSendConversionCacheTests
10+
{
11+
[Test]
12+
public void Store()
13+
{
14+
var cache = new NullSendConversionCache();
15+
var projectId = "projectId";
16+
var id = new Id("id");
17+
var objectReference = new ObjectReference() { referencedId = "referencedId" };
18+
var convertedReferences = new Dictionary<Id, ObjectReference>() { { id, objectReference } };
19+
20+
cache.StoreSendResult(projectId, convertedReferences);
21+
22+
cache.TryGetValue(projectId, id.Value, out ObjectReference? _).Should().BeFalse();
23+
24+
cache.ClearCache();
25+
26+
cache.TryGetValue(projectId, id.Value, out _).Should().BeFalse();
27+
}
28+
29+
[Test]
30+
public void Evict()
31+
{
32+
var cache = new NullSendConversionCache();
33+
var projectId = "projectId";
34+
var id = new Id("id");
35+
var objectReference = new ObjectReference() { referencedId = "referencedId" };
36+
var convertedReferences = new Dictionary<Id, ObjectReference>() { { id, objectReference } };
37+
38+
cache.StoreSendResult(projectId, convertedReferences);
39+
40+
cache.TryGetValue(projectId, id.Value, out ObjectReference? _).Should().BeFalse();
41+
42+
cache.EvictObjects([id.Value]);
43+
44+
cache.TryGetValue(projectId, id.Value, out _).Should().BeFalse();
45+
}
46+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using FluentAssertions;
2+
using NUnit.Framework;
3+
using Speckle.Connectors.Common.Caching;
4+
using Speckle.Sdk.Models;
5+
using Speckle.Sdk.Serialisation;
6+
7+
namespace Speckle.Connectors.Common.Tests.Caching;
8+
9+
public class SendConversionCacheTests
10+
{
11+
[Test]
12+
public void Store()
13+
{
14+
var cache = new SendConversionCache();
15+
var projectId = "projectId";
16+
var id = new Id("id");
17+
var objectReference = new ObjectReference() { referencedId = "referencedId" };
18+
var convertedReferences = new Dictionary<Id, ObjectReference>() { { id, objectReference } };
19+
20+
cache.StoreSendResult(projectId, convertedReferences);
21+
cache.Cache.Count.Should().Be(1);
22+
23+
cache.TryGetValue(projectId, id.Value, out ObjectReference? reference).Should().BeTrue();
24+
reference.Should().Be(objectReference);
25+
26+
cache.ClearCache();
27+
cache.Cache.Count.Should().Be(0);
28+
29+
cache.TryGetValue(projectId, id.Value, out _).Should().BeFalse();
30+
}
31+
32+
[Test]
33+
public void Evict()
34+
{
35+
var cache = new SendConversionCache();
36+
var projectId = "projectId";
37+
var id = new Id("id");
38+
var objectReference = new ObjectReference() { referencedId = "referencedId" };
39+
var convertedReferences = new Dictionary<Id, ObjectReference>() { { id, objectReference } };
40+
41+
cache.StoreSendResult(projectId, convertedReferences);
42+
cache.Cache.Count.Should().Be(1);
43+
44+
cache.TryGetValue(projectId, id.Value, out ObjectReference? reference).Should().BeTrue();
45+
reference.Should().Be(objectReference);
46+
47+
cache.EvictObjects([id.Value]);
48+
cache.Cache.Count.Should().Be(0);
49+
}
50+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("Speckle.Connectors.Common.Tests")]

Sdk/Speckle.Connectors.Common/Caching/ISendConversionCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public interface ISendConversionCache
2020
/// <para><b>Failure to do so correctly will result in cache poisoning and incorrect version creation (stale objects).</b></para>
2121
/// </summary>
2222
/// <param name="objectIds"></param>
23-
public void EvictObjects(IEnumerable<string> objectIds);
24-
public void ClearCache();
23+
void EvictObjects(IEnumerable<string> objectIds);
24+
void ClearCache();
2525
bool TryGetValue(string projectId, string applicationId, [NotNullWhen(true)] out ObjectReference? objectReference);
2626
}

Sdk/Speckle.Connectors.Common/Caching/SendConversionCache.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ namespace Speckle.Connectors.Common.Caching;
77
///<inheritdoc/>
88
public class SendConversionCache : ISendConversionCache
99
{
10-
public SendConversionCache() { }
11-
12-
private Dictionary<(string applicationId, string projectId), ObjectReference> Cache { get; set; } = new(); // NOTE: as this dude's accessed from potentially more operations at the same time, it might be safer to bless him as a concurrent dictionary.
10+
internal Dictionary<(string applicationId, string projectId), ObjectReference> Cache { get; set; } = new(); // NOTE: as this dude's accessed from potentially more operations at the same time, it might be safer to bless him as a concurrent dictionary.
1311

1412
public void StoreSendResult(string projectId, IReadOnlyDictionary<Id, ObjectReference> convertedReferences)
1513
{

0 commit comments

Comments
 (0)