Exemplo n.º 1
0
    def testAddContact(self):
        """ Tests if the bucket handles contact additions/updates correctly """
        # Test if contacts can be added to empty list
        # Add k contacts to bucket
        for i in range(constants.k):
            tmpContact = contact.Contact('tempContactID%d' % i, str(i), i, i)
            self.kbucket.addContact(tmpContact)
            self.failUnlessEqual(
                self.kbucket._contacts[i], tmpContact,
                "Contact in position %d not the same as the newly-added contact"
                % i)

        # Test if contact is not added to full list
        i += 1
        tmpContact = contact.Contact('tempContactID%d' % i, str(i), i, i)
        self.failUnlessRaises(kbucket.BucketFull, self.kbucket.addContact,
                              tmpContact)

        # Test if an existing contact is updated correctly if added again
        existingContact = self.kbucket._contacts[0]
        self.kbucket.addContact(existingContact)
        self.failUnlessEqual(
            self.kbucket._contacts.index(existingContact),
            len(self.kbucket._contacts) - 1,
            'Contact not correctly updated; it should be at the end of the list of contacts'
        )
Exemplo n.º 2
0
 def setUp(self):
     self.firstContact = contact.Contact('firstContactID', '127.0.0.1',
                                         1000, None, 1)
     self.secondContact = contact.Contact('2ndContactID', '192.168.0.1',
                                          1000, None, 32)
     self.secondContactCopy = contact.Contact('2ndContactID', '192.168.0.1',
                                              1000, None, 32)
     self.firstContactDifferentValues = contact.Contact(
         'firstContactID', '192.168.1.20', 1000, None, 50)
Exemplo n.º 3
0
    def testGetContacts(self):
        # try and get 2 contacts from empty list
        result = self.kbucket.getContacts(2)
        self.failIf(
            len(result) != 0,
            "Returned list should be empty; returned list length: %d" %
            (len(result)))

        # Add k-2 contacts
        if constants.k >= 2:
            for i in range(constants.k - 2):
                tmpContact = contact.Contact(i, i, i, i)
                self.kbucket.addContact(tmpContact)
        else:
            # add k contacts
            for i in range(constants.k):
                tmpContact = contact.Contact(i, i, i, i)
                self.kbucket.addContact(tmpContact)

        # try to get too many contacts
        # requested count greater than bucket size; should return at most k contacts
        contacts = self.kbucket.getContacts(constants.k + 3)
        self.failUnless(
            len(contacts) <= constants.k,
            'Returned list should not have more than k entries!')

        # verify returned contacts in list
        for i in range(constants.k - 2):
            self.failIf(
                self.kbucket._contacts[i].id != i,
                "Contact in position %s not same as added contact" % (str(i)))

        # try to get too many contacts
        # requested count one greater than number of contacts
        if constants.k >= 2:
            result = self.kbucket.getContacts(constants.k - 1)
            self.failIf(
                len(result) != constants.k - 2,
                "Too many contacts in returned list %s - should be %s" %
                (len(result), constants.k - 2))
        else:
            result = self.kbucket.getContacts(constants.k - 1)
            # if the count is <= 0, it should return all of it's contats
            self.failIf(
                len(result) != constants.k,
                "Too many contacts in returned list %s - should be %s" %
                (len(result), constants.k - 2))

            # try to get contacts
            # requested count less than contact number
        if constants.k >= 3:
            result = self.kbucket.getContacts(constants.k - 3)
            self.failIf(
                len(result) != constants.k - 3,
                "Too many contacts in returned list %s - should be %s" %
                (len(result), constants.k - 3))
Exemplo n.º 4
0
    def testRemoveContact(self):
        # try remove contact from empty list
        rmContact = contact.Contact('TestContactID1', '127.0.0.1', 1, 1)
        self.failUnlessRaises(ValueError, self.kbucket.removeContact, rmContact)

        # Add couple contacts
        for i in range(constants.k-2):
            tmpContact = contact.Contact('tmpTestContactID%d' % i, str(i), i, i)
            self.kbucket.addContact(tmpContact)

        # try remove contact from empty list
        self.kbucket.addContact(rmContact)
        result = self.kbucket.removeContact(rmContact)
        self.failIf(rmContact in self.kbucket._contacts, "Could not remove contact from bucket")
Exemplo n.º 5
0
    def testKeyError(self):

        # find middle, so we know where bucket will split
        bucket_middle = self.table._buckets[0].rangeMax / 2

        # fill last bucket
        self.fill_bucket(self.table._buckets[0].rangeMax - constants.k - 1)
        # -1 in previous line because own_id is in last bucket

        # fill/overflow 7 more buckets
        bucket_start = 0
        for i in range(0, constants.k):
            self.overflow_bucket(bucket_start)
            bucket_start += bucket_middle / (2**i)

        # replacement cache now has k-1 entries.
        # adding one more contact to bucket 0 used to cause a KeyError, but it should work
        self.table.addContact(
            contact.Contact(long(constants.k + 2), '127.0.0.1', 9999, None))
Exemplo n.º 6
0
 def overflow_bucket(self, bucket_min):
     bucket_size = constants.k
     self.fill_bucket(bucket_min)
     self.table.addContact(
         contact.Contact(long(bucket_min + bucket_size + 1), '127.0.0.1',
                         9999, None))
Exemplo n.º 7
0
 def fill_bucket(self, bucket_min):
     bucket_size = constants.k
     for i in range(bucket_min, bucket_min + bucket_size):
         self.table.addContact(
             contact.Contact(long(i), '127.0.0.1', 9999, None))