Exemple #1
0
def main():

    # Establish the server cluster
    s1 = Server()
    s2 = Server()
    s3 = Server()
    s4 = Server()
    s5 = Server()

    s1.setNeighbors([s2, s3, s4, s5])
    s2.setNeighbors([s1, s3, s4, s5])
    s3.setNeighbors([s2, s1, s4, s5])
    s4.setNeighbors([s2, s3, s1, s5])
    s5.setNeighbors([s2, s3, s4, s1])

    t1 = Thread(target=s1.electionTimeout)
    t1.start()
    time.sleep(5)
    t2 = Thread(target=s2.electionTimeout)
    t2.start()
    t3 = Thread(target=s3.electionTimeout)
    t3.start()
    t4 = Thread(target=s4.electionTimeout)
    t4.start()
    t5 = Thread(target=s5.electionTimeout)
    t5.start()

    # Connect the client to the cluster leader
    c1 = Client()
    c1.connectClientToLeader(s2.neighbors)

    # Use the command line
    c1.waitForCommands()
def testClientConnectionToWithoutLeadershipChangeInCluster():
    # Establish cluster
    s1 = Server()
    s2 = Server()
    s3 = Server()
    
    # Set cluster membership
    s1.setNeighbors([s2, s3])
    s2.setNeighbors([s1, s3])
    s3.setNeighbors([s1, s2])
    
    # Create cluster leader
    # Request an election
    s2.requestVotes()
    
    # Connect the client to the cluster leader through a non-leader
    c1 = Client()
    c1.connectClientToLeader(s1.neighbors)
    assertion(c1.clusterLeader.uuid == s2.uuid, "Client is connected to the leader.")
    
    c1.connectClientToLeader(s2.neighbors)
    assertion(c1.clusterLeader.uuid == s2.uuid, "Client is connected to the leader.")
    
    c1.connectClientToLeader(s3.neighbors)
    assertion(c1.clusterLeader.uuid == s2.uuid, "Client is connected to the leader.")
def testElectionTimeout():
    s1 = Server()
    s2 = Server()
    s3 = Server()
    
    s1.setNeighbors([s2, s3])
    s2.setNeighbors([s1, s3])
    s3.setNeighbors([s1, s2])
    
    t1 = Thread(target=s1.electionTimeout)
    t1.start()
    time.sleep(5)
    t2 = Thread(target=s2.electionTimeout)
    t2.start()
    t3 = Thread(target=s3.electionTimeout)
    t3.start()
    
    # Add to the leaders log a command no none else has
    s1.clusterLeader.log.append(("test", 1, False, 0))
    
    # Add a state machine no one else has
    s1.clusterLeader.stateMachine[1] = "test"

    time.sleep(5)
    assertion(s1.state == ServerState.leader, "Server should be the leader.")
    assertion(s2.state == ServerState.follower, "Server should be the follower.")
    assertion(s3.state == ServerState.follower, "Server should be the follower.")
    
    assertion(len(s1.clusterLeader.log) == 2, "Server has the right size log.")
    assertion(s1.log == s2.log, "Server has the right log.")
    assertion(s3.log == s2.log, "Server has the right log.")
    assertion(s2.log == s3.log, "Server has the right log.")
    
    assertion(len(s1.stateMachine) == 1, "Server has the right size state machine.")
    assertion(s1.stateMachine[1] == s2.stateMachine[1], "Server has the right state machine")
def testSingleServerDoesNotReceiveMajorityVotesShouldNotBecomeLeader():
    s1 = Server()
    s2 = Server()
    s3 = Server()
    
    s1.setNeighbors([s2, s3])
    s2.setNeighbors([s1, s3])
    s3.setNeighbors([s1, s2])
    
    s2.voted = True
    s3.voted = True
    
    # Request an election
    s1.requestVotes()
    
    assertion(s1.state == ServerState.candidate, "Server should remain candidate.")
    assertion(s1.votesReceived == 1, "Server should have the correct number of votes.")
def testSingleServerBecomesLeaderVoteRequest():
    s1 = Server()
    s2 = Server()
    s3 = Server()
    
    s1.setNeighbors([s2, s3])
    s2.setNeighbors([s1, s3])
    s3.setNeighbors([s1, s2])
    
    assertion(len(s1.neighbors) - 1 == 2, "Server should have the right number of neighbors.")
    
    # Request an election
    s1.requestVotes()
    
    assertion(s1.votesReceived == 3, "Server have received the right number of votes.")
    assertion(s1.state == ServerState.leader, "Server should have been elected the leader.")
    assertion(s2.voted == True, "Server should have voted.")
    assertion(s3.voted == True, "Server should have voted.")
    assertion(s1.voted == True, "Server should have voted for itself.")
    assertion(s1.log[0][1] == 0, "Server has the right term in the log.")
def testClientLoop():
    # Establish cluster
    s1 = Server()
    s2 = Server()
    s3 = Server()
    
    # Set cluster membership
    s1.setNeighbors([s2, s3])
    s2.setNeighbors([s1, s3])
    s3.setNeighbors([s1, s2])
    
    # Create cluster leader
    # Request an election
    s3.requestVotes()
    
    # Connect the client to the cluster leader through a non-leader
    c1 = Client()
    c1.connectClientToLeader(s3.neighbors)
    
    # Use the command line
    c1.waitForCommands()
def testLeaderStateMachineLogReplication():
    s1 = Server()
    s2 = Server()
    s3 = Server()
    
    s1.setNeighbors([s2, s3])
    s2.setNeighbors([s1, s3])
    s3.setNeighbors([s1, s2])
    
    # Add a state machine no one else has
    s2.stateMachine[1] = "test"
    
    # Request an election
    s2.requestVotes()
    
    # Add to the leaders log a command no none else has
    #s2.log.append(("test", 1, False, 0))
    
    assertion(s1.log[-1][0] == "New Term", "Server has the correct log.")
    assertion(s1.log[-1][1] == 1, "Server is in right term.")
    assertion(s3.clusterLeader.uuid == s2.uuid, "Server has the correct leader.")
    assertion(s1.stateMachine[1] == "test", "Server has the correct state machine.")
def testIncorrectClientCommandsToCluster():
    # Establish cluster
    s1 = Server()
    s2 = Server()
    s3 = Server()
    
    # Set cluster membership
    s1.setNeighbors([s2, s3])
    s2.setNeighbors([s1, s3])
    s3.setNeighbors([s1, s2])
    
    # Create cluster leader
    # Request an election
    s3.requestVotes()
    
    # Connect the client to the cluster leader through a non-leader
    c1 = Client()
    c1.connectClientToLeader(s1.neighbors)
    assertion(c1.clusterLeader.uuid == s3.uuid, "Client is connected to the leader.")
    
    # Test queue creation
    label1 = 1
    label2 = 2
    qid1 = c1.clusterLeader.create_Queue(label1)
    qid2 = c1.clusterLeader.create_Queue(label2)
    assertion(len(c1.clusterLeader.stateMachine) == 2, "Client successfully created queue.")
    
    # Test getting qid
    assertion(c1.clusterLeader.get_qid(label1) == qid1, "Client successfully retrieved queue id.")
    assertion(c1.clusterLeader.get_qid(label2) == qid2, "Client successfully retrieved queue id.")
    
    # Test pushing, popping, top, and qsize
    c1.clusterLeader.push(qid1, 5)
    c1.clusterLeader.push(qid1, 10)
    assertion(c1.clusterLeader.qsize(qid1) == 2, "Queue has the right size.")
    assertion(c1.clusterLeader.pop(qid1) == 5, "Client popped the right item off the queue.")
    assertion(c1.clusterLeader.top(qid1) == 10, "Queue top returned the right item off the queue.")
    assertion(c1.clusterLeader.qsize(qid1) == 1, "Queue remains the right size.")
def testClientLoopWithTimeout():
    s1 = Server()
    s2 = Server()
    s3 = Server()
    
    s1.setNeighbors([s2, s3])
    s2.setNeighbors([s1, s3])
    s3.setNeighbors([s1, s2])
    
    t1 = Thread(target=s1.electionTimeout)
    t1.start()
    time.sleep(5)
    t2 = Thread(target=s2.electionTimeout)
    t2.start()
    t3 = Thread(target=s3.electionTimeout)
    t3.start()
    
    # Connect the client to the cluster leader through a non-leader
    c1 = Client()
    c1.connectClientToLeader(s2.neighbors)
    
    # Use the command line
    c1.waitForCommands()
Exemple #10
0
def testClientAndServerTimeout():
    s1 = Server()
    s2 = Server()
    s3 = Server()
    
    s1.setNeighbors([s2, s3])
    s2.setNeighbors([s1, s3])
    s3.setNeighbors([s1, s2])
    
    t1 = Thread(target=s1.electionTimeout)
    t1.start()
    time.sleep(5)
    t2 = Thread(target=s2.electionTimeout)
    t2.start()
    t3 = Thread(target=s3.electionTimeout)
    t3.start()
    
    # Connect the client to the cluster leader through a non-leader
    c1 = Client()
    c1.connectClientToLeader(s3.neighbors)
    
    assertion(c1.clusterLeader.uuid == s1.uuid, "Client is connected to the leader.")
    
    # Test queue creation
    label1 = 1
    label2 = 2
    qid1 = c1.clusterLeader.clientCommand("create_Queue", label1)
    qid2 = c1.clusterLeader.clientCommand("create_Queue", label2)
    assertion(len(c1.clusterLeader.stateMachine) == 2, "Client successfully created queue.")
    
    # Test getting qid
    assertion(c1.clusterLeader.clientCommand("get_qid", label1) == qid1, "Client successfully retrieved queue id.")
    assertion(c1.clusterLeader.clientCommand("get_qid", label2) == qid2, "Client successfully retrieved queue id.")
    
    # Test pushing, popping, top, and qsize
    c1.clusterLeader.clientCommand("push", qid1, 5)
    c1.clusterLeader.clientCommand("push", qid1, 10)
    assertion(c1.clusterLeader.clientCommand("qsize", qid1) == 2, "Queue has the right size.")
    assertion(c1.clusterLeader.clientCommand("pop", qid1) == 5, "Client popped the right item off the queue.")
    assertion(c1.clusterLeader.clientCommand("top", qid1) == 10, "Queue top returned the right item off the queue.")
    assertion(c1.clusterLeader.clientCommand("qsize", qid1) == 1, "Queue remains the right size.")
    
    assertion(("create_Queue {0}".format(label1), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    assertion(("create_Queue {0}".format(label2), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    
    assertion(("get_qid {0}".format(label1), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    assertion(("get_qid {0}".format(label2), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    
    assertion(("push {0} {1}".format(qid1, 5), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    assertion(("push {0} {1}".format(qid1, 10), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    
    assertion(("qsize {0}".format(qid1), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    
    assertion(("pop {0}".format(qid1), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    
    assertion(("top {0}".format(qid1), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    
    assertion(("qsize {0}".format(qid1), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    
    assertion(("create_Queue {0}".format(label1), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    assertion(("create_Queue {0}".format(label2), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    
    assertion(("get_qid {0}".format(label1), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    assertion(("get_qid {0}".format(label2), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    
    assertion(("push {0} {1}".format(qid1, 5), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    assertion(("push {0} {1}".format(qid1, 10), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    
    assertion(("qsize {0}".format(qid1), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    
    assertion(("pop {0}".format(qid1), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    
    assertion(("top {0}".format(qid1), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    
    assertion(("qsize {0}".format(qid1), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    
    assertion(len(s2.stateMachine) == 2, "Follower has the right size state machine.")
    
    # Test the killing of the server and re-election
    c1.clusterLeader.clientCommand("kill")
    time.sleep(5)
    
    assertion(c1.clusterLeader != s1.uuid, "Client is connected to the new leader.")
    assertion(len(c1.clusterLeader.neighbors) == 2, "New leader has the right number of neighbors.")
    time.sleep(5)
    assertion(c1.clusterLeader.log[-1][1] == 2, "The cluster is in the right term.")    
Exemple #11
0
def testCorrectClientCommandsToCluster():
    # Establish cluster
    s1 = Server()
    s2 = Server()
    s3 = Server()
    
    # Set cluster membership
    s1.setNeighbors([s2, s3])
    s2.setNeighbors([s1, s3])
    s3.setNeighbors([s1, s2])
    
    # Create cluster leader
    # Request an election
    s3.requestVotes()
    
    # Connect the client to the cluster leader through a non-leader
    c1 = Client()
    c1.connectClientToLeader(s1.neighbors)
    assertion(c1.clusterLeader.uuid == s3.uuid, "Client is connected to the leader.")
    
    # Test queue creation
    label1 = 1
    label2 = 2
    qid1 = c1.clusterLeader.clientCommand("create_Queue", label1)
    qid2 = c1.clusterLeader.clientCommand("create_Queue", label2)
    assertion(len(c1.clusterLeader.stateMachine) == 2, "Client successfully created queue.")
    
    # Test getting qid
    assertion(c1.clusterLeader.clientCommand("get_qid", label1) == qid1, "Client successfully retrieved queue id.")
    assertion(c1.clusterLeader.clientCommand("get_qid", label2) == qid2, "Client successfully retrieved queue id.")
    
    # Test pushing, popping, top, and qsize
    c1.clusterLeader.clientCommand("push", qid1, 5)
    c1.clusterLeader.clientCommand("push", qid1, 10)
    assertion(c1.clusterLeader.clientCommand("qsize", qid1) == 2, "Queue has the right size.")
    assertion(c1.clusterLeader.clientCommand("pop", qid1) == 5, "Client popped the right item off the queue.")
    assertion(c1.clusterLeader.clientCommand("top", qid1) == 10, "Queue top returned the right item off the queue.")
    assertion(c1.clusterLeader.clientCommand("qsize", qid1) == 1, "Queue remains the right size.")
    
    assertion(("create_Queue {0}".format(label1), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    assertion(("create_Queue {0}".format(label2), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    
    assertion(("get_qid {0}".format(label1), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    assertion(("get_qid {0}".format(label2), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    
    assertion(("push {0} {1}".format(qid1, 5), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    assertion(("push {0} {1}".format(qid1, 10), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    
    assertion(("qsize {0}".format(qid1), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    
    assertion(("pop {0}".format(qid1), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    
    assertion(("top {0}".format(qid1), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    
    assertion(("qsize {0}".format(qid1), 1, False, 0) in c1.clusterLeader.log, "Leader has the right command in the log.")
    
    assertion(("create_Queue {0}".format(label1), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    assertion(("create_Queue {0}".format(label2), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    
    assertion(("get_qid {0}".format(label1), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    assertion(("get_qid {0}".format(label2), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    
    assertion(("push {0} {1}".format(qid1, 5), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    assertion(("push {0} {1}".format(qid1, 10), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    
    assertion(("qsize {0}".format(qid1), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    
    assertion(("pop {0}".format(qid1), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    
    assertion(("top {0}".format(qid1), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    
    assertion(("qsize {0}".format(qid1), 1, False, 0) in s2.log, "Follower has the right command in the log.")
    
    assertion(len(s2.stateMachine) == 2, "Follower has the right size state machine.")