Simpler replica calculation tests

This commit is contained in:
Tyler Hobbs
2013-09-20 12:36:33 -05:00
parent d3fbb116a6
commit b5016bfde6
2 changed files with 25 additions and 20 deletions

View File

@@ -57,25 +57,33 @@ def setup_test_keyspace():
cluster = Cluster() cluster = Cluster()
session = cluster.connect() session = cluster.connect()
ksname = 'test3rf'
cfname = 'test'
try: try:
results = session.execute("SELECT keyspace_name FROM system.schema_keyspaces") results = session.execute("SELECT keyspace_name FROM system.schema_keyspaces")
existing_keyspaces = [row[0] for row in results] existing_keyspaces = [row[0] for row in results]
if ksname in existing_keyspaces: for ksname in ('test1rf', 'test2rf', 'test3rf'):
session.execute("DROP KEYSPACE %s" % ksname) if ksname in existing_keyspaces:
session.execute("DROP KEYSPACE %s" % ksname)
ddl = ''' ddl = '''
CREATE KEYSPACE %s CREATE KEYSPACE test3rf
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'}''' WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '3'}'''
session.execute(ddl % ksname) session.execute(ddl)
ddl = ''' ddl = '''
CREATE TABLE %s.%s ( CREATE KEYSPACE test2rf
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '2'}'''
session.execute(ddl)
ddl = '''
CREATE KEYSPACE test1rf
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}'''
session.execute(ddl)
ddl = '''
CREATE TABLE test3rf.test (
k int PRIMARY KEY, k int PRIMARY KEY,
v int )''' v int )'''
session.execute(ddl % (ksname, cfname)) session.execute(ddl)
finally: finally:
cluster.shutdown() cluster.shutdown()

View File

@@ -3,12 +3,10 @@ try:
except ImportError: except ImportError:
import unittest # noqa import unittest # noqa
import cassandra
from cassandra import AlreadyExists from cassandra import AlreadyExists
from cassandra.cluster import Cluster from cassandra.cluster import Cluster
from cassandra.metadata import KeyspaceMetadata, TableMetadata, Token, MD5Token, TokenMap from cassandra.metadata import KeyspaceMetadata, TableMetadata, Token, MD5Token, TokenMap
from cassandra.metadata import TableMetadata, Token, MD5Token, TokenMap, Murmur3Token
from cassandra.policies import SimpleConvictionPolicy from cassandra.policies import SimpleConvictionPolicy
from cassandra.pool import Host from cassandra.pool import Host
@@ -351,7 +349,6 @@ class TestCodeCoverage(unittest.TestCase):
host = list(cluster.metadata.get_replicas('test3rf', 'key'))[0] host = list(cluster.metadata.get_replicas('test3rf', 'key'))[0]
self.assertEqual(host.datacenter, 'datacenter1') self.assertEqual(host.datacenter, 'datacenter1')
self.assertEqual(host.rack, 'rack1') self.assertEqual(host.rack, 'rack1')
self.assertEqual(host.address, '127.0.0.2')
def test_token_map(self): def test_token_map(self):
""" """
@@ -361,16 +358,16 @@ class TestCodeCoverage(unittest.TestCase):
cluster = Cluster() cluster = Cluster()
cluster.connect('test3rf') cluster.connect('test3rf')
ring = cluster.metadata.token_map.ring ring = cluster.metadata.token_map.ring
owners = list(cluster.metadata.token_map.token_to_host_owner[token] for token in ring)
get_replicas = cluster.metadata.token_map.get_replicas
# BUG: The next line fails for ksname in ('test1rf', 'test2rf', 'test3rf'):
self.assertNotEqual(list(cluster.metadata.token_map.get_replicas('test3rf', ring[0])), []) self.assertNotEqual(list(get_replicas('test3rf', ring[0])), [])
self.assertEqual(list(cluster.metadata.token_map.get_replicas('test3rf', ring[0]))[0].address, '127.0.0.1')
self.assertEqual(list(cluster.metadata.token_map.get_replicas('test3rf', ring[1]))[0].address, '127.0.0.2')
self.assertEqual(list(cluster.metadata.token_map.get_replicas('test3rf', ring[2]))[0].address, '127.0.0.3')
self.assertEqual(list(cluster.metadata.token_map.get_replicas('test3rf', Murmur3Token(ring[0].value - 1)))[0].address, '127.0.0.3') for i, token in enumerate(ring):
self.assertEqual(list(cluster.metadata.token_map.get_replicas('test3rf', Murmur3Token(ring[1].value - 1)))[0].address, '127.0.0.1') self.assertEqual(get_replicas('test3rf', token), set(owners))
self.assertEqual(list(cluster.metadata.token_map.get_replicas('test3rf', Murmur3Token(ring[2].value - 1)))[0].address, '127.0.0.2') self.assertEqual(get_replicas('test2rf', token), set([owners[i], owners[(i + 1) % 3]]))
self.assertEqual(get_replicas('test1rf', token), set([owners[i]]))
class TokenMetadataTest(unittest.TestCase): class TokenMetadataTest(unittest.TestCase):