public void Bug1433069TestBoundJobCommit()
        {
            void test()
            {
                using BatchClient batchCli = TestUtilities.OpenBatchClient(TestUtilities.GetCredentialsFromEnvironment());
                string jobId = Constants.DefaultConveniencePrefix + TestUtilities.GetMyName() + "-TestBoundJobCommit";

                try
                {
                    //
                    // Create the job
                    //
                    CloudJob cloudJob = batchCli.JobOperations.CreateJob(jobId, new PoolInformation());
                    cloudJob.PoolInformation = new PoolInformation()
                    {
                        PoolId = poolFixture.PoolId
                    };

                    testOutputHelper.WriteLine("Initial job schedule commit()");
                    cloudJob.Commit();

                    //Get the job
                    CloudJob refreshableJob = batchCli.JobOperations.GetJob(jobId);

                    //Update the bound job priority
                    const int          newJobPriority        = 5;
                    OnAllTasksComplete newOnAllTasksComplete = OnAllTasksComplete.NoAction;

                    testOutputHelper.WriteLine("Job priority is: {0}", refreshableJob.Priority);
                    refreshableJob.Priority           = newJobPriority;
                    refreshableJob.OnAllTasksComplete = newOnAllTasksComplete;
                    refreshableJob.Commit();

                    AssertJobCorrectness(batchCli.JobOperations, jobId, ref refreshableJob, poolFixture.PoolId, newJobPriority, null);

                    //Update the bound job pool name
                    //Must disable the job first before updating its pool
                    refreshableJob.Disable(DisableJobOption.Terminate);

                    //Wait for job to reach disabled state (could go to Disabling for a bit)
                    //TODO: Use a uBtilities wait helper here
                    DateTime jobDisabledStateWaitStartTime = DateTime.UtcNow;
                    TimeSpan jobDisabledTimeout            = TimeSpan.FromSeconds(120);
                    while (refreshableJob.State != JobState.Disabled)
                    {
                        testOutputHelper.WriteLine("Bug1433069TestBoundJobCommit: sleeping for (refreshableJob.State != JobState.Disabled)");
                        Thread.Sleep(TimeSpan.FromSeconds(10));
                        refreshableJob = batchCli.JobOperations.GetJob(jobId);

                        if (DateTime.UtcNow > jobDisabledStateWaitStartTime.Add(jobDisabledTimeout))
                        {
                            Assert.False(true, "Timed out waiting for job to go to disabled state");
                        }
                    }

                    const string newPoolId = "testPool";
                    refreshableJob.PoolInformation.PoolId = newPoolId;
                    refreshableJob.Commit();

                    AssertJobCorrectness(batchCli.JobOperations, jobId, ref refreshableJob, newPoolId, newJobPriority, null);

                    //Enable the job again
                    refreshableJob.Enable();

                    //Update the bound job constraints
                    JobConstraints newJobConstraints = new JobConstraints(TimeSpan.FromSeconds(200), 19);
                    refreshableJob.Constraints = newJobConstraints;
                    refreshableJob.Commit();

                    AssertJobCorrectness(batchCli.JobOperations, jobId, ref refreshableJob, newPoolId, newJobPriority, newJobConstraints);
                }
                finally
                {
                    batchCli.JobOperations.DeleteJob(jobId);
                }
            }

            SynchronizationContextHelper.RunTest(test, TestTimeout);
        }
        public void TestBoundJobVerbs()
        {
            void test()
            {
                using BatchClient batchCli = TestUtilities.OpenBatchClient(TestUtilities.GetCredentialsFromEnvironment());
                //Create a job

                string jobId = Constants.DefaultConveniencePrefix + TestUtilities.GetMyName() + "-TestBoundJobVerbs";

                try
                {
                    CloudJob cloudJob = batchCli.JobOperations.CreateJob(jobId, new PoolInformation());
                    cloudJob.PoolInformation = new PoolInformation()
                    {
                        PoolId = poolFixture.PoolId
                    };
                    cloudJob.Commit();

                    //Get the bound job
                    CloudJob job = batchCli.JobOperations.GetJob(jobId);

                    //Disable the job (via instance)
                    job.Disable(DisableJobOption.Terminate);

                    //Check the job state

                    CloudJob disabledJob = batchCli.JobOperations.GetJob(jobId);
                    testOutputHelper.WriteLine("DisabledJob State: {0}", disabledJob.State);
                    Assert.True(disabledJob.State == JobState.Disabled || disabledJob.State == JobState.Disabling);

                    //Enable the job (via instance)
                    job.Enable();

                    //Check the job state
                    CloudJob enabledJob = batchCli.JobOperations.GetJob(jobId);
                    testOutputHelper.WriteLine("EnabledJob state: {0}", enabledJob.State);
                    Assert.Equal(JobState.Active, JobState.Active);

                    //Disable the job (via operations)
                    batchCli.JobOperations.DisableJob(jobId, DisableJobOption.Terminate);

                    disabledJob = batchCli.JobOperations.GetJob(jobId);
                    testOutputHelper.WriteLine("DisabledJob State: {0}", disabledJob.State);
                    Assert.True(disabledJob.State == JobState.Disabled || disabledJob.State == JobState.Disabling);

                    //Enable the job (via operations)
                    batchCli.JobOperations.EnableJob(jobId);

                    //Check the job state
                    enabledJob = batchCli.JobOperations.GetJob(jobId);
                    testOutputHelper.WriteLine("EnabledJob state: {0}", enabledJob.State);
                    Assert.Equal(JobState.Active, JobState.Active);

                    //Terminate the job
                    job.Terminate("need some reason");

                    //Check the job state
                    CloudJob terminatedJob = batchCli.JobOperations.GetJob(jobId);
                    testOutputHelper.WriteLine("TerminatedJob state: {0}", terminatedJob.State);
                    Assert.True(terminatedJob.State == JobState.Terminating || terminatedJob.State == JobState.Completed);

                    if (terminatedJob.State == JobState.Terminating)
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(5)); //Sleep and wait for the job to finish terminating before we issue a delete
                    }

                    //Delete the job
                    job.Delete();

                    //Check that the job doesn't exist anymore

                    try
                    {
                        testOutputHelper.WriteLine("Expected Exception: testing that job does NOT exist.");

                        CloudJob deletedJob = batchCli.JobOperations.GetJob(jobId);
                        Assert.Equal(JobState.Deleting, deletedJob.State);
                    }
                    catch (Exception e)
                    {
                        Assert.IsAssignableFrom <BatchException>(e);
                        BatchException be = e as BatchException;
                        Assert.NotNull(be.RequestInformation);
                        Assert.NotNull(be.RequestInformation.BatchError);
                        Assert.Equal(BatchErrorCodeStrings.JobNotFound, be.RequestInformation.BatchError.Code);

                        testOutputHelper.WriteLine("Job was deleted successfully");
                    }
                }
                finally
                {
                    TestUtilities.DeleteJobIfExistsAsync(batchCli, jobId).Wait();
                }
            }

            SynchronizationContextHelper.RunTest(test, TestTimeout);
        }