public async Task MachinesClient_WaitFor_Stops_After_MaxWait()
        {
            var i          = 0;
            var j          = 0;
            var connection = new Mock <IConnection>();

            connection.Setup(c => c.Get <Machine>(ApiUrls.MachinesShow("psxs9sp8u"), null))
            .ReturnsAsync(() =>
            {
                var json     = System.IO.File.ReadAllText("./Fixtures/Machines_GetMachine_Start_InProgress.json");
                var result   = JsonConvert.DeserializeObject <Machine>(json);
                result.State = MachineState.Provisioning;
                Thread.Sleep(101);
                i++;
                return(result);
            });

            var     machinesClient = new MachinesClient(connection.Object);
            Machine machine        = null;
            await Assert.ThrowsExceptionAsync <TimeoutException>(async() =>
            {
                await machinesClient.Waitfor("psxs9sp8u", MachineState.Ready, pollIntervalMS: 10, maxWaitMS: 200, (m) => {
                    machine = m;
                    j++;
                });
            });

            Assert.AreEqual(MachineState.Provisioning, machine.State);
            Assert.AreEqual(2, i);
            Assert.AreEqual(2, j);
        }
 public async Task MachinesClient_WaitFor_Throws_If_Invalid_Target_State()
 {
     var connection     = new Mock <IConnection>();
     var machinesClient = new MachinesClient(connection.Object);
     await Assert.ThrowsExceptionAsync <ArgumentOutOfRangeException>(async() =>
     {
         await machinesClient.Waitfor("psxs9sp8u", MachineState.Restarting);
     });
 }
        public async Task MachinesClient_WaitFor_HappyPath()
        {
            var i          = 0;
            var j          = 0;
            var connection = new Mock <IConnection>();

            connection.Setup(c => c.Get <Machine>(ApiUrls.MachinesShow("psxs9sp8u"), null))
            .ReturnsAsync(() =>
            {
                var json   = System.IO.File.ReadAllText("./Fixtures/Machines_GetMachine_Start_InProgress.json");
                var result = JsonConvert.DeserializeObject <Machine>(json);

                if (i >= 0 && i < 4)
                {
                    result.State = MachineState.Provisioning;
                }
                else if (i >= 4 && i < 9)
                {
                    result.State = MachineState.ServiceReady;
                }
                else if (i >= 9)
                {
                    result.State = MachineState.Ready;
                }

                i++;
                return(result);
            });

            var machinesClient = new MachinesClient(connection.Object);
            var machine        = await machinesClient.Waitfor("psxs9sp8u", MachineState.Ready, pollIntervalMS : 1, maxWaitMS : 0, (m) => {
                j++;
            });

            Assert.AreEqual(MachineState.Ready, machine.State);
            Assert.AreEqual(10, i);
            Assert.AreEqual(10, j);
        }