Added put block test (1 block)

This commit is contained in:
Kevin F 2022-04-24 15:19:39 -05:00
parent ca2344c72c
commit e6b61c5f59
2 changed files with 65 additions and 5 deletions

View File

@ -1,12 +1,11 @@
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from typing import List, Tuple from typing import List
import secrets import secrets
from time import time
from asyncio import wait_for from asyncio import wait_for
from onionrblocks import Block from onionrblocks import Block
from ..dandelion import DandelionPhase, StemAcceptResult from ..dandelion import StemAcceptResult
from ..constants import BLOCK_ID_SIZE, BLOCK_MAX_SIZE from ..constants import BLOCK_ID_SIZE, BLOCK_MAX_SIZE
from ..constants import MAX_INBOUND_DANDELION_EDGE, MAX_STEM_BLOCKS_PER_STREAM from ..constants import MAX_INBOUND_DANDELION_EDGE, MAX_STEM_BLOCKS_PER_STREAM
from ..blockqueues import gossip_block_queues from ..blockqueues import gossip_block_queues
@ -16,7 +15,6 @@ block_size_digits = len(str(BLOCK_MAX_SIZE))
base_wait_timeout = 30 base_wait_timeout = 30
if TYPE_CHECKING: if TYPE_CHECKING:
from queue import Queue
from asyncio import StreamWriter, StreamReader from asyncio import StreamWriter, StreamReader
@ -33,7 +31,6 @@ async def accept_stem_blocks(
# Start getting the first block # Start getting the first block
read_routine = reader.readexactly(BLOCK_ID_SIZE) read_routine = reader.readexactly(BLOCK_ID_SIZE)
stream_start_time = int(time())
block_queue_to_use = secrets.choice(gossip_block_queues) block_queue_to_use = secrets.choice(gossip_block_queues)
@ -63,6 +60,7 @@ async def accept_stem_blocks(
block_queue_to_use.put( block_queue_to_use.put(
Block(block_id, raw_block, auto_verify=True) Block(block_id, raw_block, auto_verify=True)
) )
# Regardless of stem phase, we add to queue # Regardless of stem phase, we add to queue
# Client will decide if they are to be stemmed # Client will decide if they are to be stemmed

View File

@ -0,0 +1,62 @@
import os, uuid
from queue import Empty
TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
print("Test directory:", TEST_DIR)
os.environ["ONIONR_HOME"] = TEST_DIR
from time import sleep
from threading import Thread
import asyncio
import unittest
import sys
sys.path.append(".")
sys.path.append("src/")
from ordered_set import OrderedSet
import onionrblocks
import blockdb
from gossip.server import gossip_server
from gossip.blockqueues import gossip_block_queues
from filepaths import gossip_server_socket_file
BLOCK_MAX_SIZE = 1024 * 2000
BLOCK_MAX_SIZE_LEN = len(str(BLOCK_MAX_SIZE))
BLOCK_ID_SIZE = 128
BLOCK_STREAM_OFFSET_DIGITS = 8
class OnionrServerPutBlockTest(unittest.TestCase):
def test_put_block(self):
Thread(target=gossip_server, daemon=True).start()
sleep(0.01)
bl = onionrblocks.blockcreator.create_anonvdf_block(
b"my test block", b"txt", 2800)
async def block_put_client():
reader, writer = await asyncio.open_unix_connection(
gossip_server_socket_file)
writer.write(int(5).to_bytes(1, 'big'))
await writer.drain()
writer.write(bl.id)
writer.write(
str(len(bl.raw)).zfill(BLOCK_MAX_SIZE_LEN).encode('utf-8'))
writer.write(bl.raw)
await writer.drain()
sleep(0.03)
try:
self.assertEqual(gossip_block_queues[0].get_nowait().raw, bl.raw)
except Empty:
self.assertEqual(gossip_block_queues[1].get_nowait().raw, bl.raw)
asyncio.run(block_put_client())
unittest.main()