extern "C" void Qt_dispatch_source_signal_lambda(){
    char argv[] = "test";
    int argc = 1;
    QDispatchApplication app(argc, (char**)&argv);

        MU_BEGIN_TEST(Qt_dispatch_source_signal_lambda);

    EmitterLambda object;

	// configure the source
	QDispatchSource src(new QDispatchSourceTypeSignal(&object, SIGNAL(ready())));
    src.setTargetQueue(QDispatch::globalQueue(QDispatch::LOW));
	src.setHandler([=]{
		MU_MESSAGE("Signal was emitted");
		if(QDispatch::currentQueue().native() == dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0))
			MU_MESSAGE("Executed on low global queue");
		else if(QDispatch::currentQueue().native() == dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0))
			MU_MESSAGE("Executed on default global queue");
		else if(QDispatch::currentQueue().native() == dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0))
			MU_MESSAGE("Executed on high global queue");
		else if(QDispatch::currentQueue().native() == dispatch_get_main_queue())
			MU_MESSAGE("Executed on main queue");
		MU_ASSERT_EQUAL_HEX(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), QDispatch::currentQueue().native());
		MU_PASS("");
	});

	// trigger the signal
	object.notify();

	app.exec();
	MU_END_TEST;
}
Exemplo n.º 2
0
    void run(){

        // we should be executed on the main queue
        MU_ASSERT_EQUAL_HEX(QDispatch::currentQueue().native(), dispatch_get_main_queue());

        // assert that we can really get the timer
        MU_ASSERT_EQUAL_HEX(QDispatchTimer::current(), tested_timer);

        // test the timer interval (but only after the second run
        // as the first one will be started immediately
        if(counter > 0) {
            long diff = checked_time.elapsed();
            MU_DESC_ASSERT_LESS_THAN_EQUAL("timer not too late", diff, 3*1000);
            MU_DESC_ASSERT_LESS_THAN_EQUAL("timer not too early", 1*1000, diff);
        }

        checked_time.restart();

        // only pass when the timer fired at least 5 times
        MU_MESSAGE("\t%i", counter);
        if(counter < 5)
            counter++;
        else {
            MU_PASS("");
        }
    }
Exemplo n.º 3
0
extern "C" void Qt_dispatch_after_lambda(){
	QTime watch;
    char argv[] = "test";
    int argc = 1;
    QDispatchApplication app(argc, (char**)&argv);

        MU_BEGIN_TEST(Qt_dispatch_after_lambda);

	watch.start();

    QDispatch::globalQueue(QDispatch::DEFAULT).after(
        QTime::currentTime().addMSecs(1000),
        [=]{
            MU_MESSAGE("Should finish between 0.5s and 1.5s: %f", watch.elapsed()/1000.0);
            MU_ASSERT_GREATER_THAN_EQUAL(watch.elapsed(), 700);
        }
    );

    QDispatch::mainQueue().after(
        QTime::currentTime().addMSecs(2000),
        [=]{
            MU_MESSAGE("Should finish between 2s and 2.5s: %f", watch.elapsed()/1000.0);
            MU_ASSERT_GREATER_THAN_EQUAL(watch.elapsed(), 1800);
            MU_PASS("");
        }
    );

	app.exec();
	MU_END_TEST;
}
Exemplo n.º 4
0
extern "C" void cxx_dispatch_fibo(){
    MU_BEGIN_TEST(cxx_dispatch_fibo);

    bool ticTac = true;
    xdispatch::group theGroup;
    xdispatch::operation* theOps[6];
    int cpt = 0;
    while(cpt < 3000) {
        for(int i=0; i<6; i++) {
            theOps[i] = new operation_fibo( 8 );
            theGroup.async( theOps[i] );
        }
        theGroup.wait( DISPATCH_TIME_FOREVER );

#if 0
        if(ticTac)
            MU_MESSAGE("Tic");
        else
            MU_MESSAGE("Tac");
#endif

        ticTac = !ticTac;
        cpt++;
    }

    MU_PASS("");
    MU_END_TEST;
}
static void exec_runnables() {
    
    for(int i = 0; i < 5; i++)
        iter_block_run->run(i);
    delete iter_block_run;
    MU_ASSERT_EQUAL(counter, 1+2+3+4);
    
    block_run->run();
    delete block_run;
    MU_ASSERT_EQUAL(counter, 1+2+3+4+5);
    
    MU_PASS("");
}
extern "C" void dispatch_plusplus_blocks(void) {
    MU_BEGIN_TEST(dispatch_plusplus_blocks);

	dispatch_queue_t q = dispatch_get_main_queue();
	MU_ASSERT_NOT_NULL(q);

	dispatch_async(dispatch_get_main_queue(), ^{
        int i = 0;
        i++;
        MU_ASSERT_EQUAL(i, 1);
        
        const char* pass = "******";
		MU_PASS(pass);
	});
extern "C" void Qt_early_dispatch2_lambda(){
    MU_BEGIN_TEST(Qt_early_dispatch2_lambda);

    QDispatchQueue q = QDispatch::mainQueue();
    MU_ASSERT_NOT_NULL( q.native() );
    q.async( [=]{ MU_PASS(""); } );

    char* argv = QString("test").toAscii().data();
    int argc = 1;
    QDispatchCoreApplication app(argc,&argv);

    app.exec();
    MU_END_TEST;
}
extern "C" void cxx_dispatch_semaphore(){
	static size_t total;

    MU_BEGIN_TEST(cxx_dispatch_semaphore);
    xdispatch::semaphore* dsema = new xdispatch::semaphore(1);

    xdispatch::queue("cxx_dispatch_semaphore").apply(LAPS, new acquire_inc(dsema, total));

	delete dsema;

	MU_ASSERT_EQUAL(total, LAPS);

	MU_PASS("");
	MU_END_TEST;
}
Exemplo n.º 9
0
static void
cascade(void* context) {
	uintptr_t idx, *idxptr = (uintptr_t*)context;

	if (done) return;
	
	idx = *idxptr + 1;

	if (idx < QUEUES) {
		*idxptr = idx;
		dispatch_async_f(queues[idx], context, cascade);
	}

	if (dispatch_atomic_dec(&iterations) == 0) {
		done = 1;
		histogram();
		MU_PASS("Please check histogram to be sure");
	}
}
Exemplo n.º 10
0
extern "C" void Qt_dispatch_current_lambda(){
    char argv[] = "test";
    int argc = 1;
    QDispatchApplication app(argc, (char**)&argv);
	
    MU_BEGIN_TEST(Qt_dispatch_current_lambda);
    
    QDispatch::globalQueue().async([=]{
        QTest::qSleep(10);
        MU_MESSAGE("Queue should be global default queue");
        qDebug() << QDispatch::currentQueue();
        QDispatch::mainQueue().async(QDispatchMakeRunnable([=]{
            QTest::qSleep(10);
            MU_MESSAGE("Queue should be main queue");
            qDebug() << QDispatch::currentQueue();
            
            MU_PASS("");
        }));
    });
		
	app.exec();
	MU_END_TEST;
}
static void done(void *arg) {
	//sleep(1);
	MU_PASS("All blocks done");
}
Exemplo n.º 12
0
static void pass(void* dt){
    MU_PASS("Core API is working");
}
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("");
 }
Exemplo n.º 15
0
 void run() {
     QDispatchQueue m = QDispatch::mainQueue();
     QDispatchQueue c = QDispatch::currentQueue();
     MU_ASSERT_TRUE(m.label() == c.label());
     MU_PASS("Great!");
 }
Exemplo n.º 16
0
void group_notify(void* data){
	dispatch_queue_t m = dispatch_get_main_queue();
	dispatch_queue_t c = dispatch_get_current_queue();
	MU_ASSERT_EQUAL(m, c);
	MU_PASS("Great!");
}