queue and queue test
This commit is contained in:
		
							
								
								
									
										94
									
								
								tests.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								tests.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,94 @@
 | 
			
		||||
#define _DEFAULT_SOURCE
 | 
			
		||||
 | 
			
		||||
#include <queue.h>
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#include <stdbool.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <config.h>
 | 
			
		||||
#include <logging.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
extern bool verbose;
 | 
			
		||||
extern bool debug;
 | 
			
		||||
 | 
			
		||||
bool stopConsumer = false;
 | 
			
		||||
 | 
			
		||||
void * consumer(void * arg) {
 | 
			
		||||
    logmsg(LOG_DEBUG, "consumer thread started");
 | 
			
		||||
    t_queue *queue = (t_queue*) arg;
 | 
			
		||||
 | 
			
		||||
    while (! stopConsumer) {
 | 
			
		||||
        logmsg(LOG_INFO, "consumer waiting");
 | 
			
		||||
        char *item = (char*) getQueue(queue);
 | 
			
		||||
        logmsg(LOG_INFO, "consumer received: %s", item);
 | 
			
		||||
        free(item);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    logmsg(LOG_DEBUG, "consumer thread returning");
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
    verbose = true;
 | 
			
		||||
    debug = true;
 | 
			
		||||
 | 
			
		||||
    t_queue *queue = initQueue();
 | 
			
		||||
    logmsg(LOG_DEBUG, "queue created");
 | 
			
		||||
 | 
			
		||||
    pthread_t consumerThread;
 | 
			
		||||
    int res = pthread_create(&consumerThread, NULL, &consumer, (void*) queue);
 | 
			
		||||
    if (res != 0) {
 | 
			
		||||
        logmsg(LOG_ERR, "Error starting consumer thread");
 | 
			
		||||
        exit(1);
 | 
			
		||||
    }
 | 
			
		||||
    sched_yield();
 | 
			
		||||
    sleep(1);
 | 
			
		||||
 | 
			
		||||
    char *t = strdup("hallo1");
 | 
			
		||||
    putQueue(queue, (void*) t);
 | 
			
		||||
    logmsg(LOG_DEBUG, "item added to queue");
 | 
			
		||||
 | 
			
		||||
    t = strdup("hallo2");
 | 
			
		||||
    putQueue(queue, (void*) t);
 | 
			
		||||
    logmsg(LOG_DEBUG, "item added to queue");
 | 
			
		||||
    sleep(1);
 | 
			
		||||
 | 
			
		||||
    t = strdup("hallo3");
 | 
			
		||||
    putQueue(queue, (void*) t);
 | 
			
		||||
    logmsg(LOG_DEBUG, "item added to queue");
 | 
			
		||||
 | 
			
		||||
    t = strdup("hallo4");
 | 
			
		||||
    putQueue(queue, (void*) t);
 | 
			
		||||
    logmsg(LOG_DEBUG, "item added to queue");
 | 
			
		||||
 | 
			
		||||
// stopConsumer = true;
 | 
			
		||||
 | 
			
		||||
    t = strdup("hallo5");
 | 
			
		||||
    putQueue(queue, (void*) t);
 | 
			
		||||
    logmsg(LOG_DEBUG, "item added to queue");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    sleep(2);
 | 
			
		||||
    pthread_join(consumerThread, NULL);
 | 
			
		||||
    logmsg(LOG_DEBUG, "consumer thread joined");
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user