int main(void) { SXE_LUA *sxel = sxe_lua_new(); snprintf(s_string, sizeof s_string, "Goodbye, cruel world"); sxe_lua_tie_field(sxel, "test", "bool", 'b', &s_bool); sxe_lua_tie_field(sxel, "test", "int", 'i', &s_int); sxe_lua_tie_field(sxel, "test", "uint", 'i', &s_uint); sxe_lua_tie_field(sxel, "test", "long", 'l', &s_long); sxe_lua_tie_field(sxel, "test", "ulong", 'l', &s_ulong); sxe_lua_tie_field(sxel, "test", "str", 's', s_string, sizeof s_string); SXEL10("sxelua: loading sxelua handlers"); sxe_lua_load(sxel, "../test/test-sxe-lua.lua"); sxe_init(); sxe_lua_init(sxel); ev_loop(ev_default_loop(EVFLAG_AUTO), 0); is_eq(s_string, "Hello, world", "tied string set to 'Hello, world'"); is(s_int, 10, "tied integer set to '10'"); is(s_uint, 128, "tied unsigned set to '128'"); is(s_long, 42L, "tied long set to '42'"); is(s_ulong, -2UL, "tied unsigned long set to '-2'"); is(s_bool, 1, "tied boolean set to '1'"); return exit_status(); }
int main(void) { SXE_THREAD thread; SXE_RETURN result; unsigned i; plan_tests(6); sxe_log_hook_line_out(test_log_line); test_log_level = sxe_log_set_level(SXE_LOG_LEVEL_LIBRARY_TRACE); /* Required to do indentation test */ sxe_spinlock_construct(&ping); sxe_spinlock_construct(&pong); is(sxe_spinlock_take(&ping), SXE_SPINLOCK_STATUS_TAKEN, "Ping lock taken by main"); is((result = sxe_thread_create(&thread, test_thread_main, &ping, SXE_THREAD_OPTION_DEFAULTS)), SXE_RETURN_OK, "Created SXE thread"); sxe_spinlock_give(&ping); /* Allow thread to proceed */ /* Wait for thread_indent to be set. */ for (i = 0; thread_indent == 0 && i < TEST_YIELD_MAX; i++) { SXE_YIELD(); } ok(i < TEST_YIELD_MAX, "Thread log indent set to %u after %u yeilds", thread_indent, i); SXEL10("main: about to confirm the pong from the thread"); is(sxe_spinlock_take(&pong), SXE_SPINLOCK_STATUS_TAKEN, "Pong lock taken by main"); ok(main_indent > 0, "Main log indent set to %u", main_indent); ok(thread_indent > main_indent, "Thread indent is greater than main indent"); return exit_status(); }
static SXE_THREAD_RETURN SXE_STDCALL test_thread_main(void * lock) { SXEE81("test_thread_main(lock=%p)", lock); SXEA10(lock == &ping, "Ping lock not passed to the thread"); SXEA10(sxe_spinlock_take(&pong) == SXE_SPINLOCK_STATUS_TAKEN, "Pong lock not taken by thread"); SXEA10(sxe_spinlock_take(&ping) == SXE_SPINLOCK_STATUS_TAKEN, "Ping lock not taken by thread"); SXEL10("thread: about to pong the main thread"); sxe_spinlock_give(&pong); for (;;) { sleep(1); } SXER80("return NULL"); return (SXE_THREAD_RETURN)0; }
int main(int argc, char *argv[]) { tap_ev ev; struct object self; struct object * this = &self; char expected[1024]; pid_t pid = getpid(); plan_tests(28); MOCK_SET_HOOK(openlog, t_openlog); MOCK_SET_HOOK(syslog, t_syslog); q_syslog = tap_ev_queue_new(); sxe_log_use_syslog("my-program", LOG_NDELAY|LOG_PID, LOG_USER); ev = tap_ev_queue_shift(q_syslog); is_eq(tap_ev_identifier(ev), "openlog", "sxe_log_use_syslog() calls openlog()"); is_eq(tap_ev_arg(ev, "ident"), "my-program", "sxe_log_use_syslog() calls openlog() with correct 'ident' parameter"); is(tap_ev_arg(ev, "option"), LOG_NDELAY|LOG_PID, "sxe_log_use_syslog() calls openlog() with correct 'option' parameter"); is(tap_ev_arg(ev, "facility"), LOG_USER, "sxe_log_use_syslog() calls openlog() with correct 'facility' parameter"); SXEL10("SXEL10"); snprintf(expected, sizeof expected, "T=%d ------ 1 - SXEL10\n", pid); ev = tap_ev_queue_shift(q_syslog); is_eq(tap_ev_identifier(ev), "syslog", "SXEL10() calls syslog()"); is(tap_ev_arg(ev, "priority"), LOG_ERR, "SXEL10() maps to LOG_ERR syslog level"); is_eq(tap_ev_arg(ev, "logline"), expected, "SXEL10() is logged correctly"); SXEL21("SXEL21(%s)", "arg1"); snprintf(expected, sizeof expected, "T=%d ------ 2 - SXEL21(arg1)\n", pid); ev = tap_ev_queue_shift(q_syslog); is_eq(tap_ev_identifier(ev), "syslog", "SXEL21() calls syslog()"); is(tap_ev_arg(ev, "priority"), LOG_WARNING, "SXEL21() maps to LOG_WARNING syslog level"); is_eq(tap_ev_arg(ev, "logline"), expected, "SXEL21() is logged correctly"); SXEL32("SXEL32(%s,%d)", "arg1", 22); snprintf(expected, sizeof expected, "T=%d ------ 3 - SXEL32(arg1,22)\n", pid); ev = tap_ev_queue_shift(q_syslog); is_eq(tap_ev_identifier(ev), "syslog", "SXEL32() calls syslog()"); is(tap_ev_arg(ev, "priority"), LOG_NOTICE, "SXEL32() maps to LOG_NOTICE syslog level"); is_eq(tap_ev_arg(ev, "logline"), expected, "SXEL32() is logged correctly"); SXEL43("SXEL43(%s,%d,%u)", "arg1", 22, 44); snprintf(expected, sizeof expected, "T=%d ------ 4 - SXEL43(arg1,22,44)\n", pid); ev = tap_ev_queue_shift(q_syslog); is_eq(tap_ev_identifier(ev), "syslog", "SXEL43() calls syslog()"); is(tap_ev_arg(ev, "priority"), LOG_INFO, "SXEL43() maps to LOG_INFO syslog level"); is_eq(tap_ev_arg(ev, "logline"), expected, "SXEL43() is logged correctly"); SXEL54("SXEL54(%s,%d,%u,%x)", "arg1", 22, 44, 64); snprintf(expected, sizeof expected, "T=%d ------ 5 - SXEL54(arg1,22,44,40)\n", pid); ev = tap_ev_queue_shift(q_syslog); is_eq(tap_ev_identifier(ev), "syslog", "SXEL54() calls syslog()"); is(tap_ev_arg(ev, "priority"), LOG_DEBUG, "SXEL54() maps to LOG_DEBUG syslog level"); is_eq(tap_ev_arg(ev, "logline"), expected, "SXEL54() is logged correctly"); SXEL65("SXEL65(%s,%d,%u,%x,%.2f)", "arg1", 22, 44, 64, 3.1415926); snprintf(expected, sizeof expected, "T=%d ------ 6 - SXEL65(arg1,22,44,40,3.14)\n", pid); ev = tap_ev_queue_shift(q_syslog); is_eq(tap_ev_identifier(ev), "syslog", "SXEL65() calls syslog()"); is(tap_ev_arg(ev, "priority"), LOG_DEBUG, "SXEL65() maps to LOG_DEBUG syslog level"); is_eq(tap_ev_arg(ev, "logline"), expected, "SXEL65() is logged correctly"); this->id = 99; SXEL10I("SXEL10I"); snprintf(expected, sizeof expected, "T=%d 99 1 - SXEL10I\n", pid); ev = tap_ev_queue_shift(q_syslog); is_eq(tap_ev_identifier(ev), "syslog", "SXEL10I() calls syslog()"); is(tap_ev_arg(ev, "priority"), LOG_ERR, "SXEL10I() maps to LOG_ERR syslog level"); is_eq(tap_ev_arg(ev, "logline"), expected, "SXEL10I() is logged correctly"); this->id = 98; SXEL65I("SXEL65I(%s,%d,%u,%x,%.2f)", "arg1", 22, 44, 64, 3.1415926); snprintf(expected, sizeof expected, "T=%d 98 6 - SXEL65I(arg1,22,44,40,3.14)\n", pid); ev = tap_ev_queue_shift(q_syslog); is_eq(tap_ev_identifier(ev), "syslog", "SXEL65I() calls syslog()"); is(tap_ev_arg(ev, "priority"), LOG_DEBUG, "SXEL65I() maps to LOG_DEBUG syslog level"); is_eq(tap_ev_arg(ev, "logline"), expected, "SXEL65I() is logged correctly"); sxe_log_hook_buffer_prefix(NULL); /* For coverage */ (void)argc; (void)argv; return exit_status(); }
int main(int argc, char ** argv) { #ifdef WINDOWS_NT SXEL10("WARNING: Need to implement sxe_spawn() on Windows to run this test file!"); #else int fd; double start_time; unsigned count; unsigned id; unsigned * pool; unsigned * shared; size_t size; SXE_MMAP memmap; SXE_RETURN result; SXE_SPAWN spawn[TEST_CLIENT_INSTANCES]; if (argc > 1) { count = atoi(argv[1]); sxe_mmap_open(&memmap, "memmap"); shared = (unsigned *)(unsigned long)SXE_MMAP_ADDR(&memmap); pool = sxe_pool_from_base(shared); SXEL63("Instance %u mapped to shared pool // base=%p, pool=%p", count, shared, pool); do { usleep(10000 * count); id = sxe_pool_set_oldest_element_state_locked(pool, TEST_STATE_FREE, TEST_STATE_CLIENT_TAKE); SXEA10(id != SXE_POOL_LOCK_NEVER_TAKEN, "Got SXE_POOL_LOCK_NEVER_TAKEN");; } while (id == SXE_POOL_NO_INDEX); SXEL62("Instance %u got pool element %u", count, id); pool[id] = count; sxe_pool_set_indexed_element_state_locked(pool, id, TEST_STATE_CLIENT_TAKE, TEST_STATE_CLIENT_DONE); sxe_mmap_close(&memmap); SXEL61("Instance %u exiting", count); return 0; } plan_tests(5); ok((size = sxe_pool_size(TEST_CLIENT_INSTANCES/2, sizeof(*pool), TEST_STATE_NUMBER_OF_STATES)) >= TEST_CLIENT_INSTANCES * sizeof(*pool), "Expect pool size %u to be at least the size of the array %u", size, TEST_CLIENT_INSTANCES * sizeof(*pool)); SXEA11((fd = open("memmap", O_CREAT | O_TRUNC | O_WRONLY, 0666)) >= 0, "Failed to create file 'memmap': %s", strerror(errno)); SXEA12(ftruncate(fd, size) >= 0, "Failed to extend the file to %lu bytes: %s", size, strerror(errno)); close(fd); sxe_mmap_open(&memmap, "memmap"); shared = (unsigned *)(unsigned long)SXE_MMAP_ADDR(&memmap); pool = sxe_pool_construct(shared, "shared-pool", TEST_CLIENT_INSTANCES/2, sizeof(*pool), TEST_STATE_NUMBER_OF_STATES, SXE_POOL_LOCKS_ENABLED); sxe_register(TEST_CLIENT_INSTANCES + 1, 0); SXEA11((result = sxe_init()) == SXE_RETURN_OK, "Failed to initialize the SXE package: %s", sxe_return_to_string(result)); for (count = 1; count <= TEST_CLIENT_INSTANCES; count++) { char buffer[12]; snprintf(buffer, sizeof(buffer), "%u", count); result = sxe_spawn(NULL, &spawn[count - 1], argv[0], buffer, NULL, NULL, NULL, NULL); SXEA13(result == SXE_RETURN_OK, "Failed to spawn '%s %s': %s", argv[0], buffer, sxe_return_to_string(result)); } start_time = sxe_get_time_in_seconds(); for (count = 0; (count < TEST_CLIENT_INSTANCES); ) { SXEA10((TEST_WAIT + start_time ) > sxe_get_time_in_seconds(), "Unexpected timeout... is the hardware too slow?"); usleep(10000); id = sxe_pool_set_oldest_element_state_locked(pool, TEST_STATE_CLIENT_DONE, TEST_STATE_FREE); /* Assert here in the test. The actual service would take specific action here */ SXEA12(id != SXE_POOL_LOCK_NEVER_TAKEN, "Parent: Failed to acqure lock .. yield limit reached. id %u vs %u", id, SXE_POOL_LOCK_NEVER_TAKEN); if (id != SXE_POOL_NO_INDEX) { SXEL62("Looks like instance %u got element %u", pool[id], id); count++; } } ok(count == TEST_CLIENT_INSTANCES, "All clients got an element in the pool"); start_time = sxe_get_time_in_seconds(); for (count = 0; (count < TEST_CLIENT_INSTANCES); count++) { SXEA10((TEST_WAIT + start_time ) > sxe_get_time_in_seconds(), "Unexpected timeout... is the hardware too slow?"); waitpid(spawn[count].pid, NULL, 0); } ok(SXE_POOL_LOCK_NEVER_TAKEN != sxe_pool_lock(pool), "Forced lock to be always locked!"); id = sxe_pool_set_oldest_element_state_locked(pool, TEST_STATE_FREE, TEST_STATE_FREE); ok(id == SXE_POOL_LOCK_NEVER_TAKEN, "sxe_pool_set_oldest_element_state_locked() Failed to acquire lock"); id = sxe_pool_set_indexed_element_state_locked(pool, 0, TEST_STATE_FREE, TEST_STATE_FREE); ok(id == SXE_POOL_LOCK_NEVER_TAKEN, "sxe_pool_set_indexed_element_state_locked() Failed to acquire lock"); sxe_pool_unlock(pool); sxe_pool_override_locked(pool); /* for coverage */ sxe_mmap_close(&memmap); return exit_status(); #endif }