Skip to content

Commit

Permalink
PEP8 Fixes to Tests Folder
Browse files Browse the repository at this point in the history
  • Loading branch information
super3 committed Jan 18, 2016
1 parent 1b61bf3 commit f0e651d
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 113 deletions.
3 changes: 2 additions & 1 deletion tests/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

connected = 0


def success_notify(con):
global connected
connected = 1
Expand Down Expand Up @@ -70,5 +71,5 @@ def success_notify(con):
net.stop()

if not connected:
assert(0)
assert 0

4 changes: 1 addition & 3 deletions tests/test_dht_msg.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import logging
from unittest import TestCase
from pyp2p.lib import *
from pyp2p.dht_msg import DHT
import random
import logging

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
Expand Down
3 changes: 0 additions & 3 deletions tests/test_hybrid_reply.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from unittest import TestCase
from pyp2p.lib import *
from pyp2p.dht_msg import DHT
from pyp2p.hybrid_reply import HybridReply
import random


class TestHybridReply(TestCase):
Expand Down
108 changes: 59 additions & 49 deletions tests/test_net.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
from unittest import TestCase
from pyp2p.lib import *
from pyp2p.dht_msg import DHT
import time
import unittest
from pyp2p.net import *
from threading import Thread
from unittest import TestCase
from pyp2p.sys_clock import SysClock
from twisted.internet import reactor
from pyp2p.rendezvous_client import RendezvousClient
from pyp2p.rendezvous_server import RendezvousFactory, RendezvousProtocol, LineReceiver
import random
from threading import Thread
import time
import unittest
from pyp2p.rendezvous_server import RendezvousFactory


class TestNet(TestCase):
Expand Down Expand Up @@ -78,12 +74,11 @@ def run_rendezvous_server():
passive_port=0,
servers=rendezvous_servers
).start()
assert(net.enable_bootstrap)
assert net.enable_bootstrap
net.bootstrap()
assert(len(net.outbound))
net.stop()


time.sleep(2)
rendezvous_servers = [
{
Expand Down Expand Up @@ -118,7 +113,7 @@ def run_rendezvous_server():
time.sleep(2)
print(lan_ip)
sock = Sock(lan_ip, 8001, blocking=1)
assert(sock.connected)
assert sock.connected

# Test bootstrap.
sock.send_line("BOOTSTRAP 5")
Expand All @@ -138,7 +133,7 @@ def run_rendezvous_server():
assert("REMOTE" in ret)

# Test candidate.
msg = "CANDIDATE %s TCP 3232 2345 2245" % (lan_ip)
msg = "CANDIDATE %s TCP 3232 2345 2245" % lan_ip
sock.send_line(msg)
ret = sock.recv_line()
assert("PREDICTION" in ret)
Expand Down Expand Up @@ -197,14 +192,15 @@ def success_notify(con):
unl_value = "AnRBam11OG1IUEhGVkRKOHQ3cEs4c2dtRMWDYbvALwOowOEG0lc="
for i in range(0, 5):
"""
The reason this test sometimes fails is due to
timing: the packets need to cross in such a way that the timing for the connects that Travis CI makes to the
NATed' test node need to arrive -BEFORE- the
test nodes connect times out otherwise
subsequent connects from Travis yield RST packets.
The TCP setup for the NATed test node isn't a true NAT / is typical of NATs that are in the wild.
Alternatively: the hosts are too close.
The reason this test sometimes fails is due to timing: the packets
need to cross in such a way that the timing for the connects that
Travis CI makes to the NATed' test node need to arrive -BEFORE- the
test nodes connect times out otherwise subsequent connects from
Travis yield RST packets.
The TCP setup for the NATed test node isn't a true NAT / is typical
of NATs that are in the wild. Alternatively: the hosts are too
close.
"""
net.unl.connect(unl_value, events)

Expand All @@ -228,12 +224,12 @@ def success_notify(con):
net.stop()

if not connected:
assert(0)
assert 0

def test_net_config(self):
"""
Tests whether the Net class behaviours as expected relative to manually setting networking info
and interacting with key functions.
Tests whether the Net class behaviours as expected relative to manually
setting networking info and interacting with key functions.
"enable_bootstrap": "default",
"enable_advertise": "default",
Expand Down Expand Up @@ -491,18 +487,19 @@ def test_net_config(self):

if option == 1 or option == 0:
cmd = "net." + name + " = " + str(option)
exec(cmd)
exec cmd
assert(eval("net." + name) == option)

# Start networking.
net.start()

# Check net is started.
assert(net != None)
assert(net is not None)
assert(net.is_net_started == 1)

# Check duplicate connections state.
assert(net.enable_duplicate_ip_cons == test["expected"]["duplicate_cons"])
assert(net.enable_duplicate_ip_cons ==
test["expected"]["duplicate_cons"])

# Check config.
for key in list(config):
Expand All @@ -523,7 +520,7 @@ def test_net_config(self):

# Check the passive server is started.
if config["node_type"] == "passive":
assert(net.passive != None)
assert(net.passive is not None)

# Setup functions to check.
functions = {
Expand All @@ -542,8 +539,8 @@ def test_net_config(self):
if expected == "self":
assert(ret == net)

if expected == None:
assert(ret == None)
if expected is None:
assert(ret is None)

# Stop net.
net.stop()
Expand Down Expand Up @@ -580,7 +577,8 @@ def accept_cons(node):

# Buld networks.
for node in nodes:
node["net"] = Net(net_type="direct", node_type="passive", passive_port=node["port"], debug=1)
node["net"] = Net(net_type="direct", node_type="passive",
passive_port=node["port"], debug=1)
node["net"].disable_forwarding()
node["net"].disable_bootstrap()
node["net"].disable_advertise()
Expand All @@ -591,7 +589,8 @@ def accept_cons(node):

"""
Make connections.
Note: duplicate connections will be rejected resulting in just one connection from one node to the other nodes.
Note: duplicate connections will be rejected resulting in just one
connection from one node to the other nodes.
"""
for our_node in nodes:
for their_node in nodes:
Expand All @@ -600,7 +599,8 @@ def accept_cons(node):
continue

# Connect to them.
our_node["net"].add_node(get_lan_ip(), their_node["port"], "passive")
our_node["net"].add_node(get_lan_ip(), their_node["port"],
"passive")

# Accept cons:
for node in nodes:
Expand All @@ -620,8 +620,8 @@ def accept_cons(node):
for con in node["net"]:
con.set_blocking(blocking=1, timeout=5)
line = con.recv_line()
assert(con.connected)
assert(line == "test")
assert con.connected
assert line == "test"

# Close cons.
for node in nodes:
Expand All @@ -636,13 +636,15 @@ def accept_cons(node):
def test_00000001(self):
# Test seen messages
from pyp2p.net import rendezvous_servers
net = Net(debug=1, nat_type="preserving", node_type="simultaneous", net_type="direct", passive_port=10234)
net = Net(debug=1, nat_type="preserving", node_type="simultaneous",
net_type="direct", passive_port=10234)
net.disable_advertise()
net.disable_bootstrap()
net.disable_duplicates()
net.start()
for i in range(0, 2):
con = net.add_node(rendezvous_servers[i]["addr"], rendezvous_servers[i]["port"], "passive")
con = net.add_node(rendezvous_servers[i]["addr"],
rendezvous_servers[i]["port"], "passive")
if con is not None:
break

Expand Down Expand Up @@ -697,13 +699,15 @@ def test_00000003(self):

def test_00000002(self):
from pyp2p.net import forwarding_servers
net = Net(debug=1, nat_type="preserving", node_type="simultaneous", net_type="direct", passive_port=40408)
net = Net(debug=1, nat_type="preserving", node_type="simultaneous",
net_type="direct", passive_port=40408)
net.disable_advertise()
net.disable_bootstrap()
net.start()

# Test passive outbound connection.
net.add_node(forwarding_servers[0]["addr"], forwarding_servers[0]["port"], "passive")
net.add_node(forwarding_servers[0]["addr"],
forwarding_servers[0]["port"], "passive")
assert(len(net.outbound) == 1)
assert(net.get_connection_no() == 1)

Expand All @@ -712,10 +716,11 @@ def test_00000002(self):
def threaded_add_node(node_ip, node_port, node_type, net, events):
def add_node(node_ip, node_port, node_type, net, events):
con = net.add_node(node_ip, node_port, node_type)
if con != None:
if con is not None:
events["success"](con)

t = Thread(target=add_node, args=(node_ip, node_port, node_type, net, events))
t = Thread(target=add_node,
args=(node_ip, node_port, node_type, net, events))
t.start()

cons = []
Expand Down Expand Up @@ -749,10 +754,10 @@ def success(con):
for con in cons:
con.close()
else:
assert(0)
assert 0

def failure_notify(con):
assert(0)
assert 0

def success_notify(con):
con.close()
Expand All @@ -766,23 +771,28 @@ def success_notify(con):
# NATed VPS: AnRBam11OG1IUEhGVkRKOHQ3cEs4c2dtRMWDYbvALwOowOEG0lc=

# This is the not-NATed test node.
net.unl.connect("AlNFMHVDaEVJZ3FnZjl2cXVLcVV1c2dtRMUG79qiBu/aotbFMn4=", events)
net.unl.connect("AlNFMHVDaEVJZ3FnZjl2cXVLcVV1c2dtRMUG79qiBu/aotbFMn4=",
events)

assert(net.validate_node(forwarding_servers[0]["addr"], forwarding_servers[0]["port"]))
assert(net.validate_node(forwarding_servers[0]["addr"],
forwarding_servers[0]["port"]))

time.sleep(15)

net.stop()

def test_queued_sim_open(self):
# Test add node.
net = Net(debug=1, nat_type="preserving", node_type="simultaneous", net_type="direct", passive_port=20283)
net = Net(debug=1, nat_type="preserving", node_type="simultaneous",
net_type="direct", passive_port=20283)
net.disable_advertise()
net.disable_bootstrap()
net.start()

net.unl.connect("AlNFMHVDaEVJZ3FnZjl2cXVLcVV1c2dtRMUG79qiBu/aotbFMn4=", events=None)
net.unl.connect("AnRBam11OG1IUEhGVkRKOHQ3cEs4c2dtRMWDYbvALwOowOEG0lc=", events=None)
net.unl.connect("AlNFMHVDaEVJZ3FnZjl2cXVLcVV1c2dtRMUG79qiBu/aotbFMn4=",
events=None)
net.unl.connect("AnRBam11OG1IUEhGVkRKOHQ3cEs4c2dtRMWDYbvALwOowOEG0lc=",
events=None)
time.sleep(2)
assert(len(net.unl.pending_sim_open) == 2)
net.stop()
Expand Down Expand Up @@ -818,4 +828,4 @@ def test_add_duplicate_nodes(self):
))

node_1.stop()
net.stop()
net.stop()

0 comments on commit f0e651d

Please sign in to comment.