'This tests that hunts with a client limit terminate correctly.'

def testHuntTermination(self):

    with test_lib.FakeTime(1000, increment=1e-06):
        with implementation.GRRHunt.StartHunt(hunt_name=standard.GenericHunt.__name__, flow_runner_args=rdf_flows.FlowRunnerArgs(flow_name=transfer.GetFile.__name__), flow_args=transfer.GetFileArgs(pathspec=rdf_paths.PathSpec(path='/tmp/evil.txt', pathtype=rdf_paths.PathSpec.PathType.OS)), client_rule_set=rdf_foreman.ForemanClientRuleSet(rules=[rdf_foreman.ForemanClientRule(rule_type=rdf_foreman.ForemanClientRule.Type.REGEX, regex=rdf_foreman.ForemanRegexClientRule(attribute_name='GRR    client', attribute_regex='GRR'))]), client_limit=5, client_rate=0, expiry_time=rdfvalue.Duration('1000s'), token=self.token) as hunt:
            hunt.Run()
        foreman = aff4.FACTORY.Open('aff4:/foreman', mode='rw', token=self.token)
        for client_id in self.client_ids:
            foreman.AssignTasksToClient(client_id)
        client_mock = hunt_test_lib.SampleHuntMock()
        hunt_test_lib.TestHuntHelper(client_mock, self.client_ids, check_flow_errors=False, token=self.token)
        hunt_obj = aff4.FACTORY.Open(hunt.session_id, age=aff4.ALL_TIMES, token=self.token)
        (started, finished, errors) = hunt_obj.GetClientsCounts()
        self.assertEqual(started, 5)
        self.assertEqual(finished, 5)
        self.assertEqual(errors, 2)
        hunt_obj = aff4.FACTORY.Open(hunt.session_id, age=aff4.ALL_TIMES, token=self.token)
        self.assertEqual(hunt_obj.Get(hunt_obj.Schema.STATE), 'PAUSED')
