summaryrefslogtreecommitdiff
path: root/regress
diff options
context:
space:
mode:
authorMarco S Hyman <marc@cvs.openbsd.org>2002-01-08 18:55:47 +0000
committerMarco S Hyman <marc@cvs.openbsd.org>2002-01-08 18:55:47 +0000
commita4c50765d8e419c8282f7ac625d630914db21b43 (patch)
treec9508182b621f1c06b544151c6fc454ac10bac17 /regress
parent8d08a266cf5f857d030ffb88b11b86471c25ebe0 (diff)
Better version of pcap regression test. Use mutex and conditions to
sync threads instead of timeouts. Test passes with recent fgs@ patches (not commited yet)
Diffstat (limited to 'regress')
-rw-r--r--regress/lib/libc_r/pcap/pcap.c26
-rw-r--r--regress/lib/libpthread/pcap/pcap.c26
2 files changed, 36 insertions, 16 deletions
diff --git a/regress/lib/libc_r/pcap/pcap.c b/regress/lib/libc_r/pcap/pcap.c
index 5c7f865e74f..7bbadf93c6f 100644
--- a/regress/lib/libc_r/pcap/pcap.c
+++ b/regress/lib/libc_r/pcap/pcap.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: pcap.c,v 1.3 2001/11/11 20:20:53 marc Exp $ */
+/* $OpenBSD: pcap.c,v 1.4 2002/01/08 18:55:46 marc Exp $ */
/*
* Placed in the PUBLIC DOMAIN
*/
@@ -16,6 +16,8 @@
#define PKTCNT 3
volatile int packet_count = 0;
+pthread_mutex_t dummy;
+pthread_cond_t syncer;
void
packet_ignore(u_char *tag, const struct pcap_pkthdr *hdr, const u_char *data)
@@ -30,9 +32,12 @@ pcap_thread(void *arg)
pcap_t *handle;
SET_NAME("pcap_thread");
+ CHECKr(pthread_mutex_lock(&dummy));
handle = pcap_open_live(LOOPBACK_IF, SNAPLEN, NO_PROMISC, 0, errbuf);
if (!handle)
PANIC("You may need to run this test as UID 0 (root)");
+ CHECKr(pthread_mutex_unlock(&dummy));
+ CHECKr(pthread_cond_signal(&syncer));
ASSERT(pcap_loop(handle, PKTCNT, packet_ignore, 0) != -1);
return 0;
}
@@ -41,10 +46,11 @@ void *
ping_thread(void *arg)
{
SET_NAME("ping_thread");
+ CHECKr(pthread_mutex_lock(&dummy));
ASSERT(system("ping -c 3 127.0.0.1") == 0);
- sleep(2);
- ASSERT(packet_count == 3);
- SUCCEED;
+ CHECKr(pthread_mutex_unlock(&dummy));
+ CHECKr(pthread_cond_signal(&syncer));
+ return 0;
}
int
@@ -53,10 +59,14 @@ main(int argc, char **argv)
pthread_t pcap;
pthread_t ping;
+ CHECKr(pthread_mutex_init(&dummy, NULL));
+ CHECKr(pthread_cond_init(&syncer, NULL));
+ CHECKr(pthread_mutex_lock(&dummy));
CHECKr(pthread_create(&pcap, NULL, pcap_thread, NULL));
- sleep(1);
+ CHECKr(pthread_cond_wait(&syncer, &dummy));
CHECKr(pthread_create(&ping, NULL, ping_thread, NULL));
- while (1)
- ;
- PANIC("while");
+ CHECKr(pthread_cond_wait(&syncer, &dummy));
+ CHECKr(pthread_mutex_unlock(&dummy));
+ ASSERT(packet_count == 3);
+ SUCCEED;
}
diff --git a/regress/lib/libpthread/pcap/pcap.c b/regress/lib/libpthread/pcap/pcap.c
index 5c7f865e74f..7bbadf93c6f 100644
--- a/regress/lib/libpthread/pcap/pcap.c
+++ b/regress/lib/libpthread/pcap/pcap.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: pcap.c,v 1.3 2001/11/11 20:20:53 marc Exp $ */
+/* $OpenBSD: pcap.c,v 1.4 2002/01/08 18:55:46 marc Exp $ */
/*
* Placed in the PUBLIC DOMAIN
*/
@@ -16,6 +16,8 @@
#define PKTCNT 3
volatile int packet_count = 0;
+pthread_mutex_t dummy;
+pthread_cond_t syncer;
void
packet_ignore(u_char *tag, const struct pcap_pkthdr *hdr, const u_char *data)
@@ -30,9 +32,12 @@ pcap_thread(void *arg)
pcap_t *handle;
SET_NAME("pcap_thread");
+ CHECKr(pthread_mutex_lock(&dummy));
handle = pcap_open_live(LOOPBACK_IF, SNAPLEN, NO_PROMISC, 0, errbuf);
if (!handle)
PANIC("You may need to run this test as UID 0 (root)");
+ CHECKr(pthread_mutex_unlock(&dummy));
+ CHECKr(pthread_cond_signal(&syncer));
ASSERT(pcap_loop(handle, PKTCNT, packet_ignore, 0) != -1);
return 0;
}
@@ -41,10 +46,11 @@ void *
ping_thread(void *arg)
{
SET_NAME("ping_thread");
+ CHECKr(pthread_mutex_lock(&dummy));
ASSERT(system("ping -c 3 127.0.0.1") == 0);
- sleep(2);
- ASSERT(packet_count == 3);
- SUCCEED;
+ CHECKr(pthread_mutex_unlock(&dummy));
+ CHECKr(pthread_cond_signal(&syncer));
+ return 0;
}
int
@@ -53,10 +59,14 @@ main(int argc, char **argv)
pthread_t pcap;
pthread_t ping;
+ CHECKr(pthread_mutex_init(&dummy, NULL));
+ CHECKr(pthread_cond_init(&syncer, NULL));
+ CHECKr(pthread_mutex_lock(&dummy));
CHECKr(pthread_create(&pcap, NULL, pcap_thread, NULL));
- sleep(1);
+ CHECKr(pthread_cond_wait(&syncer, &dummy));
CHECKr(pthread_create(&ping, NULL, ping_thread, NULL));
- while (1)
- ;
- PANIC("while");
+ CHECKr(pthread_cond_wait(&syncer, &dummy));
+ CHECKr(pthread_mutex_unlock(&dummy));
+ ASSERT(packet_count == 3);
+ SUCCEED;
}