This commit is contained in:
2026-02-19 15:58:22 +07:00
parent 782a935d3d
commit a260def38d
7 changed files with 620 additions and 308 deletions

View File

@@ -0,0 +1,213 @@
#!/usr/bin/env julia
# Test script for mixed-content message testing
# Tests receiving a mix of text, json, table, image, audio, video, and binary data
# from Julia serviceA to Julia serviceB using NATSBridge.jl smartreceive
#
# This test demonstrates that any combination and any number of mixed content
# can be sent and received correctly.
using NATS, JSON, UUIDs, Dates, PrettyPrinting, DataFrames, Arrow, HTTP, Base64
# Include the bridge module
include("../src/NATSBridge.jl")
using .NATSBridge
# Configuration
const SUBJECT = "/NATSBridge_mix_test"
const NATS_URL = "nats.yiem.cc"
const FILESERVER_URL = "http://192.168.88.104:8080"
# ------------------------------------------------------------------------------------------------ #
# test mixed content transfer #
# ------------------------------------------------------------------------------------------------ #
# Helper: Log with correlation ID
function log_trace(message)
timestamp = Dates.now()
println("[$timestamp] $message")
end
# Receiver: Listen for messages and verify mixed content handling
function test_mix_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
)
log_trace("Received $(length(result)) payloads")
# Result is a list of (dataname, data, data_type) tuples
for (dataname, data, data_type) in result
log_trace("\n=== Payload: $dataname (type: $data_type) ===")
# Handle different data types
if data_type == "text"
# Text data - should be a String
if isa(data, String)
log_trace(" Type: String")
log_trace(" Length: $(length(data)) characters")
# Display first 200 characters
if length(data) > 200
log_trace(" First 200 chars: $(data[1:200])...")
else
log_trace(" Content: $data")
end
# Save to file
output_path = "./received_$dataname.txt"
write(output_path, data)
log_trace(" Saved to: $output_path")
else
log_trace(" ERROR: Expected String, got $(typeof(data))")
end
elseif data_type == "dictionary"
# Dictionary data - should be JSON object
if isa(data, JSON.Object{String, Any})
log_trace(" Type: Dict")
log_trace(" Keys: $(keys(data))")
# Display nested content
for (key, value) in data
log_trace(" $key => $value")
end
# Save to JSON file
output_path = "./received_$dataname.json"
json_str = JSON.json(data, 2)
write(output_path, json_str)
log_trace(" Saved to: $output_path")
else
log_trace(" ERROR: Expected Dict, got $(typeof(data))")
end
elseif data_type == "table"
# Table data - should be a DataFrame
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})
log_trace(" Type: Vector{UInt8} (binary)")
log_trace(" Size: $(length(data)) bytes")
# Save to file
output_path = "./received_$dataname.bin"
write(output_path, data)
log_trace(" Saved to: $output_path")
else
log_trace(" ERROR: Expected Vector{UInt8}, got $(typeof(data))")
end
elseif data_type == "audio"
# Audio data - should be Vector{UInt8}
if isa(data, Vector{UInt8})
log_trace(" Type: Vector{UInt8} (binary)")
log_trace(" Size: $(length(data)) bytes")
# Save to file
output_path = "./received_$dataname.bin"
write(output_path, data)
log_trace(" Saved to: $output_path")
else
log_trace(" ERROR: Expected Vector{UInt8}, got $(typeof(data))")
end
elseif data_type == "video"
# Video data - should be Vector{UInt8}
if isa(data, Vector{UInt8})
log_trace(" Type: Vector{UInt8} (binary)")
log_trace(" Size: $(length(data)) bytes")
# Save to file
output_path = "./received_$dataname.bin"
write(output_path, data)
log_trace(" Saved to: $output_path")
else
log_trace(" ERROR: Expected Vector{UInt8}, got $(typeof(data))")
end
elseif data_type == "binary"
# Binary data - should be Vector{UInt8}
if isa(data, Vector{UInt8})
log_trace(" Type: Vector{UInt8} (binary)")
log_trace(" Size: $(length(data)) bytes")
# Save to file
output_path = "./received_$dataname.bin"
write(output_path, data)
log_trace(" Saved to: $output_path")
else
log_trace(" ERROR: Expected Vector{UInt8}, got $(typeof(data))")
end
else
log_trace(" ERROR: Unknown data type '$data_type'")
end
end
# Summary
println("\n=== Verification Summary ===")
text_count = count(x -> x[3] == "text", result)
dict_count = count(x -> x[3] == "dictionary", result)
table_count = count(x -> x[3] == "table", result)
image_count = count(x -> x[3] == "image", result)
audio_count = count(x -> x[3] == "audio", result)
video_count = count(x -> x[3] == "video", result)
binary_count = count(x -> x[3] == "binary", result)
log_trace("Text payloads: $text_count")
log_trace("Dictionary payloads: $dict_count")
log_trace("Table payloads: $table_count")
log_trace("Image payloads: $image_count")
log_trace("Audio payloads: $audio_count")
log_trace("Video payloads: $video_count")
log_trace("Binary payloads: $binary_count")
end
# Keep listening for 2 minutes
sleep(120)
NATS.drain(conn)
end
# Run the test
println("Starting mixed-content transport test...")
println("Note: This receiver will wait for messages from the sender.")
println("Run test_julia_to_julia_mix_sender.jl first to send test data.")
# Run receiver
println("\ntesting smartreceive for mixed content")
test_mix_receive()
println("\nTest completed.")

View File

@@ -0,0 +1,196 @@
#!/usr/bin/env julia
# Test script for mixed-content message testing
# Tests sending a mix of text, json, table, image, audio, video, and binary data
# from Julia serviceA to Julia serviceB using NATSBridge.jl smartsend
#
# This test demonstrates that any combination and any number of mixed content
# can be sent and received correctly.
using NATS, JSON, UUIDs, Dates, PrettyPrinting, DataFrames, Arrow, HTTP, Base64
# Include the bridge module
include("../src/NATSBridge.jl")
using .NATSBridge
# Configuration
const SUBJECT = "/NATSBridge_mix_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 mixed content 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
# Helper: Create sample data for each type
function create_sample_data()
# Text data (small - direct transport)
text_data = "Hello! This is a test chat message. 🎉\nHow are you doing today? 😊"
# Dictionary/JSON data (medium - could be direct or link)
dict_data = Dict(
"type" => "chat",
"sender" => "serviceA",
"receiver" => "serviceB",
"metadata" => Dict(
"timestamp" => string(Dates.now()),
"priority" => "high",
"tags" => ["urgent", "chat", "test"]
),
"content" => Dict(
"text" => "This is a JSON-formatted chat message with nested structure.",
"format" => "markdown",
"mentions" => ["user1", "user2"]
)
)
# Table data (DataFrame - small - direct transport)
table_data = DataFrame(
id = 1:10,
message = ["msg_$i" for i in 1:10],
sender = ["sender_$i" for i in 1:10],
timestamp = [string(Dates.now()) for _ in 1:10],
priority = rand(1:3, 10)
)
# Image data (small binary - direct transport)
# Create a simple 10x10 pixel PNG-like data (128 bytes header + 100 pixels = 112 bytes)
# Using simple RGB data (10*10*3 = 300 bytes of pixel data)
image_width = 10
image_height = 10
image_data = UInt8[]
# PNG header (simplified)
push!(image_data, 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A)
# Simple RGB data (RGBRGBRGB...)
for i in 1:image_width*image_height
push!(image_data, 0xFF, 0x00, 0x00) # Red pixel
end
# Audio data (small binary - direct transport)
# Create a simple audio-like data (100 bytes)
audio_data = UInt8[rand(1:255) for _ in 1:100]
# Video data (small binary - direct transport)
# Create a simple video-like data (150 bytes)
video_data = UInt8[rand(1:255) for _ in 1:150]
# Binary data (small - direct transport)
binary_data = UInt8[rand(1:255) for _ in 1:200]
return (
text_data,
dict_data,
table_data,
image_data,
audio_data,
video_data,
binary_data
)
end
# Sender: Send mixed content via smartsend
function test_mix_send()
# Create sample data
(text_data, dict_data, table_data, image_data, audio_data, video_data, binary_data) = create_sample_data()
# Create payloads list - mixed content with different types
payloads = [
("chat_text", text_data, "text"),
("chat_json", dict_data, "dictionary"),
("chat_table", table_data, "table"),
("user_image", image_data, "image"),
("audio_clip", audio_data, "audio"),
("video_clip", video_data, "video"),
("binary_file", binary_data, "binary")
]
# Use smartsend with mixed content
env = NATSBridge.smartsend(
SUBJECT,
payloads, # List of (dataname, data, type) tuples
nats_url = NATS_URL,
fileserver_url = FILESERVER_URL,
fileserverUploadHandler = plik_upload_handler,
size_threshold = 1_000_000, # 1MB threshold
correlation_id = correlation_id,
msg_purpose = "chat",
sender_name = "mix_sender",
receiver_name = "",
receiver_id = "",
reply_to = "",
reply_to_msg_id = ""
)
log_trace("Sent message with $(length(env.payloads)) payloads")
# Log transport type for each payload
for (i, payload) in enumerate(env.payloads)
log_trace("Payload $i ('$payload.dataname'):")
log_trace(" Transport: $(payload.transport)")
log_trace(" Type: $(payload.type)")
log_trace(" Size: $(payload.size) bytes")
log_trace(" Encoding: $(payload.encoding)")
if payload.transport == "link"
log_trace(" URL: $(payload.data)")
end
end
# Summary
println("\n--- Transport Summary ---")
direct_count = count(p -> p.transport == "direct", env.payloads)
link_count = count(p -> p.transport == "link", env.payloads)
log_trace("Direct transport: $direct_count payloads")
log_trace("Link transport: $link_count payloads")
end
# Run the test
println("Starting mixed-content transport test...")
println("Correlation ID: $correlation_id")
# Run sender
println("start smartsend for mixed content")
test_mix_send()
println("\nTest completed.")
println("Note: Run test_julia_to_julia_mix_receiver.jl to receive the messages.")

View File

@@ -0,0 +1,83 @@
#!/usr/bin/env julia
# Test script for text transport testing
# Tests receiving 1 large and 1 small text from Julia serviceA to Julia serviceB
# Uses NATSBridge.jl smartreceive with "text" type
using NATS, JSON, UUIDs, Dates, PrettyPrinting, DataFrames, Arrow, HTTP
# Include the bridge module
include("../src/NATSBridge.jl")
using .NATSBridge
# Configuration
const SUBJECT = "/NATSBridge_text_test"
const NATS_URL = "nats.yiem.cc"
const FILESERVER_URL = "http://192.168.88.104:8080"
# ------------------------------------------------------------------------------------------------ #
# test text transfer #
# ------------------------------------------------------------------------------------------------ #
# Helper: Log with correlation ID
function log_trace(message)
timestamp = Dates.now()
println("[$timestamp] $message")
end
# Receiver: Listen for messages and verify text handling
function test_text_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 a list of (dataname, data, data_type) tuples
for (dataname, data, data_type) in result
if isa(data, String)
log_trace("Received text '$dataname' of type $data_type")
log_trace(" Length: $(length(data)) characters")
# Display first 100 characters
if length(data) > 100
log_trace(" First 100 characters: $(data[1:100])...")
else
log_trace(" Content: $data")
end
# Save to file
output_path = "./received_$dataname.txt"
write(output_path, data)
log_trace("Saved text to $output_path")
else
log_trace("Received unexpected data type for '$dataname': $(typeof(data))")
end
end
end
# Keep listening for 10 seconds
sleep(120)
NATS.drain(conn)
end
# Run the test
println("Starting text transport test...")
println("Note: This receiver will wait for messages from the sender.")
println("Run test_julia_to_julia_text_sender.jl first to send test data.")
# Run receiver
println("testing smartreceive for text")
test_text_receive()
println("Test completed.")

View File

@@ -0,0 +1,119 @@
#!/usr/bin/env julia
# Test script for text transport testing
# Tests sending 1 large and 1 small text from Julia serviceA to Julia serviceB
# Uses NATSBridge.jl smartsend with "text" type
using NATS, JSON, UUIDs, Dates, PrettyPrinting, DataFrames, Arrow, HTTP
# Include the bridge module
include("../src/NATSBridge.jl")
using .NATSBridge
# Configuration
const SUBJECT = "/NATSBridge_text_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 text 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 text via smartsend
function test_text_send()
# Create a small text (will use direct transport)
small_text = "Hello, this is a small text message. Testing direct transport via NATS."
# Create a large text (will use link transport if > 1MB)
# Generate a larger text (~2MB to ensure link transport)
large_text = join(["Line $i: This is a sample text line with some content to pad the size. " for i in 1:50000], "")
# Test data 1: small text
data1 = ("small_text", small_text, "text")
# Test data 2: large text
data2 = ("large_text", large_text, "text")
# Use smartsend with text type
# For small text: will use direct transport (Base64 encoded UTF-8)
# For large text: will use link transport (uploaded to fileserver)
env = NATSBridge.smartsend(
SUBJECT,
[data1, data2], # List of (dataname, data, type) tuples
nats_url = NATS_URL,
fileserver_url = FILESERVER_URL,
fileserverUploadHandler = plik_upload_handler,
size_threshold = 1_000_000, # 1MB threshold
correlation_id = correlation_id,
msg_purpose = "chat",
sender_name = "text_sender",
receiver_name = "",
receiver_id = "",
reply_to = "",
reply_to_msg_id = ""
)
log_trace("Sent message with $(length(env.payloads)) payloads")
# Log transport type for each payload
for (i, payload) in enumerate(env.payloads)
log_trace("Payload $i ('$payload.dataname'):")
log_trace(" Transport: $(payload.transport)")
log_trace(" Type: $(payload.type)")
log_trace(" Size: $(payload.size) bytes")
log_trace(" Encoding: $(payload.encoding)")
if payload.transport == "link"
log_trace(" URL: $(payload.data)")
end
end
end
# Run the test
println("Starting text transport test...")
println("Correlation ID: $correlation_id")
# Run sender
println("start smartsend for text")
test_text_send()
println("Test completed.")