Add simple test.
This commit is contained in:
parent
9b6ddc1940
commit
81fb413818
3 changed files with 63 additions and 1 deletions
2
.coveragerc
Normal file
2
.coveragerc
Normal file
|
@ -0,0 +1,2 @@
|
|||
[run]
|
||||
source=p2pfs
|
3
setup.py
3
setup.py
|
@ -28,6 +28,9 @@ setup(
|
|||
keywords='P2P, Networking',
|
||||
packages=find_packages(exclude=['tests']),
|
||||
install_requires=['pybase64', 'zstandard', 'coloredlogs', 'tabulate'],
|
||||
extras_require={
|
||||
'test': ['pytest-cov', 'pytest', 'coverage'],
|
||||
},
|
||||
entry_points={
|
||||
'console_scripts': [
|
||||
'p2pfs=p2pfs.__main__:main',
|
||||
|
|
|
@ -1 +1,58 @@
|
|||
from p2pfs
|
||||
import os
|
||||
import hashlib
|
||||
from p2pfs import Peer, Tracker
|
||||
|
||||
|
||||
def fmd5(fname):
|
||||
""" calculate the md5 value of a file
|
||||
:param fname: file name
|
||||
:return: md5 value.
|
||||
"""
|
||||
hash_md5 = hashlib.md5()
|
||||
with open(fname, "rb") as f:
|
||||
for chunk in iter(lambda: f.read(4096), b""):
|
||||
hash_md5.update(chunk)
|
||||
return hash_md5.hexdigest()
|
||||
|
||||
|
||||
def test_main():
|
||||
peer_1, peer_2 = Peer('localhost', 0, 'localhost', 8880), Peer('localhost', 0, 'localhost', 8880)
|
||||
tracker = Tracker('localhost', 8880)
|
||||
tracker.start()
|
||||
peer_1.start()
|
||||
peer_2.start()
|
||||
|
||||
with open('test_small_file', 'wb') as fout:
|
||||
fout.write(os.urandom(1000))
|
||||
|
||||
with open('test_big_file', 'wb') as fout:
|
||||
# write 500MB random data into the file
|
||||
for _ in range(500):
|
||||
fout.write(os.urandom(1000 * 1000))
|
||||
# peer1 publish small file and peer2 downloads it
|
||||
peer_1.publish('test_small_file')
|
||||
file_list = tracker.file_list()
|
||||
assert 'test_small_file' in file_list
|
||||
assert file_list['test_small_file']['size'] == 1000
|
||||
file_list = peer_2.list_file()
|
||||
assert 'test_small_file' in file_list
|
||||
result, msg = peer_2.download('test_small_file', 'downloaded_small_file')
|
||||
assert result is True
|
||||
assert os.path.exists('downloaded_small_file')
|
||||
assert fmd5('test_small_file') == fmd5('downloaded_small_file')
|
||||
|
||||
peer_2.publish('test_big_file')
|
||||
file_list = tracker.file_list()
|
||||
assert 'test_big_file' in file_list and 'test_small_file' in file_list
|
||||
assert file_list['test_big_file']['size'] == 500 * 1000 * 1000
|
||||
file_list = peer_1.list_file()
|
||||
assert 'test_big_file' in file_list and 'test_small_file' in file_list
|
||||
result, msg = peer_1.download('test_big_file', 'downloaded_big_file')
|
||||
assert result is True
|
||||
assert os.path.exists('downloaded_big_file')
|
||||
assert fmd5('test_big_file') == fmd5('downloaded_big_file')
|
||||
|
||||
os.remove('test_small_file')
|
||||
os.remove('test_big_file')
|
||||
os.remove('downloaded_small_file')
|
||||
os.remove('downloaded_big_file')
|
||||
|
|
Loading…
Reference in a new issue