'Gracefully handle WMI query timeouts.'

def test_wmi_sampler_timeout(self):

    from checks.libs.wmi.sampler import WMISampler
    logger = Mock()
    wmi_sampler = WMISampler(logger, 'Win32_PerfFormattedData_PerfDisk_LogicalDisk', ['AvgDiskBytesPerWrite', 'FreeMegabytes'], timeout_duration=0.1)
    SWbemServices._exec_query_run_time = 0.11
    self.assertRaises(TimeoutException, wmi_sampler.sample)
    self.assertTrue(wmi_sampler._sampling)
    self.assertTrue(logger.debug.called)
    self.assertRaises(TypeError, (lambda : len(wmi_sampler)))
    self.assertRaises(TypeError, (lambda : sum((1 for _ in wmi_sampler))))
    wmi_sampler.sample()
    self.assertFalse(wmi_sampler._sampling)
    self.assertEquals(SWbemServices.ExecQuery.call_count, 1, SWbemServices.ExecQuery.call_count)
    self.assertEquals(len(wmi_sampler), 2)
    self.assertEquals(sum((1 for _ in wmi_sampler)), 2)
