Пример #1
0
 def test_key_bytearray(self):
     # 15 letters long to hit 'if extrabytes == 3:'
     key = bytearray('lasquinceletras')
     expected = 4030895744 % 100000  # Int is from java murmur2
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)
Пример #2
0
 def test_key_bytearray(self):
     # 15 letters long to hit 'if extrabytes == 3:'
     key = bytearray('lasquinceletras')
     expected = (4030895744 & 0x7FFFFFFF) % 100000  # Int is from murmur2
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)
Пример #3
0
 def test_key_match_java(self):
     key = 'cc54d7f5-8508-4302-bc23-c5d16cfb50fd'
     key = key.decode(encoding='UTF-8', errors='strict')
     parts = xrange(10)
     expected = 4
     p = HashedPartitioner(self.T1, parts)
     part = p.partition(key, parts)
     self.assertEqual(expected, part)
Пример #4
0
 def test_key_str(self):
     """
     Bytestring keys are hashed directly.
     """
     key = b'The rain in Spain falls mainly on the plain.'
     expected = (2823782121 & 0x7FFFFFFF) % 100000  # Int is from murmur2
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)
Пример #5
0
 def test_key_large(self):
     """
     The hashed partitioner does not limit key length.
     """
     key = u''.join([u"Key:{} ".format(i) for i in range(4096)]).encode('ascii')
     expected = 1765856722 % 100000  # Int is from online hash tool
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)
Пример #6
0
 def test_key_str(self):
     """
     Bytestring keys are hashed directly.
     """
     key = b'The rain in Spain falls mainly on the plain.'
     expected = (2823782121 & 0x7FFFFFFF) % 100000  # Int is from murmur2
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)
Пример #7
0
 def test_key_bytearray(self):
     """
     :class:`bytearray` keys are hashed directly.
     """
     # 15 letters long to hit 'if extrabytes == 3:'
     key = bytearray(b'lasquinceletras')
     expected = (4030895744 & 0x7FFFFFFF) % 100000  # Int is from murmur2
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)
Пример #8
0
 def test_key_large(self):
     """
     The hashed partitioner does not limit key length.
     """
     key = u''.join([u"Key:{} ".format(i)
                     for i in range(4096)]).encode('ascii')
     expected = 1765856722 % 100000  # Int is from online hash tool
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)
Пример #9
0
 def test_key_unicode(self):
     """
     Text string keys are converted to UTF-8 and hashed to match the Java
     implementation.
     """
     key = u'슬듢芬'
     expected = (3978338664 & 0x7FFFFFFF) % 100000  # Int is from murmur2
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)
Пример #10
0
 def test_key_unicode(self):
     """
     Text string keys are converted to UTF-8 and hashed to match the Java
     implementation.
     """
     key = u'슬듢芬'
     expected = (3978338664 & 0x7FFFFFFF) % 100000  # Int is from murmur2
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)
Пример #11
0
 def test_key_match_java(self):
     """
     Hashing a UUID produces the same result the Java implementation would.
     """
     key = b'cc54d7f5-8508-4302-bc23-c5d16cfb50fd'
     key = key.decode(encoding='UTF-8', errors='strict')
     parts = range(10)
     expected = 4
     p = HashedPartitioner(self.T1, parts)
     part = p.partition(key, parts)
     self.assertEqual(expected, part)
Пример #12
0
 def test_key_match_java(self):
     """
     Hashing a UUID produces the same result the Java implementation would.
     """
     key = b'cc54d7f5-8508-4302-bc23-c5d16cfb50fd'
     key = key.decode(encoding='UTF-8', errors='strict')
     parts = range(10)
     expected = 4
     p = HashedPartitioner(self.T1, parts)
     part = p.partition(key, parts)
     self.assertEqual(expected, part)
Пример #13
0
 def test_key_integer(self):
     """
     Integer keys produce a TypeError as passing an integer likely
     represents a programming error.
     """
     key = 123456789
     p = HashedPartitioner(self.T1, self.parts)
     self.assertRaises(TypeError, p.partition, key, self.parts)
Пример #14
0
 def test_key_none(self):
     """
     A key of None results in a TypeError as it doesn't make sense to
     produce unkeyed messages with a hashed partitioner.
     """
     key = None
     p = HashedPartitioner(self.T1, self.parts)
     self.assertRaises(TypeError, p.partition, key, self.parts)
Пример #15
0
    def test_partition_distribution(self):
        parts = [1, 2, 3, 4, 5]
        p = HashedPartitioner(self.T1, parts)

        # Make sure we have decent distribution
        keycount = 10000
        key_list = []
        part_keycount = defaultdict(lambda: 0)
        key_to_part = {}
        for i in xrange(keycount):
            key = random_string(16)
            key_list.append(key)
            part = p.partition(key, parts)
            part_keycount[part] += 1
            key_to_part[key] = part

        self.assertLess(std(part_keycount.values()), keycount / 100)

        # Ensure we get the same partition for the same key
        for key in key_to_part.keys():
            part = p.partition(key, parts)
            self.assertEqual(part, key_to_part[key])
Пример #16
0
    def test_partition_distribution(self):
        parts = [1, 2, 3, 4, 5]
        p = HashedPartitioner(self.T1, parts)

        # Make sure we have decent distribution
        keycount = 10000
        key_list = []
        part_keycount = defaultdict(lambda: 0)
        key_to_part = {}
        for _i in range(keycount):
            key = random_string(16)
            key_list.append(key)
            part = p.partition(key, parts)
            part_keycount[part] += 1
            key_to_part[key] = part

        self.assertLess(std(part_keycount.values()), keycount / 100)

        # Ensure we get the same partition for the same key
        for key in key_to_part.keys():
            part = p.partition(key, parts)
            self.assertEqual(part, key_to_part[key])
Пример #17
0
 def test_key_none(self):
     key = None
     expected = 275646681 % 100000  # Int is from java murmur2
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)
Пример #18
0
 def test_key_unicode(self):
     key = u'슬듢芬'
     expected = 3978338664 % 100000  # Int is from java murmur2
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)
Пример #19
0
 def test_key_large(self):
     key = ''.join(["Key:{} ".format(i) for i in xrange(4096)])
     expected = 1765856722 % 100000  # Int is from online hash tool
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)
Пример #20
0
 def test_key_integer(self):
     key = 123456789
     expected = 2472730214 % 100000  # Int is from java murmur2
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)
Пример #21
0
 def test_key_integer(self):
     key = 123456789
     expected = (2472730214 & 0x7FFFFFFF) % 100000  # Int is from murmur2
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)
Пример #22
0
 def test_key_unicode(self):
     key = u'슬듢芬'
     expected = (3978338664 & 0x7FFFFFFF) % 100000  # Int is from murmur2
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)
Пример #23
0
 def test_key_none(self):
     key = None
     expected = 275646681 % 100000  # Int is from java murmur2
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)
Пример #24
0
 def test_key_large(self):
     key = ''.join(["Key:{} ".format(i) for i in xrange(4096)])
     expected = 1765856722 % 100000  # Int is from online hash tool
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)
Пример #25
0
 def test_key_str(self):
     key = 'The rain in Spain falls mainly on the plain.'
     expected = 2823782121 % 100000  # Int is from java murmur2
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)
Пример #26
0
 def test_key_str(self):
     key = 'The rain in Spain falls mainly on the plain.'
     expected = 2823782121 % 100000  # Int is from java murmur2
     p = HashedPartitioner(self.T1, self.parts)
     part = p.partition(key, self.parts)
     self.assertEqual(expected, part)