示例#1
0
def initiate(p):
	print("\n \t --* Testing Initialization *--\n")
	nodes = []
	points = 0
	try:
		n1 = Node("localhost", p[0])
		nodes.append(n1)
		n2 = Node("localhost", p[1])
		nodes.append(n2)
		n3 = Node("localhost", p[2])
		nodes.append(n3)
		n4 = Node("localhost", p[3])
		nodes.append(n4)
		n5 = Node("localhost", p[4])
		nodes.append(n5)
	except Exception as e:
		print ("RunTime Error in node initialization! Following exception occurred:")
		print (e)
		return
	if nodes[0].successor == ("localhost",p[0]) and nodes[0].predecessor == ("localhost",p[0]):
		print ("Initialization Successful. (+1)")
		points += 1
	else:
		print ("Wrong initialization!")
	print ("\nInitialization testing completed. Points:",points,"/ 1")
	return nodes, points
def testFileRehashing(nodes, files, sp):
    print("\n\t --* Testing File Rehashing *--\n")
    ps = [sp + 0, sp + 1, sp + 2]
    print("New nodes joining network.")
    for p in ps:
        n1 = Node("localhost", p)
        n1.join((nodes[0].host, nodes[0].port))
        nodes.append(n1)
        time.sleep(2)
    nodes.sort(key=lambda x: x.key, reverse=False)
    correct = True
    print("Checking if files rehashed correctly.")
    for i in range(len(files)):
        for j in range(len(nodes)):
            if nodes[j].hasher(files[i]) <= nodes[j].key and nodes[j].hasher(
                    files[i]) > nodes[j - 1].key or nodes[j].hasher(
                        files[i]) > nodes[-1].key and i == 0:
                if files[i] not in nodes[j].files:
                    correct = False
    if correct:
        print("All files rehashed successfully. \t(+5)")
    else:
        print("Some or all files have been rehashed incorrectly. \t(0)")
        return nodes, 0
    print("\nFile Rehashing testing completed. Points: 5 / 5")
    return nodes, 5