def run_client(fail, port):
     mc = McrouterClient(port)
     mc.connect()
     for i in range(1000):
         s = mc.stats()
         if not s:
             fail[0] = True
             return
 def run_client(fail, port):
     mc = McrouterClient(port)
     mc.connect()
     for i in range(1000):
         s = mc.stats()
         if not s:
             fail[0] = True
             return
    def test_mcrouter_states(self):
        mcr = self.get_mcrouter()
        self.assertTrue(mcr.is_alive())

        # another client
        c2 = McrouterClient(mcr.port)
        c2.connect()

        def check_all_up():
            self.assertTrue(mcr.set('key', 'value'))
            self.assertTrue(c2.set('key', 'value'))
            self.assertEqual(mcr.get('key'), 'value')
            self.assertEqual(c2.get('key'), 'value')
            stat = mcr.stats()
            self.assertEqual(stat['num_servers'], '2')
            self.assertEqual(stat['num_servers_up'], '2')
            self.assertEqual(stat['num_servers_down'], '0')

        def check_invariant():
            stat = mcr.stats()
            self.assertEqual(
                int(stat['num_servers']),
                int(stat['num_servers_new']) + int(stat['num_servers_up']) +
                int(stat['num_servers_down']) +
                int(stat['num_servers_closed']))

        check_invariant()
        check_all_up()

        # down aka hard tko
        self.mc.terminate()
        self.assertEqual(mcr.get('key'), None)
        self.assertEqual(c2.get('key'), None)
        stat = mcr.stats()
        self.assertEqual(stat['num_servers_up'], '0')
        self.assertEqual(stat['num_servers_down'], '2')
        check_invariant()

        # up again
        self.mc = Memcached(self.mc.port)
        # wait for unmarking tko
        time.sleep(1)
        check_invariant()
        check_all_up()

        # change config
        mcr.change_config(self.config2)
        # wait for mcrouter to reconfigure
        time.sleep(2)
        check_invariant()

        # make sure we dont crash
        self.assertTrue(mcr.is_alive())
    def test_mcrouter_states(self):
        mcr = self.get_mcrouter()
        self.assertTrue(mcr.is_alive())

        # another client
        c2 = McrouterClient(mcr.port)
        c2.connect()

        def check_all_up():
            self.assertTrue(mcr.set('key', 'value'))
            self.assertTrue(c2.set('key', 'value'))
            self.assertEqual(mcr.get('key'), 'value')
            self.assertEqual(c2.get('key'), 'value')
            stat = mcr.stats()
            self.assertEqual(stat['num_servers'], '2')
            self.assertEqual(stat['num_servers_up'], '2')
            self.assertEqual(stat['num_servers_down'], '0')

        def check_invariant():
            stat = mcr.stats()
            self.assertEqual(int(stat['num_servers']),
                             int(stat['num_servers_new'])
                             + int(stat['num_servers_up'])
                             + int(stat['num_servers_down'])
                             + int(stat['num_servers_closed']))

        check_invariant()
        check_all_up()

        # down aka hard tko
        self.mc.terminate()
        self.assertEqual(mcr.get('key'), None)
        self.assertEqual(c2.get('key'), None)
        stat = mcr.stats()
        self.assertEqual(stat['num_servers_up'], '0')
        self.assertEqual(stat['num_servers_down'], '2')
        check_invariant()

        # up again
        self.mc = Memcached(self.mc.port)
        # wait for unmarking tko
        time.sleep(1)
        check_invariant()
        check_all_up()

        # change config
        mcr.change_config(self.config2)
        # wait for mcrouter to reconfigure
        time.sleep(2)
        check_invariant()

        # make sure we dont crash
        self.assertTrue(mcr.is_alive())