14c87aefeSPatrick Mooney /*
24c87aefeSPatrick Mooney  * This file and its contents are supplied under the terms of the
34c87aefeSPatrick Mooney  * Common Development and Distribution License ("CDDL"), version 1.0.
44c87aefeSPatrick Mooney  * You may only use this file in accordance with the terms of version
54c87aefeSPatrick Mooney  * 1.0 of the CDDL.
64c87aefeSPatrick Mooney  *
74c87aefeSPatrick Mooney  * A full copy of the text of the CDDL should have accompanied this
84c87aefeSPatrick Mooney  * source.  A copy of the CDDL is also available via the Internet at
94c87aefeSPatrick Mooney  * http://www.illumos.org/license/CDDL.
104c87aefeSPatrick Mooney  */
114c87aefeSPatrick Mooney 
124c87aefeSPatrick Mooney /*
134c87aefeSPatrick Mooney  * Copyright 2018 Joyent, Inc.
144c87aefeSPatrick Mooney  */
154c87aefeSPatrick Mooney 
164c87aefeSPatrick Mooney /*
174c87aefeSPatrick Mooney  *        Test:	read.pause
184c87aefeSPatrick Mooney  *   Assertion: mevent_disable() can be used to pause reads.
194c87aefeSPatrick Mooney  *
204c87aefeSPatrick Mooney  *    Strategy: 1. Create a pipe
214c87aefeSPatrick Mooney  *		2. Call mevent_add() to be notified of writes to the pipe.  The
224c87aefeSPatrick Mooney  *		   callback will signal a cv.
234c87aefeSPatrick Mooney  *		3. In a loop, write to the pipe then wait on the cv.
244c87aefeSPatrick Mooney  */
254c87aefeSPatrick Mooney 
264c87aefeSPatrick Mooney #include <errno.h>
274c87aefeSPatrick Mooney #include <fcntl.h>
284c87aefeSPatrick Mooney #include <pthread.h>
294c87aefeSPatrick Mooney #include <signal.h>
304c87aefeSPatrick Mooney #include <stdio.h>
314c87aefeSPatrick Mooney #include <stdlib.h>
324c87aefeSPatrick Mooney #include <strings.h>
334c87aefeSPatrick Mooney #include <unistd.h>
344c87aefeSPatrick Mooney 
354c87aefeSPatrick Mooney #include <sys/types.h>
364c87aefeSPatrick Mooney #include <sys/stat.h>
374c87aefeSPatrick Mooney 
384c87aefeSPatrick Mooney #include "testlib.h"
394c87aefeSPatrick Mooney #include "mevent.h"
404c87aefeSPatrick Mooney 
414c87aefeSPatrick Mooney static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
424c87aefeSPatrick Mooney static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
434c87aefeSPatrick Mooney 
444c87aefeSPatrick Mooney static char cookie[] = "Chocolate chip with fudge stripes";
454c87aefeSPatrick Mooney 
464c87aefeSPatrick Mooney /*
474c87aefeSPatrick Mooney  * After this many bytes are sent, writes will get batched up, progress will be
484c87aefeSPatrick Mooney  * made on the write side via an interval timer
494c87aefeSPatrick Mooney  */
504c87aefeSPatrick Mooney const int pauseat = 8;
514c87aefeSPatrick Mooney 
524c87aefeSPatrick Mooney static void
munch(int fd,enum ev_type ev,void * arg)534c87aefeSPatrick Mooney munch(int fd, enum ev_type ev, void *arg)
544c87aefeSPatrick Mooney {
554c87aefeSPatrick Mooney 	static int i = 0;
564c87aefeSPatrick Mooney 	char buf[sizeof (cookie)] = { 0 };
574c87aefeSPatrick Mooney 	ssize_t nbytes;
584c87aefeSPatrick Mooney 	ssize_t expected;
594c87aefeSPatrick Mooney 
604c87aefeSPatrick Mooney 	ASSERT_INT_EQ(("bad event"), ev, EVF_READ);
614c87aefeSPatrick Mooney 	ASSERT_PTR_EQ(("bad cookie"), arg, cookie);
624c87aefeSPatrick Mooney 
634c87aefeSPatrick Mooney 	/*
644c87aefeSPatrick Mooney 	 * For the first while, expect data to come a byte at a time.  After the
654c87aefeSPatrick Mooney 	 * pause, we should get a burst with the rest of the data.
664c87aefeSPatrick Mooney 	 */
674c87aefeSPatrick Mooney 	if (i > pauseat) {
684c87aefeSPatrick Mooney 		expected = strlen(cookie) - pauseat - 1;
694c87aefeSPatrick Mooney 	} else {
704c87aefeSPatrick Mooney 		expected = 1;
714c87aefeSPatrick Mooney 	}
724c87aefeSPatrick Mooney 
734c87aefeSPatrick Mooney 	if ((nbytes = read(fd, buf, sizeof (buf))) < 0) {
744c87aefeSPatrick Mooney 		FAIL_ERRNO("bad read");
754c87aefeSPatrick Mooney 	}
764c87aefeSPatrick Mooney 	VERBOSE(("read %ld bytes '%s'", nbytes, buf));
774c87aefeSPatrick Mooney 
784c87aefeSPatrick Mooney 	ASSERT_INT64_EQ(("wanted a byte of cookie"), nbytes, expected);
794c87aefeSPatrick Mooney 
804c87aefeSPatrick Mooney 	if (expected == 1) {
814c87aefeSPatrick Mooney 		ASSERT_CHAR_EQ(("bad byte %d of cookie", i), buf[0], cookie[i]);
824c87aefeSPatrick Mooney 	} else {
834c87aefeSPatrick Mooney 		ASSERT_STR_EQ(("bad last half of cookie"), buf, &cookie[i]);
844c87aefeSPatrick Mooney 	}
854c87aefeSPatrick Mooney 
864c87aefeSPatrick Mooney 	pthread_mutex_lock(&mtx);
874c87aefeSPatrick Mooney 	pthread_cond_signal(&cv);
884c87aefeSPatrick Mooney 	VERBOSE(("wakeup"));
894c87aefeSPatrick Mooney 	pthread_mutex_unlock(&mtx);
904c87aefeSPatrick Mooney 
914c87aefeSPatrick Mooney 	i++;
924c87aefeSPatrick Mooney }
934c87aefeSPatrick Mooney 
944c87aefeSPatrick Mooney static void
tick(int ms,enum ev_type ev,void * arg)954c87aefeSPatrick Mooney tick(int ms, enum ev_type ev, void *arg)
964c87aefeSPatrick Mooney {
974c87aefeSPatrick Mooney 	pthread_mutex_lock(&mtx);
984c87aefeSPatrick Mooney 	pthread_cond_signal(&cv);
994c87aefeSPatrick Mooney 	VERBOSE(("wakeup"));
1004c87aefeSPatrick Mooney 	pthread_mutex_unlock(&mtx);
1014c87aefeSPatrick Mooney }
1024c87aefeSPatrick Mooney 
1034c87aefeSPatrick Mooney int
main(int argc,const char * argv[])1044c87aefeSPatrick Mooney main(int argc, const char *argv[])
1054c87aefeSPatrick Mooney {
1064c87aefeSPatrick Mooney 	int pipefds[2];
1074c87aefeSPatrick Mooney 	struct mevent *evp, *timer;
1084c87aefeSPatrick Mooney 	ssize_t written;
1094c87aefeSPatrick Mooney 
1104c87aefeSPatrick Mooney 	start_test(argv[0], 5);
1114c87aefeSPatrick Mooney 	start_event_thread();
1124c87aefeSPatrick Mooney 
1134c87aefeSPatrick Mooney 	if (pipe(pipefds) != 0) {
1144c87aefeSPatrick Mooney 		FAIL_ERRNO("pipe");
1154c87aefeSPatrick Mooney 	}
1164c87aefeSPatrick Mooney 	if (fcntl(pipefds[0], F_SETFL, O_NONBLOCK) != 0) {
1174c87aefeSPatrick Mooney 		FAIL_ERRNO("set pipe nonblocking");
1184c87aefeSPatrick Mooney 	}
1194c87aefeSPatrick Mooney 
1204c87aefeSPatrick Mooney 	evp = mevent_add(pipefds[0], EVF_READ, munch, cookie);
1214c87aefeSPatrick Mooney 	ASSERT_PTR_NEQ(("mevent_add pipefd"), evp, NULL);
1224c87aefeSPatrick Mooney 
1234c87aefeSPatrick Mooney 	for (int i = 0; cookie[i] != 0; i++) {
1244c87aefeSPatrick Mooney 		pthread_mutex_lock(&mtx);
1254c87aefeSPatrick Mooney 		written = write(pipefds[1], cookie + i, 1);
1264c87aefeSPatrick Mooney 		if (written < 0) {
1274c87aefeSPatrick Mooney 			FAIL_ERRNO("bad write");
1284c87aefeSPatrick Mooney 		}
1294c87aefeSPatrick Mooney 		ASSERT_INT64_EQ(("write byte %d of cookie", i), written, 1);
1304c87aefeSPatrick Mooney 
1314c87aefeSPatrick Mooney 		/* Wait for it to be read */
1324c87aefeSPatrick Mooney 		pthread_cond_wait(&cv, &mtx);
1334c87aefeSPatrick Mooney 		pthread_mutex_unlock(&mtx);
1344c87aefeSPatrick Mooney 
1354c87aefeSPatrick Mooney 		if (i == pauseat) {
1364c87aefeSPatrick Mooney 			timer = mevent_add(10, EVF_TIMER, tick,
1374c87aefeSPatrick Mooney 			    &cookie[pauseat]);
1384c87aefeSPatrick Mooney 			ASSERT_PTR_NEQ(("mevent_add timer"), timer, NULL);
1394c87aefeSPatrick Mooney 			VERBOSE(("disable munch"));
1404c87aefeSPatrick Mooney 			mevent_disable(evp);
1414c87aefeSPatrick Mooney 		}
1424c87aefeSPatrick Mooney 	}
1434c87aefeSPatrick Mooney 
1444c87aefeSPatrick Mooney 	pthread_mutex_lock(&mtx);
1454c87aefeSPatrick Mooney 
1464c87aefeSPatrick Mooney 	mevent_enable(evp);
1474c87aefeSPatrick Mooney 
1484c87aefeSPatrick Mooney 	pthread_cond_wait(&cv, &mtx);
1494c87aefeSPatrick Mooney 	pthread_mutex_unlock(&mtx);
1504c87aefeSPatrick Mooney 
1514c87aefeSPatrick Mooney 	PASS();
1524c87aefeSPatrick Mooney }