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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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])
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])
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)
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)
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)
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)
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)
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)
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)