public void ParallelTaskWithOneActionTest() { var timings = new ConcurrentBag <Timing>(); Action action = () => { var timing = new Timing { StartTime = DateTime.Now }; Thread.Sleep(500); timing.FinishTime = DateTime.Now; timings.Add(timing); }; var task = ParallelTask.StartNew(action, 10); task.Wait(); var maxStartTime = timings.Select(t => t.StartTime).Max(); var maxFinishTime = timings.Select(t => t.FinishTime).Min(); foreach (var timing in timings) { System.Console.WriteLine("{0} - {1}", timing.StartTime, timing.FinishTime); } Assert.IsTrue(maxStartTime < maxFinishTime); }
private async Task ConnectAsync(CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { try { _isConnected = false; if (string.IsNullOrEmpty(_options.IdentityUid) || string.IsNullOrEmpty(_options.Password)) { continue; } using (var webSocketClient = new ClientWebSocket()) { using (var timeout = new CancellationTokenSource(_options.ReconnectDelay)) { var encodedIdentityUid = HttpUtility.UrlEncode(_options.IdentityUid); var encodedChannelUid = HttpUtility.UrlEncode(_options.ChannelUid); var encodedPassword = HttpUtility.UrlEncode(_options.Password); var url = $"wss://{_options.Host}/Connectors/{encodedIdentityUid}/Channels/{encodedChannelUid}?password={encodedPassword}"; await webSocketClient.ConnectAsync(new Uri(url, UriKind.Absolute), timeout.Token).ConfigureAwait(false); } _channel = new ConnectorChannel(webSocketClient, _logger); _logger.LogInformation($"Connected with Wirehome.Cloud at host '{_options.Host}'."); _isConnected = true; while (_channel.IsConnected && !cancellationToken.IsCancellationRequested) { var receiveResult = await _channel.ReceiveMessageAsync(cancellationToken).ConfigureAwait(false); if (receiveResult.CloseConnection || cancellationToken.IsCancellationRequested) { break; } if (receiveResult.Message == null) { continue; } ParallelTask.Start(() => TryProcessCloudMessageAsync(receiveResult.Message, cancellationToken), cancellationToken, _logger); } } } catch (Exception exception) { _isConnected = false; _channel = null; _logger.LogError(exception, $"Error while connecting with Wirehome.Cloud service at host '{_options.Host}'."); } finally { await Task.Delay(TimeSpan.FromSeconds(10), cancellationToken).ConfigureAwait(false); } } }
private int DoParallelTasks() { TaskStats stats = RunData.Points.CurrentStats; InitTasksArray(); ParallelTask[] t = runningParallelTasks = new ParallelTask[repetitions * tasks.Count]; // prepare threads int index = 0; for (int k = 0; k < repetitions; k++) { for (int i = 0; i < tasksArray.Length; i++) { PerfTask task = (PerfTask)(tasksArray[i].Clone()); t[index++] = new ParallelTask(this, task); } } // run threads StartThreads(t); if (Stop) { foreach (ParallelTask task in t) { task.Task.StopNow(); } } // wait for all threads to complete int count = 0; for (int i = 0; i < t.Length; i++) { t[i].Join(); count += t[i].Count; if (t[i].Task is TaskSequence sub && sub.countsByTime != null) { if (countsByTime == null) { countsByTime = new int[sub.countsByTime.Length]; } else if (countsByTime.Length < sub.countsByTime.Length) { countsByTime = ArrayUtil.Grow(countsByTime, sub.countsByTime.Length); } for (int j = 0; j < sub.countsByTime.Length; j++) { countsByTime[j] += sub.countsByTime[j]; } } } if (countsByTime != null) { stats.SetCountsByTime(countsByTime, logByTimeMsec); } // return total count return(count); }
public void ExecuteRunsMultipleSuccessfulTasks() { // Initialise the task var subTasks = new List <ParallelTestTask>(); for (var loop = 1; loop <= 5; loop++) { subTasks.Add(new ParallelTestTask { TaskNumber = loop, Result = IntegrationStatus.Success }); } var task = new ParallelTask { Tasks = subTasks.ToArray() }; // Setup the mocks var logger = mocks.DynamicMock <ILogger>(); var result = GenerateResultMock(false); mocks.ReplayAll(); // Run the actual task task.Run(result); // Verify the results mocks.VerifyAll(); Assert.AreEqual(IntegrationStatus.Success, result.Status, "Status does not match"); }
public void Start() { _publisher = new SsdpDevicePublisher(); //var rootDevice = new SsdpRootDevice //{ // Uuid = "c6faa85a-d7e9-48b7-8c54-7459c4d9c329", // CacheLifetime = TimeSpan.Zero, // //UrlBase = new Uri("http://localhost"), // //PresentationUrl = new Uri("configurator", UriKind.Relative), // FriendlyName = "Wirehome.Core", // Manufacturer = "Wirehome", // //ManufacturerUrl = new Uri("https://github.com/chkr1011/Wirehome.Core/"), // ModelNumber = WirehomeCoreVersion.Version, // //ModelUrl = new Uri("app", UriKind.Relative), // ModelName = "Wirehome.Core", // ModelDescription = "Wirehome.Core", //}; //_publisher.AddDevice(rootDevice); ParallelTask.Start(SearchAsync, CancellationToken.None, _logger); }
public void ParallelTaskWithMultipleActionsTest() { var actions = new Action[10]; var timings = new ConcurrentBag <Timing>(); for (var i = 0; i < actions.Length; i++) { actions[i] = () => { var timing = new Timing { StartTime = DateTime.Now }; Thread.Sleep(500); timing.FinishTime = DateTime.Now; timings.Add(timing); }; } var task = ParallelTask.StartNew(actions); task.Wait(); var maxStartTime = timings.Select(t => t.StartTime).Max(); var maxFinishTime = timings.Select(t => t.FinishTime).Min(); foreach (var timing in timings) { System.Console.WriteLine("{0} - {1}", timing.StartTime, timing.FinishTime); } Assert.IsTrue(maxStartTime < maxFinishTime); }
private ParallelTask RecvTaskFromMaster() { var polarization = Mpi.RecvInt(Mpi.Master, (int)Tag.TaskData, Mpi.CommWorld); var frequency = Mpi.RecvDouble(Mpi.Master, (int)Tag.TaskData, Mpi.CommWorld); return(ParallelTask.NewFrequencyTask(frequency, polarization)); }
public void ExecuteRunsSuccessAndFailureTasks() { // Initialise the task var subTasks = new List <ParallelTestTask>(); for (var loop = 1; loop <= 5; loop++) { subTasks.Add(new ParallelTestTask { TaskNumber = loop, Result = loop >= 3 ? IntegrationStatus.Failure : IntegrationStatus.Success }); } var task = new ParallelTask { Tasks = subTasks.ToArray() }; // Setup the mocks var logger = mocks.Create <ILogger>().Object; var result = GenerateResultMock(false); // Run the actual task task.Run(result); // Verify the results VerifyResultMock(result, false); mocks.VerifyAll(); Assert.AreEqual(IntegrationStatus.Failure, result.Status, "Status does not match"); }
public static ITask DoInParallelWith(this ITask original, MonoBehaviour holder, params ITask[] tasks) { var result = new ParallelTask(holder); AddTo(result, original, tasks); return(result); }
public async Task ParallelTasksVerifyInParallel() { var task = new ParallelTask(TaskName, this.tasks); await task.VerifyAsync(this.Context); Assert.True(this.maxExecuting > 1); }
public async Task ParallelTasksExecuteInParallel() { var task = new ParallelTask(this.tasks); await task.ExecuteAsync(this.Context); Assert.True(this.maxExecuting > 1); }
public void ReadMinimalConfig() { var config = @"<parallel> <tasks/> </parallel>"; var task = new ParallelTask(); NetReflector.Read(config, task); }
public async Task ConnectAsync(CoapClientConnectOptions options, CancellationToken cancellationToken) { _connectOptions = options ?? throw new ArgumentNullException(nameof(options)); await _lowLevelClient.ConnectAsync(options, cancellationToken).ConfigureAwait(false); _cancellationToken = new CancellationTokenSource(); ParallelTask.StartLongRunning(() => ReceiveMessages(_cancellationToken.Token), _cancellationToken.Token); }
protected override void OnStart() { lock (_notifications) { Load(); } ParallelTask.Start(() => RemoveNotificationsAsync(_systemCancellationToken.Token), _systemCancellationToken.Token, _logger); }
public void Show(string message, double time = 2000) { MessageLabel.Content = message; IsToasting = true; ParallelTask task = new ParallelTask(time); task.AddTask(SafelyHide); task.Start(); }
protected virtual void OnStartNewTask(ParallelTask e) { var handler = StartNewTask; if (handler != null) { handler(this, e); } }
protected virtual void Awake() { _onOpen = new ParallelTask(this); _onClose = new ParallelTask(this); AddOnOpen(new ActionTask(OnOpen)); AddOnOpen(_blurTask.ToTask()); AddOnClose(new ActionTask(OnClose)); AddOnClose(DisappearAnimation().DoInSequenceWith(this, new ActionTask(Clear))); }
private static void TestSimpleParallel() { var bb = new BlackBoard(); var main = new ParallelTask(2, 3, "Main Parallel"); main.AddChildTask(new TestSuccessLeaf("1")); main.AddChildTask(new TestRunningLeaf("2")); main.AddChildTask(new TestFailureLeaf("3")); SetTimer(main, bb); }
public object Run(object t, ParallelTask task) { var model = t as CrawlTaskModel; var results = new List <ExtractResult>(); var reporter = task.Progress as IProgress <string>; reporter.Report("正在读取Feed记录"); var feed = FeedLiteDb.GetFeed(model.FeedId); reporter.Report("正在下载 Feed"); var compile = new CompileFeedAddress(); feed.Address = compile.Compile(feed.Address); var job = new FeedJob(); var snap = job.DoTask(feed, false); reporter.Report("Feed 下载完成"); var block = RuiJiExpression.ParserBlock(feed.RuiJiExpression); var feedResult = RuiJiExtracter.Extract(snap.Content, block); results.Add(feedResult); reporter.Report("正在提取Feed地址"); var j = new FeedExtractJob(); var urls = j.ExtractAddress(snap); reporter.Report("Feed地址提取完成"); foreach (var url in urls) { reporter.Report("正在提取地址 " + url); var r = ContentQueue.Instance.Extract(url); results.AddRange(r); } reporter.Report("计算完成"); if (!model.IncludeContent) { results.ForEach((m) => { ClearContent(m); }); } return(results); }
public void TestCrawlTaskFunc() { var task = new ParallelTask(); var model = new CrawlTaskModel(); model.FeedId = 3; var fun = new CrawlTaskFunc(); var result = fun.Run(model, task); Assert.True(result != null); }
public void ReadFullConfig() { var config = @"<parallel> <description>Testing</description> <tasks/> <dynamicValues/> </parallel>"; var task = new ParallelTask(); NetReflector.Read(config, task); Assert.AreEqual("Testing", task.Description); }
public async Task ParallelTask_Start() { var parallelTask = new ParallelTask(); var result = await parallelTask.Start(SlowProcess(5000), async(slowProcess) => { var amount = await SlowProcess(5500); amount += await slowProcess; return(amount); }); Assert.Equal(449918.84m, result); }
public void ParallelTaskFailsOnTooShortParallelCountArgumentTest() { var task = ParallelTask.StartNew(() => { }, 1); Assert.IsNotNull(task); Assert.IsNotNull(task.Exception); Assert.IsInstanceOf <AggregateException>(task.Exception); var aggregateException = task.Exception; Assert.AreEqual(1, aggregateException.InnerExceptions.Count); Assert.IsInstanceOf <ArgumentOutOfRangeException>(aggregateException.InnerExceptions.First()); }
public void ParallelTaskFailsOnNullActionsArgumentTest() { var task = ParallelTask.StartNew(null); Assert.IsNotNull(task); Assert.IsNotNull(task.Exception); Assert.IsInstanceOf <AggregateException>(task.Exception); var aggregateException = task.Exception; Assert.AreEqual(1, aggregateException.InnerExceptions.Count); Assert.IsInstanceOf <ArgumentNullException>(aggregateException.InnerExceptions.First()); }
public ParallelTaskCollection(int initialSize, string name = null) : base(initialSize) { _currentWrapper = new ParallelTask(this); if (name == null) { _name = "ParallelTaskCollection".FastConcat(GetHashCode()); } else { _name = name; } }
protected override void OnStart() { _storageService.SafeReadSerializedValue(out _options, DefaultDirectoryNames.Configuration, MqttServiceOptions.Filename); IsLowLevelMqttLoggingEnabled = _options.EnableLogging; var mqttFactory = new MqttFactory(); IMqttNetLogger mqttNetLogger; if (IsLowLevelMqttLoggingEnabled) { mqttNetLogger = new LoggerAdapter(_logger); } else { mqttNetLogger = new MqttNetLogger(); } _webSocketServerAdapter = new MqttWebSocketServerAdapter(mqttNetLogger); var serverAdapters = new List <IMqttServerAdapter> { new MqttTcpServerAdapter(mqttNetLogger), _webSocketServerAdapter }; _mqttServer = mqttFactory.CreateMqttServer(serverAdapters, mqttNetLogger); _mqttServer.UseApplicationMessageReceivedHandler(new MqttApplicationMessageReceivedHandlerDelegate(e => OnApplicationMessageReceived(e))); var serverOptions = new MqttServerOptionsBuilder() .WithDefaultEndpointPort(_options.ServerPort) .WithConnectionValidator(ValidateClientConnection) .WithPersistentSessions(); if (_options.PersistRetainedMessages) { var storage = new MqttServerStorage(_storageService, _systemCancellationToken, _logger); storage.Start(); serverOptions.WithStorage(storage); } _mqttServer.StartAsync(serverOptions.Build()).GetAwaiter().GetResult(); _systemCancellationToken.Token.Register(() => { _mqttServer.StopAsync().GetAwaiter().GetResult(); }); ParallelTask.StartLongRunning(ProcessIncomingMqttMessages, _systemCancellationToken.Token, _logger); }
protected override void OnStart() { _backupService.ExcludePathFromBackup(Path.Combine(_storageService.DataPath, "History")); _storageService.SafeReadSerializedValue(out _options, DefaultDirectoryNames.Configuration, ComponentHistoryServiceOptions.Filename); if (!_options.IsEnabled) { _logger.LogInformation("Component history is disabled."); return; } ParallelTask.Start(() => TryProcessWorkItems(_systemCancellationToken.Token), _systemCancellationToken.Token, _logger); ParallelTask.Start(() => TryUpdateComponentStatusValues(_systemCancellationToken.Token), _systemCancellationToken.Token, _logger); }
public void ValidatePassesForTasksSection() { var task = new ParallelTask(); var project = new Project { Tasks = new ITask[] { task } }; var errorProcessor = mocks.Create <IConfigurationErrorProcesser>(MockBehavior.Strict).Object; task.Validate(null, ConfigurationTrace.Start(project), errorProcessor); mocks.VerifyAll(); }
public void ValidateFailsForPublishersSection() { var task = new ParallelTask(); var project = new Project { Publishers = new ITask[] { task } }; var errorProcessor = mocks.Create <IConfigurationErrorProcesser>(MockBehavior.Strict).Object; Mock.Get(errorProcessor).Setup(_errorProcessor => _errorProcessor.ProcessWarning(It.IsAny <string>())).Verifiable(); task.Validate(null, ConfigurationTrace.Start(project), errorProcessor); mocks.VerifyAll(); }
public void Show(double duration = 0) { TrasitionMargin(Border.Margin, new Thickness(0), Border.Name); _isHidden = false; TopLevelContent.IsEnabled = true; if (duration > 0) { if (_hideParallelTask != null) { _hideParallelTask.RequestStop(); } _hideParallelTask = new ParallelTask(duration); _hideParallelTask.AddTask(InvokeHide); _hideParallelTask.Start(); } }
public void ParallelTaskNameConstructorSetsName() { var task = new ParallelTask(TaskName, this.tasks); Assert.Equal(TaskName, task.Name); }
public async Task ParallelTasksVerifyInParallel() { var task = new ParallelTask(TaskName, this.tasks); await task.VerifyAsync(this.Context); Assert.True(this.maxExecuting > 1); }
public async Task ParallelTasksExecuteInParallel() { var task = new ParallelTask(this.tasks); await task.ExecuteAsync(this.Context); Assert.True(this.maxExecuting > 1); }
protected override BehaviorTask createTask() { ParallelTask pTask = new ParallelTask(); return pTask; }