148 lines
3.4 KiB
Markdown
148 lines
3.4 KiB
Markdown
# Test Scenarios for Bi-Directional Data Bridge
|
|
|
|
## Scenario 1: Command & Control (Small JSON)
|
|
Tests small JSON payloads (< 1MB) sent directly via NATS.
|
|
|
|
### Julia (Receiver)
|
|
```julia
|
|
using NATS
|
|
using JSON3
|
|
|
|
# Subscribe to control subject
|
|
subscribe(nats, "control") do msg
|
|
env = MessageEnvelope(String(msg.data))
|
|
|
|
# Parse JSON payload
|
|
config = JSON3.read(env.payload)
|
|
|
|
# Execute simulation with parameters
|
|
step_size = config.step_size
|
|
iterations = config.iterations
|
|
|
|
# Send acknowledgment
|
|
response = Dict("status" => "Running", "correlation_id" => env.correlation_id)
|
|
publish(nats, "control_response", JSON3.stringify(response))
|
|
end
|
|
```
|
|
|
|
### JavaScript (Sender)
|
|
```javascript
|
|
const { SmartSend } = require('./js_bridge');
|
|
|
|
// Create small JSON config
|
|
const config = {
|
|
step_size: 0.01,
|
|
iterations: 1000
|
|
};
|
|
|
|
// Send via SmartSend with type="json"
|
|
await SmartSend("control", config, "json");
|
|
```
|
|
|
|
## Scenario 2: Deep Dive Analysis (Large Arrow Table)
|
|
Tests large Arrow tables (> 1MB) sent via HTTP fileserver.
|
|
|
|
### Julia (Sender)
|
|
```julia
|
|
using Arrow
|
|
using DataFrames
|
|
|
|
# Create large DataFrame (500MB, 10 million rows)
|
|
df = DataFrame(
|
|
id = 1:10_000_000,
|
|
value = rand(10_000_000),
|
|
category = rand(["A", "B", "C"], 10_000_000)
|
|
)
|
|
|
|
# Convert to Arrow IPC stream and send
|
|
await SmartSend("analysis_results", df, "table");
|
|
```
|
|
|
|
### JavaScript (Receiver)
|
|
```javascript
|
|
const { SmartReceive } = require('./js_bridge');
|
|
|
|
// Receive message with URL
|
|
const result = await SmartReceive(msg);
|
|
|
|
// Fetch data from HTTP server
|
|
const table = result.data;
|
|
|
|
// Load into Perspective.js or D3
|
|
// Use table data for visualization
|
|
```
|
|
|
|
## Scenario 3: Live Binary Processing
|
|
Tests binary data (binary) sent from JS to Julia for FFT/transcription.
|
|
|
|
### JavaScript (Sender)
|
|
```javascript
|
|
const { SmartSend } = require('./js_bridge');
|
|
|
|
// Capture binary chunk (2 seconds, 44.1kHz, 1 channel)
|
|
const binaryData = await navigator.mediaDevices.getUserMedia({ binary: true });
|
|
|
|
// Send as binary with metadata headers
|
|
await SmartSend("binary_input", binaryData, "binary", {
|
|
metadata: {
|
|
sample_rate: 44100,
|
|
channels: 1
|
|
}
|
|
});
|
|
```
|
|
|
|
### Julia (Receiver)
|
|
```julia
|
|
using WAV
|
|
using DSP
|
|
|
|
# Receive binary data
|
|
function process_binary(data)
|
|
# Perform FFT or AI transcription
|
|
spectrum = fft(data)
|
|
|
|
# Send results back (JSON + Arrow table)
|
|
results = Dict("transcription" => "sample text", "spectrum" => spectrum)
|
|
await SmartSend("binary_output", results, "json")
|
|
end
|
|
```
|
|
|
|
## Scenario 4: Catch-Up (JetStream)
|
|
Tests temporal decoupling with NATS JetStream.
|
|
|
|
### Julia (Producer)
|
|
```julia
|
|
# Publish to JetStream
|
|
using NATS
|
|
|
|
function publish_health_status(nats)
|
|
jetstream = JetStream(nats, "health_updates")
|
|
|
|
while true
|
|
status = Dict("cpu" => rand(), "memory" => rand())
|
|
publish(jetstream, "health", status)
|
|
sleep(5) # Every 5 seconds
|
|
end
|
|
end
|
|
```
|
|
|
|
### JavaScript (Consumer)
|
|
```javascript
|
|
const { connect } = require('nats');
|
|
|
|
const nc = await connect({ servers: ['nats://localhost:4222'] });
|
|
const js = nc.jetstream();
|
|
|
|
// Request replay from last 10 minutes
|
|
const consumer = await js.pullSubscribe("health", {
|
|
durable_name: "catchup",
|
|
max_batch: 100,
|
|
max_ack_wait: 30000
|
|
});
|
|
|
|
// Process historical and real-time messages
|
|
for await (const msg of consumer) {
|
|
const result = await SmartReceive(msg);
|
|
// Process the data
|
|
msg.ack();
|
|
} |