This commit is contained in:
2026-02-13 06:33:38 +07:00
parent 56738bdc2d
commit cc8e232299
3 changed files with 27 additions and 119 deletions

View File

@@ -21,126 +21,22 @@ correlation_id = string(uuid4())
# test file transfer #
# ------------------------------------------------------------------------------------------------ #
# # File path for large binary payload test
# const LARGE_FILE_PATH = "./testFile_small.zip"
# const filename = basename(LARGE_FILE_PATH)
# File path for large binary payload test
const FILE_PATH = "./testFile_small.zip"
const filename = basename(FILE_PATH)
# # Helper: Log with correlation ID
# function log_trace(message)
# timestamp = Dates.now()
# println("[$timestamp] [Correlation: $correlation_id] $message")
# end
# # Sender: Send large binary file via smartsend
# function test_large_binary_send()
# conn = NATS.connect(NATS_URL)
# # Read the large file as binary data
# log_trace("Reading large file: $LARGE_FILE_PATH")
# file_data = read(LARGE_FILE_PATH)
# file_size = length(file_data)
# log_trace("File size: $file_size bytes")
# # Use smartsend with binary type - will automatically use link transport
# # if file size exceeds the threshold (1MB by default)
# env = NATSBridge.smartsend(
# SUBJECT,
# file_data,
# "binary",
# nats_url = NATS_URL,
# fileserver_url = FILESERVER_URL;
# dataname=filename
# )
# log_trace("Sent message with transport: $(env.transport)")
# log_trace("Envelope type: $(env.type)")
# # Check if link transport was used
# if env.transport == "link"
# log_trace("Using link transport - file uploaded to HTTP server")
# log_trace("URL: $(env.url)")
# else
# log_trace("Using direct transport - payload sent via NATS")
# end
# NATS.drain(conn)
# end
# # Receiver: Listen for messages and verify large payload handling
# function test_large_binary_receive()
# conn = NATS.connect(NATS_URL)
# NATS.subscribe(conn, SUBJECT) do msg
# log_trace("Received message on $(msg.subject)")
# # Use NATSBridge.smartreceive to handle the data
# result = NATSBridge.smartreceive(msg)
# # Check transport type
# if result.envelope.transport == "direct"
# log_trace("Received direct transport")
# else
# # For link transport, result.data is the URL
# log_trace("Received link transport")
# end
# # Verify the received data matches the original
# if result.envelope.type == "binary"
# if isa(result.data, Vector{UInt8})
# file_size = length(result.data)
# log_trace("Received $(file_size) bytes of binary data")
# # Save received data to a test file
# println("metadata ", result.envelope.metadata)
# dataname = result.envelope.metadata["dataname"]
# if dataname != "NA"
# output_path = "./new_$dataname"
# write(output_path, result.data)
# log_trace("Saved received data to $output_path")
# end
# # Verify file size
# original_size = length(read(LARGE_FILE_PATH))
# if file_size == result.envelope.metadata["content_length"]
# log_trace("SUCCESS: File size matches! Original: $(result.envelope.metadata["content_length"]) bytes")
# else
# log_trace("WARNING: File size mismatch! Original: $(result.envelope.metadata["content_length"]), Received: $file_size")
# end
# end
# end
# end
# # Keep listening for 10 seconds
# sleep(120)
# NATS.drain(conn)
# end
# # Run the test
# println("Starting large binary payload test...")
# println("Correlation ID: $correlation_id")
# println("Large file: $LARGE_FILE_PATH")
# # Run sender first
# println("start smartsend")
# test_large_binary_send()
# # # Run receiver
# # println("testing smartreceive")
# # test_large_binary_receive()
# println("Test completed.")
# ------------------------------------------------------------------------------------------------ #
# test JSON message transfer #
# ------------------------------------------------------------------------------------------------ #
# Helper: Log with correlation ID
function log_trace(message)
timestamp = Dates.now()
println("[$timestamp] [Correlation: $correlation_id] $message")
end
# Sender: Send large binary file via smartsend
function test_json_send()
function test_large_binary_send()
conn = NATS.connect(NATS_URL)
# Read the large file as binary data
log_trace("Reading large file: $LARGE_FILE_PATH")
file_data = read(LARGE_FILE_PATH)
log_trace("Reading large file: $FILE_PATH")
file_data = read(FILE_PATH)
file_size = length(file_data)
log_trace("File size: $file_size bytes")
@@ -171,7 +67,7 @@ function test_json_send()
end
# Receiver: Listen for messages and verify large payload handling
function test_json_receive()
function test_large_binary_receive()
conn = NATS.connect(NATS_URL)
NATS.subscribe(conn, SUBJECT) do msg
log_trace("Received message on $(msg.subject)")
@@ -202,7 +98,7 @@ function test_json_receive()
end
# Verify file size
original_size = length(read(LARGE_FILE_PATH))
original_size = length(read(FILE_PATH))
if file_size == result.envelope.metadata["content_length"]
log_trace("SUCCESS: File size matches! Original: $(result.envelope.metadata["content_length"]) bytes")
else
@@ -218,8 +114,20 @@ function test_json_receive()
end
# Run the test
println("Starting large binary payload test...")
println("Correlation ID: $correlation_id")
println("File: $FILE_PATH")
# Run sender first
println("start smartsend")
test_large_binary_send()
# # Run receiver
# println("testing smartreceive")
# test_large_binary_receive()
println("Test completed.")