public void TryAdd_SingleThread_Success() { var height = 1; var blockInfoCache = new BlockInfoCache(1); var res = blockInfoCache.TryAdd(new SideChainBlockData { SideChainHeight = height }); Assert.True(res); Assert.True(blockInfoCache.TargetChainHeight() == height + 1); }
public void TryAdd_SingleThread_Fail() { var height = 2; var initTarget = 1; var blockInfoCache = new BlockInfoCache(initTarget); var res = blockInfoCache.TryAdd(new SideChainBlockData { SideChainHeight = height }); Assert.False(res); Assert.True(blockInfoCache.TargetChainHeight() == initTarget); }
public void TryAdd_DataContinuous() { var initTarget = 1; var blockInfoCache = new BlockInfoCache(initTarget); var i = 0; while (i < 5) { blockInfoCache.TryAdd(new SideChainBlockData { SideChainHeight = i++ }); } Assert.True(blockInfoCache.TargetChainHeight() == 5); }
public void TargetHeight_WithEmptyQueue() { var initTarget = 1; var blockInfoCache = new BlockInfoCache(initTarget); blockInfoCache.TryAdd(new SideChainBlockData { SideChainHeight = 1 }); blockInfoCache.TryAdd(new SideChainBlockData { SideChainHeight = 2 }); blockInfoCache.TryTake(1, out _, false); blockInfoCache.TryTake(2, out _, false); Assert.True(blockInfoCache.TargetChainHeight() == 3); }
public void TryAdd_MultiThreads_WithSameData() { var initTarget = 1; var blockInfoCache = new BlockInfoCache(initTarget); var i = 0; var taskList = new List <Task>(); while (i++ < 5) { var t = Task.Run(() => blockInfoCache.TryAdd(new SideChainBlockData { SideChainHeight = initTarget })); taskList.Add(t); } Task.WaitAll(taskList.ToArray()); Assert.True(blockInfoCache.TargetChainHeight() == initTarget + 1); }
public void TryAdd_DataNotContinuous() { var initTarget = 1; var blockInfoCache = new BlockInfoCache(initTarget); blockInfoCache.TryAdd(new SideChainBlockData { SideChainHeight = 1 }); blockInfoCache.TryAdd(new SideChainBlockData { SideChainHeight = 2 }); // 3 is absent. blockInfoCache.TryAdd(new SideChainBlockData { SideChainHeight = 4 }); Assert.True(blockInfoCache.TargetChainHeight() == 3); }