Files
nostr_core_lib/tests/simple_async_test.c

70 lines
2.1 KiB
C

#define _DEFAULT_SOURCE
#include "../nostr_core/nostr_core.h"
#include "../cjson/cJSON.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// Test callback function
static int callback_count = 0;
void test_callback(const char* relay_url, const char* event_id,
int success, const char* message, void* user_data) {
callback_count++;
printf("📡 Callback %d: Relay %s, Success: %s\n",
callback_count, relay_url, success ? "YES" : "NO");
if (message) {
printf(" Message: %s\n", message);
}
}
int main() {
printf("🧪 Simple Async Publish Test\n");
printf("============================\n");
// Create pool
nostr_relay_pool_t* pool = nostr_relay_pool_create(NULL);
if (!pool) {
printf("❌ Failed to create pool\n");
return 1;
}
// Create a test event
cJSON* event = cJSON_CreateObject();
cJSON_AddStringToObject(event, "id", "test_event_simple");
cJSON_AddNumberToObject(event, "kind", 1);
cJSON_AddStringToObject(event, "content", "Test async publish");
cJSON_AddNumberToObject(event, "created_at", time(NULL));
cJSON_AddStringToObject(event, "pubkey", "test_pubkey");
cJSON_AddStringToObject(event, "sig", "test_signature");
// Test with non-existent relay (should trigger connection failure callback)
const char* test_relays[] = {"ws://nonexistent.example.com"};
printf("🚀 Testing async publish...\n");
// Call async publish
int sent_count = nostr_relay_pool_publish_async(
pool, test_relays, 1, event, test_callback, NULL);
printf("📊 Sent to %d relays\n", sent_count);
// Wait a bit for callback
printf("⏳ Waiting for callback...\n");
for (int i = 0; i < 5 && callback_count == 0; i++) {
nostr_relay_pool_poll(pool, 100);
usleep(100000); // 100ms
}
printf("\n📈 Results:\n");
printf(" Callbacks received: %d\n", callback_count);
// Cleanup
cJSON_Delete(event);
nostr_relay_pool_destroy(pool);
printf("\n✅ Simple async test completed!\n");
return callback_count > 0 ? 0 : 1;
}