summaryrefslogtreecommitdiffstats
path: root/modules/sensors/tests
diff options
context:
space:
mode:
authorAaron Whyte <awhyte@google.com>2013-10-29 15:17:01 -0700
committerMike Lockwood <lockwood@google.com>2013-11-14 11:25:00 -0800
commitc69f3a70ecbc4303cdfdea961f7a2a4a8f58fa05 (patch)
treefe98851b7d8bdb3832770b08ea9502936212a4d0 /modules/sensors/tests
parent92863c14b7d36f74ec715b45ca6adc8bf95dc87c (diff)
downloadhardware_libhardware-c69f3a70ecbc4303cdfdea961f7a2a4a8f58fa05.zip
hardware_libhardware-c69f3a70ecbc4303cdfdea961f7a2a4a8f58fa05.tar.gz
hardware_libhardware-c69f3a70ecbc4303cdfdea961f7a2a4a8f58fa05.tar.bz2
Tests SensorEventQueue I/O when the queue is full.
Reduced debug logging in multihal. Change-Id: I30e80630ce5d97776c53f26d6096ef59b9b2f35a
Diffstat (limited to 'modules/sensors/tests')
-rw-r--r--modules/sensors/tests/SensorEventQueue_test.cpp116
1 files changed, 112 insertions, 4 deletions
diff --git a/modules/sensors/tests/SensorEventQueue_test.cpp b/modules/sensors/tests/SensorEventQueue_test.cpp
index cbe4377..3218bb9 100644
--- a/modules/sensors/tests/SensorEventQueue_test.cpp
+++ b/modules/sensors/tests/SensorEventQueue_test.cpp
@@ -26,14 +26,22 @@ bool checkWritableBufferSize(SensorEventQueue* queue, int requested, int expecte
bool checkSize(SensorEventQueue* queue, int expected) {
int actual = queue->getSize();
if (actual != expected) {
- printf("Expected queue size was %d; actual was %d", expected, actual);
+ printf("Expected queue size was %d; actual was %d\n", expected, actual);
+ return false;
+ }
+ return true;
+}
+
+bool checkInt(char* msg, int expected, int actual) {
+ if (actual != expected) {
+ printf("%s; expected %d; actual was %d\n", msg, expected, actual);
return false;
}
return true;
}
bool testSimpleWriteSizeCounts() {
- printf("TEST testSimpleWriteSizeCounts\n");
+ printf("testSimpleWriteSizeCounts\n");
SensorEventQueue* queue = new SensorEventQueue(10);
if (!checkSize(queue, 0)) return false;
if (!checkWritableBufferSize(queue, 11, 10)) return false;
@@ -55,7 +63,7 @@ bool testSimpleWriteSizeCounts() {
}
bool testWrappingWriteSizeCounts() {
- printf("TEST testWrappingWriteSizeCounts\n");
+ printf("testWrappingWriteSizeCounts\n");
SensorEventQueue* queue = new SensorEventQueue(10);
queue->markAsWritten(9);
if (!checkSize(queue, 9)) return false;
@@ -80,9 +88,109 @@ bool testWrappingWriteSizeCounts() {
return true;
}
+
+
+struct TaskContext {
+ bool success;
+ SensorEventQueue* queue;
+};
+
+static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t dataAvailableCond = PTHREAD_COND_INITIALIZER;
+
+int FULL_QUEUE_CAPACITY = 5;
+int FULL_QUEUE_EVENT_COUNT = 31;
+
+void *fullQueueWriterTask(void* ptr) {
+ TaskContext* ctx = (TaskContext*)ptr;
+ SensorEventQueue* queue = ctx->queue;
+ ctx->success = true;
+ int totalWaits = 0;
+ int totalWrites = 0;
+ sensors_event_t* buffer;
+
+ while (totalWrites < FULL_QUEUE_EVENT_COUNT) {
+ pthread_mutex_lock(&mutex);
+ if (queue->waitForSpace(&mutex)) {
+ totalWaits++;
+ printf(".");
+ }
+ int writableSize = queue->getWritableRegion(FULL_QUEUE_CAPACITY, &buffer);
+ queue->markAsWritten(writableSize);
+ totalWrites += writableSize;
+ for (int i = 0; i < writableSize; i++) {
+ printf("w");
+ }
+ pthread_cond_broadcast(&dataAvailableCond);
+ pthread_mutex_unlock(&mutex);
+ }
+ printf("\n");
+
+ ctx->success =
+ checkInt("totalWrites", FULL_QUEUE_EVENT_COUNT, totalWrites) &&
+ checkInt("totalWaits", FULL_QUEUE_EVENT_COUNT - FULL_QUEUE_CAPACITY, totalWaits);
+ return NULL;
+}
+
+bool fullQueueReaderShouldRead(int queueSize, int totalReads) {
+ if (queueSize == 0) {
+ return false;
+ }
+ int totalWrites = totalReads + queueSize;
+ return queueSize == FULL_QUEUE_CAPACITY || totalWrites == FULL_QUEUE_EVENT_COUNT;
+}
+
+void* fullQueueReaderTask(void* ptr) {
+ TaskContext* ctx = (TaskContext*)ptr;
+ SensorEventQueue* queue = ctx->queue;
+ int totalReads = 0;
+ while (totalReads < FULL_QUEUE_EVENT_COUNT) {
+ pthread_mutex_lock(&mutex);
+ // Only read if there are events,
+ // and either the queue is full, or if we're reading the last few events.
+ while (!fullQueueReaderShouldRead(queue->getSize(), totalReads)) {
+ pthread_cond_wait(&dataAvailableCond, &mutex);
+ }
+ queue->dequeue();
+ totalReads++;
+ printf("r");
+ pthread_mutex_unlock(&mutex);
+ }
+ printf("\n");
+ ctx->success = ctx->success && checkInt("totalreads", FULL_QUEUE_EVENT_COUNT, totalReads);
+ return NULL;
+}
+
+// Test internal queue-full waiting and broadcasting.
+bool testFullQueueIo() {
+ printf("testFullQueueIo\n");
+ SensorEventQueue* queue = new SensorEventQueue(FULL_QUEUE_CAPACITY);
+
+ TaskContext readerCtx;
+ readerCtx.success = true;
+ readerCtx.queue = queue;
+
+ TaskContext writerCtx;
+ writerCtx.success = true;
+ writerCtx.queue = queue;
+
+ pthread_t writer, reader;
+ pthread_create(&reader, NULL, fullQueueReaderTask, &readerCtx);
+ pthread_create(&writer, NULL, fullQueueWriterTask, &writerCtx);
+
+ pthread_join(writer, NULL);
+ pthread_join(reader, NULL);
+
+ if (!readerCtx.success || !writerCtx.success) return false;
+ printf("passed\n");
+ return true;
+}
+
+
int main(int argc, char **argv) {
if (testSimpleWriteSizeCounts() &&
- testWrappingWriteSizeCounts()) {
+ testWrappingWriteSizeCounts() &&
+ testFullQueueIo()) {
printf("ALL PASSED\n");
} else {
printf("SOMETHING FAILED\n");