xref: /illumos-gate/usr/src/cmd/bhyve/test/tests/mevent/read_pause.c (revision dd72704bd9e794056c558153663c739e2012d721)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2018 Joyent, Inc.
14  */
15 
16 /*
17  *        Test:	read.pause
18  *   Assertion: mevent_disable() can be used to pause reads.
19  *
20  *    Strategy: 1. Create a pipe
21  *		2. Call mevent_add() to be notified of writes to the pipe.  The
22  *		   callback will signal a cv.
23  *		3. In a loop, write to the pipe then wait on the cv.
24  */
25 
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <pthread.h>
29 #include <signal.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <strings.h>
33 #include <unistd.h>
34 
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 
38 #include "testlib.h"
39 #include "mevent.h"
40 
41 static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
42 static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
43 
44 static char cookie[] = "Chocolate chip with fudge stripes";
45 
46 /*
47  * After this many bytes are sent, writes will get batched up, progress will be
48  * made on the write side via an interval timer
49  */
50 const int pauseat = 8;
51 
52 static void
53 munch(int fd, enum ev_type ev, void *arg)
54 {
55 	static int i = 0;
56 	char buf[sizeof (cookie)] = { 0 };
57 	ssize_t nbytes;
58 	ssize_t expected;
59 
60 	ASSERT_INT_EQ(("bad event"), ev, EVF_READ);
61 	ASSERT_PTR_EQ(("bad cookie"), arg, cookie);
62 
63 	/*
64 	 * For the first while, expect data to come a byte at a time.  After the
65 	 * pause, we should get a burst with the rest of the data.
66 	 */
67 	if (i > pauseat) {
68 		expected = strlen(cookie) - pauseat - 1;
69 	} else {
70 		expected = 1;
71 	}
72 
73 	if ((nbytes = read(fd, buf, sizeof (buf))) < 0) {
74 		FAIL_ERRNO("bad read");
75 	}
76 	VERBOSE(("read %ld bytes '%s'", nbytes, buf));
77 
78 	ASSERT_INT64_EQ(("wanted a byte of cookie"), nbytes, expected);
79 
80 	if (expected == 1) {
81 		ASSERT_CHAR_EQ(("bad byte %d of cookie", i), buf[0], cookie[i]);
82 	} else {
83 		ASSERT_STR_EQ(("bad last half of cookie"), buf, &cookie[i]);
84 	}
85 
86 	pthread_mutex_lock(&mtx);
87 	pthread_cond_signal(&cv);
88 	VERBOSE(("wakeup"));
89 	pthread_mutex_unlock(&mtx);
90 
91 	i++;
92 }
93 
94 static void
95 tick(int ms, enum ev_type ev, void *arg)
96 {
97 	pthread_mutex_lock(&mtx);
98 	pthread_cond_signal(&cv);
99 	VERBOSE(("wakeup"));
100 	pthread_mutex_unlock(&mtx);
101 }
102 
103 int
104 main(int argc, const char *argv[])
105 {
106 	int pipefds[2];
107 	struct mevent *evp, *timer;
108 	ssize_t written;
109 
110 	start_test(argv[0], 5);
111 	start_event_thread();
112 
113 	if (pipe(pipefds) != 0) {
114 		FAIL_ERRNO("pipe");
115 	}
116 	if (fcntl(pipefds[0], F_SETFL, O_NONBLOCK) != 0) {
117 		FAIL_ERRNO("set pipe nonblocking");
118 	}
119 
120 	evp = mevent_add(pipefds[0], EVF_READ, munch, cookie);
121 	ASSERT_PTR_NEQ(("mevent_add pipefd"), evp, NULL);
122 
123 	for (int i = 0; cookie[i] != 0; i++) {
124 		pthread_mutex_lock(&mtx);
125 		written = write(pipefds[1], cookie + i, 1);
126 		if (written < 0) {
127 			FAIL_ERRNO("bad write");
128 		}
129 		ASSERT_INT64_EQ(("write byte %d of cookie", i), written, 1);
130 
131 		/* Wait for it to be read */
132 		pthread_cond_wait(&cv, &mtx);
133 		pthread_mutex_unlock(&mtx);
134 
135 		if (i == pauseat) {
136 			timer = mevent_add(10, EVF_TIMER, tick,
137 			    &cookie[pauseat]);
138 			ASSERT_PTR_NEQ(("mevent_add timer"), timer, NULL);
139 			VERBOSE(("disable munch"));
140 			mevent_disable(evp);
141 		}
142 	}
143 
144 	pthread_mutex_lock(&mtx);
145 
146 	mevent_enable(evp);
147 
148 	pthread_cond_wait(&cv, &mtx);
149 	pthread_mutex_unlock(&mtx);
150 
151 	PASS();
152 }
153