update
This commit is contained in:
163
test/test_mpy_binary_sender.py
Normal file
163
test/test_mpy_binary_sender.py
Normal file
@@ -0,0 +1,163 @@
|
||||
"""
|
||||
MicroPython Binary Sender Test
|
||||
Tests the smartsend function with binary/image/audio/video payloads
|
||||
|
||||
Note: This test is designed for both MicroPython and desktop Python
|
||||
for compatibility testing.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import base64
|
||||
|
||||
# Add parent directory to path
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from natbridge_mpy import smartsend, DEFAULT_BROKER_URL, DEFAULT_FILESERVER_URL, DEFAULT_SIZE_THRESHOLD, MAX_PAYLOAD_SIZE
|
||||
|
||||
TEST_SUBJECT = '/test/binary'
|
||||
TEST_BROKER_URL = os.environ.get('NATS_URL', 'nats://localhost:4222')
|
||||
TEST_FILESERVER_URL = os.environ.get('FILESERVER_URL', 'http://localhost:8080')
|
||||
|
||||
|
||||
def run_test():
|
||||
print('=== MicroPython Binary Sender Test ===\n')
|
||||
|
||||
from natbridge_mpy import _generate_uuid
|
||||
correlation_id = 'mpy-binary-test-' + _generate_uuid()
|
||||
print(f'Correlation ID: {correlation_id}')
|
||||
print(f'Subject: {TEST_SUBJECT}')
|
||||
print(f'Broker URL: {TEST_BROKER_URL}')
|
||||
print(f'Default Size Threshold: {DEFAULT_SIZE_THRESHOLD} bytes')
|
||||
print(f'Max Payload Size: {MAX_PAYLOAD_SIZE} bytes\n')
|
||||
|
||||
# Test data - binary data for different types
|
||||
image_data = bytearray([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]) # PNG header
|
||||
audio_data = bytearray([0x46, 0x4C, 0x41, 0x43, 0x00, 0x00, 0x00, 0x00]) # FLAC header
|
||||
video_data = bytearray([0x00, 0x00, 0x00, 0x18, 0x66, 0x74, 0x79, 0x70]) # MP4 header
|
||||
generic_binary = bytearray([0xDE, 0xAD, 0xBE, 0xEF, 0xCA, 0xFE, 0xBA, 0xBE])
|
||||
|
||||
test_data = [
|
||||
('image', bytes(image_data), 'image'),
|
||||
('audio', bytes(audio_data), 'audio'),
|
||||
('video', bytes(video_data), 'video'),
|
||||
('binary', bytes(generic_binary), 'binary')
|
||||
]
|
||||
|
||||
try:
|
||||
# Send the message
|
||||
print('Sending binary payloads...')
|
||||
env, env_json_str = smartsend(
|
||||
TEST_SUBJECT,
|
||||
test_data,
|
||||
broker_url=TEST_BROKER_URL,
|
||||
fileserver_url=TEST_FILESERVER_URL,
|
||||
correlation_id=correlation_id,
|
||||
msg_purpose='test',
|
||||
sender_name='mpy-binary-test',
|
||||
is_publish=False
|
||||
)
|
||||
|
||||
print('\n=== Envelope Created ===')
|
||||
print(f'Correlation ID: {env["correlation_id"]}')
|
||||
print(f'Message ID: {env["msg_id"]}')
|
||||
print(f'Timestamp: {env["timestamp"]}')
|
||||
print(f'Subject: {env["send_to"]}')
|
||||
print(f'Purpose: {env["msg_purpose"]}')
|
||||
print(f'Sender: {env["sender_name"]}')
|
||||
print(f'Payloads: {len(env["payloads"])}\n')
|
||||
|
||||
# Validate envelope structure
|
||||
print('=== Validation ===')
|
||||
passed = True
|
||||
|
||||
if len(env['payloads']) != 4:
|
||||
print(f'❌ Expected 4 payloads, got {len(env["payloads"])}')
|
||||
passed = False
|
||||
else:
|
||||
print('✅ Correct number of payloads')
|
||||
|
||||
# Test each payload
|
||||
expected_datanames = ['image', 'audio', 'video', 'binary']
|
||||
expected_types = ['image', 'audio', 'video', 'binary']
|
||||
expected_data = [bytes(image_data), bytes(audio_data), bytes(video_data), bytes(generic_binary)]
|
||||
|
||||
for i in range(len(env['payloads'])):
|
||||
payload = env['payloads'][i]
|
||||
|
||||
if payload['dataname'] != expected_datanames[i]:
|
||||
print(f"❌ Payload {i + 1}: Expected dataname '{expected_datanames[i]}', got '{payload['dataname']}'")
|
||||
passed = False
|
||||
else:
|
||||
print(f'✅ Payload {i + 1}: Correct dataname')
|
||||
|
||||
if payload['payload_type'] != expected_types[i]:
|
||||
print(f"❌ Payload {i + 1}: Expected type '{expected_types[i]}', got '{payload['payload_type']}'")
|
||||
passed = False
|
||||
else:
|
||||
print(f'✅ Payload {i + 1}: Correct type')
|
||||
|
||||
if payload['transport'] != 'direct':
|
||||
print(f"❌ Payload {i + 1}: Expected transport 'direct', got '{payload['transport']}'")
|
||||
passed = False
|
||||
else:
|
||||
print(f'✅ Payload {i + 1}: Correct transport')
|
||||
|
||||
if payload['encoding'] != 'base64':
|
||||
print(f"❌ Payload {i + 1}: Expected encoding 'base64', got '{payload['encoding']}'")
|
||||
passed = False
|
||||
else:
|
||||
print(f'✅ Payload {i + 1}: Correct encoding')
|
||||
|
||||
# Decode and verify the data
|
||||
decoded_data = base64.b64decode(payload['data'])
|
||||
original_data = expected_data[i]
|
||||
|
||||
if decoded_data != original_data:
|
||||
print(f'❌ Payload {i + 1}: Data integrity mismatch')
|
||||
passed = False
|
||||
else:
|
||||
print(f'✅ Payload {i + 1}: Data integrity verified')
|
||||
|
||||
print(f' Size: {payload["size"]} bytes\n')
|
||||
|
||||
# Test with larger binary data
|
||||
print('=== Large Binary Data Test ===')
|
||||
large_data = bytes([0xFF] * 1000) # 1KB of binary data
|
||||
large_test_data = [
|
||||
('large_binary', large_data, 'binary')
|
||||
]
|
||||
|
||||
large_env, _ = smartsend(
|
||||
TEST_SUBJECT,
|
||||
large_test_data,
|
||||
broker_url=TEST_BROKER_URL,
|
||||
fileserver_url=TEST_FILESERVER_URL,
|
||||
correlation_id='large-' + correlation_id,
|
||||
is_publish=False
|
||||
)
|
||||
|
||||
if len(large_env['payloads']) == 1 and large_env['payloads'][0]['size'] == 1000:
|
||||
print('✅ Large binary data handled correctly')
|
||||
else:
|
||||
print('❌ Large binary data handling failed')
|
||||
passed = False
|
||||
|
||||
# Final result
|
||||
print('\n=== Test Result ===')
|
||||
if passed:
|
||||
print('✅ ALL TESTS PASSED')
|
||||
sys.exit(0)
|
||||
else:
|
||||
print('❌ SOME TESTS FAILED')
|
||||
sys.exit(1)
|
||||
|
||||
except Exception as e:
|
||||
print(f'❌ Test failed with error: {e}')
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run_test()
|
||||
Reference in New Issue
Block a user