public void 繞行SafeCollection操作集合_不同執行緒同時對SafeCollection新增與刪除元素_不應擲出例外() { var safeCollection = new SafeCollection <int> { 1, 2, 3, 4, 5 }; Task.Run(() => { while (true) { safeCollection.Add(0); } }); Task.Run(() => { while (true) { safeCollection.Remove(0); } }); Task.Run(() => { while (true) { safeCollection.Clear(); } }); Action action = () => IterateCollection(safeCollection).Wait(); action.Should().NotThrow(); }
internal void Start() { isStart = true; Task.Run(() => { while (isStart) { if (RunningTasks.Count < MaxTaskCount) { DataInfo commandinfo; if (commandInfos.TryDequeue(out commandinfo)) { Task task = new Task(() => { var command = DbHelper.Current.Get <PoolCommand>(DataType.CommandType, commandinfo.ID); HeartbeatCommand.UpdateHeartTime(commandinfo.State); PoolJob.TcpServer.ReceivedCommandAction(commandinfo.State, command); }); task.ContinueWith(t => { RunningTasks.Remove(task); }); RunningTasks.Add(task); task.Start(); } } } }); }
public void SafeCollection中移除指定元素_集合包含該元素且移除元素成功_集合長度減少1且回傳True() { var safeCollection = new SafeCollection <int> { 1, 2, 3, 4, 5 }; var isSuccessful = safeCollection.Remove(4); const int expectedCount = 4; safeCollection.Count.Should().Be(expectedCount); isSuccessful.Should().Be(true); }
public void SafeCollection中移除指定元素_集合不包含該元素且移除元素失敗_集合長度不變且回傳False() { var safeCollection = new SafeCollection <int> { 1, 2, 3, 4, 5 }; var isSuccessful = safeCollection.Remove(6); const int expectedCount = 5; safeCollection.Count.Should().Be(expectedCount); isSuccessful.Should().Be(false); }
internal void Start() { isStart = true; Task.Run(() => { while (isStart) { if (RunningTasks.Count < MaxTaskCount) { Task command; if (loginCommands.TryDequeue(out command)) { command.ContinueWith(t => { RunningTasks.Remove(command); }); RunningTasks.Add(command); command.Start(); } } } }); }
internal void Start() { isStart = true; Task.Run(() => { while (isStart) { if (RunningTasks.Count < MaxTaskCount) { DataInfo analysisDataInfo; if (analysisDataIds.TryDequeue(out analysisDataInfo)) { Task task = new Task(() => { var analysisData = DbHelper.Current.Get <byte[]>(DataType.ReceiveType, analysisDataInfo.ID); var state = analysisDataInfo.State; var buffer = analysisData; var commandDataList = new List <byte[]>(); var index = 0; List <byte> bytes = null; while (index < buffer.Length) { if (bytes == null) { if ((index + 3) < buffer.Length && buffer[index] == PoolCommand.DefaultPrefixBytes[0] && buffer[index + 1] == PoolCommand.DefaultPrefixBytes[1] && buffer[index + 2] == PoolCommand.DefaultPrefixBytes[2] && buffer[index + 3] == PoolCommand.DefaultPrefixBytes[3]) { bytes = new List <byte>(); bytes.AddRange(PoolCommand.DefaultPrefixBytes); index += 4; } else { index++; } } else { if ((index + 3) < buffer.Length && buffer[index] == PoolCommand.DefaultSuffixBytes[0] && buffer[index + 1] == PoolCommand.DefaultSuffixBytes[1] && buffer[index + 2] == PoolCommand.DefaultSuffixBytes[2] && buffer[index + 3] == PoolCommand.DefaultSuffixBytes[3]) { bytes.AddRange(PoolCommand.DefaultSuffixBytes); commandDataList.Add(bytes.ToArray()); bytes = null; index += 4; } else { bytes.Add(buffer[index]); index++; } } } foreach (var data in commandDataList) { try { var cmd = PoolCommand.ConvertBytesToMessage(data); if (cmd != null) { MsgPool.Current.AddCommand(new CommandState { State = state, Command = cmd }); } } catch (Exception ex) { LogHelper.Error("Error occured on deserialize messgae: " + ex.Message, ex); } } }); task.ContinueWith(t => { RunningTasks.Remove(task); }); RunningTasks.Add(task); task.Start(); } } } }); }