再次优化代码附加单元测试

This commit is contained in:
xuejiaming 2022-07-21 10:00:21 +08:00
parent d3ee27f3ec
commit 022e063dad
2 changed files with 40 additions and 3 deletions

View File

@ -23,11 +23,11 @@ namespace ShardingCore.Core.Collections
{
get
{
if (_copyList?.Count != _list.Count)
if (_copyList.Count != _list.Count)
{
lock (COPY_LOCK)
{
if (_copyList?.Count != _list.Count)
if (_copyList.Count != _list.Count)
{
_copyList = _list.ToList();
}
@ -38,7 +38,7 @@ namespace ShardingCore.Core.Collections
}
}
public List<T> _copyList;
public List<T> _copyList=new List<T>(0);
public void Append(T value)
{

View File

@ -0,0 +1,37 @@
using System.Diagnostics;
using ShardingCore.Core.Collections;
using Xunit;
namespace ShardingCore.CommonTest
{
public class CommonTest
{
[Fact]
public void TestList()
{
var list = new List<string>(){"1","2","3"};
var safeReadAppendList = new SafeReadAppendList<string>(list);
Assert.True(safeReadAppendList.CopyList.Count==3);
for (int i = 0; i < list.Count; i++)
{
Assert.Equal(list[i],safeReadAppendList.CopyList[i]);
}
list.Add("4");
safeReadAppendList.Append("4");
Assert.True(safeReadAppendList.CopyList.Count==4);
for (int i = 0; i < list.Count; i++)
{
Assert.Equal(list[i],safeReadAppendList.CopyList[i]);
}
Stopwatch sp = Stopwatch.StartNew();
for (int i = 0; i < 1000000000; i++)
{
var copyListCount = safeReadAppendList.CopyList.Count;
}
sp.Stop();
Console.WriteLine(sp.ElapsedMilliseconds);
}
}
}