add test
This commit is contained in:
98
test/julia_to_julia_dict_receiver.jl
Normal file
98
test/julia_to_julia_dict_receiver.jl
Normal file
@@ -0,0 +1,98 @@
|
||||
#!/usr/bin/env julia
|
||||
# Test script for dictionary transfer from Julia serviceA to Julia serviceB
|
||||
# Demonstrates the "Command & Control" scenario (small dictionary) using NATSBridge
|
||||
#
|
||||
# This is serviceB - the receiver that receives a dummy dictionary from serviceA
|
||||
|
||||
using NATSBridge
|
||||
using UUIDs
|
||||
using JSON
|
||||
using Dates
|
||||
|
||||
# Include the NATSBridge module
|
||||
include("src/NATSBridge.jl")
|
||||
|
||||
# Configuration
|
||||
const SUBJECT = "/NATSBridge_dict_test"
|
||||
const NATS_URL = "nats://localhost:4222"
|
||||
|
||||
# Helper: Log with correlation ID
|
||||
function log_trace(correlation_id::String, message::String)
|
||||
timestamp = Dates.now()
|
||||
println("[$timestamp] [Correlation: $correlation_id] $message")
|
||||
end
|
||||
|
||||
|
||||
# Receiver: Receive and process dictionary from serviceA
|
||||
function receive_dictionary()
|
||||
# Connect to NATS
|
||||
conn = NATS.connect(NATS_URL)
|
||||
|
||||
# Subscribe to the subject
|
||||
subscription = NATS.subscribe(conn, SUBJECT)
|
||||
|
||||
println("Listening for dictionary messages on '$SUBJECT'...")
|
||||
println("Press Ctrl+C to stop listening.")
|
||||
|
||||
# Listen for messages
|
||||
while true
|
||||
# Wait for a message with a 1-second timeout
|
||||
msg = NATS.waitfor(subscription, 1.0)
|
||||
|
||||
if msg !== nothing
|
||||
# Extract correlation ID for logging
|
||||
json_data = JSON.parse(String(msg.payload))
|
||||
cid = json_data["correlationId"]
|
||||
|
||||
log_trace(cid, "Received message from $(json_data["senderName"])")
|
||||
|
||||
# Process the message using smartreceive
|
||||
payloads = NATSBridge.smartreceive(
|
||||
msg;
|
||||
fileserverDownloadHandler = (url, max_retries, base_delay, max_delay, cid) ->
|
||||
NATSBridge._fetch_with_backoff(url, max_retries, base_delay, max_delay, cid),
|
||||
max_retries = 5,
|
||||
base_delay = 100,
|
||||
max_delay = 5000
|
||||
)
|
||||
|
||||
log_trace(cid, "Processed $(length(payloads)) payload(s)")
|
||||
|
||||
# Process each payload
|
||||
for (dataname, data, payload_type) in payloads
|
||||
log_trace(cid, "Payload '$dataname' type: $payload_type")
|
||||
|
||||
# Handle dictionary type
|
||||
if payload_type == "dictionary"
|
||||
println("\nReceived dictionary:")
|
||||
println(JSON.json(data, 2))
|
||||
|
||||
# Extract and display specific fields
|
||||
if isa(data, Dict)
|
||||
command = get(data, "command", "unknown")
|
||||
println("\nCommand: $command")
|
||||
|
||||
# Optionally send acknowledgment
|
||||
reply_to = get(json_data, "replyTo", "")
|
||||
if !isempty(reply_to)
|
||||
log_trace(cid, "Reply to: $reply_to")
|
||||
# Could send ACK here
|
||||
end
|
||||
end
|
||||
else
|
||||
println("\nReceived non-dictionary payload: $dataname (type: $payload_type)")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# Run the receiver
|
||||
println("Starting dictionary receiver...")
|
||||
println("Subject: $SUBJECT")
|
||||
println("NATS URL: $NATS_URL")
|
||||
println("="^50)
|
||||
|
||||
# Run receiver (this will block and listen for messages)
|
||||
receive_dictionary()
|
||||
Reference in New Issue
Block a user