]> code.delx.au - pulseaudio/commitdiff
tests: add a connection stress test
authorDaniel Mack <zonque@gmail.com>
Thu, 31 Mar 2011 12:37:40 +0000 (14:37 +0200)
committerColin Guthrie <colin@mageia.org>
Fri, 1 Apr 2011 13:15:01 +0000 (14:15 +0100)
This test is based on a threaded main loop and was written to hunt an
evil race condition.

src/Makefile.am
src/tests/connect-stress.c [new file with mode: 0644]

index 3bec5e86b9ace156f79622b1c4bac76398809fc5..bdeddedd6bbd69b30c0313edd53ce828b7121b8d 100644 (file)
@@ -240,7 +240,7 @@ pabrowse_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS)
 #         Test programs           #
 ###################################
 
-# missing: mcalign-test flist-test pacat-simple parec-simple sync-playback rtstutter stripnul interpol-test thread-test
+# missing: mcalign-test flist-test pacat-simple parec-simple sync-playback rtstutter stripnul interpol-test thread-test connect-stress
 
 TESTS = \
                mainloop-test \
@@ -303,7 +303,8 @@ TESTS_BINARIES = \
                rtstutter \
                stripnul \
                lock-autospawn-test \
-               prioq-test
+               prioq-test \
+               connect-stress
 
 if !OS_IS_WIN32
 TESTS += \
@@ -563,6 +564,12 @@ usergroup_test_LDADD = $(AM_LDADD) libpulsecore-@PA_MAJORMINOR@.la libpulse.la l
 usergroup_test_CFLAGS = $(AM_CFLAGS)
 usergroup_test_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS)
 
+connect_stress_SOURCES = tests/connect-stress.c
+connect_stress_LDADD = $(AM_LDADD) libpulse.la libpulsecommon-@PA_MAJORMINOR@.la
+connect_stress_CFLAGS = $(AM_CFLAGS)
+connect_stress_LDFLAGS = $(AM_LDFLAGS) $(BINLDFLAGS)
+
+
 ###################################
 #         Common library          #
 ###################################
diff --git a/src/tests/connect-stress.c b/src/tests/connect-stress.c
new file mode 100644 (file)
index 0000000..f427195
--- /dev/null
@@ -0,0 +1,198 @@
+/***
+  This file is part of PulseAudio.
+
+  PulseAudio is free software; you can redistribute it and/or modify
+  it under the terms of the GNU Lesser General Public License as published
+  by the Free Software Foundation; either version 2.1 of the License,
+  or (at your option) any later version.
+
+  PulseAudio is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with PulseAudio; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <signal.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <pulse/pulseaudio.h>
+#include <pulse/mainloop.h>
+
+#define NSTREAMS 16
+#define NTESTS 1000
+#define SAMPLE_HZ 44100
+
+static pa_context *context = NULL;
+static pa_stream *streams[NSTREAMS];
+static pa_threaded_mainloop *mainloop = NULL;
+
+static const pa_sample_spec sample_spec = {
+    .format = PA_SAMPLE_FLOAT32,
+    .rate = SAMPLE_HZ,
+    .channels = 1
+};
+
+static void context_state_callback(pa_context *c, void *userdata);
+
+static void connect(const char *name, int *try) {
+    int ret;
+    pa_mainloop_api *api;
+
+    /* Set up a new main loop */
+    mainloop = pa_threaded_mainloop_new();
+    assert(mainloop);
+
+    api = pa_threaded_mainloop_get_api(mainloop);
+    context = pa_context_new(api, name);
+    assert(context);
+
+    pa_context_set_state_callback(context, context_state_callback, try);
+
+    /* Connect the context */
+    if (pa_context_connect(context, NULL, 0, NULL) < 0) {
+        fprintf(stderr, "pa_context_connect() failed.\n");
+        abort();
+    }
+
+    ret = pa_threaded_mainloop_start(mainloop);
+    assert(ret == 0);
+}
+
+static void disconnect(void) {
+    int i;
+
+    assert(mainloop);
+    assert(context);
+
+    pa_threaded_mainloop_lock(mainloop);
+
+    for (i = 0; i < NSTREAMS; i++)
+        if (streams[i]) {
+            pa_stream_disconnect(streams[i]);
+            pa_stream_unref(streams[i]);
+            streams[i] = NULL;
+        }
+
+    pa_context_disconnect(context);
+    context = NULL;
+
+    pa_threaded_mainloop_unlock(mainloop);
+    pa_threaded_mainloop_stop(mainloop);
+    pa_threaded_mainloop_free(mainloop);
+    mainloop = NULL;
+}
+
+static const pa_buffer_attr buffer_attr = {
+    .maxlength = SAMPLE_HZ * sizeof(float) * NSTREAMS,
+    .tlength = (uint32_t) -1,
+    .prebuf = 0, /* Setting prebuf to 0 guarantees us the the streams will run synchronously, no matter what */
+    .minreq = (uint32_t) -1,
+    .fragsize = 0
+};
+
+static void stream_write_callback(pa_stream *stream, size_t nbytes, void *userdata) {
+    char silence[8192];
+
+    memset(silence, 0, sizeof(silence));
+
+    while (nbytes) {
+        int n = MIN(sizeof(silence), nbytes);
+        pa_stream_write(stream, silence, n, NULL, 0, 0);
+        nbytes -= n;
+    }
+}
+
+static void stream_state_callback(pa_stream *s, void *userdata) {
+    assert(s);
+
+    switch (pa_stream_get_state(s)) {
+        case PA_STREAM_UNCONNECTED:
+        case PA_STREAM_CREATING:
+        case PA_STREAM_TERMINATED:
+        case PA_STREAM_READY:
+            break;
+
+        default:
+        case PA_STREAM_FAILED:
+            fprintf(stderr, "Stream error: %s\n", pa_strerror(pa_context_errno(pa_stream_get_context(s))));
+            abort();
+    }
+}
+
+static void context_state_callback(pa_context *c, void *userdata) {
+    int *try;
+
+    assert(c);
+    assert(userdata);
+
+    try = (int*)userdata;
+
+    switch (pa_context_get_state(c)) {
+        case PA_CONTEXT_CONNECTING:
+        case PA_CONTEXT_AUTHORIZING:
+        case PA_CONTEXT_SETTING_NAME:
+            break;
+
+        case PA_CONTEXT_READY: {
+
+            int i;
+            fprintf(stderr, "Connection (%d of %d) established.\n", (*try)+1, NTESTS);
+
+            for (i = 0; i < NSTREAMS; i++) {
+                char name[64];
+
+                snprintf(name, sizeof(name), "stream #%i", i);
+                streams[i] = pa_stream_new(c, name, &sample_spec, NULL);
+                assert(streams[i]);
+                pa_stream_set_state_callback(streams[i], stream_state_callback, NULL);
+                pa_stream_set_write_callback(streams[i], stream_write_callback, NULL);
+                pa_stream_connect_playback(streams[i], NULL, &buffer_attr, 0, NULL, NULL);
+            }
+
+            break;
+        }
+
+        case PA_CONTEXT_TERMINATED:
+            fprintf(stderr, "Connection terminated.\n");
+            pa_context_unref(context);
+            context = NULL;
+            break;
+
+        case PA_CONTEXT_FAILED:
+        default:
+            fprintf(stderr, "Context error: %s\n", pa_strerror(pa_context_errno(c)));
+            abort();
+    }
+}
+
+int main(int argc, char *argv[]) {
+    int i;
+
+    for (i = 0; i < NSTREAMS; i++)
+        streams[i] = NULL;
+
+    for (i = 0; i < NTESTS; i++) {
+        connect(argv[0], &i);
+        usleep(random() % 500000);
+        disconnect();
+        usleep(random() % 500000);
+    }
+
+    fprintf(stderr, "Done.\n");
+
+    return 0;
+}