private void runButton_Click(object sender, EventArgs e) { using (JobRunner jobRunner = new JobRunner()) { jobRunner.ConnectionString = _connectionString; jobRunner.Initialize(); string jobName = jobsComboBox.SelectedItem.ToString(); jobRunner.Run(jobName); MessageBox.Show("Done"); } }
public void Reuse() { var onThread = new int[4]; var sums = new int[4]; var _ = new GameState(); using (var runner = new JobRunner(_, 4, 10)) { runner.Initialize(); // setup the jobs Job <int> j1, j2, j3, j4; { j1 = runner.CreateJob(TestTask, 0); j2 = runner.CreateJob(TestTask, 1); j3 = runner.CreateJob(TestTask, 2); j4 = runner.CreateJob(TestTask, 3); } // wait for them to finish var comp = runner.StartJobs(j1, j2, j3, j4); comp.WaitForCompletion(); // got the right answer Assert.Equal(49_995_000, sums[0]); Assert.Equal(49_995_000, sums[1]); Assert.Equal(49_995_000, sums[2]); Assert.Equal(49_995_000, sums[3]); // run them _again_ var comp2 = runner.StartJobs(j1, j2, j3, j4); comp2.WaitForCompletion(); // got the right answer, again Assert.Equal(2 * 49_995_000, sums[0]); Assert.Equal(2 * 49_995_000, sums[1]); Assert.Equal(2 * 49_995_000, sums[2]); Assert.Equal(2 * 49_995_000, sums[3]); } // just a dumb task to do void TestTask(GameState ignore, int ix) { for (var i = 0; i < 10_000; i++) { sums[ix] += i; } } }
private TestHostContext CreateTestContext([CallerMemberName] String testName = "") { var hc = new TestHostContext(this, testName); _jobEc = new Agent.Worker.ExecutionContext(); _config = new Mock <IConfigurationStore>(); _extensions = new Mock <IExtensionManager>(); _jobExtension = new Mock <IJobExtension>(); _jobServer = new Mock <IJobServer>(); _jobServerQueue = new Mock <IJobServerQueue>(); _proxyConfig = new Mock <IVstsAgentWebProxy>(); _cert = new Mock <IAgentCertificateManager>(); _taskServer = new Mock <ITaskServer>(); _stepRunner = new Mock <IStepsRunner>(); _logger = new Mock <IPagingLogger>(); _temp = new Mock <ITempDirectoryManager>(); _diagnosticLogManager = new Mock <IDiagnosticLogManager>(); if (_tokenSource != null) { _tokenSource.Dispose(); _tokenSource = null; } _tokenSource = new CancellationTokenSource(); var expressionManager = new ExpressionManager(); expressionManager.Initialize(hc); hc.SetSingleton <IExpressionManager>(expressionManager); _jobRunner = new JobRunner(); _jobRunner.Initialize(hc); TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = new Timeline(Guid.NewGuid()); JobEnvironment environment = new JobEnvironment(); environment.Variables[Constants.Variables.System.Culture] = "en-US"; environment.SystemConnection = new ServiceEndpoint() { Url = new Uri("https://test.visualstudio.com"), Authorization = new EndpointAuthorization() { Scheme = "Test", } }; environment.SystemConnection.Authorization.Parameters["AccessToken"] = "token"; List <TaskInstance> tasks = new List <TaskInstance>(); Guid JobId = Guid.NewGuid(); _message = new AgentJobRequestMessage(plan, timeline, JobId, testName, testName, environment, tasks); _extensions.Setup(x => x.GetExtensions <IJobExtension>()). Returns(new[] { _jobExtension.Object }.ToList()); _initResult.PreJobSteps.Clear(); _initResult.JobSteps.Clear(); _initResult.PostJobStep.Clear(); _jobExtension.Setup(x => x.InitializeJob(It.IsAny <IExecutionContext>(), It.IsAny <AgentJobRequestMessage>())). Returns(Task.FromResult(_initResult)); _jobExtension.Setup(x => x.HostType) .Returns <string>(null); _proxyConfig.Setup(x => x.ProxyAddress) .Returns(string.Empty); var settings = new AgentSettings { AgentId = 1, AgentName = "agent1", ServerUrl = "https://test.visualstudio.com", WorkFolder = "_work", }; _config.Setup(x => x.GetSettings()) .Returns(settings); _logger.Setup(x => x.Setup(It.IsAny <Guid>(), It.IsAny <Guid>())); hc.SetSingleton(_config.Object); hc.SetSingleton(_jobServer.Object); hc.SetSingleton(_jobServerQueue.Object); hc.SetSingleton(_proxyConfig.Object); hc.SetSingleton(_cert.Object); hc.SetSingleton(_taskServer.Object); hc.SetSingleton(_stepRunner.Object); hc.SetSingleton(_extensions.Object); hc.SetSingleton(_temp.Object); hc.SetSingleton(_diagnosticLogManager.Object); hc.EnqueueInstance <IExecutionContext>(_jobEc); hc.EnqueueInstance <IPagingLogger>(_logger.Object); return(hc); }
private TestHostContext CreateTestContext([CallerMemberName] String testName = "") { var hc = new TestHostContext(this, testName); _jobEc = new Runner.Worker.ExecutionContext(); _config = new Mock <IConfigurationStore>(); _extensions = new Mock <IExtensionManager>(); _jobExtension = new Mock <IJobExtension>(); _jobServer = new Mock <IJobServer>(); _jobServerQueue = new Mock <IJobServerQueue>(); _stepRunner = new Mock <IStepsRunner>(); _logger = new Mock <IPagingLogger>(); _temp = new Mock <ITempDirectoryManager>(); _diagnosticLogManager = new Mock <IDiagnosticLogManager>(); if (_tokenSource != null) { _tokenSource.Dispose(); _tokenSource = null; } _tokenSource = new CancellationTokenSource(); var expressionManager = new ExpressionManager(); expressionManager.Initialize(hc); hc.SetSingleton <IExpressionManager>(expressionManager); _jobRunner = new JobRunner(); _jobRunner.Initialize(hc); TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); TimelineReference timeline = new Timeline(Guid.NewGuid()); Guid jobId = Guid.NewGuid(); _message = new Pipelines.AgentJobRequestMessage(plan, timeline, jobId, testName, testName, null, null, null, new Dictionary <string, VariableValue>(), new List <MaskHint>(), new Pipelines.JobResources(), new Pipelines.ContextData.DictionaryContextData(), new Pipelines.WorkspaceOptions(), new List <Pipelines.ActionStep>(), null, null); _message.Variables[Constants.Variables.System.Culture] = "en-US"; _message.Resources.Endpoints.Add(new ServiceEndpoint() { Name = WellKnownServiceEndpointNames.SystemVssConnection, Url = new Uri("https://pipelines.actions.githubusercontent.com"), Authorization = new EndpointAuthorization() { Scheme = "Test", Parameters = { { "AccessToken", "token" } } }, }); _message.Resources.Repositories.Add(new Pipelines.RepositoryResource() { Alias = Pipelines.PipelineConstants.SelfAlias, Id = "github", Version = "sha1" }); _message.ContextData.Add("github", new Pipelines.ContextData.DictionaryContextData()); _initResult.Clear(); _jobExtension.Setup(x => x.InitializeJob(It.IsAny <IExecutionContext>(), It.IsAny <Pipelines.AgentJobRequestMessage>())). Returns(Task.FromResult(_initResult)); var settings = new RunnerSettings { AgentId = 1, AgentName = "agent1", ServerUrl = "https://pipelines.actions.githubusercontent.com", WorkFolder = "_work", }; _config.Setup(x => x.GetSettings()) .Returns(settings); _logger.Setup(x => x.Setup(It.IsAny <Guid>(), It.IsAny <Guid>())); hc.SetSingleton(_config.Object); hc.SetSingleton(_jobServer.Object); hc.SetSingleton(_jobServerQueue.Object); hc.SetSingleton(_stepRunner.Object); hc.SetSingleton(_extensions.Object); hc.SetSingleton(_temp.Object); hc.SetSingleton(_diagnosticLogManager.Object); hc.EnqueueInstance <IExecutionContext>(_jobEc); hc.EnqueueInstance <IPagingLogger>(_logger.Object); hc.EnqueueInstance <IJobExtension>(_jobExtension.Object); return(hc); }
public void Aggressive() { var observedToRun = 0; var rand = new Random(2018_11_27); var _ = new GameState(); // more threads than tasks using (var runner = new JobRunner(_, 16, 100)) { runner.Initialize(); Stress(runner); } // fewer threads than tasks using (var runner = new JobRunner(_, 4, 100)) { runner.Initialize(); Stress(runner); } void Stress(JobRunner runner) { var jobs = Enumerable.Range(0, 100).Select(r => runner.CreateJob(TestTask, r)).ToArray(); observedToRun = 0; var expectedToRun = 0; for (var i = 0; i < 10_000; i++) { var numJobs = rand.Next(8) + 1; var subset = jobs.OrderBy(o => Guid.NewGuid()).Take(numJobs).ToArray(); JobsCompletionToken tok; switch (numJobs) { case 1: tok = runner.StartJobs(subset[0]); break; case 2: tok = runner.StartJobs(subset[0], subset[1]); break; case 3: tok = runner.StartJobs(subset[0], subset[1], subset[2]); break; case 4: tok = runner.StartJobs(subset[0], subset[1], subset[2], subset[3]); break; case 5: tok = runner.StartJobs(subset[0], subset[1], subset[2], subset[3], subset[4]); break; case 6: tok = runner.StartJobs(subset[0], subset[1], subset[2], subset[3], subset[4], subset[5]); break; case 7: tok = runner.StartJobs(subset[0], subset[1], subset[2], subset[3], subset[4], subset[5], subset[6]); break; case 8: tok = runner.StartJobs(subset[0], subset[1], subset[2], subset[3], subset[4], subset[5], subset[6], subset[7]); break; default: throw new Exception("wuh?"); } expectedToRun += numJobs; tok.WaitForCompletion(); } Assert.Equal(expectedToRun, observedToRun); } // Just does a bunch of junk void TestTask(GameState ignore, int ix) { var sum = 0; for (var i = 0; i < 10_000; i++) { sum += i; } Interlocked.Increment(ref observedToRun); } }
public void InParallel() { var startTicks = new long[4]; var stopTicks = new long[4]; var onThread = new int[4]; var sums = new int[4]; var _ = new GameState(); using (var runner = new JobRunner(_, 4, 10)) { runner.Initialize(); // setup the jobs Job <int> j1, j2, j3, j4; { j1 = runner.CreateJob(TestTask, 0); j2 = runner.CreateJob(TestTask, 1); j3 = runner.CreateJob(TestTask, 2); j4 = runner.CreateJob(TestTask, 3); } // wait for them to finish var comp = runner.StartJobs(j1, j2, j3, j4); comp.WaitForCompletion(); // no goofy time travel for (var i = 0; i < 4; i++) { Assert.True(stopTicks[i] > startTicks[i]); } // ran on different thread Assert.False(onThread[0] == onThread[1]); Assert.False(onThread[0] == onThread[2]); Assert.False(onThread[0] == onThread[3]); Assert.False(onThread[1] == onThread[2]); Assert.False(onThread[1] == onThread[3]); Assert.False(onThread[2] == onThread[3]); // got the right answer Assert.Equal(49_995_000, sums[0]); Assert.Equal(49_995_000, sums[1]); Assert.Equal(49_995_000, sums[2]); Assert.Equal(49_995_000, sums[3]); } // just a dumb task to do void TestTask(GameState ignore, int ix) { startTicks[ix] = Stopwatch.GetTimestamp(); onThread[ix] = Thread.CurrentThread.ManagedThreadId; Thread.Sleep(10); for (var i = 0; i < 10_000; i++) { sums[ix] += i; } stopTicks[ix] = Stopwatch.GetTimestamp(); } }