From a2380282ffa8f958b320052cdffec023f03394fb Mon Sep 17 00:00:00 2001 From: narawat Date: Mon, 9 Mar 2026 02:29:14 +0700 Subject: [PATCH] add julia test file --- test/test_julia_file_receiver.jl | 84 ---------------- test/test_julia_file_sender.jl | 123 ----------------------- test/test_julia_mix_payloads_receiver.jl | 48 ++------- test/test_julia_mix_payloads_sender.jl | 10 +- 4 files changed, 13 insertions(+), 252 deletions(-) delete mode 100644 test/test_julia_file_receiver.jl delete mode 100644 test/test_julia_file_sender.jl diff --git a/test/test_julia_file_receiver.jl b/test/test_julia_file_receiver.jl deleted file mode 100644 index bc2632e..0000000 --- a/test/test_julia_file_receiver.jl +++ /dev/null @@ -1,84 +0,0 @@ -#!/usr/bin/env julia -# Test script for large payload testing using binary transport -# Tests sending a large file (> 1MB) via smartsend with binary type -# Updated to match NATSBridge.jl API - -using NATS, JSON, UUIDs, Dates, PrettyPrinting, DataFrames, Arrow, HTTP - - -# workdir = - -# Include the bridge module -include("../src/NATSBridge.jl") -using .NATSBridge - -# Configuration -const SUBJECT = "/NATSBridge_test" -const NATS_URL = "nats.yiem.cc" -const FILESERVER_URL = "http://192.168.88.104:8080" - - - -# ------------------------------------------------------------------------------------------------ # -# test file transfer # -# ------------------------------------------------------------------------------------------------ # - -# Helper: Log with correlation ID -function log_trace(message) - timestamp = Dates.now() - println("[$timestamp] $message") -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 - # API: smartreceive(msg, download_handler; max_retries, base_delay, max_delay) - result = NATSBridge.smartreceive( - msg; - max_retries = 5, - base_delay = 100, - max_delay = 5000 - ) - - # Result is an envelope dictionary with payloads field containing list of (dataname, data, data_type) tuples - for (dataname, data, data_type) in result["payloads"] - # Check transport type from the envelope - # For link transport, data is the URL string - # For direct transport, data is the actual payload bytes - - if isa(data, Vector{UInt8}) - file_size = length(data) - log_trace("Received $(file_size) bytes of binary data for '$dataname' of type $data_type") - - # Save received data to a test file - output_path = "./new_$dataname" - write(output_path, data) - log_trace("Saved received data to $output_path") - else - log_trace("Received $(file_size) bytes of binary data for '$dataname' of type $data_type") - end - end - end - - # Keep listening for 10 seconds - sleep(120) - NATS.drain(conn) -end - - -# Run the test -println("Starting large binary payload test...") - -# # Run sender first -# println("start smartsend") -# test_large_binary_send() - -# Run receiver -println("testing smartreceive") -test_large_binary_receive() - -println("Test completed.") \ No newline at end of file diff --git a/test/test_julia_file_sender.jl b/test/test_julia_file_sender.jl deleted file mode 100644 index abce1c2..0000000 --- a/test/test_julia_file_sender.jl +++ /dev/null @@ -1,123 +0,0 @@ -#!/usr/bin/env julia -# Test script for large payload testing using binary transport -# Tests sending a large file (> 1MB) via smartsend with binary type -# Updated to match NATSBridge.jl API - -using NATS, JSON, UUIDs, Dates, PrettyPrinting, DataFrames, Arrow, HTTP - - -# workdir = - -# Include the bridge module -include("../src/NATSBridge.jl") -using .NATSBridge - -# Configuration -const SUBJECT = "/NATSBridge_test" -const NATS_URL = "nats.yiem.cc" -const FILESERVER_URL = "http://192.168.88.104:8080" - -# Create correlation ID for tracing -correlation_id = string(uuid4()) - - -# ------------------------------------------------------------------------------------------------ # -# test file transfer # -# ------------------------------------------------------------------------------------------------ # - - -# Helper: Log with correlation ID -function log_trace(message) - timestamp = Dates.now() - println("[$timestamp] [Correlation: $correlation_id] $message") -end - -# File upload handler for plik server -function plik_upload_handler(fileserver_url::String, dataname::String, data::Vector{UInt8})::Dict{String, Any} - # Get upload ID - url_getUploadID = "$fileserver_url/upload" - headers = ["Content-Type" => "application/json"] - body = """{ "OneShot" : true }""" - httpResponse = HTTP.request("POST", url_getUploadID, headers, body; body_is_form=false) - responseJson = JSON.parse(String(httpResponse.body)) - uploadid = responseJson["id"] - uploadtoken = responseJson["uploadToken"] - - # Upload file - file_multipart = HTTP.Multipart(dataname, IOBuffer(data), "application/octet-stream") - url_upload = "$fileserver_url/file/$uploadid" - headers = ["X-UploadToken" => uploadtoken] - - form = HTTP.Form(Dict("file" => file_multipart)) - httpResponse = HTTP.post(url_upload, headers, form) - responseJson = JSON.parse(String(httpResponse.body)) - - fileid = responseJson["id"] - url = "$fileserver_url/file/$uploadid/$fileid/$dataname" - - return Dict("status" => httpResponse.status, "uploadid" => uploadid, "fileid" => fileid, "url" => url) -end - -# Sender: Send large binary file via smartsend -function test_large_binary_send() - # Read the large file as binary data - - # test data 1 - file_path1 = "./testFile_large.zip" - file_data1 = read(file_path1) - filename1 = basename(file_path1) - data1 = (filename1, file_data1, "binary") - - # test data 2 - file_path2 = "./testFile_small.zip" - file_data2 = read(file_path2) - filename2 = basename(file_path2) - data2 = (filename2, file_data2, "binary") - - - - # Use smartsend with binary type - will automatically use link transport - # if file size exceeds the threshold (1MB by default) - # API: smartsend(subject, [(dataname, data, type), ...]; keywords...) - env, env_json_str = NATSBridge.smartsend( - SUBJECT, - [data1, data2]; # List of (dataname, data, type) tuples - broker_url = NATS_URL; - fileserver_url = FILESERVER_URL, - fileserver_upload_handler = plik_upload_handler, - size_threshold = 1_000_000, - correlation_id = correlation_id, - msg_purpose = "chat", - sender_name = "sender", - receiver_name = "", - receiver_id = "", - reply_to = "", - reply_to_msg_id = "", - is_publish = true # Publish the message to NATS - ) - - log_trace("Sent message with transport: $(env.payloads[1].transport)") - log_trace("Envelope type: $(env.payloads[1].payload_type)") - - # Check if link transport was used - if env.payloads[1].transport == "link" - log_trace("Using link transport - file uploaded to HTTP server") - log_trace("URL: $(env.payloads[1].data)") - else - log_trace("Using direct transport - payload sent via NATS") - end -end - -# Run the test -println("Starting large binary payload test...") -println("Correlation ID: $correlation_id") - -# Run sender first -println("start smartsend") -test_large_binary_send() - -# Run receiver -# println("testing smartreceive") -# test_large_binary_receive() - -println("Test completed.") \ No newline at end of file diff --git a/test/test_julia_mix_payloads_receiver.jl b/test/test_julia_mix_payloads_receiver.jl index 86d4413..4ccb932 100644 --- a/test/test_julia_mix_payloads_receiver.jl +++ b/test/test_julia_mix_payloads_receiver.jl @@ -13,7 +13,7 @@ include("../src/NATSBridge.jl") using .NATSBridge # Configuration -const SUBJECT = "/NATSBridge_mix_test" +const SUBJECT = "/natsbridge" const NATS_URL = "nats.yiem.cc" const FILESERVER_URL = "http://192.168.88.104:8080" @@ -97,15 +97,10 @@ function test_mix_receive() # Arrow table data - should be Arrow.Table if isa(data, Arrow.Table) log_trace(" Type: Arrow.Table") - log_trace(" Dimensions: $(size(data, 1)) rows x $(size(data, 2)) columns") - log_trace(" Columns: $(names(data))") - - # Display first few rows - log_trace(" First 5 rows:") - display(data[1:min(5, size(data, 1)), :]) # Convert to DataFrame for display and save df = DataFrame(data) + @show df[1:3, :] output_path = "./received_$dataname.arrow" io = IOBuffer() Arrow.write(io, data) @@ -117,18 +112,13 @@ function test_mix_receive() elseif data_type == "jsontable" # JSON table data - should be Vector{Dict} or Vector{NamedTuple} - if isa(data, Vector{<:Union{JSON.Object, Dict, NamedTuple}}) + @show "jsontable" typeof(data) + if isa(data, Vector{Any}) log_trace(" Type: Vector{Dict/NamedTuple}") - log_trace(" Number of rows: $(length(data))") - - # Display first few rows - log_trace(" First 3 rows:") - for i in 1:min(3, length(data)) - log_trace(" Row $i: $(data[i])") - end # Convert to DataFrame for display and save df = DataFrame(data) + @show df[1:3, :] log_trace(" Converted to DataFrame: $(size(df, 1)) rows x $(size(df, 2)) columns") # Save as JSON file @@ -147,28 +137,6 @@ function test_mix_receive() log_trace(" ERROR: Expected Vector{Dict/NamedTuple}, got $(typeof(data))") end - elseif data_type == "table" - # Table data - should be a DataFrame (backward compatibility) - data = DataFrame(data) - if isa(data, DataFrame) - log_trace(" Type: DataFrame") - log_trace(" Dimensions: $(size(data, 1)) rows x $(size(data, 2)) columns") - log_trace(" Columns: $(names(data))") - - # Display first few rows - log_trace(" First 5 rows:") - display(data[1:min(5, size(data, 1)), :]) - - # Save to Arrow file - output_path = "./received_$dataname.arrow" - io = IOBuffer() - Arrow.write(io, data) - write(output_path, take!(io)) - log_trace(" Saved to: $output_path") - else - log_trace(" ERROR: Expected DataFrame, got $(typeof(data))") - end - elseif data_type == "image" # Image data - should be Vector{UInt8} if isa(data, Vector{UInt8}) @@ -218,7 +186,7 @@ function test_mix_receive() log_trace(" Size: $(length(data)) bytes") # Save to file - output_path = "./received_$dataname.bin" + output_path = "./received_$dataname" write(output_path, data) log_trace(" Saved to: $output_path") else @@ -258,12 +226,12 @@ function test_mix_receive() if data_type in ["image", "audio", "video", "binary"] log_trace("$dataname: $(length(data)) bytes (binary)") elseif data_type == "arrowtable" - log_trace("$dataname: $(size(data, 1)) rows x $(size(data, 2)) columns (Arrow.Table)") + # log_trace("$dataname: $(size(data, 1)) rows x $(size(data, 2)) columns (Arrow.Table)") elseif data_type == "jsontable" log_trace("$dataname: $(length(data)) rows (Vector{Dict/NamedTuple})") elseif data_type == "table" data = DataFrame(data) - log_trace("$dataname: $(size(data, 1)) rows x $(size(data, 2)) columns (DataFrame)") + # log_trace("$dataname: $(size(data, 1)) rows x $(size(data, 2)) columns (DataFrame)") elseif data_type == "dictionary" log_trace("$dataname: $(length(JSON.json(data))) bytes (Dict)") elseif data_type == "text" diff --git a/test/test_julia_mix_payloads_sender.jl b/test/test_julia_mix_payloads_sender.jl index fac92a9..cb5b955 100644 --- a/test/test_julia_mix_payloads_sender.jl +++ b/test/test_julia_mix_payloads_sender.jl @@ -18,7 +18,7 @@ include("../src/NATSBridge.jl") using .NATSBridge # Configuration -const SUBJECT = "/test/mix" +const SUBJECT = "/natsbridge" const NATS_URL = "nats.yiem.cc" const FILESERVER_URL = "http://192.168.88.104:8080" @@ -122,10 +122,10 @@ function create_sample_data() # ~1.5MB of JSON data (150,000 rows) - should trigger link transport # NATSBridge.jl handles serialization: DataFrame -> Vector{Dict} -> JSON json_table_large = DataFrame( - id = 1:150_000, - name = ["user_$i" for i in 1:150_000], - score = rand(50:100, 150_000), - active = rand([true, false], 150_000) + id = 1:1_500_000, + name = ["user_$i" for i in 1:1_500_000], + score = rand(50:100, 1_500_000), + active = rand([true, false], 1_500_000) ) # Audio data (small binary - direct transport)