public void Bug1996130_JobTaskVerbsFailAfterDoubleRefresh()
        {
            void test()
            {
                using BatchClient batchCli = TestUtilities.OpenBatchClient(TestUtilities.GetCredentialsFromEnvironment());
                string jobId = "Bug1996130Job-" + TestUtilities.GetMyName();

                try
                {
                    // get a job/task to test. use workflow
                    CloudJob boundJob = null;
                    {
                        // need a bound job/task for the tests so set one up
                        CloudJob tsh = batchCli.JobOperations.CreateJob(jobId, new PoolInformation());
                        tsh.PoolInformation.PoolId = poolFixture.PoolId;
                        tsh.Commit();

                        boundJob = batchCli.JobOperations.GetJob(jobId);

                        boundJob.AddTask(new CloudTask("Bug1996130_task", "cmd /c hostname"));
                    }

                    // test task double refresh
                    {
                        // get the task
                        CloudTask boundTask = batchCli.JobOperations.ListTasks(jobId).First();

                        // double refresh
                        boundTask.Refresh();
                        boundTask.Refresh(); // this branch of the bug actually fixed in the other doublerefesh checkin by matthchr

                        // do verbs
                        boundTask.Refresh();
                        boundTask.Delete();

                        Thread.Sleep(5000);  // give server time to do its deed

                        List <CloudTask> tasks = batchCli.JobOperations.ListTasks(jobId).ToList();

                        // confirm delete suceeded
                        Assert.Empty(tasks);
                    }

                    // test job double refresh and verbs
                    {
                        boundJob = batchCli.JobOperations.GetJob(jobId);

                        // double refresh to taint the instance... lost path variable
                        boundJob.Refresh();
                        boundJob.Refresh();  // this used to fail/throw

                        boundJob.Refresh();  // this should fail but does not
                        boundJob.Delete();   // yet another verb that suceeds

                        CloudJob job = batchCli.JobOperations.ListJobs().ToList().FirstOrDefault(j => j.Id == jobId);

                        // confirm job delete suceeded
                        Assert.True(job == null || (JobState.Deleting == job.State));
                    }
                }
                finally
                {
                    TestUtilities.DeleteJobIfExistsAsync(batchCli, jobId).Wait();
                }
            }

            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);
        }