Forráskód Böngészése

async: Working test for async write

Serj Kalichev 3 éve
szülő
commit
1bac8e0062
1 módosított fájl, 32 hozzáadás és 3 törlés
  1. 32 3
      faux/async/testc_async.c

+ 32 - 3
faux/async/testc_async.c

@@ -3,6 +3,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <unistd.h>
 
 #include "faux/str.h"
 #include "faux/async.h"
@@ -15,9 +16,10 @@ static bool_t stall_cb(faux_async_t *async, size_t len, void *user_data)
 	if (!o_flag)
 		return BOOL_FALSE;
 	*o_flag = BOOL_TRUE;
-	printf("Stall callback %lu\n", len);
+//	printf("Stall %lu\n", len);
 
-	async = async;
+	async = async; // Happy compiler
+	len = len; // Happy compiler
 
 	return BOOL_TRUE;
 }
@@ -25,6 +27,9 @@ static bool_t stall_cb(faux_async_t *async, size_t len, void *user_data)
 int testc_faux_async(void)
 {
 	const size_t len = 9000000l;
+	const size_t read_chunk = 1000;
+	char *read_buf = NULL;
+	ssize_t readed = 0;
 	char *src_file = NULL;
 	int ret = -1; // Pessimistic return value
 	char *src_fn = NULL;
@@ -34,6 +39,7 @@ int testc_faux_async(void)
 	int fd = -1;
 	faux_async_t *out = NULL;
 	bool_t o_flag = BOOL_FALSE;
+	int pipefd[2] = {-1, -1};
 
 	// Prepare files
 	src_file = faux_zmalloc(len);
@@ -43,9 +49,14 @@ int testc_faux_async(void)
 	}
 	src_fn = faux_testc_tmpfile_deploy(src_file, len);
 
+	if (pipe(pipefd) < 0)
+		goto parse_error;
+	read_buf = faux_malloc(read_chunk);
+
 	dst_fn = faux_str_sprintf("%s/dst", getenv(FAUX_TESTC_TMPDIR_ENV));
 	fd = open(dst_fn, O_WRONLY | O_CREAT | O_TRUNC, 0600);
-	out = faux_async_new(fd);
+
+	out = faux_async_new(pipefd[1]);
 	faux_async_set_stall_cb(out, stall_cb, &o_flag);
 	faux_async_set_overflow(out, len + 1);
 	if (faux_async_write(out, src_file, len) < 0) {
@@ -53,11 +64,25 @@ int testc_faux_async(void)
 		goto parse_error;
 	}
 
+	// "Async" pipe write and sync pipe read
 	while (o_flag) {
 		o_flag = BOOL_FALSE;
 		faux_async_out(out);
+		readed = read(pipefd[0], read_buf, read_chunk);
+		if (readed < 0)
+			continue;
+		if (write(fd, read_buf, readed) < 0)
+			continue;
 	}
 
+	// Read the rest data
+	close(pipefd[1]);
+	pipefd[1] = -1;
+	while ((readed = read(pipefd[0], read_buf, read_chunk)) > 0)
+		if (write(fd, read_buf, readed) < 0)
+			continue;
+
+	// Compare etalon file and generated file
 	if (faux_testc_file_cmp(dst_fn, src_fn) != 0) {
 		fprintf(stderr, "Destination file %s is not equal to source %s\n",
 			dst_fn, src_fn);
@@ -67,6 +92,10 @@ int testc_faux_async(void)
 	ret = 0; // success
 
 parse_error:
+	if (pipefd[0] >= 0)
+		close(pipefd[0]);
+	if (pipefd[1] >= 0)
+		close(pipefd[1]);
 	faux_async_free(out);
 	faux_str_free(dst_fn);
 	faux_str_free(src_fn);