Esempio n. 1
0
static void dispatch_0(void* context) {
    time_delta_t* previous = (time_delta_t*)context;
    dispatch_time_t now_c = dispatch_time(0, 0);
    MU_MESSAGE("must finish between 0.0s and 0.5s: %f",(now_c-previous->start)/(float)NSEC_PER_SEC);
    MU_ASSERT_TRUE(0<=(now_c - previous->min));
    MU_ASSERT_TRUE(0<=(previous->max - now_c));

    dispatch_async_f(dispatch_get_current_queue(), NULL, done);
}
Esempio n. 2
0
static void dispatch_6(void* context) {
    dispatch_time_t time_b;
    time_delta_t* previous = (time_delta_t*)context;
    time_delta_t* delta = (time_delta_t*)malloc(sizeof(time_delta_t));
    dispatch_time_t now_a = dispatch_time(0, 0);
    MU_MESSAGE("must finish between 5.5s and 6.5s: %f",(now_a-previous->start)/(float)NSEC_PER_SEC);
    MU_ASSERT_TRUE(0<=(now_a - previous->min));
    MU_ASSERT_TRUE(0<=(previous->max - now_a));

    delta->min = dispatch_time(0,  1.5*NSEC_PER_SEC);
    time_b = dispatch_time(0,    2*NSEC_PER_SEC);
    delta->max = dispatch_time(0,  2.5*NSEC_PER_SEC);
    delta->start = dispatch_time(0,0);
    dispatch_after_f(time_b, dispatch_get_current_queue(), delta, dispatch_2);
}
extern "C" void
        dispatch_after_lambda(void)
{
    MU_BEGIN_TEST(dispatch_after_lambda);

    dispatch_async(dispatch_get_main_queue(), [=]{
		dispatch_time_t time_a_min = dispatch_time(0,  5.5*NSEC_PER_SEC);
        dispatch_time_t time_a     = dispatch_time(0,  6.0*NSEC_PER_SEC);
		dispatch_time_t time_a_max = dispatch_time(0,  6.5*NSEC_PER_SEC);
		dispatch_time_t time_a_start = dispatch_time(0,0);
        dispatch_after(time_a, dispatch_get_current_queue(), [=]{
			dispatch_time_t now_a = dispatch_time(0, 0);
			MU_MESSAGE("must finish between 5.5s and 6.5s: %f",(now_a-time_a_start)/(float)NSEC_PER_SEC);
			MU_ASSERT_TRUE(0<=(now_a - time_a_min));
			MU_ASSERT_TRUE(0<=(time_a_max - now_a));

			dispatch_time_t time_b_min = dispatch_time(0,  1.5*NSEC_PER_SEC);
			dispatch_time_t time_b     = dispatch_time(0,    2*NSEC_PER_SEC);
			dispatch_time_t time_b_max = dispatch_time(0,  2.5*NSEC_PER_SEC);
			dispatch_time_t time_b_start = dispatch_time(0,0);
            dispatch_after(time_b, dispatch_get_current_queue(), [=]{
				dispatch_time_t now_b = dispatch_time(0, 0);
				MU_MESSAGE("must finish between 1.5s and 2.5s: %f",(now_b-time_b_start)/(float)NSEC_PER_SEC);
				MU_ASSERT_TRUE(0<=(now_b - time_b_min));
				MU_ASSERT_TRUE(0<=(time_b_max - now_b));

#if 1 // FIXME: Nesting three lambdas seems to be broken...
				dispatch_time_t time_c_min = dispatch_time(0,  0*NSEC_PER_SEC);
				dispatch_time_t time_c     = dispatch_time(0,  0*NSEC_PER_SEC);
				dispatch_time_t time_c_max = dispatch_time(0,  .5*NSEC_PER_SEC);                
				dispatch_time_t time_c_start = dispatch_time(0, 0);
                dispatch_after(time_c, dispatch_get_current_queue(), [=]{
					dispatch_time_t now_c = dispatch_time(0, 0);
					MU_MESSAGE("must finish between 0s and .5s:  %f",(now_c-time_c_start)/(float)NSEC_PER_SEC);
					MU_ASSERT_TRUE(0<=(now_c - time_c_min));
					MU_ASSERT_TRUE(0<=(time_c_max - now_c));

					dispatch_async_f(dispatch_get_current_queue(), NULL, done);
				});
#else
                dispatch_async_f(dispatch_get_current_queue(), NULL, done);
#endif
			});
		});
	});

	dispatch_main();
	MU_FAIL("Should never reach this");
	MU_END_TEST;
}
static void
		collect(void *context)
{
	uint64_t delta;
	long double math;
	size_t i;

	if (--count_down) {
		return;
	}

	delta = dispatch_time(0,0) - start;
	math = (long double)delta;
	math /= COUNT * COUNT * 2ul + COUNT * 2ul;

	MU_MESSAGE("lap: %ld", lap_count_down);
	MU_MESSAGE("count: %lu", COUNT);
	MU_MESSAGE("delta: %llu ns", (uintmax_t)delta);
	MU_MESSAGE("math: %Lf ns / lap", math);

	for (i = 0; i < COUNT; i++) {
		dispatch_release(queues[i]);
	}

	// our malloc could be a lot better,
	// this result is really a malloc torture test
	MU_ASSERT_TRUE((unsigned long)math<10000);

	if (--lap_count_down) {
		do_test();
		return;
	}

	// give the threads some time to settle before test_stop() runs "leaks"
	// ...also note, this is a total cheat.   dispatch_after lets this
	// thread go idle, so dispatch cleans up the continuations cache.
	// Doign the "old style" sleep left that stuff around and leaks
	// took a LONG TIME to complete.   Long enough that the test harness
	// decided to kill us.
	dispatch_after_f(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), NULL, MU_PASS_AFTER_DELAY);
}
extern "C" void cxx_dispatch_proc_lambda(void)
{
    dispatch_source_t proc_native;
	int res;
	pid_t pid;

    MU_BEGIN_TEST(cxx_dispatch_proc_lambda);
	
	// Creates a process and register multiple observers.  Send a signal,
	// exit the process, etc., and verify all observers were notified.
	
	//
	// Simple child process that sleeps 2 seconds.
	//
	
	posix_spawnattr_t attr;
	res = posix_spawnattr_init(&attr);
    MU_ASSERT_TRUE( res );
	res = posix_spawnattr_setflags(&attr, POSIX_SPAWN_START_SUSPENDED);
    MU_ASSERT_TRUE( res );

	char* args[] = {
        (char*)"/bin/sleep", (char*)"2", NULL
	};
	
	res = posix_spawnp(&pid, args[0], NULL, &attr, args, NULL);
	if (res < 0) {
		perror(args[0]);
		exit(127);
	}

	res = posix_spawnattr_destroy(&attr);
    MU_ASSERT_TRUE( res );

    xdispatch::queue* completion = new xdispatch::queue("completion");
	
    MU_ASSERT_TRUE(pid > 0);

	//
	// Suspend the "completion" queue when each observer is created.
	// Each observer resumes the queue when the child process exits.
	// If the queue is resumed too few times (indicating that not all
	// observers received the exit event) then the test case will not exit
	// within the alloted time and result in failure.
	//
	
	int i;
	for (i = 0; i < PID_CNT; ++i) {
        completion->suspend();
        proc_native = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid, DISPATCH_PROC_EXIT, dispatch_get_main_queue());
        xdispatch::source* proc = new xdispatch::source( new xdispatch::native_source(proc_native) );
        MU_DESC_ASSERT_NOT_NULL("DISPATCH_SOURCE_TYPE_PROC", proc);

        proc->handler([=]{
            long flags = dispatch_source_get_data( proc->native_source() );
            MU_DESC_ASSERT_EQUAL("DISPATCH_PROC_EXIT", flags, DISPATCH_PROC_EXIT);
			event_cnt++;
            completion->resume();
		});

        proc->resume();
	}


	//
	// The completion block will be pending on the completion queue until it
	// has been fully resumed, at which point the test will exit successfully.
	//

    completion->async([=]{
		int status;
		int res2 = waitpid(pid, &status, 0);
        MU_ASSERT_TRUE(res2 != -1);
        MU_DESC_ASSERT_EQUAL("Sub-process exited", WEXITSTATUS(status) | WTERMSIG(status), 0);
        MU_DESC_ASSERT_EQUAL("Event count", event_cnt, PID_CNT);
        MU_PASS("");
	});

	kill(pid, SIGCONT);

    xdispatch::exec();

    MU_FAIL("Should never reach this");
    MU_END_TEST;
}
 void operator ()(){
   MU_ASSERT_TRUE(xdispatch::source::data<std::string>() == "any working");
   MU_PASS("");
 }
 void run() {
     QDispatchQueue m = QDispatch::mainQueue();
     QDispatchQueue c = QDispatch::currentQueue();
     MU_ASSERT_TRUE(m.label() == c.label());
     MU_PASS("Great!");
 }