static void sxe_dirwatch_event(EV_P_ ev_io * io, int revents) { char buffer[8192]; int length; unsigned offset; struct inotify_event * event; SXE_UNUSED_PARAMETER(revents); SXEE62("(fd=%d, revents=%08x)", io->fd, revents); SXEA11((length = read(io->fd, buffer, sizeof(buffer))) >= 0, "sxe_dirwatch_event: Failed to read events from inotify: %s", strerror(errno)); for (offset = 0; offset < (unsigned)length;) { SXE_LIST_WALKER walker; SXE_DIRWATCH * dirwatch; int flags = 0; SXEA12(length - offset >= sizeof(struct inotify_event), "Odd sized chunk left in buffer %u (expected inotify event of %u bytes)", length - offset, sizeof(struct inotify_event)); event = (struct inotify_event *)&buffer[offset]; offset += sizeof(struct inotify_event); SXEA12(length - offset >= event->len, "Chunk left in buffer %u (expected length %u)", length - offset, event->len); offset += event->len; SXEL63("dirwatch_event: fd=%d flags=%08x file=%s", event->wd, event->mask, event->name); sxe_list_walker_construct(&walker, &sxe_dirwatch_list); while ((dirwatch = (SXE_DIRWATCH *)sxe_list_walker_step(&walker)) != NULL) { if (dirwatch->fd == event->wd) { break; } } SXEA11(dirwatch, "No watched directory found with fd %d", event->wd); flags |= (event->mask & IN_CREATE) ? SXE_DIRWATCH_CREATED : 0; flags |= (event->mask & IN_MOVED_TO) ? SXE_DIRWATCH_CREATED : 0; flags |= (event->mask & IN_MODIFY) ? SXE_DIRWATCH_MODIFIED : 0; flags |= (event->mask & IN_DELETE) ? SXE_DIRWATCH_DELETED : 0; flags |= (event->mask & IN_MOVED_FROM) ? SXE_DIRWATCH_DELETED : 0; dirwatch->notify(EV_A_ event->name, flags, dirwatch->user_data); } SXER60("return"); }
void sxe_sync_ev_init(unsigned concurrency, void (*send_event)(void * sync, void * user_data)) { unsigned short port; SXEE82("sxe_sync_ev_init(concurrency=%u,send_event=%p)", concurrency, send_event); sxe_sync_ev_pool = sxe_pool_new("http_sync_ev", concurrency, sizeof(SXE_SYNC_EV), 2); sxe_sync_ev_sock = sxe_sync_ev_socket(); sxe_sync_generic_event = send_event; sxe_sync_ev_addr.sin_family = AF_INET; sxe_sync_ev_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); for (port = 1025; ; port++) { sxe_sync_ev_addr.sin_port = htons(port); if (bind(sxe_sync_ev_sock, (struct sockaddr *)&sxe_sync_ev_addr, sizeof(sxe_sync_ev_addr)) >= 0) { break; } SXEA12(port < USHRT_MAX, "Not able to bind any port between 1024 and %hu: %s", port, /* Coverage Exclusion - Assert */ sxe_socket_get_last_error_as_str()); } /* Coverage Exclusion - Not Reached */ SXEL81("Listening on port %hu", port); ev_io_init(&sxe_sync_ev_io, sxe_sync_ev_read, _open_osfhandle(sxe_sync_ev_sock, 0), EV_READ); ev_io_start(ev_default_loop(0), &sxe_sync_ev_io); SXER80("return"); }
static char * test_log_next(void) { static char line[512]; char * result; SXEA12(test_output_pipe != NULL, "test_log_next: No test program is running: trying running '%s %s' manually", test_program_name, test_program_arg); if ((result = fgets(line, sizeof(line), test_output_pipe)) == NULL) { SXEA12(pclose(test_output_pipe) >= 0, "%s: Failed to pclose: %s", test_program_name, strerror(errno)); test_output_pipe = NULL; } return result; }
static char * test_log_first(const char * arg) { char buffer[512]; if (test_output_pipe != NULL) { SXEA12(pclose(test_output_pipe) >= 0, "%s: Failed to pclose: %s", test_program_name, strerror(errno)); } test_program_arg = arg; buffer[sizeof(buffer) - 1] = '\0'; strncpy(buffer, test_program_name, sizeof(buffer) - 1); strncat(buffer, " ", sizeof(buffer) - 1); strncat(buffer, arg, sizeof(buffer) - 1); SXEA12((test_output_pipe = popen(buffer, "r")) != NULL, "Failed to popen '%s': %s", buffer, strerror(errno)); return test_log_next(); }
static int sxe_sync_ev_socket(void) { int sock; SXEE80("sxe_sync_ev_socket()"); SXEA11((sock = socket(AF_INET, SOCK_DGRAM, 0)) != SXE_SOCKET_INVALID, "Error creating sync socket: %s", sxe_socket_get_last_error_as_str()); SXEA12(sxe_socket_set_nonblock(sock, 1) >= 0, "socket %d: couldn't set non-blocking flag: %s", sock, sxe_socket_get_last_error_as_str()); SXER81("return sock=%d", sock); return sock; }
int main(int argc, char ** argv) { const char * line; unsigned i; if (argc > 1) { test_program_arg = argv[1]; sxe_log_hook_line_out(test_log_line_out_to_stdout); SXEA10(sxe_log_hook_line_out(test_log_line_out_to_stdout) == test_log_line_out_to_stdout, "sxe_log_hook_line_out failed to hook test_log_line_out_to_stdout"); if (strcmp(argv[1], "1") == 0) { if (getenv("SXE_LOG_LEVEL") != NULL) { SXED80("should not see this (level too high)", strlen("should not see this (level too high)")); /* Cover early out in dump */ } SXEL40("BOO"); } else if (strcmp(argv[1], "2") == 0) { #ifndef LOCAL_SXE_DEBUG sxe_log_set_indent_maximum(~0U); #endif sxe_log_set_level(SXE_LOG_LEVEL_WARNING); SXEE50("level_five()"); test_level_six(SXE_LOG_LEVEL_LIBRARY_DUMP); SXER50("return // five"); SXEE50("level_five()"); test_level_six(SXE_LOG_LEVEL_WARNING); SXER50("return // five"); SXEL20("that's all, folks"); } exit(0); } test_program_name = argv[0]; plan_tests(3 * TEST_LINES_EXPECTED + 3); /* Tests for different log level settings */ tap_test_case_name("Level settings"); ok((line = test_log_first("1")) != NULL, "Test log at default level wrote a line"); diag("line = %s", line); SXEA12(putenv((char *)(intptr_t)"SXE_LOG_LEVEL=2") >= 0, "%s: Failed to putenv: %s", test_program_name, strerror(errno)); ok(test_log_first("1") == NULL, "Test log with SXE_LOG_LEVEL=2 failed to write a line"); /* TODO: Replace putenvs with calls to the TBD function that allows setting fine grained levels programmatically. */ SXEA12(putenv((char *)(intptr_t)"SXE_LOG_LEVEL_LIBSXE=5") >= 0, "%s: Failed to putenv: %s", test_program_name, strerror(errno)); ok((line = test_log_first("1")) != NULL, "Test log with SXE_LOG_LEVEL_LIBSXE=5 wrote a line"); diag("line = %s", line); SXEA12(putenv((char *)(intptr_t)"SXE_LOG_LEVEL_LIBSXE_LIB_SXE_LOG=2") >= 0, "%s: Failed to setenv: %s", test_program_name, strerror(errno)); ok(test_log_first("1") == NULL, "Test log with SXE_LOG_LEVEL_LIBSXE_LIB_SXE_LOG=2 failed to write a line"); SXEA12(putenv((char *)(intptr_t)"SXE_LOG_LEVEL_LIBSXE_LIB_SXE_LOG_TEST_TEST_SXE_LOG_LEVELS=7") >= 0, "%s: Failed to putenv: %s", test_program_name, strerror(errno)); ok((line = test_log_first("1")) != NULL, "Test log with SXE_LOG_LEVEL_LIBSXE_LIB_SXE_LOG_TEST_TEST_SXE_LOG_LEVELS=7 wrote a line"); diag("line = %s", line); /* Remove the more specific environment variables */ SXEA12(unsetenv("SXE_LOG_LEVEL_LIBSXE") == 0, "%s: unsetenv failed: %s", test_program_name, strerror(errno)); SXEA12(unsetenv("SXE_LOG_LEVEL_LIBSXE_LIB_SXE_LOG") == 0, "%s: unsetenv failed: %s", test_program_name, strerror(errno)); SXEA12(unsetenv("SXE_LOG_LEVEL_LIBSXE_LIB_SXE_LOG_TEST_TEST_SXE_LOG_LEVELS") == 0, "%s: unsetenv failed: %s", test_program_name, strerror(errno)); /* Tests for indentation interacting with log level */ tap_test_case_name("Indentation"); line = test_log_first("2"); for (i = 0; i < TEST_LINES_EXPECTED; i++) { ok(line != NULL, "Got line %u", 2 * i + 1); ok(strstr(line, test_expected[i]) != NULL, "Found '%s' in '%.*s'", test_expected[i], (int)strlen(line) - 1, line); if (i > 2) { ok(test_log_next() != NULL, "Got line %u", 2 * i + 2); } line = test_log_next(); } ok(line == NULL, "Got EOF"); 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 }