diff --git a/src/ShardingCore/Core/Collections/SafeReadAppendList.cs b/src/ShardingCore/Core/Collections/SafeReadAppendList.cs index eccb525e..3521f9b9 100644 --- a/src/ShardingCore/Core/Collections/SafeReadAppendList.cs +++ b/src/ShardingCore/Core/Collections/SafeReadAppendList.cs @@ -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 _copyList; + public List _copyList=new List(0); public void Append(T value) { diff --git a/test/ShardingCore.CommonTest/CommonTest.cs b/test/ShardingCore.CommonTest/CommonTest.cs new file mode 100644 index 00000000..a7bbf758 --- /dev/null +++ b/test/ShardingCore.CommonTest/CommonTest.cs @@ -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(){"1","2","3"}; + var safeReadAppendList = new SafeReadAppendList(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); + } + } + +}