Commit 1982516d authored by Eric Stadtherr's avatar Eric Stadtherr Committed by Felix Delattre
Browse files

Fixed problems in threading tests

parent 8833bba9
......@@ -60,6 +60,9 @@
extern struct projectionconfig * get_projection(const char * srs);
extern mapnik::box2d<double> tile2prjbounds(struct projectionconfig * prj, int x, int y, int z);
// mutex to guard access to the shared render request counter
static pthread_mutex_t item_counter_lock;
std::string get_current_stderr() {
FILE * input = fopen("stderr.out", "r+");
std::string log_lines;
......@@ -82,34 +85,31 @@ struct item * init_render_request(enum protoCmd type) {
struct item * item = (struct item *)malloc(sizeof(struct item));
bzero(item, sizeof(struct item));
item->req.ver = PROTO_VER;
strcpy(item->req.xmlname,"default");
strcpy(item->req.xmlname, "default");
item->req.cmd = type;
pthread_mutex_lock(&item_counter_lock);
item->mx = counter++;
pthread_mutex_unlock(&item_counter_lock);
return item;
}
void *addition_thread(void * arg) {
void * addition_thread(void * arg) {
struct request_queue * queue = (struct request_queue *)arg;
struct item * item;
struct timespec time;
unsigned int seed = syscall(SYS_gettid);
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
time.tv_sec = mts.tv_sec;
time.tv_nsec = mts.tv_nsec;
#else
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time);
#endif
seed *= (unsigned int)time.tv_nsec;
uint64_t threadid;
#ifdef __MACH__ // Mac OS X does not support SYS_gettid
pthread_threadid_np(NULL, &threadid);
#else
threadid = syscall(SYS_gettid);
#endif
// Requests need to be unique across threads to avoid being discarded as duplicates,
// thereby ensuring the queue counts can be compared correctly.
// To that end, use a thread ID for the Y coordinate, complementing
// the existing sequence counter used for the X coordinate.
for (int i = 0; i < NO_QUEUE_REQUESTS; i++) {
item = init_render_request(cmdDirty);
item->my = rand_r(&seed);
item->mx = rand_r(&seed);
item->my = threadid;
request_queue_add_request(queue, item);
}
return NULL;
......@@ -385,7 +385,7 @@ TEST_CASE( "renderd/queueing", "request queueing") {
REQUIRE( request_queue_no_requests_queued(queue, cmdRenderPrio) == REQ_LIMIT );
REQUIRE( request_queue_no_requests_queued(queue, cmdDirty) == (i - REQ_LIMIT) );
} else {
//Requests should be dropped alltogether
//Requests should be dropped altogether
REQUIRE( res == cmdNotDone );
REQUIRE( request_queue_no_requests_queued(queue, cmdRenderPrio) == REQ_LIMIT );
REQUIRE( request_queue_no_requests_queued(queue, cmdDirty) == DIRTY_LIMIT);
......@@ -417,7 +417,7 @@ TEST_CASE( "renderd/queueing", "request queueing") {
pthread_join(addition_threads[i], &status);
}
INFO("Itteration " << j);
INFO("Iteration " << j);
REQUIRE( request_queue_no_requests_queued(queue, cmdDirty) == (NO_THREADS * NO_QUEUE_REQUESTS) );
request_queue_close(queue);
......@@ -442,7 +442,6 @@ TEST_CASE( "renderd/queueing", "request queueing") {
REQUIRE( request_queue_no_requests_queued(queue, cmdDirty) == (NO_THREADS*NO_QUEUE_REQUESTS) );
for (int i = 0; i < NO_THREADS; i++) {
if (pthread_create(&fetch_threads[i], NULL, fetch_thread,
(void *) queue)) {
......@@ -455,7 +454,7 @@ TEST_CASE( "renderd/queueing", "request queueing") {
pthread_join(fetch_threads[i], &status);
}
INFO("Itteration " << j);
INFO("Iteration " << j);
REQUIRE( request_queue_no_requests_queued(queue, cmdDirty) == 0 );
request_queue_close(queue);
......@@ -495,7 +494,7 @@ TEST_CASE( "renderd/queueing", "request queueing") {
pthread_join(addition_threads[i], &status);
}
INFO("Itteration " << j);
INFO("Iteration " << j);
REQUIRE( request_queue_no_requests_queued(queue, cmdDirty) == 0 );
request_queue_close(queue);
......@@ -568,7 +567,7 @@ TEST_CASE( "renderd", "tile generation" ) {
REQUIRE( found > -1 );
}
// we run this test twice to ensure that our stderr reading is working correctlyu
// we run this test twice to ensure that our stderr reading is working correctly
SECTION("render_init 2", "should throw nice error if paths are invalid") {
render_init("doesnotexist","doesnotexist",1);
std::string log_lines = get_current_stderr();
......@@ -949,14 +948,16 @@ int main (int argc, char* const argv[])
// allows us to catch and read it in these tests to validate
// the stderr contains the right messages
// http://stackoverflow.com/questions/13533655/how-to-listen-to-stderr-in-c-c-for-sending-to-callback
FILE * stream = freopen("stderr.out", "w", stderr);
FILE *stream = freopen("stderr.out", "w", stderr);
if (!stream) {
perror("Redirection of stderr failed");
return 1;
}
//setvbuf(stream, 0, _IOLBF, 0); // No Buffering
openlog("renderd", LOG_PID | LOG_PERROR, LOG_DAEMON);
pthread_mutex_init(&item_counter_lock, NULL);
int result = Catch::Main( argc, argv );
pthread_mutex_destroy(&item_counter_lock);
return result;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment