void test_multiple_workers( void ) { CountPtr< JobQueue > q = new JobQueue( 0 ); const size_t num_workers = 32; const size_t num_jobs = 64; const size_t times = 512; std::vector< int > a( num_jobs ); std::vector< CountPtr< IncJob > > j( num_jobs ); for( size_t i=0; i<num_jobs; ++i ) { j[ i ] = new IncJob; j[ i ]->p = &a[ i ]; j[ i ]->setPriority( times ); q->addJob( j[ i ].get() ); } std::vector< CountPtr< Worker > > w( num_workers ); for( size_t i=0; i<num_workers; ++i ) { w[ i ] = new Worker; w[ i ]->q = q; CPPUNIT_ASSERT_NO_THROW( w[ i ]->start() ); } // Has lower priority than the IncJobs CountPtr< EndWorker > e( new EndWorker ); for( size_t i=0; i<num_workers; ++i ) { CPPUNIT_ASSERT_NO_THROW( q->addJob( e.get() ) ); } for( size_t i=0; i<num_workers; ++i ) { CPPUNIT_ASSERT_NO_THROW( w[ i ]->join() ); w[ i ]->unref(); // TODO: Fix Refcounted for Collectable with NULL GarbageCollector } for( size_t i=0; i<num_jobs; ++i ) { CPPUNIT_ASSERT_EQUAL( int( times ), a[ i ] ); } }
virtual size_t doit( CountPtr< JobQueue > queue ) { ++(*p); if( getPriority() > 1 ) { setPriority( getPriority()-1 ); queue->addJob( this ); } return 0; }
void test_push_pop_priority( void ) { CountPtr< JobQueue > q = new JobQueue( 0 ); const size_t num_jobs = 16; std::vector< CountPtr< NrJob > > jobs( num_jobs ); for( size_t i=0; i<num_jobs; ++i ) { jobs[ i ] = new NrJob; jobs[ i ]->nr = i; jobs[ i ]->setPriority( i ); CPPUNIT_ASSERT_EQUAL( i, jobs[ i ]->getPriority() ); q->addJob( jobs[ i ].get() ); // std::cerr << "q.addJob( " << (void*)&jobs[ i ] << " ), nr = " << i << std::endl; } for( size_t i=0; i<num_jobs; ++i ) { CountPtr< JobQueue::Job > job = q->getJob(); // std::cerr << "utest_JobQueue: job = " << (void*)job.get() << ", nr = " << job->doit() << std::endl; CPPUNIT_ASSERT_EQUAL( num_jobs-i-1, job->work( q ) ); CPPUNIT_ASSERT_EQUAL( num_jobs-i-1, job->getPriority() ); } }