void test_timer(void) { dispatch_test_start("Dispatch Update Timer"); dispatch_queue_t main_q = dispatch_get_main_queue(); //test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue()); __block int i = 0; struct timeval start_time; gettimeofday(&start_time, NULL); dispatch_source_t s = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q); test_ptr_notnull("dispatch_source_create", s); dispatch_source_set_timer(s, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), NSEC_PER_SEC, 0); dispatch_source_set_cancel_handler(s, ^{ struct timeval end_time; gettimeofday(&end_time, NULL); // Make sure we actually managed to adjust the interval // duration. Seven one second ticks would blow past // this. test_long_less_than("total duration", end_time.tv_sec - start_time.tv_sec, 3); test_stop(); dispatch_release(s); });
int main(void) { const char *path = "/usr/share/dict/words"; struct stat sb; dispatch_test_start("Dispatch Source Read"); int infd = open(path, O_RDONLY); if (infd == -1) { perror(path); exit(EXIT_FAILURE); } if (fstat(infd, &sb) == -1) { perror(path); exit(EXIT_FAILURE); } bytes_total = sb.st_size; if (fcntl(infd, F_SETFL, O_NONBLOCK) != 0) { perror(path); exit(EXIT_FAILURE); } if (!dispatch_test_check_evfilt_read_for_fd(infd)) { test_skip("EVFILT_READ kevent not firing for test file"); test_fin(NULL); } dispatch_queue_t main_q = dispatch_get_main_queue(); test_ptr_notnull("dispatch_get_main_queue", main_q); dispatch_source_t reader = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, infd, 0, main_q); test_ptr_notnull("dispatch_source_create", reader); assert(reader); dispatch_source_set_event_handler(reader, ^{ size_t estimated = dispatch_source_get_data(reader); fprintf(stderr, "bytes available: %zu\n", estimated); test_double_less_than_or_equal("estimated", estimated, bytes_total - bytes_read); const ssize_t bufsiz = 1024*500; // 500 KB buffer static char buffer[1024*500]; // 500 KB buffer ssize_t actual = read(infd, buffer, sizeof(buffer)); bytes_read += actual; printf("bytes read: %zd\n", actual); if (actual < bufsiz) { actual = read(infd, buffer, sizeof(buffer)); bytes_read += actual; // confirm EOF condition test_long("EOF", actual, 0); dispatch_source_cancel(reader); } });
int main(void) { static long total; dispatch_semaphore_t dsema; dispatch_test_start("Dispatch Semaphore"); dsema = dispatch_semaphore_create(1); assert(dsema); dispatch_apply(LAPS, dispatch_get_global_queue(0, 0), ^(size_t idx __attribute__((unused))) { dispatch_semaphore_wait(dsema, DISPATCH_TIME_FOREVER); total++; dispatch_semaphore_signal(dsema); });
void test_timer(void) { dispatch_test_start("Dispatch Source Timer, bit 31"); dispatch_queue_t main_q = dispatch_get_main_queue(); //test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue()); struct timeval start_time; static dispatch_source_t s; s = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q); test_ptr_notnull("dispatch_source_create", s); dispatch_source_set_timer(s, dispatch_time(DISPATCH_TIME_NOW, 0x80000000ull), 0x80000000ull, 0); gettimeofday(&start_time, NULL); dispatch_source_set_event_handler(s, ^{ dispatch_source_cancel(s); });
void test_timer(void) { dispatch_test_start("Dispatch Suspend Timer"); dispatch_queue_t main_q = dispatch_get_main_queue(); //test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue()); __block int i = 0, i_prime = 0; __block int j = 0; tweedledee = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q); test_ptr_notnull("dispatch_source_timer_create", tweedledee); dispatch_source_set_timer(tweedledee, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), NSEC_PER_SEC, 0); dispatch_source_set_cancel_handler(tweedledee, ^ { dispatch_release(tweedledee); });
void test_timer(void) { dispatch_test_start("Dispatch Source Timer, bit 63"); //uint64_t interval = 0xffffffffffffffffull; uint64_t interval = 0x8000000000000001ull; dispatch_queue_t mainq = dispatch_get_main_queue(); __block int i = 0; struct timeval start_time; gettimeofday(&start_time, NULL); static dispatch_source_t ds; ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, mainq); assert(ds); dispatch_source_set_event_handler(ds, ^{ assert(i < 1); printf("%d\n", i++); });
void test_after(void) { __block dispatch_time_t time_a_min, time_a, time_a_max; __block dispatch_time_t time_b_min, time_b, time_b_max; __block dispatch_time_t time_c_min, time_c, time_c_max; dispatch_test_start("Dispatch After"); dispatch_async(dispatch_get_main_queue(), ^{ time_a_min = dispatch_time(0, 5.5*NSEC_PER_SEC); time_a = dispatch_time(0, 6*NSEC_PER_SEC); time_a_max = dispatch_time(0, 6.5*NSEC_PER_SEC); dispatch_after(time_a, dispatch_get_main_queue(), ^{ dispatch_time_t now_a = dispatch_time(0, 0); test_long_less_than("can't finish faster than 5.5s", 0, now_a - time_a_min); test_long_less_than("must finish faster than 6.5s", 0, time_a_max - now_a); time_b_min = dispatch_time(0, 1.5*NSEC_PER_SEC); time_b = dispatch_time(0, 2*NSEC_PER_SEC); time_b_max = dispatch_time(0, 2.5*NSEC_PER_SEC); dispatch_after(time_b, dispatch_get_main_queue(), ^{ dispatch_time_t now_b = dispatch_time(0, 0); test_long_less_than("can't finish faster than 1.5s", 0, now_b - time_b_min); test_long_less_than("must finish faster than 2.5s", 0, time_b_max - now_b); time_c_min = dispatch_time(0, 0*NSEC_PER_SEC); time_c = dispatch_time(0, 0*NSEC_PER_SEC); time_c_max = dispatch_time(0, .5*NSEC_PER_SEC); dispatch_after(time_c, dispatch_get_main_queue(), ^{ dispatch_time_t now_c = dispatch_time(0, 0); test_long_less_than("can't finish faster than 0s", 0, now_c - time_c_min); test_long_less_than("must finish faster than .5s", 0, time_c_max - now_c); dispatch_async_f(dispatch_get_main_queue(), NULL, done); }); }); });
int main(void) { dispatch_test_start("Dispatch VM Pressure test"); // rdar://problem/7000945 if (!dispatch_test_check_evfilt_vm()) { test_skip("EVFILT_VM not supported"); test_stop(); return 0; } initial = time(NULL); uint64_t memsize = _dispatch_get_memory_size(); max_page_count = MIN(memsize, MAXMEM) / ALLOC_SIZE; pages = calloc(max_page_count, sizeof(char*)); vm_queue = dispatch_queue_create("VM Pressure", NULL); vm_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VM, 0, DISPATCH_VM_PRESSURE, vm_queue); dispatch_source_set_event_handler(vm_source, ^{ if (!page_count) { // Too much memory pressure already to start the test test_skip("Memory pressure at start of test"); cleanup(); } if (__sync_add_and_fetch(&handler_call_count, 1) != NOTIFICATIONS) { log_msg("Ignoring vm pressure notification\n"); interval = 1; return; } test_long("dispatch_source_get_data()", dispatch_source_get_data(vm_source), NOTE_VM_PRESSURE); int32_t i, pc = page_count + 1; for (i = 0; i < pc && pages[i]; ++i) { free(pages[i]); pages[i] = NULL; } log_msg("Freed %ldMB\n", pg2mb(i)); });
int main(void) { dispatch_test_start("Dispatch Queue Finalizer"); #ifdef __LP64__ ctxt_magic = (void*)((uintptr_t)arc4random() << 32 | arc4random()); #else ctxt_magic = (void*)arc4random(); #endif // we need a non-NULL value for the tests to work properly if (ctxt_magic == NULL) { ctxt_magic = &ctxt_magic; } dispatch_queue_t q = dispatch_queue_create("com.apple.testing.finalizer", NULL); test_ptr_notnull("dispatch_queue_new", q); dispatch_set_finalizer_f(q, finalize); dispatch_queue_t q_null_context = dispatch_queue_create("com.apple.testing.finalizer.context_null", NULL); dispatch_set_context(q_null_context, NULL); dispatch_set_finalizer_f(q_null_context, never_call); dispatch_release(q_null_context); // Don't test k dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), dispatch_get_main_queue(), ^{ // Usign async to set the context helps test that blocks are // run before the release as opposed to just thrown away. dispatch_async(q, ^{ dispatch_set_context(q, ctxt_magic); }); dispatch_release(q); });
int main(int argc, char** argv) { struct hostent *he; int sockfd, clientfd; struct sockaddr_in addr1, addr2, server; socklen_t addr2len; socklen_t addr1len; pid_t clientid; const char *path = "/usr/share/dict/words"; int read_fd, fd; if (argc == 2) { // Client dispatch_test_start(NULL); if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { test_errno("Client-socket()", errno, 0); test_stop(); } if ((he = gethostbyname("localhost")) == NULL) { fprintf(stderr, "Client-gethostbyname() failed\n"); test_stop(); } memcpy(&server.sin_addr, he->h_addr_list[0], he->h_length); server.sin_family = AF_INET; server.sin_port = atoi(argv[1]); fprintf(stderr, "Client-connecting on port ... %d\n", server.sin_port); if (connect(sockfd, (struct sockaddr *)&server, sizeof(server))) { test_errno("client-connect()", errno, 0); test_stop(); } // Read from the socket and compare the contents are what we expect fd = open(path, O_RDONLY); if (fd == -1) { test_errno("client-open", errno, 0); test_stop(); } #ifdef F_NOCACHE if (fcntl(fd, F_NOCACHE, 1)) { test_errno("client-fcntl F_NOCACHE", errno, 0); test_stop(); } #else // investigate what the impact of lack of file cache disabling has // for this test #endif struct stat sb; if (fstat(fd, &sb)) { test_errno("client-fstat", errno, 0); test_stop(); } size_t size = sb.st_size; __block dispatch_data_t g_d1 = dispatch_data_empty; __block dispatch_data_t g_d2 = dispatch_data_empty; __block int g_error = 0; dispatch_group_t g = dispatch_group_create(); dispatch_group_enter(g); dispatch_read(fd, size, dispatch_get_global_queue(0, 0), ^(dispatch_data_t d1, int error) { test_errno("Client-dict-read error", error, 0); test_long("Client-dict-dispatch data size", dispatch_data_get_size(d1), size); dispatch_retain(d1); g_d1 = d1; dispatch_group_leave(g); });