[vlc-commits] test: test case for the previous commit
Rémi Denis-Courmont
git at videolan.org
Wed Jul 1 18:22:09 CEST 2015
vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Tue Jun 30 23:52:51 2015 +0300| [d5567521070b428b13124b172c52565fb612b331] | committer: Rémi Denis-Courmont
test: test case for the previous commit
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=d5567521070b428b13124b172c52565fb612b331
---
src/Makefile.am | 3 ++
src/test/interrupt.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 145 insertions(+)
diff --git a/src/Makefile.am b/src/Makefile.am
index 31d45c4..d9bdeff 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -539,6 +539,7 @@ check_PROGRAMS = \
test_block \
test_dictionary \
test_i18n_atof \
+ test_interrupt \
test_md5 \
test_picture_pool \
test_timer \
@@ -555,6 +556,8 @@ test_block_DEPENDENCIES =
test_dictionary_SOURCES = test/dictionary.c
test_i18n_atof_SOURCES = test/i18n_atof.c
+test_interrupt_SOURCES = test/interrupt.c
+test_interrupt_LDADD = $(LDADD) $(LIBS_libvlccore) $(LIBPTHREAD)
test_md5_SOURCES = test/md5.c
test_picture_pool_SOURCES = test/picture_pool.c
test_timer_SOURCES = test/timer.c
diff --git a/src/test/interrupt.c b/src/test/interrupt.c
new file mode 100644
index 0000000..02d4841
--- /dev/null
+++ b/src/test/interrupt.c
@@ -0,0 +1,142 @@
+/*****************************************************************************
+ * interrupt.c: Test for interrupt context API
+ *****************************************************************************
+ * Copyright (C) 2015 Rémi Denis-Courmont
+ *
+ * This program 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.
+ *
+ * This program 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#undef NDEBUG
+#include <assert.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include <vlc_common.h>
+#include <vlc_threads.h>
+#include <vlc_interrupt.h>
+
+static vlc_sem_t sem;
+
+static void test_context_simple(vlc_interrupt_t *ctx)
+{
+ vlc_interrupt_t *octx;
+
+ vlc_interrupt_set(ctx);
+ octx = vlc_interrupt_set(NULL);
+ assert(octx == ctx);
+ octx = vlc_interrupt_set(ctx);
+ assert(octx == NULL);
+ octx = vlc_interrupt_set(ctx);
+ assert(octx == ctx);
+
+ /* BIG FAT WARNING: This is only meant to test the vlc_cond_wait_i11e()
+ * function. This is NOT a good example of how to use the function in
+ * normal code. */
+ vlc_sem_post(&sem);
+ assert(vlc_sem_wait_i11e(&sem) == 0);
+
+ vlc_interrupt_raise(ctx);
+ assert(vlc_sem_wait_i11e(&sem) == EINTR);
+
+ vlc_sem_post(&sem);
+ vlc_interrupt_raise(ctx);
+ assert(vlc_sem_wait_i11e(&sem) == EINTR);
+ assert(vlc_sem_wait_i11e(&sem) == 0);
+
+ vlc_interrupt_raise(ctx);
+ vlc_sem_post(&sem);
+ assert(vlc_sem_wait_i11e(&sem) == EINTR);
+ assert(vlc_sem_wait_i11e(&sem) == 0);
+
+ octx = vlc_interrupt_set(NULL);
+ assert(octx == ctx);
+ octx = vlc_interrupt_set(NULL);
+ assert(octx == NULL);
+}
+
+static void *test_thread_simple(void *data)
+{
+ vlc_interrupt_t *ctx = data;
+
+ vlc_interrupt_set(ctx);
+ assert(vlc_sem_wait_i11e(&sem) == EINTR);
+ assert(vlc_sem_wait_i11e(&sem) == 0);
+ vlc_sem_wait(&sem);
+
+ test_context_simple(ctx);
+ return NULL;
+}
+
+static void *test_thread_cleanup(void *data)
+{
+ vlc_interrupt_t *ctx = data;
+
+ test_context_simple(ctx);
+ /* Test context clearing on exit */
+ vlc_interrupt_set(ctx);
+ return NULL;
+}
+
+static void *test_thread_cancel(void *data)
+{
+ vlc_interrupt_t *ctx = data;
+
+ test_context_simple(ctx);
+ /* Test context clearing on cancellation */
+ vlc_interrupt_set(ctx);
+ for (;;)
+ pause();
+
+ vlc_assert_unreachable();
+}
+
+int main (void)
+{
+ vlc_interrupt_t *ctx;
+ vlc_thread_t th;
+
+ alarm(2);
+
+ ctx = vlc_interrupt_create();
+ assert(ctx != NULL);
+ vlc_interrupt_destroy(ctx);
+
+ vlc_sem_init(&sem, 0);
+ ctx = vlc_interrupt_create();
+ assert(ctx != NULL);
+
+ test_context_simple(ctx);
+
+ assert(!vlc_clone(&th, test_thread_simple, ctx, VLC_THREAD_PRIORITY_LOW));
+ vlc_interrupt_raise(ctx);
+ vlc_sem_post(&sem);
+ vlc_sem_post(&sem);
+ vlc_join(th, NULL);
+
+ assert(!vlc_clone(&th, test_thread_cleanup, ctx, VLC_THREAD_PRIORITY_LOW));
+ vlc_join(th, NULL);
+
+ assert(!vlc_clone(&th, test_thread_cancel, ctx, VLC_THREAD_PRIORITY_LOW));
+ vlc_cancel(th);
+ vlc_join(th, NULL);
+
+ vlc_interrupt_destroy(ctx);
+ vlc_sem_destroy(&sem);
+ return 0;
+}
More information about the vlc-commits
mailing list