Skip to content

Commit

Permalink
Move 2 tests to new paradigm + start writing new TestBase
Browse files Browse the repository at this point in the history
  • Loading branch information
lsankar4033 committed Aug 5, 2020
1 parent ac13c51 commit 9ea5c7c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 48 deletions.
38 changes: 16 additions & 22 deletions scripts/dv5/single_node.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
from pyrum import SubprocessConn, Rumor
import trio
from .utils import with_rumor

@with_rumor
async def run(rumor, args):
try:
await rumor.host.start()

async def run(args):
async with SubprocessConn(cmd='rumor bare') as conn:
async with trio.open_nursery() as nursery:
try:
rumor = Rumor(conn, nursery)
await rumor.host.start()
# TODO: check type of result
enr = await rumor.enr.make()
await rumor.dv5.run(args['enr'])

# TODO: check type of result
enr = await rumor.enr.make()
# TODO: result validation
result = await rumor.dv5.ping(target=args['enr'])
# TODO: result validation
result = await rumor.dv5.lookup(target=args['enr')

await rumor.dv5.run(args['enr'])
await rumor.dv5.cancel()

# TODO: result validation
result = await rumor.dv5.ping(target=args['enr'])
return 0

# TODO: result validation
result = await rumor.dv5.lookup(target=args['enr')

await rumor.dv5.cancel()

return (0, None)

finally:
nursery.cancel_scope.cancel()
except:
return 1
40 changes: 17 additions & 23 deletions scripts/reqresp/ping.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
import sys

import trio
from eth2spec.utils.ssz.ssz_typing import uint64
from pyrum import SubprocessConn, Rumor
from sclients import connect_rumor

from ..utils import *

from lib.console import ConsoleWriter
from ..utils import parse_response, with_rumor

async def test_ping(enr):
async with SubprocessConn(cmd='rumor bare') as conn:
async with trio.open_nursery() as nursery:
rumor = Rumor(conn, nursery)
peer_id = await connect_rumor(rumor, enr)

for i in range(5):
req = uint64(i).encode_bytes().hex()
resp = await rumor.rpc.ping.req.raw(peer_id, req, raw=True, compression='snappy')
resp_data = parse_response(resp)
@with_rumor
async def run(rumor, args):
peer_id = await connect_rumor(rumor, args['enr'])

pong = uint64.decode_bytes(bytes.fromhex(resp_data))
return_code = 0
for i in range(5):
req = uint64(i).encode_bytes().hex()
resp = await rumor.rpc.ping.req.raw(peer_id, req, raw=True, compression='snappy')
(rc, resp_data) = parse_response(resp)
if rc != 0:
return_code = rc
continue

if not isinstance(pong, int) or pong < 0:
print(
f'ping -- invalid ping response: {pong}',
file=sys.stderr
)
pong = uint64.decode_bytes(bytes.fromhex(resp_data))

nursery.cancel_scope.cancel()
if not isinstance(pong, int) or pong < 0:
return_code = 1

if __name__ == '__main__':
args = parse_args('--enr')
trio.run(test_ping, args.enr)
return return_code
33 changes: 30 additions & 3 deletions scripts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,36 @@ def compare_vals(expected, actual, name):
)


# NOTE: Eventually, return code will be handled on an object
def parse_response(resp):
if not('chunk' in resp and 'data' in resp['chunk']):
print(f'received bad response: {resp}', file=sys.stderr)
return None
return (1, None)

return resp['chunk']['data']
return (0, resp['chunk']['data'])

class TestBase:

def __init__(self, args, cw=ConsoleWriter(None, None, None)):
self.args = args
self.cw = cw
self.return_code = 0

def parse_rumor_response(self, resp):
if not('chunk' in resp and 'data' in resp['chunk']):
self.cw.info(f'received bad response: {resp}', file=sys.stderr)
self.return_code = 1
return None

return resp['chunk']['data']

def compare_containers(self, expected: Container, actual: Container):
if expected != actual:
error_str = ''
for field in expected.fields():
self.compare_vals(getattr(expected, field), getattr(actual, field), field)


def compare_vals(self, expected, actual, name):
if expected != actual:
self.cw.info(f'{name} -- expected {expected} actual {actual}')
self.return_code = 1

0 comments on commit 9ea5c7c

Please sign in to comment.