Skip to content

Commit 197329d

Browse files
committed
fix: concurrency conflict in HeaderCache.Count
1 parent 2a95c72 commit 197329d

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (C) 2015-2024 The Neo Project.
2+
//
3+
// UT_HeaderCache.cs file belongs to the neo project and is free
4+
// software distributed under the MIT software license, see the
5+
// accompanying file LICENSE in the main directory of the
6+
// repository or http://www.opensource.org/licenses/mit-license.php
7+
// for more details.
8+
//
9+
// Redistribution and use in source and binary forms with or without
10+
// modifications are permitted.
11+
12+
using FluentAssertions;
13+
using Microsoft.VisualStudio.TestTools.UnitTesting;
14+
using Neo.Ledger;
15+
using Neo.Network.P2P.Payloads;
16+
using System;
17+
18+
19+
namespace Neo.UnitTests.Ledger
20+
{
21+
[TestClass]
22+
public class UT_HeaderCache
23+
{
24+
25+
[TestMethod]
26+
public void TestHeaderCache()
27+
{
28+
// test add, remove, get, tryremove, tryget, enumerator
29+
var cache = new HeaderCache();
30+
var header = new Header();
31+
header.Index = 1;
32+
cache.Add(header);
33+
34+
// TryGet returns true if the header is in the cache, false otherwise
35+
var got = cache[1];
36+
got.Should().NotBeNull();
37+
got.Index.Should().Be(1);
38+
39+
var count = cache.Count;
40+
count.Should().Be(1);
41+
42+
var full = cache.Full;
43+
full.Should().BeFalse();
44+
45+
var last = cache.Last;
46+
last.Should().NotBeNull();
47+
last.Index.Should().Be(1);
48+
49+
got = cache[2];
50+
got.Should().BeNull();
51+
52+
// enumerate
53+
var enumerator = cache.GetEnumerator();
54+
enumerator.MoveNext().Should().BeTrue();
55+
enumerator.Current.Index.Should().Be(1);
56+
enumerator.MoveNext().Should().BeFalse();
57+
58+
var removed = cache.TryRemoveFirst(out header);
59+
removed.Should().BeTrue();
60+
61+
count = cache.Count;
62+
count.Should().Be(0);
63+
64+
full = cache.Full;
65+
full.Should().BeFalse();
66+
67+
last = cache.Last;
68+
last.Should().BeNull();
69+
70+
got = cache[1];
71+
got.Should().BeNull();
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)