Ejemplo n.º 1
0
    def testMapping(self):
        """Check that the mapping is valid."""
        m = master.DataMaster(7000, self.mock_service)
        self.assertNotEqual(m, None)
        server1 = m.RegisterServer("127.0.0.1", 7001)
        server2 = m.RegisterServer("127.0.0.1", 7002)
        server3 = m.RegisterServer("127.0.0.1", 7003)
        self.assertTrue(m.AllRegistered())
        mapping = m.LoadMapping()
        self.assertNotEqual(mapping, None)
        self.assertEqual(mapping.num_servers, 4)
        self.assertEqual(len(mapping.servers), 4)

        # Check server information.
        self.assertEqual(mapping.servers[0].address, "127.0.0.1")
        self.assertEqual(mapping.servers[0].port, 7000)
        self.assertEqual(mapping.servers[0].index, 0)
        for idx, server in [(1, server1), (2, server2), (3, server3)]:
            self.assertEqual(mapping.servers[idx].address, server.Address())
            self.assertEqual(mapping.servers[idx].port, server.Port())
            self.assertEqual(mapping.servers[idx].index, server.Index())
            self.assertTrue(server.IsRegistered())

        # Check intervals.
        interval1 = server1.Interval()
        interval2 = server2.Interval()
        interval3 = server3.Interval()

        self.assertEqual(interval1.end, interval2.start)
        self.assertEqual(interval2.end, interval3.start)
        self.assertEqual(interval1.end - interval1.end,
                         interval2.end - interval2.end)
        self.assertEqual(interval1.end - interval1.end,
                         interval3.end - interval3.end)
        self.assertEqual(interval3.end, constants.MAX_RANGE)

        # Check that mapping to a server works.
        self.assertEqual(utils._FindServerInMapping(mapping, 0x0), 0)
        self.assertEqual(
            utils._FindServerInMapping(mapping, constants.MAX_RANGE / 4), 1)
        self.assertEqual(
            utils._FindServerInMapping(mapping, constants.MAX_RANGE / 4 + 1),
            1)
        self.assertEqual(
            utils._FindServerInMapping(mapping, constants.MAX_RANGE / 2), 2)
        half_fifth = constants.MAX_RANGE / 2 + constants.MAX_RANGE / 5
        self.assertEqual(utils._FindServerInMapping(mapping, half_fifth), 2)
        self.assertEqual(
            utils._FindServerInMapping(mapping, constants.MAX_RANGE / 4 * 3),
            3)
        self.assertEqual(
            utils._FindServerInMapping(mapping, constants.MAX_RANGE), 3)
Ejemplo n.º 2
0
  def testMapping(self):
    """Check that the mapping is valid."""
    m = master.DataMaster(7000, self.mock_service)
    self.assertNotEqual(m, None)
    server1 = m.RegisterServer("127.0.0.1", 7001)
    server2 = m.RegisterServer("127.0.0.1", 7002)
    server3 = m.RegisterServer("127.0.0.1", 3000)
    self.assertTrue(m.AllRegistered())
    mapping = m.LoadMapping()
    self.assertNotEqual(mapping, None)
    self.assertEqual(mapping.num_servers, 4)
    self.assertEqual(len(mapping.servers), 4)

    # Check server information.
    self.assertEqual(mapping.servers[0].address, "127.0.0.1")
    self.assertEqual(mapping.servers[0].port, 7000)
    self.assertEqual(mapping.servers[0].index, 0)
    for idx, server in [(1, server1), (2, server2), (3, server3)]:
      self.assertEqual(mapping.servers[idx].address, server.Address())
      self.assertEqual(mapping.servers[idx].port, server.Port())
      self.assertEqual(mapping.servers[idx].index, server.Index())
      self.assertTrue(server.IsRegistered())

    # Check intervals.
    interval1 = server1.Interval()
    interval2 = server2.Interval()
    interval3 = server3.Interval()

    self.assertEqual(interval1.end, interval2.start)
    self.assertEqual(interval2.end, interval3.start)
    self.assertEqual(interval1.end - interval1.end,
                     interval2.end - interval2.end)
    self.assertEqual(interval1.end - interval1.end,
                     interval3.end - interval3.end)
    self.assertEqual(interval3.end, constants.MAX_RANGE)

    # Check that mapping to a server works.
    self.assertEqual(utils._FindServerInMapping(mapping, 0x0), 0)
    self.assertEqual(utils._FindServerInMapping(mapping,
                                                constants.MAX_RANGE / 4), 1)
    self.assertEqual(utils._FindServerInMapping(mapping,
                                                constants.MAX_RANGE / 4 + 1), 1)
    self.assertEqual(utils._FindServerInMapping(mapping,
                                                constants.MAX_RANGE / 2), 2)
    half_fifth = constants.MAX_RANGE / 2 + constants.MAX_RANGE / 5
    self.assertEqual(utils._FindServerInMapping(mapping, half_fifth), 2)
    self.assertEqual(utils._FindServerInMapping(mapping,
                                                constants.MAX_RANGE / 4 * 3), 3)
    self.assertEqual(utils._FindServerInMapping(mapping,
                                                constants.MAX_RANGE), 3)