update
This commit is contained in:
@@ -1,80 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
// Test script for Dictionary transport testing
|
||||
// Tests receiving 1 large and 1 small Dictionaries via direct and link transport
|
||||
// Uses NATSBridge.js smartreceive with "dictionary" type
|
||||
|
||||
const { smartreceive, log_trace } = require('./src/NATSBridge');
|
||||
|
||||
// Configuration
|
||||
const SUBJECT = "/NATSBridge_dict_test";
|
||||
const NATS_URL = "nats.yiem.cc";
|
||||
|
||||
// Helper: Log with correlation ID
|
||||
function log_trace(message) {
|
||||
const timestamp = new Date().toISOString();
|
||||
console.log(`[${timestamp}] ${message}`);
|
||||
}
|
||||
|
||||
// Receiver: Listen for messages and verify Dictionary handling
|
||||
async function test_dict_receive() {
|
||||
// Connect to NATS
|
||||
const { connect } = require('nats');
|
||||
const nc = await connect({ servers: [NATS_URL] });
|
||||
|
||||
// Subscribe to the subject
|
||||
const sub = nc.subscribe(SUBJECT);
|
||||
|
||||
for await (const msg of sub) {
|
||||
log_trace(`Received message on ${msg.subject}`);
|
||||
|
||||
// Use NATSBridge.smartreceive to handle the data
|
||||
const result = await smartreceive(
|
||||
msg,
|
||||
{
|
||||
maxRetries: 5,
|
||||
baseDelay: 100,
|
||||
maxDelay: 5000
|
||||
}
|
||||
);
|
||||
|
||||
// Result is an envelope dictionary with payloads field
|
||||
// Access payloads with result.payloads
|
||||
for (const { dataname, data, type } of result.payloads) {
|
||||
if (typeof data === 'object' && data !== null && !Array.isArray(data)) {
|
||||
log_trace(`Received Dictionary '${dataname}' of type ${type}`);
|
||||
|
||||
// Display dictionary contents
|
||||
console.log(" Contents:");
|
||||
for (const [key, value] of Object.entries(data)) {
|
||||
console.log(` ${key} => ${value}`);
|
||||
}
|
||||
|
||||
// Save to JSON file
|
||||
const fs = require('fs');
|
||||
const output_path = `./received_${dataname}.json`;
|
||||
const json_str = JSON.stringify(data, null, 2);
|
||||
fs.writeFileSync(output_path, json_str);
|
||||
log_trace(`Saved Dictionary to ${output_path}`);
|
||||
} else {
|
||||
log_trace(`Received unexpected data type for '${dataname}': ${typeof data}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Keep listening for 10 seconds
|
||||
setTimeout(() => {
|
||||
nc.close();
|
||||
process.exit(0);
|
||||
}, 120000);
|
||||
}
|
||||
|
||||
// Run the test
|
||||
console.log("Starting Dictionary transport test...");
|
||||
console.log("Note: This receiver will wait for messages from the sender.");
|
||||
console.log("Run test_js_to_js_dict_sender.js first to send test data.");
|
||||
|
||||
// Run receiver
|
||||
console.log("testing smartreceive");
|
||||
test_dict_receive();
|
||||
|
||||
console.log("Test completed.");
|
||||
@@ -1,165 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
// Test script for Dictionary transport testing
|
||||
// Tests sending 1 large and 1 small Dictionaries via direct and link transport
|
||||
// Uses NATSBridge.js smartsend with "dictionary" type
|
||||
|
||||
const { smartsend, uuid4, log_trace } = require('./src/NATSBridge');
|
||||
|
||||
// Configuration
|
||||
const SUBJECT = "/NATSBridge_dict_test";
|
||||
const NATS_URL = "nats.yiem.cc";
|
||||
const FILESERVER_URL = "http://192.168.88.104:8080";
|
||||
|
||||
// Create correlation ID for tracing
|
||||
const correlation_id = uuid4();
|
||||
|
||||
// Helper: Log with correlation ID
|
||||
function log_trace(message) {
|
||||
const timestamp = new Date().toISOString();
|
||||
console.log(`[${timestamp}] [Correlation: ${correlation_id}] ${message}`);
|
||||
}
|
||||
|
||||
// File upload handler for plik server
|
||||
async function plik_upload_handler(fileserver_url, dataname, data, correlation_id) {
|
||||
// Get upload ID
|
||||
const url_getUploadID = `${fileserver_url}/upload`;
|
||||
const headers = {
|
||||
"Content-Type": "application/json"
|
||||
};
|
||||
const body = JSON.stringify({ OneShot: true });
|
||||
|
||||
let response = await fetch(url_getUploadID, {
|
||||
method: "POST",
|
||||
headers: headers,
|
||||
body: body
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to get upload ID: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const responseJson = await response.json();
|
||||
const uploadid = responseJson.id;
|
||||
const uploadtoken = responseJson.uploadToken;
|
||||
|
||||
// Upload file
|
||||
const formData = new FormData();
|
||||
const blob = new Blob([data], { type: "application/octet-stream" });
|
||||
formData.append("file", blob, dataname);
|
||||
|
||||
response = await fetch(`${fileserver_url}/file/${uploadid}`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"X-UploadToken": uploadtoken
|
||||
},
|
||||
body: formData
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to upload file: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const fileResponseJson = await response.json();
|
||||
const fileid = fileResponseJson.id;
|
||||
|
||||
const url = `${fileserver_url}/file/${uploadid}/${fileid}/${encodeURIComponent(dataname)}`;
|
||||
|
||||
return {
|
||||
status: response.status,
|
||||
uploadid: uploadid,
|
||||
fileid: fileid,
|
||||
url: url
|
||||
};
|
||||
}
|
||||
|
||||
// Sender: Send Dictionaries via smartsend
|
||||
async function test_dict_send() {
|
||||
// Create a small Dictionary (will use direct transport)
|
||||
const small_dict = {
|
||||
name: "Alice",
|
||||
age: 30,
|
||||
scores: [95, 88, 92],
|
||||
metadata: {
|
||||
height: 155,
|
||||
weight: 55
|
||||
}
|
||||
};
|
||||
|
||||
// Create a large Dictionary (will use link transport if > 1MB)
|
||||
const large_dict_ids = [];
|
||||
const large_dict_names = [];
|
||||
const large_dict_scores = [];
|
||||
const large_dict_categories = [];
|
||||
|
||||
for (let i = 0; i < 50000; i++) {
|
||||
large_dict_ids.push(i + 1);
|
||||
large_dict_names.push(`User_${i}`);
|
||||
large_dict_scores.push(Math.floor(Math.random() * 100) + 1);
|
||||
large_dict_categories.push(`Category_${Math.floor(Math.random() * 10) + 1}`);
|
||||
}
|
||||
|
||||
const large_dict = {
|
||||
ids: large_dict_ids,
|
||||
names: large_dict_names,
|
||||
scores: large_dict_scores,
|
||||
categories: large_dict_categories,
|
||||
metadata: {
|
||||
source: "test_generator",
|
||||
timestamp: new Date().toISOString()
|
||||
}
|
||||
};
|
||||
|
||||
// Test data 1: small Dictionary
|
||||
const data1 = { dataname: "small_dict", data: small_dict, type: "dictionary" };
|
||||
|
||||
// Test data 2: large Dictionary
|
||||
const data2 = { dataname: "large_dict", data: large_dict, type: "dictionary" };
|
||||
|
||||
// Use smartsend with dictionary type
|
||||
// For small Dictionary: will use direct transport (JSON encoded)
|
||||
// For large Dictionary: will use link transport (uploaded to fileserver)
|
||||
const { env, env_json_str } = await smartsend(
|
||||
SUBJECT,
|
||||
[data1, data2],
|
||||
{
|
||||
natsUrl: NATS_URL,
|
||||
fileserverUrl: FILESERVER_URL,
|
||||
fileserverUploadHandler: plik_upload_handler,
|
||||
sizeThreshold: 1_000_000,
|
||||
correlationId: correlation_id,
|
||||
msgPurpose: "chat",
|
||||
senderName: "dict_sender",
|
||||
receiverName: "",
|
||||
receiverId: "",
|
||||
replyTo: "",
|
||||
replyToMsgId: "",
|
||||
isPublish: true // Publish the message to NATS
|
||||
}
|
||||
);
|
||||
|
||||
log_trace(`Sent message with ${env.payloads.length} payloads`);
|
||||
|
||||
// Log transport type for each payload
|
||||
for (let i = 0; i < env.payloads.length; i++) {
|
||||
const payload = env.payloads[i];
|
||||
log_trace(`Payload ${i + 1} ('${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}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Run the test
|
||||
console.log("Starting Dictionary transport test...");
|
||||
console.log(`Correlation ID: ${correlation_id}`);
|
||||
|
||||
// Run sender
|
||||
console.log("start smartsend for dictionaries");
|
||||
test_dict_send();
|
||||
|
||||
console.log("Test completed.");
|
||||
@@ -1,71 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
// Test script for large payload testing using binary transport
|
||||
// Tests receiving a large file (> 1MB) via smartsend with binary type
|
||||
|
||||
const { smartreceive, log_trace } = require('./src/NATSBridge');
|
||||
|
||||
// Configuration
|
||||
const SUBJECT = "/NATSBridge_test";
|
||||
const NATS_URL = "nats.yiem.cc";
|
||||
|
||||
// Helper: Log with correlation ID
|
||||
function log_trace(message) {
|
||||
const timestamp = new Date().toISOString();
|
||||
console.log(`[${timestamp}] ${message}`);
|
||||
}
|
||||
|
||||
// Receiver: Listen for messages and verify large payload handling
|
||||
async function test_large_binary_receive() {
|
||||
// Connect to NATS
|
||||
const { connect } = require('nats');
|
||||
const nc = await connect({ servers: [NATS_URL] });
|
||||
|
||||
// Subscribe to the subject
|
||||
const sub = nc.subscribe(SUBJECT);
|
||||
|
||||
for await (const msg of sub) {
|
||||
log_trace(`Received message on ${msg.subject}`);
|
||||
|
||||
// Use NATSBridge.smartreceive to handle the data
|
||||
const result = await smartreceive(
|
||||
msg,
|
||||
{
|
||||
maxRetries: 5,
|
||||
baseDelay: 100,
|
||||
maxDelay: 5000
|
||||
}
|
||||
);
|
||||
|
||||
// Result is an envelope dictionary with payloads field
|
||||
// Access payloads with result.payloads
|
||||
for (const { dataname, data, type } of result.payloads) {
|
||||
if (data instanceof Uint8Array || Array.isArray(data)) {
|
||||
const file_size = data.length;
|
||||
log_trace(`Received ${file_size} bytes of binary data for '${dataname}' of type ${type}`);
|
||||
|
||||
// Save received data to a test file
|
||||
const fs = require('fs');
|
||||
const output_path = `./new_${dataname}`;
|
||||
fs.writeFileSync(output_path, Buffer.from(data));
|
||||
log_trace(`Saved received data to ${output_path}`);
|
||||
} else {
|
||||
log_trace(`Received unexpected data type for '${dataname}': ${typeof data}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Keep listening for 10 seconds
|
||||
setTimeout(() => {
|
||||
nc.close();
|
||||
process.exit(0);
|
||||
}, 120000);
|
||||
}
|
||||
|
||||
// Run the test
|
||||
console.log("Starting large binary payload test...");
|
||||
|
||||
// Run receiver
|
||||
console.log("testing smartreceive");
|
||||
test_large_binary_receive();
|
||||
|
||||
console.log("Test completed.");
|
||||
@@ -1,144 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
// Test script for large payload testing using binary transport
|
||||
// Tests sending a large file (> 1MB) via smartsend with binary type
|
||||
|
||||
const { smartsend, uuid4, log_trace } = require('./src/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
|
||||
const correlation_id = uuid4();
|
||||
|
||||
// Helper: Log with correlation ID
|
||||
function log_trace(message) {
|
||||
const timestamp = new Date().toISOString();
|
||||
console.log(`[${timestamp}] [Correlation: ${correlation_id}] ${message}`);
|
||||
}
|
||||
|
||||
// File upload handler for plik server
|
||||
async function plik_upload_handler(fileserver_url, dataname, data, correlation_id) {
|
||||
log_trace(correlation_id, `Uploading ${dataname} to fileserver: ${fileserver_url}`);
|
||||
|
||||
// Step 1: Get upload ID and token
|
||||
const url_getUploadID = `${fileserver_url}/upload`;
|
||||
const headers = {
|
||||
"Content-Type": "application/json"
|
||||
};
|
||||
const body = JSON.stringify({ OneShot: true });
|
||||
|
||||
let response = await fetch(url_getUploadID, {
|
||||
method: "POST",
|
||||
headers: headers,
|
||||
body: body
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to get upload ID: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const responseJson = await response.json();
|
||||
const uploadid = responseJson.id;
|
||||
const uploadtoken = responseJson.uploadToken;
|
||||
|
||||
// Step 2: Upload file data
|
||||
const url_upload = `${fileserver_url}/file/${uploadid}`;
|
||||
|
||||
// Create multipart form data
|
||||
const formData = new FormData();
|
||||
const blob = new Blob([data], { type: "application/octet-stream" });
|
||||
formData.append("file", blob, dataname);
|
||||
|
||||
response = await fetch(url_upload, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"X-UploadToken": uploadtoken
|
||||
},
|
||||
body: formData
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to upload file: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const fileResponseJson = await response.json();
|
||||
const fileid = fileResponseJson.id;
|
||||
|
||||
// Build the download URL
|
||||
const url = `${fileserver_url}/file/${uploadid}/${fileid}/${encodeURIComponent(dataname)}`;
|
||||
|
||||
log_trace(correlation_id, `Uploaded to URL: ${url}`);
|
||||
|
||||
return {
|
||||
status: response.status,
|
||||
uploadid: uploadid,
|
||||
fileid: fileid,
|
||||
url: url
|
||||
};
|
||||
}
|
||||
|
||||
// Sender: Send large binary file via smartsend
|
||||
async function test_large_binary_send() {
|
||||
// Read the large file as binary data
|
||||
const fs = require('fs');
|
||||
|
||||
// Test data 1
|
||||
const file_path1 = './testFile_large.zip';
|
||||
const file_data1 = fs.readFileSync(file_path1);
|
||||
const filename1 = 'testFile_large.zip';
|
||||
const data1 = { dataname: filename1, data: file_data1, type: "binary" };
|
||||
|
||||
// Test data 2
|
||||
const file_path2 = './testFile_small.zip';
|
||||
const file_data2 = fs.readFileSync(file_path2);
|
||||
const filename2 = 'testFile_small.zip';
|
||||
const data2 = { dataname: filename2, data: file_data2, type: "binary" };
|
||||
|
||||
// Use smartsend with binary type - will automatically use link transport
|
||||
// if file size exceeds the threshold (1MB by default)
|
||||
const { env, env_json_str } = await smartsend(
|
||||
SUBJECT,
|
||||
[data1, data2],
|
||||
{
|
||||
natsUrl: NATS_URL,
|
||||
fileserverUrl: FILESERVER_URL,
|
||||
fileserverUploadHandler: plik_upload_handler,
|
||||
sizeThreshold: 1_000_000,
|
||||
correlationId: correlation_id,
|
||||
msgPurpose: "chat",
|
||||
senderName: "sender",
|
||||
receiverName: "",
|
||||
receiverId: "",
|
||||
replyTo: "",
|
||||
replyToMsgId: "",
|
||||
isPublish: true // Publish the message to NATS
|
||||
}
|
||||
);
|
||||
|
||||
log_trace(`Sent message with transport: ${env.payloads[0].transport}`);
|
||||
log_trace(`Envelope type: ${env.payloads[0].type}`);
|
||||
|
||||
// Check if link transport was used
|
||||
if (env.payloads[0].transport === "link") {
|
||||
log_trace("Using link transport - file uploaded to HTTP server");
|
||||
log_trace(`URL: ${env.payloads[0].data}`);
|
||||
} else {
|
||||
log_trace("Using direct transport - payload sent via NATS");
|
||||
}
|
||||
}
|
||||
|
||||
// Run the test
|
||||
console.log("Starting large binary payload test...");
|
||||
console.log(`Correlation ID: ${correlation_id}`);
|
||||
|
||||
// Run sender first
|
||||
console.log("start smartsend");
|
||||
test_large_binary_send();
|
||||
|
||||
// Run receiver
|
||||
// console.log("testing smartreceive");
|
||||
// test_large_binary_receive();
|
||||
|
||||
console.log("Test completed.");
|
||||
@@ -1,277 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
// Test script for mixed-content message testing
|
||||
// Tests sending a mix of text, json, table, image, audio, video, and binary data
|
||||
// from JavaScript serviceA to JavaScript serviceB using NATSBridge.js smartsend
|
||||
//
|
||||
// This test demonstrates that any combination and any number of mixed content
|
||||
// can be sent and received correctly.
|
||||
|
||||
const { smartsend, uuid4, log_trace, _serialize_data } = require('./src/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
|
||||
const correlation_id = uuid4();
|
||||
|
||||
// Helper: Log with correlation ID
|
||||
function log_trace(message) {
|
||||
const timestamp = new Date().toISOString();
|
||||
console.log(`[${timestamp}] [Correlation: ${correlation_id}] ${message}`);
|
||||
}
|
||||
|
||||
// File upload handler for plik server
|
||||
async function plik_upload_handler(fileserver_url, dataname, data, correlation_id) {
|
||||
log_trace(correlation_id, `Uploading ${dataname} to fileserver: ${fileserver_url}`);
|
||||
|
||||
// Step 1: Get upload ID and token
|
||||
const url_getUploadID = `${fileserver_url}/upload`;
|
||||
const headers = {
|
||||
"Content-Type": "application/json"
|
||||
};
|
||||
const body = JSON.stringify({ OneShot: true });
|
||||
|
||||
let response = await fetch(url_getUploadID, {
|
||||
method: "POST",
|
||||
headers: headers,
|
||||
body: body
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to get upload ID: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const responseJson = await response.json();
|
||||
const uploadid = responseJson.id;
|
||||
const uploadtoken = responseJson.uploadToken;
|
||||
|
||||
// Step 2: Upload file data
|
||||
const url_upload = `${fileserver_url}/file/${uploadid}`;
|
||||
|
||||
// Create multipart form data
|
||||
const formData = new FormData();
|
||||
const blob = new Blob([data], { type: "application/octet-stream" });
|
||||
formData.append("file", blob, dataname);
|
||||
|
||||
response = await fetch(url_upload, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"X-UploadToken": uploadtoken
|
||||
},
|
||||
body: formData
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to upload file: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const fileResponseJson = await response.json();
|
||||
const fileid = fileResponseJson.id;
|
||||
|
||||
// Build the download URL
|
||||
const url = `${fileserver_url}/file/${uploadid}/${fileid}/${encodeURIComponent(dataname)}`;
|
||||
|
||||
log_trace(correlation_id, `Uploaded to URL: ${url}`);
|
||||
|
||||
return {
|
||||
status: response.status,
|
||||
uploadid: uploadid,
|
||||
fileid: fileid,
|
||||
url: url
|
||||
};
|
||||
}
|
||||
|
||||
// Helper: Create sample data for each type
|
||||
function create_sample_data() {
|
||||
// Text data (small - direct transport)
|
||||
const text_data = "Hello! This is a test chat message. 🎉\nHow are you doing today? 😊";
|
||||
|
||||
// Dictionary/JSON data (medium - could be direct or link)
|
||||
const dict_data = {
|
||||
type: "chat",
|
||||
sender: "serviceA",
|
||||
receiver: "serviceB",
|
||||
metadata: {
|
||||
timestamp: new Date().toISOString(),
|
||||
priority: "high",
|
||||
tags: ["urgent", "chat", "test"]
|
||||
},
|
||||
content: {
|
||||
text: "This is a JSON-formatted chat message with nested structure.",
|
||||
format: "markdown",
|
||||
mentions: ["user1", "user2"]
|
||||
}
|
||||
};
|
||||
|
||||
// Table data (small - direct transport) - NOT IMPLEMENTED (requires apache-arrow)
|
||||
// const table_data_small = {...};
|
||||
|
||||
// Table data (large - link transport) - NOT IMPLEMENTED (requires apache-arrow)
|
||||
// const table_data_large = {...};
|
||||
|
||||
// Image data (small binary - direct transport)
|
||||
// Create a simple 10x10 pixel PNG-like data
|
||||
const image_width = 10;
|
||||
const image_height = 10;
|
||||
let image_data = new Uint8Array(128); // PNG header + pixel data
|
||||
// PNG header
|
||||
image_data[0] = 0x89;
|
||||
image_data[1] = 0x50;
|
||||
image_data[2] = 0x4E;
|
||||
image_data[3] = 0x47;
|
||||
image_data[4] = 0x0D;
|
||||
image_data[5] = 0x0A;
|
||||
image_data[6] = 0x1A;
|
||||
image_data[7] = 0x0A;
|
||||
// Simple RGB data (10*10*3 = 300 bytes)
|
||||
for (let i = 0; i < 300; i++) {
|
||||
image_data[i + 8] = 0xFF; // Red pixel
|
||||
}
|
||||
|
||||
// Image data (large - link transport)
|
||||
const large_image_width = 500;
|
||||
const large_image_height = 1000;
|
||||
const large_image_data = new Uint8Array(large_image_width * large_image_height * 3 + 8);
|
||||
// PNG header
|
||||
large_image_data[0] = 0x89;
|
||||
large_image_data[1] = 0x50;
|
||||
large_image_data[2] = 0x4E;
|
||||
large_image_data[3] = 0x47;
|
||||
large_image_data[4] = 0x0D;
|
||||
large_image_data[5] = 0x0A;
|
||||
large_image_data[6] = 0x1A;
|
||||
large_image_data[7] = 0x0A;
|
||||
// Random RGB data
|
||||
for (let i = 0; i < large_image_width * large_image_height * 3; i++) {
|
||||
large_image_data[i + 8] = Math.floor(Math.random() * 255);
|
||||
}
|
||||
|
||||
// Audio data (small binary - direct transport)
|
||||
const audio_data = new Uint8Array(100);
|
||||
for (let i = 0; i < 100; i++) {
|
||||
audio_data[i] = Math.floor(Math.random() * 255);
|
||||
}
|
||||
|
||||
// Audio data (large - link transport)
|
||||
const large_audio_data = new Uint8Array(1_500_000);
|
||||
for (let i = 0; i < 1_500_000; i++) {
|
||||
large_audio_data[i] = Math.floor(Math.random() * 255);
|
||||
}
|
||||
|
||||
// Video data (small binary - direct transport)
|
||||
const video_data = new Uint8Array(150);
|
||||
for (let i = 0; i < 150; i++) {
|
||||
video_data[i] = Math.floor(Math.random() * 255);
|
||||
}
|
||||
|
||||
// Video data (large - link transport)
|
||||
const large_video_data = new Uint8Array(1_500_000);
|
||||
for (let i = 0; i < 1_500_000; i++) {
|
||||
large_video_data[i] = Math.floor(Math.random() * 255);
|
||||
}
|
||||
|
||||
// Binary data (small - direct transport)
|
||||
const binary_data = new Uint8Array(200);
|
||||
for (let i = 0; i < 200; i++) {
|
||||
binary_data[i] = Math.floor(Math.random() * 255);
|
||||
}
|
||||
|
||||
// Binary data (large - link transport)
|
||||
const large_binary_data = new Uint8Array(1_500_000);
|
||||
for (let i = 0; i < 1_500_000; i++) {
|
||||
large_binary_data[i] = Math.floor(Math.random() * 255);
|
||||
}
|
||||
|
||||
return {
|
||||
text_data,
|
||||
dict_data,
|
||||
// table_data_small,
|
||||
// table_data_large,
|
||||
image_data,
|
||||
large_image_data,
|
||||
audio_data,
|
||||
large_audio_data,
|
||||
video_data,
|
||||
large_video_data,
|
||||
binary_data,
|
||||
large_binary_data
|
||||
};
|
||||
}
|
||||
|
||||
// Sender: Send mixed content via smartsend
|
||||
async function test_mix_send() {
|
||||
// Create sample data
|
||||
const { text_data, dict_data, image_data, large_image_data, audio_data, large_audio_data, video_data, large_video_data, binary_data, large_binary_data } = create_sample_data();
|
||||
|
||||
// Create payloads list - mixed content with both small and large data
|
||||
// Small data uses direct transport, large data uses link transport
|
||||
const payloads = [
|
||||
// Small data (direct transport) - text, dictionary
|
||||
{ dataname: "chat_text", data: text_data, type: "text" },
|
||||
{ dataname: "chat_json", data: dict_data, type: "dictionary" },
|
||||
// { dataname: "chat_table_small", data: table_data_small, type: "table" },
|
||||
|
||||
// Large data (link transport) - large image, large audio, large video, large binary
|
||||
// { dataname: "chat_table_large", data: table_data_large, type: "table" },
|
||||
{ dataname: "user_image_large", data: large_image_data, type: "image" },
|
||||
{ dataname: "audio_clip_large", data: large_audio_data, type: "audio" },
|
||||
{ dataname: "video_clip_large", data: large_video_data, type: "video" },
|
||||
{ dataname: "binary_file_large", data: large_binary_data, type: "binary" }
|
||||
];
|
||||
|
||||
// Use smartsend with mixed content
|
||||
const { env, env_json_str } = await smartsend(
|
||||
SUBJECT,
|
||||
payloads,
|
||||
{
|
||||
natsUrl: NATS_URL,
|
||||
fileserverUrl: FILESERVER_URL,
|
||||
fileserverUploadHandler: plik_upload_handler,
|
||||
sizeThreshold: 1_000_000,
|
||||
correlationId: correlation_id,
|
||||
msgPurpose: "chat",
|
||||
senderName: "mix_sender",
|
||||
receiverName: "",
|
||||
receiverId: "",
|
||||
replyTo: "",
|
||||
replyToMsgId: "",
|
||||
isPublish: true // Publish the message to NATS
|
||||
}
|
||||
);
|
||||
|
||||
log_trace(`Sent message with ${env.payloads.length} payloads`);
|
||||
|
||||
// Log transport type for each payload
|
||||
for (let i = 0; i < env.payloads.length; i++) {
|
||||
const payload = env.payloads[i];
|
||||
log_trace(`Payload ${i + 1} ('${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}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Summary
|
||||
console.log("\n--- Transport Summary ---");
|
||||
const direct_count = env.payloads.filter(p => p.transport === "direct").length;
|
||||
const link_count = env.payloads.filter(p => p.transport === "link").length;
|
||||
log_trace(`Direct transport: ${direct_count} payloads`);
|
||||
log_trace(`Link transport: ${link_count} payloads`);
|
||||
}
|
||||
|
||||
// Run the test
|
||||
console.log("Starting mixed-content transport test...");
|
||||
console.log(`Correlation ID: ${correlation_id}`);
|
||||
|
||||
// Run sender
|
||||
console.log("start smartsend for mixed content");
|
||||
test_mix_send();
|
||||
|
||||
console.log("\nTest completed.");
|
||||
console.log("Note: Run test_js_to_js_mix_receiver.js to receive the messages.");
|
||||
@@ -1,173 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
// Test script for mixed-content message testing
|
||||
// Tests receiving a mix of text, json, table, image, audio, video, and binary data
|
||||
// from JavaScript serviceA to JavaScript serviceB using NATSBridge.js smartreceive
|
||||
//
|
||||
// This test demonstrates that any combination and any number of mixed content
|
||||
// can be sent and received correctly.
|
||||
|
||||
const { smartreceive, log_trace } = require('./src/NATSBridge');
|
||||
|
||||
// Configuration
|
||||
const SUBJECT = "/NATSBridge_mix_test";
|
||||
const NATS_URL = "nats.yiem.cc";
|
||||
|
||||
// Helper: Log with correlation ID
|
||||
function log_trace(message) {
|
||||
const timestamp = new Date().toISOString();
|
||||
console.log(`[${timestamp}] ${message}`);
|
||||
}
|
||||
|
||||
// Receiver: Listen for messages and verify mixed content handling
|
||||
async function test_mix_receive() {
|
||||
// Connect to NATS
|
||||
const { connect } = require('nats');
|
||||
const nc = await connect({ servers: [NATS_URL] });
|
||||
|
||||
// Subscribe to the subject
|
||||
const sub = nc.subscribe(SUBJECT);
|
||||
|
||||
for await (const msg of sub) {
|
||||
log_trace(`Received message on ${msg.subject}`);
|
||||
|
||||
// Use NATSBridge.smartreceive to handle the data
|
||||
const result = await smartreceive(
|
||||
msg,
|
||||
{
|
||||
maxRetries: 5,
|
||||
baseDelay: 100,
|
||||
maxDelay: 5000
|
||||
}
|
||||
);
|
||||
|
||||
log_trace(`Received ${result.payloads.length} payloads`);
|
||||
|
||||
// Result is an envelope dictionary with payloads field
|
||||
// Access payloads with result.payloads
|
||||
for (const { dataname, data, type } of result.payloads) {
|
||||
log_trace(`\n=== Payload: ${dataname} (type: ${type}) ===`);
|
||||
|
||||
// Handle different data types
|
||||
if (type === "text") {
|
||||
// Text data - should be a String
|
||||
if (typeof data === 'string') {
|
||||
log_trace(` Type: String`);
|
||||
log_trace(` Length: ${data.length} characters`);
|
||||
|
||||
// Display first 200 characters
|
||||
if (data.length > 200) {
|
||||
log_trace(` First 200 chars: ${data.substring(0, 200)}...`);
|
||||
} else {
|
||||
log_trace(` Content: ${data}`);
|
||||
}
|
||||
|
||||
// Save to file
|
||||
const fs = require('fs');
|
||||
const output_path = `./received_${dataname}.txt`;
|
||||
fs.writeFileSync(output_path, data);
|
||||
log_trace(` Saved to: ${output_path}`);
|
||||
} else {
|
||||
log_trace(` ERROR: Expected String, got ${typeof data}`);
|
||||
}
|
||||
|
||||
} else if (type === "dictionary") {
|
||||
// Dictionary data - should be an object
|
||||
if (typeof data === 'object' && data !== null && !Array.isArray(data)) {
|
||||
log_trace(` Type: Object`);
|
||||
log_trace(` Keys: ${Object.keys(data).join(', ')}`);
|
||||
|
||||
// Display nested content
|
||||
for (const [key, value] of Object.entries(data)) {
|
||||
log_trace(` ${key} => ${value}`);
|
||||
}
|
||||
|
||||
// Save to JSON file
|
||||
const fs = require('fs');
|
||||
const output_path = `./received_${dataname}.json`;
|
||||
const json_str = JSON.stringify(data, null, 2);
|
||||
fs.writeFileSync(output_path, json_str);
|
||||
log_trace(` Saved to: ${output_path}`);
|
||||
} else {
|
||||
log_trace(` ERROR: Expected Object, got ${typeof data}`);
|
||||
}
|
||||
|
||||
} else if (type === "table") {
|
||||
// Table data - should be an array of objects (requires apache-arrow)
|
||||
log_trace(` Type: Array (requires apache-arrow for full deserialization)`);
|
||||
if (Array.isArray(data)) {
|
||||
log_trace(` Length: ${data.length} items`);
|
||||
log_trace(` First item: ${JSON.stringify(data[0])}`);
|
||||
} else {
|
||||
log_trace(` ERROR: Expected Array, got ${typeof data}`);
|
||||
}
|
||||
|
||||
} else if (type === "image" || type === "audio" || type === "video" || type === "binary") {
|
||||
// Binary data - should be Uint8Array
|
||||
if (data instanceof Uint8Array || Array.isArray(data)) {
|
||||
log_trace(` Type: Uint8Array (binary)`);
|
||||
log_trace(` Size: ${data.length} bytes`);
|
||||
|
||||
// Save to file
|
||||
const fs = require('fs');
|
||||
const output_path = `./received_${dataname}.bin`;
|
||||
fs.writeFileSync(output_path, Buffer.from(data));
|
||||
log_trace(` Saved to: ${output_path}`);
|
||||
} else {
|
||||
log_trace(` ERROR: Expected Uint8Array, got ${typeof data}`);
|
||||
}
|
||||
|
||||
} else {
|
||||
log_trace(` ERROR: Unknown data type '${type}'`);
|
||||
}
|
||||
}
|
||||
|
||||
// Summary
|
||||
console.log("\n=== Verification Summary ===");
|
||||
const text_count = result.payloads.filter(x => x.type === "text").length;
|
||||
const dict_count = result.payloads.filter(x => x.type === "dictionary").length;
|
||||
const table_count = result.payloads.filter(x => x.type === "table").length;
|
||||
const image_count = result.payloads.filter(x => x.type === "image").length;
|
||||
const audio_count = result.payloads.filter(x => x.type === "audio").length;
|
||||
const video_count = result.payloads.filter(x => x.type === "video").length;
|
||||
const binary_count = result.payloads.filter(x => x.type === "binary").length;
|
||||
|
||||
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}`);
|
||||
|
||||
// Print transport type info for each payload if available
|
||||
console.log("\n=== Payload Details ===");
|
||||
for (const { dataname, data, type } of result.payloads) {
|
||||
if (["image", "audio", "video", "binary"].includes(type)) {
|
||||
log_trace(`${dataname}: ${data.length} bytes (binary)`);
|
||||
} else if (type === "table") {
|
||||
log_trace(`${dataname}: ${data.length} items (Array)`);
|
||||
} else if (type === "dictionary") {
|
||||
log_trace(`${dataname}: ${JSON.stringify(data).length} bytes (Object)`);
|
||||
} else if (type === "text") {
|
||||
log_trace(`${dataname}: ${data.length} characters (String)`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Keep listening for 2 minutes
|
||||
setTimeout(() => {
|
||||
nc.close();
|
||||
process.exit(0);
|
||||
}, 120000);
|
||||
}
|
||||
|
||||
// Run the test
|
||||
console.log("Starting mixed-content transport test...");
|
||||
console.log("Note: This receiver will wait for messages from the sender.");
|
||||
console.log("Run test_js_to_js_mix_sender.js first to send test data.");
|
||||
|
||||
// Run receiver
|
||||
console.log("\ntesting smartreceive for mixed content");
|
||||
test_mix_receive();
|
||||
|
||||
console.log("\nTest completed.");
|
||||
@@ -1,87 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
// Test script for Table transport testing
|
||||
// Tests receiving 1 large and 1 small Tables via direct and link transport
|
||||
// Uses NATSBridge.js smartreceive with "table" type
|
||||
//
|
||||
// Note: This test requires the apache-arrow library to deserialize table data.
|
||||
// The JavaScript implementation uses apache-arrow for Arrow IPC deserialization.
|
||||
|
||||
const { smartreceive, log_trace } = require('./src/NATSBridge');
|
||||
|
||||
// Configuration
|
||||
const SUBJECT = "/NATSBridge_table_test";
|
||||
const NATS_URL = "nats.yiem.cc";
|
||||
|
||||
// Helper: Log with correlation ID
|
||||
function log_trace(message) {
|
||||
const timestamp = new Date().toISOString();
|
||||
console.log(`[${timestamp}] ${message}`);
|
||||
}
|
||||
|
||||
// Receiver: Listen for messages and verify Table handling
|
||||
async function test_table_receive() {
|
||||
// Connect to NATS
|
||||
const { connect } = require('nats');
|
||||
const nc = await connect({ servers: [NATS_URL] });
|
||||
|
||||
// Subscribe to the subject
|
||||
const sub = nc.subscribe(SUBJECT);
|
||||
|
||||
for await (const msg of sub) {
|
||||
log_trace(`Received message on ${msg.subject}`);
|
||||
|
||||
// Use NATSBridge.smartreceive to handle the data
|
||||
const result = await smartreceive(
|
||||
msg,
|
||||
{
|
||||
maxRetries: 5,
|
||||
baseDelay: 100,
|
||||
maxDelay: 5000
|
||||
}
|
||||
);
|
||||
|
||||
// Result is an envelope dictionary with payloads field
|
||||
// Access payloads with result.payloads
|
||||
for (const { dataname, data, type } of result.payloads) {
|
||||
if (Array.isArray(data)) {
|
||||
log_trace(`Received Table '${dataname}' of type ${type}`);
|
||||
|
||||
// Display table contents
|
||||
console.log(` Dimensions: ${data.length} rows x ${data.length > 0 ? Object.keys(data[0]).length : 0} columns`);
|
||||
console.log(` Columns: ${data.length > 0 ? Object.keys(data[0]).join(', ') : ''}`);
|
||||
|
||||
// Display first few rows
|
||||
console.log(` First 5 rows:`);
|
||||
for (let i = 0; i < Math.min(5, data.length); i++) {
|
||||
console.log(` Row ${i}: ${JSON.stringify(data[i])}`);
|
||||
}
|
||||
|
||||
// Save to JSON file
|
||||
const fs = require('fs');
|
||||
const output_path = `./received_${dataname}.json`;
|
||||
const json_str = JSON.stringify(data, null, 2);
|
||||
fs.writeFileSync(output_path, json_str);
|
||||
log_trace(`Saved Table to ${output_path}`);
|
||||
} else {
|
||||
log_trace(`Received unexpected data type for '${dataname}': ${typeof data}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Keep listening for 10 seconds
|
||||
setTimeout(() => {
|
||||
nc.close();
|
||||
process.exit(0);
|
||||
}, 120000);
|
||||
}
|
||||
|
||||
// Run the test
|
||||
console.log("Starting Table transport test...");
|
||||
console.log("Note: This receiver will wait for messages from the sender.");
|
||||
console.log("Run test_js_to_js_table_sender.js first to send test data.");
|
||||
|
||||
// Run receiver
|
||||
console.log("testing smartreceive");
|
||||
test_table_receive();
|
||||
|
||||
console.log("Test completed.");
|
||||
@@ -1,165 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
// Test script for Table transport testing
|
||||
// Tests sending 1 large and 1 small Tables via direct and link transport
|
||||
// Uses NATSBridge.js smartsend with "table" type
|
||||
//
|
||||
// Note: This test requires the apache-arrow library to serialize/deserialize table data.
|
||||
// The JavaScript implementation uses apache-arrow for Arrow IPC serialization.
|
||||
|
||||
const { smartsend, uuid4, log_trace } = require('./src/NATSBridge');
|
||||
|
||||
// Configuration
|
||||
const SUBJECT = "/NATSBridge_table_test";
|
||||
const NATS_URL = "nats.yiem.cc";
|
||||
const FILESERVER_URL = "http://192.168.88.104:8080";
|
||||
|
||||
// Create correlation ID for tracing
|
||||
const correlation_id = uuid4();
|
||||
|
||||
// Helper: Log with correlation ID
|
||||
function log_trace(message) {
|
||||
const timestamp = new Date().toISOString();
|
||||
console.log(`[${timestamp}] [Correlation: ${correlation_id}] ${message}`);
|
||||
}
|
||||
|
||||
// File upload handler for plik server
|
||||
async function plik_upload_handler(fileserver_url, dataname, data, correlation_id) {
|
||||
log_trace(correlation_id, `Uploading ${dataname} to fileserver: ${fileserver_url}`);
|
||||
|
||||
// Step 1: Get upload ID and token
|
||||
const url_getUploadID = `${fileserver_url}/upload`;
|
||||
const headers = {
|
||||
"Content-Type": "application/json"
|
||||
};
|
||||
const body = JSON.stringify({ OneShot: true });
|
||||
|
||||
let response = await fetch(url_getUploadID, {
|
||||
method: "POST",
|
||||
headers: headers,
|
||||
body: body
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to get upload ID: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const responseJson = await response.json();
|
||||
const uploadid = responseJson.id;
|
||||
const uploadtoken = responseJson.uploadToken;
|
||||
|
||||
// Step 2: Upload file data
|
||||
const url_upload = `${fileserver_url}/file/${uploadid}`;
|
||||
|
||||
// Create multipart form data
|
||||
const formData = new FormData();
|
||||
const blob = new Blob([data], { type: "application/octet-stream" });
|
||||
formData.append("file", blob, dataname);
|
||||
|
||||
response = await fetch(url_upload, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"X-UploadToken": uploadtoken
|
||||
},
|
||||
body: formData
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to upload file: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const fileResponseJson = await response.json();
|
||||
const fileid = fileResponseJson.id;
|
||||
|
||||
// Build the download URL
|
||||
const url = `${fileserver_url}/file/${uploadid}/${fileid}/${encodeURIComponent(dataname)}`;
|
||||
|
||||
log_trace(correlation_id, `Uploaded to URL: ${url}`);
|
||||
|
||||
return {
|
||||
status: response.status,
|
||||
uploadid: uploadid,
|
||||
fileid: fileid,
|
||||
url: url
|
||||
};
|
||||
}
|
||||
|
||||
// Sender: Send Tables via smartsend
|
||||
async function test_table_send() {
|
||||
// Note: This test requires apache-arrow library to create Arrow IPC data.
|
||||
// For now, we'll use a simple array of objects as table data.
|
||||
// In production, you would use the apache-arrow library to create Arrow IPC data.
|
||||
|
||||
// Create a small Table (will use direct transport)
|
||||
const small_table = [
|
||||
{ id: 1, name: "Alice", score: 95 },
|
||||
{ id: 2, name: "Bob", score: 88 },
|
||||
{ id: 3, name: "Charlie", score: 92 }
|
||||
];
|
||||
|
||||
// Create a large Table (will use link transport if > 1MB)
|
||||
// Generate a larger dataset (~2MB to ensure link transport)
|
||||
const large_table = [];
|
||||
for (let i = 0; i < 50000; i++) {
|
||||
large_table.push({
|
||||
id: i,
|
||||
message: `msg_${i}`,
|
||||
sender: `sender_${i}`,
|
||||
timestamp: new Date().toISOString(),
|
||||
priority: Math.floor(Math.random() * 3) + 1
|
||||
});
|
||||
}
|
||||
|
||||
// Test data 1: small Table
|
||||
const data1 = { dataname: "small_table", data: small_table, type: "table" };
|
||||
|
||||
// Test data 2: large Table
|
||||
const data2 = { dataname: "large_table", data: large_table, type: "table" };
|
||||
|
||||
// Use smartsend with table type
|
||||
// For small Table: will use direct transport (Arrow IPC encoded)
|
||||
// For large Table: will use link transport (uploaded to fileserver)
|
||||
const { env, env_json_str } = await smartsend(
|
||||
SUBJECT,
|
||||
[data1, data2],
|
||||
{
|
||||
natsUrl: NATS_URL,
|
||||
fileserverUrl: FILESERVER_URL,
|
||||
fileserverUploadHandler: plik_upload_handler,
|
||||
sizeThreshold: 1_000_000,
|
||||
correlationId: correlation_id,
|
||||
msgPurpose: "chat",
|
||||
senderName: "table_sender",
|
||||
receiverName: "",
|
||||
receiverId: "",
|
||||
replyTo: "",
|
||||
replyToMsgId: "",
|
||||
isPublish: true // Publish the message to NATS
|
||||
}
|
||||
);
|
||||
|
||||
log_trace(`Sent message with ${env.payloads.length} payloads`);
|
||||
|
||||
// Log transport type for each payload
|
||||
for (let i = 0; i < env.payloads.length; i++) {
|
||||
const payload = env.payloads[i];
|
||||
log_trace(`Payload ${i + 1} ('${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}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Run the test
|
||||
console.log("Starting Table transport test...");
|
||||
console.log(`Correlation ID: ${correlation_id}`);
|
||||
|
||||
// Run sender
|
||||
console.log("start smartsend for tables");
|
||||
test_table_send();
|
||||
|
||||
console.log("Test completed.");
|
||||
@@ -1,81 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
// Test script for text transport testing
|
||||
// Tests receiving 1 large and 1 small text from JavaScript serviceA to JavaScript serviceB
|
||||
// Uses NATSBridge.js smartreceive with "text" type
|
||||
|
||||
const { smartreceive, log_trace } = require('./src/NATSBridge');
|
||||
|
||||
// Configuration
|
||||
const SUBJECT = "/NATSBridge_text_test";
|
||||
const NATS_URL = "nats.yiem.cc";
|
||||
|
||||
// Helper: Log with correlation ID
|
||||
function log_trace(message) {
|
||||
const timestamp = new Date().toISOString();
|
||||
console.log(`[${timestamp}] ${message}`);
|
||||
}
|
||||
|
||||
// Receiver: Listen for messages and verify text handling
|
||||
async function test_text_receive() {
|
||||
// Connect to NATS
|
||||
const { connect } = require('nats');
|
||||
const nc = await connect({ servers: [NATS_URL] });
|
||||
|
||||
// Subscribe to the subject
|
||||
const sub = nc.subscribe(SUBJECT);
|
||||
|
||||
for await (const msg of sub) {
|
||||
log_trace(`Received message on ${msg.subject}`);
|
||||
|
||||
// Use NATSBridge.smartreceive to handle the data
|
||||
const result = await smartreceive(
|
||||
msg,
|
||||
{
|
||||
maxRetries: 5,
|
||||
baseDelay: 100,
|
||||
maxDelay: 5000
|
||||
}
|
||||
);
|
||||
|
||||
// Result is an envelope dictionary with payloads field
|
||||
// Access payloads with result.payloads
|
||||
for (const { dataname, data, type } of result.payloads) {
|
||||
if (typeof data === 'string') {
|
||||
log_trace(`Received text '${dataname}' of type ${type}`);
|
||||
log_trace(` Length: ${data.length} characters`);
|
||||
|
||||
// Display first 100 characters
|
||||
if (data.length > 100) {
|
||||
log_trace(` First 100 characters: ${data.substring(0, 100)}...`);
|
||||
} else {
|
||||
log_trace(` Content: ${data}`);
|
||||
}
|
||||
|
||||
// Save to file
|
||||
const fs = require('fs');
|
||||
const output_path = `./received_${dataname}.txt`;
|
||||
fs.writeFileSync(output_path, data);
|
||||
log_trace(`Saved text to ${output_path}`);
|
||||
} else {
|
||||
log_trace(`Received unexpected data type for '${dataname}': ${typeof data}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Keep listening for 10 seconds
|
||||
setTimeout(() => {
|
||||
nc.close();
|
||||
process.exit(0);
|
||||
}, 120000);
|
||||
}
|
||||
|
||||
// Run the test
|
||||
console.log("Starting text transport test...");
|
||||
console.log("Note: This receiver will wait for messages from the sender.");
|
||||
console.log("Run test_js_to_js_text_sender.js first to send test data.");
|
||||
|
||||
// Run receiver
|
||||
console.log("testing smartreceive for text");
|
||||
test_text_receive();
|
||||
|
||||
console.log("Test completed.");
|
||||
@@ -1,141 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
// Test script for text transport testing
|
||||
// Tests sending 1 large and 1 small text from JavaScript serviceA to JavaScript serviceB
|
||||
// Uses NATSBridge.js smartsend with "text" type
|
||||
|
||||
const { smartsend, uuid4, log_trace } = require('./src/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
|
||||
const correlation_id = uuid4();
|
||||
|
||||
// Helper: Log with correlation ID
|
||||
function log_trace(message) {
|
||||
const timestamp = new Date().toISOString();
|
||||
console.log(`[${timestamp}] [Correlation: ${correlation_id}] ${message}`);
|
||||
}
|
||||
|
||||
// File upload handler for plik server
|
||||
async function plik_upload_handler(fileserver_url, dataname, data, correlation_id) {
|
||||
// Get upload ID
|
||||
const url_getUploadID = `${fileserver_url}/upload`;
|
||||
const headers = {
|
||||
"Content-Type": "application/json"
|
||||
};
|
||||
const body = JSON.stringify({ OneShot: true });
|
||||
|
||||
let response = await fetch(url_getUploadID, {
|
||||
method: "POST",
|
||||
headers: headers,
|
||||
body: body
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to get upload ID: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const responseJson = await response.json();
|
||||
const uploadid = responseJson.id;
|
||||
const uploadtoken = responseJson.uploadToken;
|
||||
|
||||
// Upload file
|
||||
const formData = new FormData();
|
||||
const blob = new Blob([data], { type: "application/octet-stream" });
|
||||
formData.append("file", blob, dataname);
|
||||
|
||||
response = await fetch(`${fileserver_url}/file/${uploadid}`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"X-UploadToken": uploadtoken
|
||||
},
|
||||
body: formData
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to upload file: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const fileResponseJson = await response.json();
|
||||
const fileid = fileResponseJson.id;
|
||||
|
||||
const url = `${fileserver_url}/file/${uploadid}/${fileid}/${encodeURIComponent(dataname)}`;
|
||||
|
||||
return {
|
||||
status: response.status,
|
||||
uploadid: uploadid,
|
||||
fileid: fileid,
|
||||
url: url
|
||||
};
|
||||
}
|
||||
|
||||
// Sender: Send text via smartsend
|
||||
async function test_text_send() {
|
||||
// Create a small text (will use direct transport)
|
||||
const 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)
|
||||
const large_text_lines = [];
|
||||
for (let i = 0; i < 50000; i++) {
|
||||
large_text_lines.push(`Line ${i}: This is a sample text line with some content to pad the size. `);
|
||||
}
|
||||
const large_text = large_text_lines.join("");
|
||||
|
||||
// Test data 1: small text
|
||||
const data1 = { dataname: "small_text", data: small_text, type: "text" };
|
||||
|
||||
// Test data 2: large text
|
||||
const data2 = { dataname: "large_text", data: large_text, type: "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)
|
||||
const { env, env_json_str } = await smartsend(
|
||||
SUBJECT,
|
||||
[data1, data2],
|
||||
{
|
||||
natsUrl: NATS_URL,
|
||||
fileserverUrl: FILESERVER_URL,
|
||||
fileserverUploadHandler: plik_upload_handler,
|
||||
sizeThreshold: 1_000_000,
|
||||
correlationId: correlation_id,
|
||||
msgPurpose: "chat",
|
||||
senderName: "text_sender",
|
||||
receiverName: "",
|
||||
receiverId: "",
|
||||
replyTo: "",
|
||||
replyToMsgId: "",
|
||||
isPublish: true // Publish the message to NATS
|
||||
}
|
||||
);
|
||||
|
||||
log_trace(`Sent message with ${env.payloads.length} payloads`);
|
||||
|
||||
// Log transport type for each payload
|
||||
for (let i = 0; i < env.payloads.length; i++) {
|
||||
const payload = env.payloads[i];
|
||||
log_trace(`Payload ${i + 1} ('${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}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Run the test
|
||||
console.log("Starting text transport test...");
|
||||
console.log(`Correlation ID: ${correlation_id}`);
|
||||
|
||||
// Run sender
|
||||
console.log("start smartsend for text");
|
||||
test_text_send();
|
||||
|
||||
console.log("Test completed.");
|
||||
@@ -1,207 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Basic functionality test for nats_bridge.py
|
||||
Tests the core classes and functions without NATS connection
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add src to path for import
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
|
||||
|
||||
from nats_bridge import (
|
||||
MessagePayload,
|
||||
MessageEnvelope,
|
||||
smartsend,
|
||||
smartreceive,
|
||||
log_trace,
|
||||
generate_uuid,
|
||||
get_timestamp,
|
||||
_serialize_data,
|
||||
_deserialize_data
|
||||
)
|
||||
import json
|
||||
|
||||
|
||||
def test_message_payload():
|
||||
"""Test MessagePayload class"""
|
||||
print("\n=== Testing MessagePayload ===")
|
||||
|
||||
# Test direct transport with text
|
||||
payload1 = MessagePayload(
|
||||
data="Hello World",
|
||||
msg_type="text",
|
||||
id="test-id-1",
|
||||
dataname="message",
|
||||
transport="direct",
|
||||
encoding="base64",
|
||||
size=11
|
||||
)
|
||||
|
||||
assert payload1.id == "test-id-1"
|
||||
assert payload1.dataname == "message"
|
||||
assert payload1.type == "text"
|
||||
assert payload1.transport == "direct"
|
||||
assert payload1.encoding == "base64"
|
||||
assert payload1.size == 11
|
||||
print(" [PASS] MessagePayload with text data")
|
||||
|
||||
# Test link transport with URL
|
||||
payload2 = MessagePayload(
|
||||
data="http://example.com/file.txt",
|
||||
msg_type="binary",
|
||||
id="test-id-2",
|
||||
dataname="file",
|
||||
transport="link",
|
||||
encoding="none",
|
||||
size=1000
|
||||
)
|
||||
|
||||
assert payload2.transport == "link"
|
||||
assert payload2.data == "http://example.com/file.txt"
|
||||
print(" [PASS] MessagePayload with link transport")
|
||||
|
||||
# Test to_dict method
|
||||
payload_dict = payload1.to_dict()
|
||||
assert "id" in payload_dict
|
||||
assert "dataname" in payload_dict
|
||||
assert "type" in payload_dict
|
||||
assert "transport" in payload_dict
|
||||
assert "data" in payload_dict
|
||||
print(" [PASS] MessagePayload.to_dict() method")
|
||||
|
||||
|
||||
def test_message_envelope():
|
||||
"""Test MessageEnvelope class"""
|
||||
print("\n=== Testing MessageEnvelope ===")
|
||||
|
||||
# Create payloads
|
||||
payload1 = MessagePayload("Hello", "text", id="p1", dataname="msg1")
|
||||
payload2 = MessagePayload("http://example.com/file", "binary", id="p2", dataname="file", transport="link")
|
||||
|
||||
# Create envelope
|
||||
env = MessageEnvelope(
|
||||
send_to="/test/subject",
|
||||
payloads=[payload1, payload2],
|
||||
correlation_id="test-correlation-id",
|
||||
msg_id="test-msg-id",
|
||||
msg_purpose="chat",
|
||||
sender_name="test_sender",
|
||||
receiver_name="test_receiver",
|
||||
reply_to="/test/reply"
|
||||
)
|
||||
|
||||
assert env.send_to == "/test/subject"
|
||||
assert env.correlation_id == "test-correlation-id"
|
||||
assert env.msg_id == "test-msg-id"
|
||||
assert env.msg_purpose == "chat"
|
||||
assert len(env.payloads) == 2
|
||||
print(" [PASS] MessageEnvelope creation")
|
||||
|
||||
# Test to_json method
|
||||
json_str = env.to_json()
|
||||
json_data = json.loads(json_str)
|
||||
assert json_data["sendTo"] == "/test/subject"
|
||||
assert json_data["correlationId"] == "test-correlation-id"
|
||||
assert json_data["msgPurpose"] == "chat"
|
||||
assert len(json_data["payloads"]) == 2
|
||||
print(" [PASS] MessageEnvelope.to_json() method")
|
||||
|
||||
|
||||
def test_serialize_data():
|
||||
"""Test _serialize_data function"""
|
||||
print("\n=== Testing _serialize_data ===")
|
||||
|
||||
# Test text serialization
|
||||
text_bytes = _serialize_data("Hello", "text")
|
||||
assert isinstance(text_bytes, bytes)
|
||||
assert text_bytes == b"Hello"
|
||||
print(" [PASS] Text serialization")
|
||||
|
||||
# Test dictionary serialization
|
||||
dict_data = {"key": "value", "number": 42}
|
||||
dict_bytes = _serialize_data(dict_data, "dictionary")
|
||||
assert isinstance(dict_bytes, bytes)
|
||||
parsed = json.loads(dict_bytes.decode('utf-8'))
|
||||
assert parsed["key"] == "value"
|
||||
print(" [PASS] Dictionary serialization")
|
||||
|
||||
# Test binary serialization
|
||||
binary_data = b"\x00\x01\x02"
|
||||
binary_bytes = _serialize_data(binary_data, "binary")
|
||||
assert binary_bytes == b"\x00\x01\x02"
|
||||
print(" [PASS] Binary serialization")
|
||||
|
||||
# Test image serialization
|
||||
image_data = bytes([1, 2, 3, 4, 5])
|
||||
image_bytes = _serialize_data(image_data, "image")
|
||||
assert image_bytes == image_data
|
||||
print(" [PASS] Image serialization")
|
||||
|
||||
|
||||
def test_deserialize_data():
|
||||
"""Test _deserialize_data function"""
|
||||
print("\n=== Testing _deserialize_data ===")
|
||||
|
||||
# Test text deserialization
|
||||
text_bytes = b"Hello"
|
||||
text_data = _deserialize_data(text_bytes, "text", "test-correlation-id")
|
||||
assert text_data == "Hello"
|
||||
print(" [PASS] Text deserialization")
|
||||
|
||||
# Test dictionary deserialization
|
||||
dict_bytes = b'{"key": "value"}'
|
||||
dict_data = _deserialize_data(dict_bytes, "dictionary", "test-correlation-id")
|
||||
assert dict_data == {"key": "value"}
|
||||
print(" [PASS] Dictionary deserialization")
|
||||
|
||||
# Test binary deserialization
|
||||
binary_data = b"\x00\x01\x02"
|
||||
binary_result = _deserialize_data(binary_data, "binary", "test-correlation-id")
|
||||
assert binary_result == b"\x00\x01\x02"
|
||||
print(" [PASS] Binary deserialization")
|
||||
|
||||
|
||||
def test_utilities():
|
||||
"""Test utility functions"""
|
||||
print("\n=== Testing Utility Functions ===")
|
||||
|
||||
# Test generate_uuid
|
||||
uuid1 = generate_uuid()
|
||||
uuid2 = generate_uuid()
|
||||
assert uuid1 != uuid2
|
||||
print(f" [PASS] generate_uuid() - generated: {uuid1}")
|
||||
|
||||
# Test get_timestamp
|
||||
timestamp = get_timestamp()
|
||||
assert "T" in timestamp
|
||||
print(f" [PASS] get_timestamp() - generated: {timestamp}")
|
||||
|
||||
|
||||
def main():
|
||||
"""Run all tests"""
|
||||
print("=" * 60)
|
||||
print("NATSBridge Python/Micropython - Basic Functionality Tests")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
test_message_payload()
|
||||
test_message_envelope()
|
||||
test_serialize_data()
|
||||
test_deserialize_data()
|
||||
test_utilities()
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("ALL TESTS PASSED!")
|
||||
print("=" * 60)
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n[FAIL] Test failed with error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,70 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for dictionary transport testing - Receiver
|
||||
Tests receiving dictionary messages via NATS using nats_bridge.py smartreceive
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
|
||||
# Add src to path for import
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
|
||||
|
||||
from nats_bridge import smartreceive, log_trace
|
||||
import nats
|
||||
import asyncio
|
||||
|
||||
# Configuration
|
||||
SUBJECT = "/NATSBridge_dict_test"
|
||||
NATS_URL = "nats://nats.yiem.cc:4222"
|
||||
|
||||
|
||||
async def main():
|
||||
log_trace("", f"Starting dictionary transport receiver test...")
|
||||
log_trace("", f"Note: This receiver will wait for messages from the sender.")
|
||||
log_trace("", f"Run test_micropython_dict_sender.py first to send test data.")
|
||||
|
||||
# Connect to NATS
|
||||
nc = await nats.connect(NATS_URL)
|
||||
log_trace("", f"Connected to NATS at {NATS_URL}")
|
||||
|
||||
# Subscribe to the subject
|
||||
async def message_handler(msg):
|
||||
log_trace("", f"Received message on {msg.subject}")
|
||||
|
||||
# Use smartreceive to handle the data
|
||||
result = smartreceive(msg.data)
|
||||
|
||||
# Result is an envelope dictionary with payloads field containing list of (dataname, data, data_type) tuples
|
||||
for dataname, data, data_type in result["payloads"]:
|
||||
if isinstance(data, dict):
|
||||
log_trace(result.get("correlationId", ""), f"Received dictionary '{dataname}' of type {data_type}")
|
||||
log_trace(result.get("correlationId", ""), f" Keys: {list(data.keys())}")
|
||||
|
||||
# Display first few items for small dicts
|
||||
if isinstance(data, dict) and len(data) <= 10:
|
||||
log_trace(result.get("correlationId", ""), f" Content: {json.dumps(data, indent=2)}")
|
||||
else:
|
||||
# For large dicts, show summary
|
||||
log_trace(result.get("correlationId", ""), f" Summary: {json.dumps(data, default=str)[:200]}...")
|
||||
|
||||
# Save to file
|
||||
output_path = f"./received_{dataname}.json"
|
||||
with open(output_path, 'w') as f:
|
||||
json.dump(data, f, indent=2)
|
||||
log_trace(result.get("correlationId", ""), f"Saved dictionary to {output_path}")
|
||||
else:
|
||||
log_trace(result.get("correlationId", ""), f"Received unexpected data type for '{dataname}': {type(data)}")
|
||||
|
||||
sid = await nc.subscribe(SUBJECT, cb=message_handler)
|
||||
log_trace("", f"Subscribed to {SUBJECT} with subscription ID: {sid}")
|
||||
|
||||
# Keep listening for 120 seconds
|
||||
await asyncio.sleep(120)
|
||||
await nc.close()
|
||||
log_trace("", "Test completed.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -1,100 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for dictionary transport testing - Micropython
|
||||
Tests sending dictionary messages via NATS using nats_bridge.py smartsend
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add src to path for import
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
|
||||
|
||||
from nats_bridge import smartsend, log_trace
|
||||
import uuid
|
||||
|
||||
# Configuration
|
||||
SUBJECT = "/NATSBridge_dict_test"
|
||||
NATS_URL = "nats://nats.yiem.cc:4222"
|
||||
FILESERVER_URL = "http://192.168.88.104:8080"
|
||||
SIZE_THRESHOLD = 1_000_000 # 1MB
|
||||
|
||||
# Create correlation ID for tracing
|
||||
correlation_id = str(uuid.uuid4())
|
||||
|
||||
|
||||
def main():
|
||||
# Create a small dictionary (will use direct transport)
|
||||
small_dict = {
|
||||
"name": "test",
|
||||
"value": 42,
|
||||
"enabled": True,
|
||||
"metadata": {
|
||||
"version": "1.0.0",
|
||||
"timestamp": "2026-02-22T12:00:00Z"
|
||||
}
|
||||
}
|
||||
|
||||
# Create a large dictionary (will use link transport if > 1MB)
|
||||
# Generate a larger dictionary (~2MB to ensure link transport)
|
||||
large_dict = {
|
||||
"id": str(uuid.uuid4()),
|
||||
"items": [
|
||||
{
|
||||
"index": i,
|
||||
"name": f"item_{i}",
|
||||
"value": i * 1.5,
|
||||
"data": "x" * 10000 # Large string per item
|
||||
}
|
||||
for i in range(200)
|
||||
],
|
||||
"metadata": {
|
||||
"count": 200,
|
||||
"created": "2026-02-22T12:00:00Z"
|
||||
}
|
||||
}
|
||||
|
||||
# Test data 1: small dictionary
|
||||
data1 = ("small_dict", small_dict, "dictionary")
|
||||
|
||||
# Test data 2: large dictionary
|
||||
data2 = ("large_dict", large_dict, "dictionary")
|
||||
|
||||
log_trace(correlation_id, f"Starting smartsend for subject: {SUBJECT}")
|
||||
log_trace(correlation_id, f"Correlation ID: {correlation_id}")
|
||||
|
||||
# Use smartsend with dictionary type
|
||||
env, env_json_str = smartsend(
|
||||
SUBJECT,
|
||||
[data1, data2], # List of (dataname, data, type) tuples
|
||||
nats_url=NATS_URL,
|
||||
fileserver_url=FILESERVER_URL,
|
||||
size_threshold=SIZE_THRESHOLD,
|
||||
correlation_id=correlation_id,
|
||||
msg_purpose="chat",
|
||||
sender_name="dict_sender",
|
||||
receiver_name="",
|
||||
receiver_id="",
|
||||
reply_to="",
|
||||
reply_to_msg_id="",
|
||||
is_publish=True # Publish the message to NATS
|
||||
)
|
||||
|
||||
log_trace(correlation_id, f"Sent message with {len(env.payloads)} payloads")
|
||||
|
||||
# Log transport type for each payload
|
||||
for i, payload in enumerate(env.payloads):
|
||||
log_trace(correlation_id, f"Payload {i+1} ('{payload.dataname}'):")
|
||||
log_trace(correlation_id, f" Transport: {payload.transport}")
|
||||
log_trace(correlation_id, f" Type: {payload.type}")
|
||||
log_trace(correlation_id, f" Size: {payload.size} bytes")
|
||||
log_trace(correlation_id, f" Encoding: {payload.encoding}")
|
||||
|
||||
if payload.transport == "link":
|
||||
log_trace(correlation_id, f" URL: {payload.data}")
|
||||
|
||||
print(f"Test completed. Correlation ID: {correlation_id}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,65 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for file transport testing - Receiver
|
||||
Tests receiving binary files via NATS using nats_bridge.py smartreceive
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add src to path for import
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
|
||||
|
||||
from nats_bridge import smartreceive, log_trace
|
||||
import nats
|
||||
import asyncio
|
||||
|
||||
# Configuration
|
||||
SUBJECT = "/NATSBridge_file_test"
|
||||
NATS_URL = "nats://nats.yiem.cc:4222"
|
||||
|
||||
|
||||
async def main():
|
||||
log_trace("", f"Starting file transport receiver test...")
|
||||
log_trace("", f"Note: This receiver will wait for messages from the sender.")
|
||||
log_trace("", f"Run test_micropython_file_sender.py first to send test data.")
|
||||
|
||||
# Connect to NATS
|
||||
nc = await nats.connect(NATS_URL)
|
||||
log_trace("", f"Connected to NATS at {NATS_URL}")
|
||||
|
||||
# Subscribe to the subject
|
||||
async def message_handler(msg):
|
||||
log_trace("", f"Received message on {msg.subject}")
|
||||
|
||||
# Use smartreceive to handle the data
|
||||
result = smartreceive(msg.data)
|
||||
|
||||
# Result is an envelope dictionary with payloads field containing list of (dataname, data, data_type) tuples
|
||||
for dataname, data, data_type in result["payloads"]:
|
||||
if isinstance(data, bytes):
|
||||
log_trace(result.get("correlationId", ""), f"Received binary '{dataname}' of type {data_type}")
|
||||
log_trace(result.get("correlationId", ""), f" Size: {len(data)} bytes")
|
||||
|
||||
# Display first 100 bytes as hex
|
||||
log_trace(result.get("correlationId", ""), f" First 100 bytes (hex): {data[:100].hex()}")
|
||||
|
||||
# Save to file
|
||||
output_path = f"./received_{dataname}.bin"
|
||||
with open(output_path, 'wb') as f:
|
||||
f.write(data)
|
||||
log_trace(result.get("correlationId", ""), f"Saved binary to {output_path}")
|
||||
else:
|
||||
log_trace(result.get("correlationId", ""), f"Received unexpected data type for '{dataname}': {type(data)}")
|
||||
|
||||
sid = await nc.subscribe(SUBJECT, cb=message_handler)
|
||||
log_trace("", f"Subscribed to {SUBJECT} with subscription ID: {sid}")
|
||||
|
||||
# Keep listening for 120 seconds
|
||||
await asyncio.sleep(120)
|
||||
await nc.close()
|
||||
log_trace("", "Test completed.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -1,80 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for file transport testing - Micropython
|
||||
Tests sending binary files via NATS using nats_bridge.py smartsend
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add src to path for import
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
|
||||
|
||||
from nats_bridge import smartsend, log_trace
|
||||
import uuid
|
||||
|
||||
# Configuration
|
||||
SUBJECT = "/NATSBridge_file_test"
|
||||
NATS_URL = "nats://nats.yiem.cc:4222"
|
||||
FILESERVER_URL = "http://192.168.88.104:8080"
|
||||
SIZE_THRESHOLD = 1_000_000 # 1MB
|
||||
|
||||
# Create correlation ID for tracing
|
||||
correlation_id = str(uuid.uuid4())
|
||||
|
||||
|
||||
def main():
|
||||
# Create small binary data (will use direct transport)
|
||||
small_binary = b"This is small binary data for testing direct transport."
|
||||
small_binary += b"\x00" * 100 # Add some null bytes
|
||||
|
||||
# Create large binary data (will use link transport if > 1MB)
|
||||
# Generate a larger binary (~2MB to ensure link transport)
|
||||
large_binary = bytes([
|
||||
(i * 7) % 256 for i in range(2_000_000)
|
||||
])
|
||||
|
||||
# Test data 1: small binary (direct transport)
|
||||
data1 = ("small_binary", small_binary, "binary")
|
||||
|
||||
# Test data 2: large binary (link transport)
|
||||
data2 = ("large_binary", large_binary, "binary")
|
||||
|
||||
log_trace(correlation_id, f"Starting smartsend for subject: {SUBJECT}")
|
||||
log_trace(correlation_id, f"Correlation ID: {correlation_id}")
|
||||
|
||||
# Use smartsend with binary type
|
||||
env, env_json_str = smartsend(
|
||||
SUBJECT,
|
||||
[data1, data2], # List of (dataname, data, type) tuples
|
||||
nats_url=NATS_URL,
|
||||
fileserver_url=FILESERVER_URL,
|
||||
size_threshold=SIZE_THRESHOLD,
|
||||
correlation_id=correlation_id,
|
||||
msg_purpose="chat",
|
||||
sender_name="file_sender",
|
||||
receiver_name="",
|
||||
receiver_id="",
|
||||
reply_to="",
|
||||
reply_to_msg_id="",
|
||||
is_publish=True # Publish the message to NATS
|
||||
)
|
||||
|
||||
log_trace(correlation_id, f"Sent message with {len(env.payloads)} payloads")
|
||||
|
||||
# Log transport type for each payload
|
||||
for i, payload in enumerate(env.payloads):
|
||||
log_trace(correlation_id, f"Payload {i+1} ('{payload.dataname}'):")
|
||||
log_trace(correlation_id, f" Transport: {payload.transport}")
|
||||
log_trace(correlation_id, f" Type: {payload.type}")
|
||||
log_trace(correlation_id, f" Size: {payload.size} bytes")
|
||||
log_trace(correlation_id, f" Encoding: {payload.encoding}")
|
||||
|
||||
if payload.transport == "link":
|
||||
log_trace(correlation_id, f" URL: {payload.data}")
|
||||
|
||||
print(f"Test completed. Correlation ID: {correlation_id}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,97 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for mixed payload testing - Receiver
|
||||
Tests receiving mixed payload types via NATS using nats_bridge.py smartreceive
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
|
||||
# Add src to path for import
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
|
||||
|
||||
from nats_bridge import smartreceive, log_trace
|
||||
import nats
|
||||
import asyncio
|
||||
|
||||
# Configuration
|
||||
SUBJECT = "/NATSBridge_mixed_test"
|
||||
NATS_URL = "nats://nats.yiem.cc:4222"
|
||||
|
||||
|
||||
async def main():
|
||||
log_trace("", f"Starting mixed payload receiver test...")
|
||||
log_trace("", f"Note: This receiver will wait for messages from the sender.")
|
||||
log_trace("", f"Run test_micropython_mixed_sender.py first to send test data.")
|
||||
|
||||
# Connect to NATS
|
||||
nc = await nats.connect(NATS_URL)
|
||||
log_trace("", f"Connected to NATS at {NATS_URL}")
|
||||
|
||||
# Subscribe to the subject
|
||||
async def message_handler(msg):
|
||||
log_trace("", f"Received message on {msg.subject}")
|
||||
|
||||
# Use smartreceive to handle the data
|
||||
result = smartreceive(msg.data)
|
||||
|
||||
log_trace(result.get("correlationId", ""), f"Received envelope with {len(result['payloads'])} payloads")
|
||||
|
||||
# Result is an envelope dictionary with payloads field containing list of (dataname, data, data_type) tuples
|
||||
for dataname, data, data_type in result["payloads"]:
|
||||
log_trace(result.get("correlationId", ""), f"\n--- Payload: {dataname} (type: {data_type}) ---")
|
||||
|
||||
if isinstance(data, str):
|
||||
log_trace(result.get("correlationId", ""), f" Type: text/string")
|
||||
log_trace(result.get("correlationId", ""), f" Length: {len(data)} characters")
|
||||
if len(data) <= 100:
|
||||
log_trace(result.get("correlationId", ""), f" Content: {data}")
|
||||
else:
|
||||
log_trace(result.get("correlationId", ""), f" First 100 chars: {data[:100]}...")
|
||||
# Save to file
|
||||
output_path = f"./received_{dataname}.txt"
|
||||
with open(output_path, 'w') as f:
|
||||
f.write(data)
|
||||
log_trace(result.get("correlationId", ""), f" Saved to: {output_path}")
|
||||
|
||||
elif isinstance(data, dict):
|
||||
log_trace(result.get("correlationId", ""), f" Type: dictionary")
|
||||
log_trace(result.get("correlationId", ""), f" Keys: {list(data.keys())}")
|
||||
log_trace(result.get("correlationId", ""), f" Content: {json.dumps(data, indent=2)}")
|
||||
# Save to file
|
||||
output_path = f"./received_{dataname}.json"
|
||||
with open(output_path, 'w') as f:
|
||||
json.dump(data, f, indent=2)
|
||||
log_trace(result.get("correlationId", ""), f" Saved to: {output_path}")
|
||||
|
||||
elif isinstance(data, bytes):
|
||||
log_trace(result.get("correlationId", ""), f" Type: binary")
|
||||
log_trace(result.get("correlationId", ""), f" Size: {len(data)} bytes")
|
||||
log_trace(result.get("correlationId", ""), f" First 100 bytes (hex): {data[:100].hex()}")
|
||||
# Save to file
|
||||
output_path = f"./received_{dataname}.bin"
|
||||
with open(output_path, 'wb') as f:
|
||||
f.write(data)
|
||||
log_trace(result.get("correlationId", ""), f" Saved to: {output_path}")
|
||||
else:
|
||||
log_trace(result.get("correlationId", ""), f" Received unexpected data type: {type(data)}")
|
||||
|
||||
# Log envelope metadata
|
||||
log_trace(result.get("correlationId", ""), f"\n--- Envelope Metadata ---")
|
||||
log_trace(result.get("correlationId", ""), f" Correlation ID: {result.get('correlationId', 'N/A')}")
|
||||
log_trace(result.get("correlationId", ""), f" Message ID: {result.get('msgId', 'N/A')}")
|
||||
log_trace(result.get("correlationId", ""), f" Sender: {result.get('senderName', 'N/A')}")
|
||||
log_trace(result.get("correlationId", ""), f" Purpose: {result.get('msgPurpose', 'N/A')}")
|
||||
|
||||
sid = await nc.subscribe(SUBJECT, cb=message_handler)
|
||||
log_trace("", f"Subscribed to {SUBJECT} with subscription ID: {sid}")
|
||||
|
||||
# Keep listening for 120 seconds
|
||||
await asyncio.sleep(120)
|
||||
await nc.close()
|
||||
log_trace("", "Test completed.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -1,94 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for mixed payload testing - Micropython
|
||||
Tests sending mixed payload types via NATS using nats_bridge.py smartsend
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add src to path for import
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
|
||||
|
||||
from nats_bridge import smartsend, log_trace
|
||||
import uuid
|
||||
|
||||
# Configuration
|
||||
SUBJECT = "/NATSBridge_mixed_test"
|
||||
NATS_URL = "nats://nats.yiem.cc:4222"
|
||||
FILESERVER_URL = "http://192.168.88.104:8080"
|
||||
SIZE_THRESHOLD = 1_000_000 # 1MB
|
||||
|
||||
# Create correlation ID for tracing
|
||||
correlation_id = str(uuid.uuid4())
|
||||
|
||||
|
||||
def main():
|
||||
# Create payloads for mixed content test
|
||||
|
||||
# 1. Small text (direct transport)
|
||||
text_data = "Hello, this is a text message for testing mixed payloads!"
|
||||
|
||||
# 2. Small dictionary (direct transport)
|
||||
dict_data = {
|
||||
"status": "ok",
|
||||
"code": 200,
|
||||
"message": "Test successful",
|
||||
"items": [1, 2, 3]
|
||||
}
|
||||
|
||||
# 3. Small binary (direct transport)
|
||||
binary_data = b"\x00\x01\x02\x03\x04\x05" + b"\xff" * 100
|
||||
|
||||
# 4. Large text (link transport - will use fileserver)
|
||||
large_text = "\n".join([
|
||||
f"Line {i}: This is a large text payload for link transport testing. " * 50
|
||||
for i in range(100)
|
||||
])
|
||||
|
||||
# Test data list - mixed payload types
|
||||
data = [
|
||||
("message_text", text_data, "text"),
|
||||
("config_dict", dict_data, "dictionary"),
|
||||
("small_binary", binary_data, "binary"),
|
||||
("large_text", large_text, "text"),
|
||||
]
|
||||
|
||||
log_trace(correlation_id, f"Starting smartsend for subject: {SUBJECT}")
|
||||
log_trace(correlation_id, f"Correlation ID: {correlation_id}")
|
||||
|
||||
# Use smartsend with mixed types
|
||||
env, env_json_str = smartsend(
|
||||
SUBJECT,
|
||||
data, # List of (dataname, data, type) tuples
|
||||
nats_url=NATS_URL,
|
||||
fileserver_url=FILESERVER_URL,
|
||||
size_threshold=SIZE_THRESHOLD,
|
||||
correlation_id=correlation_id,
|
||||
msg_purpose="chat",
|
||||
sender_name="mixed_sender",
|
||||
receiver_name="",
|
||||
receiver_id="",
|
||||
reply_to="",
|
||||
reply_to_msg_id="",
|
||||
is_publish=True # Publish the message to NATS
|
||||
)
|
||||
|
||||
log_trace(correlation_id, f"Sent message with {len(env.payloads)} payloads")
|
||||
|
||||
# Log transport type for each payload
|
||||
for i, payload in enumerate(env.payloads):
|
||||
log_trace(correlation_id, f"Payload {i+1} ('{payload.dataname}'):")
|
||||
log_trace(correlation_id, f" Transport: {payload.transport}")
|
||||
log_trace(correlation_id, f" Type: {payload.type}")
|
||||
log_trace(correlation_id, f" Size: {payload.size} bytes")
|
||||
log_trace(correlation_id, f" Encoding: {payload.encoding}")
|
||||
|
||||
if payload.transport == "link":
|
||||
log_trace(correlation_id, f" URL: {payload.data}")
|
||||
|
||||
print(f"Test completed. Correlation ID: {correlation_id}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,69 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for text transport testing - Receiver
|
||||
Tests receiving text messages via NATS using nats_bridge.py smartreceive
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import json
|
||||
|
||||
# Add src to path for import
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
|
||||
|
||||
from nats_bridge import smartreceive, log_trace
|
||||
import nats
|
||||
import asyncio
|
||||
|
||||
# Configuration
|
||||
SUBJECT = "/NATSBridge_text_test"
|
||||
NATS_URL = "nats://nats.yiem.cc:4222"
|
||||
|
||||
|
||||
async def main():
|
||||
log_trace("", f"Starting text transport receiver test...")
|
||||
log_trace("", f"Note: This receiver will wait for messages from the sender.")
|
||||
log_trace("", f"Run test_micropython_text_sender.py first to send test data.")
|
||||
|
||||
# Connect to NATS
|
||||
nc = await nats.connect(NATS_URL)
|
||||
log_trace("", f"Connected to NATS at {NATS_URL}")
|
||||
|
||||
# Subscribe to the subject
|
||||
async def message_handler(msg):
|
||||
log_trace("", f"Received message on {msg.subject}")
|
||||
|
||||
# Use smartreceive to handle the data
|
||||
result = smartreceive(msg.data)
|
||||
|
||||
# Result is an envelope dictionary with payloads field containing list of (dataname, data, data_type) tuples
|
||||
for dataname, data, data_type in result["payloads"]:
|
||||
if isinstance(data, str):
|
||||
log_trace(result.get("correlationId", ""), f"Received text '{dataname}' of type {data_type}")
|
||||
log_trace(result.get("correlationId", ""), f" Length: {len(data)} characters")
|
||||
|
||||
# Display first 100 characters
|
||||
if len(data) > 100:
|
||||
log_trace(result.get("correlationId", ""), f" First 100 characters: {data[:100]}...")
|
||||
else:
|
||||
log_trace(result.get("correlationId", ""), f" Content: {data}")
|
||||
|
||||
# Save to file
|
||||
output_path = f"./received_{dataname}.txt"
|
||||
with open(output_path, 'w') as f:
|
||||
f.write(data)
|
||||
log_trace(result.get("correlationId", ""), f"Saved text to {output_path}")
|
||||
else:
|
||||
log_trace(result.get("correlationId", ""), f"Received unexpected data type for '{dataname}': {type(data)}")
|
||||
|
||||
sid = await nc.subscribe(SUBJECT, cb=message_handler)
|
||||
log_trace("", f"Subscribed to {SUBJECT} with subscription ID: {sid}")
|
||||
|
||||
# Keep listening for 120 seconds
|
||||
await asyncio.sleep(120)
|
||||
await nc.close()
|
||||
log_trace("", "Test completed.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -1,82 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test script for text transport testing - Micropython
|
||||
Tests sending text messages via NATS using nats_bridge.py smartsend
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add src to path for import
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src'))
|
||||
|
||||
from nats_bridge import smartsend, log_trace
|
||||
import uuid
|
||||
|
||||
# Configuration
|
||||
SUBJECT = "/NATSBridge_text_test"
|
||||
NATS_URL = "nats://nats.yiem.cc:4222"
|
||||
FILESERVER_URL = "http://192.168.88.104:8080"
|
||||
SIZE_THRESHOLD = 1_000_000 # 1MB
|
||||
|
||||
# Create correlation ID for tracing
|
||||
correlation_id = str(uuid.uuid4())
|
||||
|
||||
|
||||
def main():
|
||||
# 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 = "\n".join([
|
||||
f"Line {i}: This is a sample text line with some content to pad the size. " * 100
|
||||
for i in range(500)
|
||||
])
|
||||
|
||||
# Test data 1: small text
|
||||
data1 = ("small_text", small_text, "text")
|
||||
|
||||
# Test data 2: large text
|
||||
data2 = ("large_text", large_text, "text")
|
||||
|
||||
log_trace(correlation_id, f"Starting smartsend for subject: {SUBJECT}")
|
||||
log_trace(correlation_id, f"Correlation ID: {correlation_id}")
|
||||
|
||||
# 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, env_json_str = smartsend(
|
||||
SUBJECT,
|
||||
[data1, data2], # List of (dataname, data, type) tuples
|
||||
nats_url=NATS_URL,
|
||||
fileserver_url=FILESERVER_URL,
|
||||
size_threshold=SIZE_THRESHOLD,
|
||||
correlation_id=correlation_id,
|
||||
msg_purpose="chat",
|
||||
sender_name="text_sender",
|
||||
receiver_name="",
|
||||
receiver_id="",
|
||||
reply_to="",
|
||||
reply_to_msg_id="",
|
||||
is_publish=True # Publish the message to NATS
|
||||
)
|
||||
|
||||
log_trace(correlation_id, f"Sent message with {len(env.payloads)} payloads")
|
||||
|
||||
# Log transport type for each payload
|
||||
for i, payload in enumerate(env.payloads):
|
||||
log_trace(correlation_id, f"Payload {i+1} ('{payload.dataname}'):")
|
||||
log_trace(correlation_id, f" Transport: {payload.transport}")
|
||||
log_trace(correlation_id, f" Type: {payload.type}")
|
||||
log_trace(correlation_id, f" Size: {payload.size} bytes")
|
||||
log_trace(correlation_id, f" Encoding: {payload.encoding}")
|
||||
|
||||
if payload.transport == "link":
|
||||
log_trace(correlation_id, f" URL: {payload.data}")
|
||||
|
||||
print(f"Test completed. Correlation ID: {correlation_id}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user