[libdispatch-changes] [205] branches/Lion/testing
source_changes at macosforge.org
source_changes at macosforge.org
Thu Aug 18 21:26:24 PDT 2011
Revision: 205
http://trac.macosforge.org/projects/libdispatch/changeset/205
Author: dsteffen at apple.com
Date: 2011-08-18 21:26:23 -0700 (Thu, 18 Aug 2011)
Log Message:
-----------
rearrange testsuite files
Added Paths:
-----------
branches/Lion/testing/bsdtestharness.c
branches/Lion/testing/bsdtests.c
branches/Lion/testing/bsdtests.h
branches/Lion/testing/bsdtestsummarize.c
branches/Lion/testing/cffd.c
branches/Lion/testing/dispatch_queue_finalizer.c
branches/Lion/testing/shims.h
Removed Paths:
-------------
branches/Lion/testing/Makefile.darwin
branches/Lion/testing/dispatch_cffd.c
branches/Lion/testing/dispatch_test.c
branches/Lion/testing/dispatch_test.h
branches/Lion/testing/fd_stress.c
branches/Lion/testing/func.c
branches/Lion/testing/harness.c
branches/Lion/testing/os_shims.h
branches/Lion/testing/queue_finalizer.c
branches/Lion/testing/summarize.c
Deleted: branches/Lion/testing/Makefile.darwin
===================================================================
--- branches/Lion/testing/Makefile.darwin 2011-08-18 20:57:32 UTC (rev 204)
+++ branches/Lion/testing/Makefile.darwin 2011-08-19 04:26:23 UTC (rev 205)
@@ -1,118 +0,0 @@
-# No workie: dispatch_sema
-
-MAKECMD= $(MAKE) -f Makefile.darwin
-
-TESTS= dispatch_apply \
- dispatch_api \
- dispatch_c99 \
- dispatch_cffd \
- dispatch_debug \
- queue_finalizer \
- dispatch_group \
- dispatch_pingpong \
- dispatch_plusplus \
- dispatch_priority \
- dispatch_priority2 \
- dispatch_proc \
- dispatch_read \
- dispatch_after \
- dispatch_sema \
- dispatch_timer_bit31 \
- dispatch_timer_bit63 \
- dispatch_starfish \
- dispatch_cascade \
- dispatch_drift \
- dispatch_readsync \
- nsoperation
-
-all: harness summarize bench $(TESTS)
- @lipo -remove x86_64 -output dispatch_timer_bit31 dispatch_timer_bit31 2>/dev/null || true
-
-logs: $(addsuffix .testlog, $(TESTS))
-debuglogs: $(addsuffix .debuglog, $(TESTS))
-
-testbots:
- $(MAKECMD) test
-
-test: clean-logs
- $(MAKECMD) _test
-
-_test: all logs debuglogs
- @cat *.testlog *.debuglog
- @cat *.testlog *.debuglog | ./summarize
-
-# Override ARCHS and SDKROOT to cross-build test suite
-
-SRCS = dispatch_test.c
-OBJS = $(SRCS:%.c=%.o)
-ARCHS=i386 x86_64 ppc
-CFLAGS = -Werror -Wall -Wextra -Wshadow -mdynamic-no-pic -Os -g $(patsubst %, -arch %,$(ARCHS)) -DDISPATCH_NO_LEGACY -I..
-CPPFLAGS = $(CFLAGS)
-LDFLAGS = $(patsubst %, -arch %,$(ARCHS))
-LDLIBS = -lstdc++
-
-ifneq ($(SDKROOT),)
-CFLAGS += -isysroot $(SDKROOT)
-LDFLAGS += -isysroot $(SDKROOT)
-CC = xcrun -sdk $(SDKROOT) gcc
-endif
-
-harness: harness.o $(OBJS)
-summarize: summarize.o
-bench: bench.o func.o
- $(CC) $(LDFLAGS) -framework Foundation $(LDLIBS) -o $@ $^
-
-bench.o: bench.mm
- $(CC) -x objective-c++ $(CFLAGS) -c $^ -o $@
-func.o: func.c
- $(CC) -x c++ $(CFLAGS) -c $^ -o $@
-
-dispatch_apply: dispatch_apply.o $(OBJS)
-dispatch_api: dispatch_api.o $(OBJS)
-dispatch_c99: dispatch_c99.o $(OBJS)
-dispatch_cffd: dispatch_cffd.o $(OBJS)
- $(CC) $(LDFLAGS) -framework CoreFoundation -o $@ $^
-dispatch_debug: dispatch_debug.o $(OBJS)
-dispatch_group: dispatch_group.o $(OBJS)
-dispatch_pingpong: dispatch_pingpong.o $(OBJS)
-dispatch_plusplus: dispatch_plusplus.o $(OBJS)
-dispatch_priority: dispatch_priority.o $(OBJS)
-dispatch_priority2: dispatch_priority2.o $(OBJS)
-dispatch_proc: dispatch_proc.o $(OBJS)
-queue_finalizer: queue_finalizer.o $(OBJS)
-dispatch_read: dispatch_read.o $(OBJS)
-dispatch_read2: dispatch_read2.o $(OBJS)
-dispatch_after: dispatch_after.o $(OBJS)
-dispatch_timer: dispatch_timer.o $(OBJS)
-dispatch_suspend_timer: dispatch_suspend_timer.o $(OBJS)
-dispatch_sema: dispatch_sema.o $(OBJS)
-dispatch_timer_bit31: dispatch_timer_bit31.o $(OBJS)
-dispatch_timer_bit63: dispatch_timer_bit63.o $(OBJS)
-dispatch_drift: dispatch_drift.o $(OBJS)
-dispatch_starfish: dispatch_starfish.o $(OBJS)
-dispatch_cascade: dispatch_cascade.o $(OBJS)
-dispatch_readsync: dispatch_readsync.o $(OBJS)
-ENVIRON_nsoperation = NOLEAKS=1
-nsoperation: nsoperation.o $(OBJS)
- $(CC) $(LDFLAGS) -framework Foundation -o $@ $^
-
-dispatch_api.o: dispatch_api.c
- $(CC) -c $(CFLAGS) -include $(SDKROOT)/usr/include/dispatch/dispatch.h -pendantic -o $@ $^
-
-dispatch_c99.o: dispatch_c99.c
- $(CC) -c $(CFLAGS) -std=c99 -pedantic -o $@ $^
-
-dispatch_priority2.o: dispatch_priority.c
- $(CC) -c $(CFLAGS) -DUSE_SET_TARGET_QUEUE=1 -o $@ $^
-
-$(addsuffix .testlog, $(TESTS)): harness $(TESTS)
- $(ENVIRON_$(basename $@)) ./harness ./$(basename $@) > $@
-
-$(addsuffix .debuglog, $(TESTS)): harness $(TESTS)
- $(ENVIRON_$(basename $@)) DYLD_IMAGE_SUFFIX=_debug ./harness ./$(basename $@) > $@
-
-clean-logs:
- rm -f *.testlog *.debuglog *.leakslog
-
-clean: clean-logs
- rm -f *.o *.dSYM bench harness summarize $(TESTS)
Copied: branches/Lion/testing/bsdtestharness.c (from rev 200, branches/Lion/testing/harness.c)
===================================================================
--- branches/Lion/testing/bsdtestharness.c (rev 0)
+++ branches/Lion/testing/bsdtestharness.c 2011-08-19 04:26:23 UTC (rev 205)
@@ -0,0 +1,130 @@
+/*
+ * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+#include "config/config.h"
+
+#include <dispatch/dispatch.h>
+#include <assert.h>
+#include <spawn.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <signal.h>
+#include <mach/clock_types.h>
+
+#include "dispatch_test.h"
+
+extern char **environ;
+
+int
+main(int argc, char *argv[])
+{
+ dispatch_source_t proc;
+ dispatch_source_t sig;
+ int res;
+ pid_t pid;
+
+ if (argc < 2) {
+ fprintf(stderr, "usage: harness [...]\n");
+ exit(1);
+ }
+
+ //
+ // Start the test in a suspended state. Will send SIGCONT once ready.
+ //
+
+ posix_spawnattr_t attr;
+ res = posix_spawnattr_init(&attr);
+ assert(res == 0);
+ res = posix_spawnattr_setflags(&attr, POSIX_SPAWN_START_SUSPENDED);
+ assert(res == 0);
+
+ int i;
+ char** newargv = calloc(argc, sizeof(void*));
+ for (i = 1; i < argc; ++i) {
+ newargv[i-1] = argv[i];
+ }
+ newargv[i-1] = NULL;
+
+ res = posix_spawnp(&pid, newargv[0], NULL, &attr, newargv, environ);
+ if (res) {
+ errno = res;
+ perror(newargv[0]);
+ exit(EXIT_FAILURE);
+ }
+ //fprintf(stderr, "pid = %d\n", pid);
+ assert(pid > 0);
+
+ //
+ // Monitor the test process exit status.
+ //
+
+ dispatch_queue_t main_q = dispatch_get_main_queue();
+
+ proc = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid, DISPATCH_PROC_EXIT, main_q);
+ assert(proc);
+
+ dispatch_source_set_event_handler(proc, ^{
+ int status;
+ int res2 = waitpid(pid, &status, 0);
+ assert(res2 != -1);
+ test_long("Process exited", WEXITSTATUS(status) | WTERMSIG(status), 0);
+ exit(0);
+ });
+
+ dispatch_resume(proc);
+
+ //
+ // Forcibly stop currently running test after 30 second timeout.
+ //
+
+ uint64_t timeout = 30LL * NSEC_PER_SEC;
+
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeout), main_q, ^{
+ kill(pid, SIGKILL);
+ fprintf(stderr, "Terminating unresponsive process (%0.1lfs)\n", (double)timeout/NSEC_PER_SEC);
+ });
+
+ //
+ // Control-c forcibly stops currently running test.
+ //
+
+ // Ignore the default signal handler so that dispatch can handle it.
+ signal(SIGINT, SIG_IGN);
+
+ sig = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGINT, 0, main_q);
+ assert(sig);
+
+ dispatch_source_set_event_handler(sig, ^{
+ fprintf(stderr, "Terminating process due to signal\n");
+ kill(pid, SIGKILL);
+ });
+ dispatch_resume(sig);
+
+ //
+ // Start the test.
+ //
+
+ kill(pid, SIGCONT);
+
+ dispatch_main();
+
+ return 0;
+}
Copied: branches/Lion/testing/bsdtests.c (from rev 200, branches/Lion/testing/dispatch_test.c)
===================================================================
--- branches/Lion/testing/bsdtests.c (rev 0)
+++ branches/Lion/testing/bsdtests.c 2011-08-19 04:26:23 UTC (rev 205)
@@ -0,0 +1,156 @@
+#include "config/config.h"
+
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/errno.h>
+#include <string.h>
+
+#include "dispatch_test.h"
+
+#define _test_print(_file, _line, _desc, \
+ _expr, _fmt1, _val1, _fmt2, _val2) do { \
+ const char* _exprstr = _expr ? "PASS" : "FAIL"; \
+ char _linestr[BUFSIZ]; \
+ if (!_expr) { \
+ snprintf(_linestr, sizeof(_linestr), \
+ " (%s:%ld)", _file, _line); \
+ } else { \
+ _linestr[0] = 0; \
+ } \
+ if (_fmt2 == 0) { \
+ printf("\tValue: " _fmt1 "\n" \
+ "[%s] %s%s\n", \
+ _val1, \
+ _exprstr, \
+ _desc, \
+ _linestr); \
+ } else { \
+ printf("\tActual: " _fmt1 "\n" \
+ "\tExpected: " _fmt2 "\n" \
+ "[%s] %s%s\n", \
+ _val1, \
+ _val2, \
+ _exprstr, \
+ _desc, \
+ _linestr); \
+ } \
+ if (!_expr) { \
+ printf("\t%s:%ld\n", _file, _line); \
+ } \
+ fflush(stdout); \
+} while (0);
+
+void
+test_start(const char* desc) {
+ printf("\n==================================================\n");
+ printf("[TEST] %s\n", desc);
+ printf("[PID] %d\n", getpid());
+ printf("==================================================\n\n");
+ usleep(100000); // give 'gdb --waitfor=' a chance to find this proc
+}
+
+#define test_ptr_null(a,b) _test_ptr_null(__FILE__, __LINE__, a, b)
+void
+_test_ptr_null(const char* file, long line, const char* desc, const void* ptr) {
+ _test_print(file, line, desc,
+ (ptr == NULL), "%p", ptr, "%p", (void*)0);
+}
+
+#define test_ptr_notnull(a,b) _test_ptr_notnull(__FILE__, __LINE__, a, b)
+void
+_test_ptr_notnull(const char* file, long line, const char* desc, const void* ptr) {
+ _test_print(file, line, desc,
+ (ptr != NULL), "%p", ptr, "%p", ptr ?: (void*)~0);
+}
+
+#define test_ptr(a,b,c) _test_ptr(__FILE__, __LINE__, a, b, c)
+void
+_test_ptr(const char* file, long line, const char* desc, const void* actual, const void* expected) {
+ _test_print(file, line, desc,
+ (actual == expected), "%p", actual, "%p", expected);
+}
+
+#define test_long(a,b,c) _test_long(__FILE__, __LINE__, a, b, c)
+void
+_test_long(const char* file, long line, const char* desc, long actual, long expected) {
+ _test_print(file, line, desc,
+ (actual == expected), "%ld", actual, "%ld", expected);
+}
+
+#define test_long_less_than(a, b, c) _test_long_less_than(__FILE__, __LINE__, a, b, c)
+void
+_test_long_less_than(const char* file, long line, const char* desc, long actual, long expected_max) {
+ _test_print(file, line, desc, (actual < expected_max), "%ld", actual, "<%ld", expected_max);
+}
+
+#define test_double_less_than(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m)
+void
+_test_double_less_than(const char* file, long line, const char* desc, double val, double max_expected) {
+ _test_print(file, line, desc, (val < max_expected), "%f", val, "<%f", max_expected);
+}
+
+#define test_double_less_than_or_equal(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m)
+void
+_test_double_less_than_or_equal(const char* file, long line, const char* desc, double val, double max_expected) {
+ _test_print(file, line, desc, (val <= max_expected), "%f", val, "<%f", max_expected);
+}
+
+#define test_errno(a,b,c) _test_errno(__FILE__, __LINE__, a, b, c)
+void
+_test_errno(const char* file, long line, const char* desc, long actual, long expected) {
+ char* actual_str;
+ char* expected_str;
+ asprintf(&actual_str, "%ld\t%s", actual, actual ? strerror(actual) : "");
+ asprintf(&expected_str, "%ld\t%s", expected, expected ? strerror(expected) : "");
+ _test_print(file, line, desc,
+ (actual == expected), "%s", actual_str, "%s", expected_str);
+ free(actual_str);
+ free(expected_str);
+}
+
+#include <spawn.h>
+
+extern char **environ;
+
+void
+test_stop(void) {
+ test_stop_after_delay((void *)(intptr_t)0);
+}
+
+void
+test_stop_after_delay(void *delay) {
+#if HAVE_LEAKS
+ int res;
+ pid_t pid;
+ char pidstr[10];
+#endif
+
+ if (delay != NULL) {
+ sleep((int)(intptr_t)delay);
+ }
+
+#if HAVE_LEAKS
+ if (getenv("NOLEAKS")) _exit(EXIT_SUCCESS);
+
+ /* leaks doesn't work against debug variant malloc */
+ if (getenv("DYLD_IMAGE_SUFFIX")) _exit(EXIT_SUCCESS);
+
+ snprintf(pidstr, sizeof(pidstr), "%d", getpid());
+ char* args[] = { "./leaks-wrapper", pidstr, NULL };
+ res = posix_spawnp(&pid, args[0], NULL, NULL, args, environ);
+ if (res == 0 && pid > 0) {
+ int status;
+ waitpid(pid, &status, 0);
+ test_long("Leaks", status, 0);
+ } else {
+ perror(args[0]);
+ }
+#endif
+ _exit(EXIT_SUCCESS);
+}
Copied: branches/Lion/testing/bsdtests.h (from rev 200, branches/Lion/testing/dispatch_test.h)
===================================================================
--- branches/Lion/testing/bsdtests.h (rev 0)
+++ branches/Lion/testing/bsdtests.h 2011-08-19 04:26:23 UTC (rev 205)
@@ -0,0 +1,36 @@
+#include <errno.h>
+#include "os_shims.h"
+#include <dispatch/dispatch.h>
+
+__DISPATCH_BEGIN_DECLS
+
+void test_start(const char* desc);
+void test_stop(void);
+void test_stop_after_delay(void *delay);
+
+void _test_ptr_null(const char* file, long line, const char* desc, const void* ptr);
+#define test_ptr_null(a,b) _test_ptr_null(__FILE__, __LINE__, a, b)
+
+void _test_ptr_notnull(const char* file, long line, const char* desc, const void* ptr);
+#define test_ptr_notnull(a,b) _test_ptr_notnull(__FILE__, __LINE__, a, b)
+
+void _test_ptr(const char* file, long line, const char* desc, const void* actual, const void* expected);
+#define test_ptr(a,b,c) _test_ptr(__FILE__, __LINE__, a, b, c)
+
+void _test_long(const char* file, long line, const char* desc, long actual, long expected);
+#define test_long(a,b,c) _test_long(__FILE__, __LINE__, a, b, c)
+
+void _test_long_less_than(const char* file, long line, const char* desc, long actual, long max_expected);
+#define test_long_less_than(a,b,c) _test_long_less_than(__FILE__, __LINE__, a, b, c)
+
+void _test_double_less_than_or_equal(const char* file, long line, const char* desc, double val, double max_expected);
+#define test_double_less_than_or_equal(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m)
+
+void _test_double_less_than(const char* file, long line, const char* desc, double val, double max_expected);
+#define test_double_less_than(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m)
+
+void _test_errno(const char* file, long line, const char* desc, long actual, long expected);
+#define test_errno(a,b,c) _test_errno(__FILE__, __LINE__, a, b, c)
+
+__DISPATCH_END_DECLS
+
Copied: branches/Lion/testing/bsdtestsummarize.c (from rev 200, branches/Lion/testing/summarize.c)
===================================================================
--- branches/Lion/testing/bsdtestsummarize.c (rev 0)
+++ branches/Lion/testing/bsdtestsummarize.c 2011-08-19 04:26:23 UTC (rev 205)
@@ -0,0 +1,98 @@
+/*
+ * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+#include "os_shims.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int
+has_prefix(const char* str, const char* prefix) {
+ return (strncmp(str, prefix, strlen(prefix)) == 0);
+}
+
+int
+print_summary(FILE* f, long total, long pass, long fail) {
+ fprintf(f, "Total: %ld\n", total);
+ fprintf(f, "Passed: %ld (%0.0lf%%)\n", pass, ((double)pass / (double)total) * (double)100.0);
+ fprintf(f, "Failed: %ld (%0.0lf%%)\n", fail, ((double)fail / (double)total) * (double)100.0);
+ fprintf(f, "\n");
+ return 0;
+}
+
+int main(int argc, char* argv[]) {
+ if (argc > 1) {
+ fprintf(stderr, "%s: usage: summarize\n", argv[0]);
+ exit(1);
+ }
+
+ /*
+ FILE* f = fopen(argv[1], "w");
+ if (f == NULL) {
+ perror(argv[1]);
+ exit(1);
+ }
+ */
+ FILE* f = stdout;
+
+ fprintf(f, "\n==================================================\n");
+ fprintf(f, "[SUMMARY] Test Summary\n");
+ fprintf(f, "==================================================\n\n");
+
+ size_t len;
+ char* ln;
+ long total = 0;
+ long pass = 0;
+ long fail = 0;
+ long total_total = 0;
+ long total_pass = 0;
+ long total_fail = 0;
+ for(;;) {
+ ln = fgetln(stdin, &len);
+ //if (ln) fprintf(stdout, "%.*s", (int)len, ln);
+ if (ln == NULL || has_prefix(ln, "[TEST]")) {
+ if (total) {
+ print_summary(f, total, pass, fail);
+ }
+ total_total += total;
+ total_pass += pass;
+ total_fail += fail;
+ total = 0;
+ pass = 0;
+ fail = 0;
+ if (ln) {
+ fprintf(f, "%.*s", (int)len, ln);
+ } else {
+ fprintf(f, "[TOTAL]\n");
+ print_summary(f, total_total, total_pass, total_fail);
+ break;
+ }
+ } else if (has_prefix(ln, "[PASS]")) {
+ ++total;
+ ++pass;
+ } else if (has_prefix(ln, "[FAIL]")) {
+ ++total;
+ ++fail;
+ }
+ }
+
+ return (total_fail ? EXIT_FAILURE : EXIT_SUCCESS);
+}
Copied: branches/Lion/testing/cffd.c (from rev 200, branches/Lion/testing/dispatch_cffd.c)
===================================================================
--- branches/Lion/testing/cffd.c (rev 0)
+++ branches/Lion/testing/cffd.c 2011-08-19 04:26:23 UTC (rev 205)
@@ -0,0 +1,161 @@
+/*
+ * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+#include "config/config.h"
+
+#include <dispatch/dispatch.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/param.h>
+#include <sys/ucred.h>
+#include <sys/mount.h>
+#include <sys/queue.h>
+#include <sys/errno.h>
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/time.h>
+
+#include <CoreServices/CoreServices.h>
+
+#include "dispatch_test.h"
+
+int debug = 0;
+
+#define DEBUG(...) do { \
+ if (debug) fprintf(stderr, __VA_ARGS__); \
+ } while(0);
+
+#define assert_errno(str, expr) do { \
+ if (!(expr)) { \
+ fprintf(stderr, "%s: %s\n", (str), strerror(errno)); \
+ exit(1); \
+ } } while(0);
+
+int
+init_kqueue(void)
+{
+ int kq;
+ int res;
+ struct kevent ke;
+ static struct timespec t0;
+
+ kq = kqueue();
+ assert_errno("kqueue", kq >= 0);
+
+ EV_SET(&ke, 1, EVFILT_TIMER, EV_ADD, NOTE_SECONDS, 1, 0);
+
+ res = kevent(kq, &ke, 1, NULL, 0, &t0);
+ assert_errno("kevent", res == 0);
+
+ return kq;
+}
+
+int
+read_kevent(int kq)
+{
+ int res;
+ struct kevent ke;
+ //static struct timespec t0;
+
+ res = kevent(kq, NULL, 0, &ke, 1, NULL);
+ assert_errno("kevent", res >= 0);
+
+ fprintf(stdout, "kevent.data = %ld\n", ke.data);
+
+ return (res < 0);
+}
+
+
+static void
+cffd_callback(CFFileDescriptorRef cffd,
+ CFOptionFlags callBackTypes __attribute__((unused)),
+ void *info __attribute__((unused)))
+{
+ int kq;
+
+ kq = CFFileDescriptorGetNativeDescriptor(cffd);
+ if (read_kevent(kq) == 0) {
+ // ...
+ }
+
+ CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack);
+}
+
+void
+timer()
+{
+ dispatch_source_t ds;
+ ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
+ assert(ds);
+ dispatch_source_set_timer(ds, dispatch_time(0, 1*NSEC_PER_SEC), NSEC_PER_SEC, 0);
+ dispatch_source_set_event_handler(ds, ^{
+ printf("ping\n");
+ });
+ dispatch_resume(ds);
+}
+
+void
+hangup()
+{
+ dispatch_source_t ds;
+ ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGHUP, 0, dispatch_get_main_queue());
+ assert(ds);
+ dispatch_source_set_event_handler(ds, ^{
+ printf("hangup\n");
+ });
+ dispatch_resume(ds);
+}
+
+int
+main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
+{
+ int kq;
+ CFFileDescriptorRef cffd;
+ CFRunLoopSourceRef rls;
+ CFFileDescriptorContext ctx;
+
+ test_start("CFFileDescriptor");
+
+ signal(SIGHUP, SIG_IGN);
+
+ kq = init_kqueue();
+
+ memset(&ctx, 0, sizeof(CFFileDescriptorContext));
+ cffd = CFFileDescriptorCreate(NULL, kq, 1, cffd_callback, &ctx);
+ assert(cffd);
+
+ rls = CFFileDescriptorCreateRunLoopSource(NULL, cffd, 0);
+ assert(rls);
+ CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
+ CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack);
+
+// timer();
+// hangup();
+
+ CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10.0, false);
+
+ test_stop();
+
+ return 0;
+}
+
Deleted: branches/Lion/testing/dispatch_cffd.c
===================================================================
--- branches/Lion/testing/dispatch_cffd.c 2011-08-18 20:57:32 UTC (rev 204)
+++ branches/Lion/testing/dispatch_cffd.c 2011-08-19 04:26:23 UTC (rev 205)
@@ -1,161 +0,0 @@
-/*
- * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
- *
- * @APPLE_APACHE_LICENSE_HEADER_START@
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * @APPLE_APACHE_LICENSE_HEADER_END@
- */
-
-#include "config/config.h"
-
-#include <dispatch/dispatch.h>
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#include <sys/param.h>
-#include <sys/ucred.h>
-#include <sys/mount.h>
-#include <sys/queue.h>
-#include <sys/errno.h>
-#include <sys/types.h>
-#include <sys/event.h>
-#include <sys/time.h>
-
-#include <CoreServices/CoreServices.h>
-
-#include "dispatch_test.h"
-
-int debug = 0;
-
-#define DEBUG(...) do { \
- if (debug) fprintf(stderr, __VA_ARGS__); \
- } while(0);
-
-#define assert_errno(str, expr) do { \
- if (!(expr)) { \
- fprintf(stderr, "%s: %s\n", (str), strerror(errno)); \
- exit(1); \
- } } while(0);
-
-int
-init_kqueue(void)
-{
- int kq;
- int res;
- struct kevent ke;
- static struct timespec t0;
-
- kq = kqueue();
- assert_errno("kqueue", kq >= 0);
-
- EV_SET(&ke, 1, EVFILT_TIMER, EV_ADD, NOTE_SECONDS, 1, 0);
-
- res = kevent(kq, &ke, 1, NULL, 0, &t0);
- assert_errno("kevent", res == 0);
-
- return kq;
-}
-
-int
-read_kevent(int kq)
-{
- int res;
- struct kevent ke;
- //static struct timespec t0;
-
- res = kevent(kq, NULL, 0, &ke, 1, NULL);
- assert_errno("kevent", res >= 0);
-
- fprintf(stdout, "kevent.data = %ld\n", ke.data);
-
- return (res < 0);
-}
-
-
-static void
-cffd_callback(CFFileDescriptorRef cffd,
- CFOptionFlags callBackTypes __attribute__((unused)),
- void *info __attribute__((unused)))
-{
- int kq;
-
- kq = CFFileDescriptorGetNativeDescriptor(cffd);
- if (read_kevent(kq) == 0) {
- // ...
- }
-
- CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack);
-}
-
-void
-timer()
-{
- dispatch_source_t ds;
- ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
- assert(ds);
- dispatch_source_set_timer(ds, dispatch_time(0, 1*NSEC_PER_SEC), NSEC_PER_SEC, 0);
- dispatch_source_set_event_handler(ds, ^{
- printf("ping\n");
- });
- dispatch_resume(ds);
-}
-
-void
-hangup()
-{
- dispatch_source_t ds;
- ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGHUP, 0, dispatch_get_main_queue());
- assert(ds);
- dispatch_source_set_event_handler(ds, ^{
- printf("hangup\n");
- });
- dispatch_resume(ds);
-}
-
-int
-main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
-{
- int kq;
- CFFileDescriptorRef cffd;
- CFRunLoopSourceRef rls;
- CFFileDescriptorContext ctx;
-
- test_start("CFFileDescriptor");
-
- signal(SIGHUP, SIG_IGN);
-
- kq = init_kqueue();
-
- memset(&ctx, 0, sizeof(CFFileDescriptorContext));
- cffd = CFFileDescriptorCreate(NULL, kq, 1, cffd_callback, &ctx);
- assert(cffd);
-
- rls = CFFileDescriptorCreateRunLoopSource(NULL, cffd, 0);
- assert(rls);
- CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopDefaultMode);
- CFFileDescriptorEnableCallBacks(cffd, kCFFileDescriptorReadCallBack);
-
-// timer();
-// hangup();
-
- CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10.0, false);
-
- test_stop();
-
- return 0;
-}
-
Copied: branches/Lion/testing/dispatch_queue_finalizer.c (from rev 200, branches/Lion/testing/queue_finalizer.c)
===================================================================
--- branches/Lion/testing/dispatch_queue_finalizer.c (rev 0)
+++ branches/Lion/testing/dispatch_queue_finalizer.c 2011-08-19 04:26:23 UTC (rev 205)
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+#include "config/config.h"
+
+#include <dispatch/dispatch.h>
+
+#include <unistd.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#include "dispatch_test.h"
+
+void *ctxt_magic;
+
+static void
+finalizer(void *ctxt)
+{
+ test_ptr("finalizer ran", ctxt, ctxt_magic);
+ test_stop();
+}
+
+int
+main(void) {
+ test_start("Dispatch Queue Finalizer");
+
+#ifdef __LP64__
+ ctxt_magic = (void*)((uintptr_t)arc4random() << 32 | arc4random());
+#else
+ ctxt_magic = (void*)arc4random();
+#endif
+
+ dispatch_queue_t q = dispatch_queue_create(NULL, NULL);
+ test_ptr_notnull("dispatch_queue_new", q);
+
+ dispatch_set_context(q, ctxt_magic);
+
+ dispatch_set_finalizer_f(q, finalizer);
+
+ dispatch_release(q);
+
+ dispatch_main();
+
+ return 0;
+}
Deleted: branches/Lion/testing/dispatch_test.c
===================================================================
--- branches/Lion/testing/dispatch_test.c 2011-08-18 20:57:32 UTC (rev 204)
+++ branches/Lion/testing/dispatch_test.c 2011-08-19 04:26:23 UTC (rev 205)
@@ -1,156 +0,0 @@
-#include "config/config.h"
-
-#include <sys/types.h>
-#include <sys/wait.h>
-
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <errno.h>
-#include <sys/errno.h>
-#include <string.h>
-
-#include "dispatch_test.h"
-
-#define _test_print(_file, _line, _desc, \
- _expr, _fmt1, _val1, _fmt2, _val2) do { \
- const char* _exprstr = _expr ? "PASS" : "FAIL"; \
- char _linestr[BUFSIZ]; \
- if (!_expr) { \
- snprintf(_linestr, sizeof(_linestr), \
- " (%s:%ld)", _file, _line); \
- } else { \
- _linestr[0] = 0; \
- } \
- if (_fmt2 == 0) { \
- printf("\tValue: " _fmt1 "\n" \
- "[%s] %s%s\n", \
- _val1, \
- _exprstr, \
- _desc, \
- _linestr); \
- } else { \
- printf("\tActual: " _fmt1 "\n" \
- "\tExpected: " _fmt2 "\n" \
- "[%s] %s%s\n", \
- _val1, \
- _val2, \
- _exprstr, \
- _desc, \
- _linestr); \
- } \
- if (!_expr) { \
- printf("\t%s:%ld\n", _file, _line); \
- } \
- fflush(stdout); \
-} while (0);
-
-void
-test_start(const char* desc) {
- printf("\n==================================================\n");
- printf("[TEST] %s\n", desc);
- printf("[PID] %d\n", getpid());
- printf("==================================================\n\n");
- usleep(100000); // give 'gdb --waitfor=' a chance to find this proc
-}
-
-#define test_ptr_null(a,b) _test_ptr_null(__FILE__, __LINE__, a, b)
-void
-_test_ptr_null(const char* file, long line, const char* desc, const void* ptr) {
- _test_print(file, line, desc,
- (ptr == NULL), "%p", ptr, "%p", (void*)0);
-}
-
-#define test_ptr_notnull(a,b) _test_ptr_notnull(__FILE__, __LINE__, a, b)
-void
-_test_ptr_notnull(const char* file, long line, const char* desc, const void* ptr) {
- _test_print(file, line, desc,
- (ptr != NULL), "%p", ptr, "%p", ptr ?: (void*)~0);
-}
-
-#define test_ptr(a,b,c) _test_ptr(__FILE__, __LINE__, a, b, c)
-void
-_test_ptr(const char* file, long line, const char* desc, const void* actual, const void* expected) {
- _test_print(file, line, desc,
- (actual == expected), "%p", actual, "%p", expected);
-}
-
-#define test_long(a,b,c) _test_long(__FILE__, __LINE__, a, b, c)
-void
-_test_long(const char* file, long line, const char* desc, long actual, long expected) {
- _test_print(file, line, desc,
- (actual == expected), "%ld", actual, "%ld", expected);
-}
-
-#define test_long_less_than(a, b, c) _test_long_less_than(__FILE__, __LINE__, a, b, c)
-void
-_test_long_less_than(const char* file, long line, const char* desc, long actual, long expected_max) {
- _test_print(file, line, desc, (actual < expected_max), "%ld", actual, "<%ld", expected_max);
-}
-
-#define test_double_less_than(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m)
-void
-_test_double_less_than(const char* file, long line, const char* desc, double val, double max_expected) {
- _test_print(file, line, desc, (val < max_expected), "%f", val, "<%f", max_expected);
-}
-
-#define test_double_less_than_or_equal(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m)
-void
-_test_double_less_than_or_equal(const char* file, long line, const char* desc, double val, double max_expected) {
- _test_print(file, line, desc, (val <= max_expected), "%f", val, "<%f", max_expected);
-}
-
-#define test_errno(a,b,c) _test_errno(__FILE__, __LINE__, a, b, c)
-void
-_test_errno(const char* file, long line, const char* desc, long actual, long expected) {
- char* actual_str;
- char* expected_str;
- asprintf(&actual_str, "%ld\t%s", actual, actual ? strerror(actual) : "");
- asprintf(&expected_str, "%ld\t%s", expected, expected ? strerror(expected) : "");
- _test_print(file, line, desc,
- (actual == expected), "%s", actual_str, "%s", expected_str);
- free(actual_str);
- free(expected_str);
-}
-
-#include <spawn.h>
-
-extern char **environ;
-
-void
-test_stop(void) {
- test_stop_after_delay((void *)(intptr_t)0);
-}
-
-void
-test_stop_after_delay(void *delay) {
-#if HAVE_LEAKS
- int res;
- pid_t pid;
- char pidstr[10];
-#endif
-
- if (delay != NULL) {
- sleep((int)(intptr_t)delay);
- }
-
-#if HAVE_LEAKS
- if (getenv("NOLEAKS")) _exit(EXIT_SUCCESS);
-
- /* leaks doesn't work against debug variant malloc */
- if (getenv("DYLD_IMAGE_SUFFIX")) _exit(EXIT_SUCCESS);
-
- snprintf(pidstr, sizeof(pidstr), "%d", getpid());
- char* args[] = { "./leaks-wrapper", pidstr, NULL };
- res = posix_spawnp(&pid, args[0], NULL, NULL, args, environ);
- if (res == 0 && pid > 0) {
- int status;
- waitpid(pid, &status, 0);
- test_long("Leaks", status, 0);
- } else {
- perror(args[0]);
- }
-#endif
- _exit(EXIT_SUCCESS);
-}
Deleted: branches/Lion/testing/dispatch_test.h
===================================================================
--- branches/Lion/testing/dispatch_test.h 2011-08-18 20:57:32 UTC (rev 204)
+++ branches/Lion/testing/dispatch_test.h 2011-08-19 04:26:23 UTC (rev 205)
@@ -1,36 +0,0 @@
-#include <errno.h>
-#include "os_shims.h"
-#include <dispatch/dispatch.h>
-
-__DISPATCH_BEGIN_DECLS
-
-void test_start(const char* desc);
-void test_stop(void);
-void test_stop_after_delay(void *delay);
-
-void _test_ptr_null(const char* file, long line, const char* desc, const void* ptr);
-#define test_ptr_null(a,b) _test_ptr_null(__FILE__, __LINE__, a, b)
-
-void _test_ptr_notnull(const char* file, long line, const char* desc, const void* ptr);
-#define test_ptr_notnull(a,b) _test_ptr_notnull(__FILE__, __LINE__, a, b)
-
-void _test_ptr(const char* file, long line, const char* desc, const void* actual, const void* expected);
-#define test_ptr(a,b,c) _test_ptr(__FILE__, __LINE__, a, b, c)
-
-void _test_long(const char* file, long line, const char* desc, long actual, long expected);
-#define test_long(a,b,c) _test_long(__FILE__, __LINE__, a, b, c)
-
-void _test_long_less_than(const char* file, long line, const char* desc, long actual, long max_expected);
-#define test_long_less_than(a,b,c) _test_long_less_than(__FILE__, __LINE__, a, b, c)
-
-void _test_double_less_than_or_equal(const char* file, long line, const char* desc, double val, double max_expected);
-#define test_double_less_than_or_equal(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m)
-
-void _test_double_less_than(const char* file, long line, const char* desc, double val, double max_expected);
-#define test_double_less_than(d, v, m) _test_double_less_than(__FILE__, __LINE__, d, v, m)
-
-void _test_errno(const char* file, long line, const char* desc, long actual, long expected);
-#define test_errno(a,b,c) _test_errno(__FILE__, __LINE__, a, b, c)
-
-__DISPATCH_END_DECLS
-
Deleted: branches/Lion/testing/fd_stress.c
===================================================================
--- branches/Lion/testing/fd_stress.c 2011-08-18 20:57:32 UTC (rev 204)
+++ branches/Lion/testing/fd_stress.c 2011-08-19 04:26:23 UTC (rev 205)
@@ -1,479 +0,0 @@
-/*
- * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
- *
- * @APPLE_APACHE_LICENSE_HEADER_START@
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * @APPLE_APACHE_LICENSE_HEADER_END@
- */
-
-/*
- * fd_stress.c
- *
- * Stress test for dispatch read and write sources.
- */
-
-#include "config/config.h"
-
-#include <dispatch/dispatch.h>
-
-#include <assert.h>
-#include <CommonCrypto/CommonDigest.h>
-#include <errno.h>
-#include <fcntl.h>
-#include <netdb.h>
-#include <netinet/in.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/param.h>
-#include <unistd.h>
-
-static inline size_t max(size_t a, size_t b) {
- return (a > b) ? a : b;
-}
-
-static inline size_t min(size_t a, size_t b) {
- return (a < b) ? a : b;
-}
-
-int debug = 0;
-
-#define DEBUG(...) do { \
- if (debug) fprintf(stderr, __VA_ARGS__); \
- } while(0);
-
-#define assert_errno(str, expr) do { \
- if (!(expr)) { \
- fprintf(stderr, "%s: %s\n", (str), strerror(errno)); \
- exit(1); \
- } } while(0);
-
-#define assert_gai_errno(str, expr) do { \
- if (!(expr)) { \
- fprintf(stderr, "%s: %s\n", (str), gai_strerror(errno)); \
- exit(1); \
- } } while(0);
-
-
-/* sock_context
- *
- * Context structure used by the reader and writer queues.
- *
- * Writers begin by generating a random length and writing it to the descriptor.
- * The write buffer is filled with a random byte value and written until empty
- * or until the total length is reached. The write buffer is refilled with more
- * random data when empty. Each write updates an MD5 digest which is written to
- * the descriptor once the total length is reached.
- *
- * Readers begin by reading the total length of data. The read buffer is filled
- * and an MD5 digest is computed on the bytes as they are received. Once the
- * total length of data has be read, an MD5 digest is read from the descriptor
- * and compared with the computed value.
- */
-struct sock_context {
- enum {
- LENGTH,
- DATA,
- CKSUM,
- DONE,
- } state;
- char label[64];
- uint32_t len;
- off_t offset;
- char buf[8192];
- size_t buflen;
- CC_MD5_CTX md5ctx;
- char md5[CC_MD5_DIGEST_LENGTH];
-};
-
-dispatch_source_t
-create_writer(int wfd, dispatch_block_t completion)
-{
- dispatch_source_t ds;
- struct sock_context *ctx = calloc(1, sizeof(struct sock_context));
- assert(ctx);
-
- snprintf(ctx->label, sizeof(ctx->label), "writer.fd.%d", wfd);
- dispatch_queue_t queue = dispatch_queue_create(ctx->label, 0);
-
- ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_WRITE, wfd, 0, queue);
- assert(ds);
- dispatch_release(queue);
-
- uint32_t len;
- do {
- len = (arc4random() & 0x7FFF);
- } while (len == 0);
- ctx->state = LENGTH;
- CC_MD5_Init(&ctx->md5ctx);
- ctx->len = len;
- ctx->buflen = sizeof(len);
- len = htonl(len);
- memcpy(ctx->buf, &len, ctx->buflen);
- DEBUG("%s: LENGTH %d\n", ctx->label, ctx->len);
-
- dispatch_source_set_event_handler(ds, ^{
- DEBUG("%s: available %ld\n", ctx->label, dispatch_source_get_data(ds));
- ssize_t res;
- size_t wrsz = min(ctx->len, ctx->buflen);
- res = write(wfd, &ctx->buf[ctx->offset], wrsz);
- DEBUG("%s: write(%d, %p, %ld): %ld\n", ctx->label, wfd, &ctx->buf[ctx->offset], wrsz, res);
- if (res > 0) {
- if (ctx->state == DATA) {
- CC_MD5_Update(&ctx->md5ctx, &ctx->buf[ctx->offset], res);
- ctx->len -= res;
- }
- ctx->offset += res;
- ctx->buflen -= res;
- assert(ctx->offset >= 0);
- assert(ctx->len >= 0);
- assert(ctx->buflen >= 0);
- if (ctx->buflen == 0 || ctx->len == 0) {
- if (ctx->state == LENGTH) {
- // finished writing length, move on to data.
- ctx->state = DATA;
- ctx->buflen = sizeof(ctx->buf);
- char pattern = arc4random() & 0xFF;
- memset(ctx->buf, pattern, ctx->buflen);
- } else if (ctx->state == DATA && ctx->len == 0) {
- // finished writing data, move on to cksum.
- ctx->state = CKSUM;
- ctx->len = sizeof(ctx->md5);
- ctx->buflen = sizeof(ctx->md5);
- CC_MD5_Final(ctx->md5, &ctx->md5ctx);
- memcpy(ctx->buf, ctx->md5, ctx->buflen);
- } else if (ctx->state == DATA) {
- ctx->buflen = sizeof(ctx->buf);
- char pattern = arc4random() & 0xFF;
- memset(ctx->buf, pattern, ctx->buflen);
- } else if (ctx->state == CKSUM) {
- ctx->state = DONE;
- dispatch_source_cancel(ds);
- } else {
- assert(0);
- }
- ctx->offset = 0;
- }
- } else if (res == 0) {
- assert(ctx->state == DONE);
- assert(0);
- } else if (res == -1 && errno == EAGAIN) {
- DEBUG("%s: EAGAIN\n", ctx->label);
- } else {
- assert_errno("write", res >= 0);
- }
- });
- dispatch_source_set_cancel_handler(ds, ^{
- DEBUG("%s: close(%d)\n", ctx->label, wfd);
- int res = close(wfd);
- assert_errno("close", res == 0);
- completion();
- dispatch_release(ds);
- free(ctx);
- });
- dispatch_resume(ds);
- return ds;
-}
-
-dispatch_source_t
-create_reader(int rfd, dispatch_block_t completion)
-{
- dispatch_source_t ds;
- struct sock_context *ctx = calloc(1, sizeof(struct sock_context));
- assert(ctx);
-
- snprintf(ctx->label, sizeof(ctx->label), "reader.fd.%d", rfd);
- dispatch_queue_t queue = dispatch_queue_create(ctx->label, 0);
-
- ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, rfd, 0, queue);
- assert(ds);
- dispatch_release(queue);
-
- ctx->state = LENGTH;
- ctx->len = sizeof(ctx->len);
- ctx->buflen = sizeof(ctx->len);
- CC_MD5_Init(&ctx->md5ctx);
-
- dispatch_source_set_event_handler(ds, ^{
- DEBUG("%s: available %ld\n", ctx->label, dispatch_source_get_data(ds));
- ssize_t res;
- size_t rdsz = min(ctx->len, ctx->buflen);
- res = read(rfd, &ctx->buf[ctx->offset], rdsz);
- DEBUG("%s: read(%d,%p,%ld): %ld\n", ctx->label, rfd, &ctx->buf[ctx->offset], rdsz, res);
-
- // log unexpected data lengths...
- long expected = dispatch_source_get_data(ds);
- long actual = res;
- if (actual >= 0 && (actual != expected && actual != rdsz)) {
- fprintf(stderr, "%s: expected %ld, actual %ld (rdsz = %ld)\n", ctx->label, expected, actual, rdsz);
- }
-
- if (res > 0) {
- if (ctx->state == DATA) {
- CC_MD5_Update(&ctx->md5ctx, &ctx->buf[ctx->offset], res);
- ctx->len -= res;
- }
- ctx->offset += res;
- ctx->buflen -= res;
- if (ctx->buflen == 0 || ctx->len == 0) {
- if (ctx->state == LENGTH) {
- // buffer is full, interpret as uint32_t
- memcpy(&ctx->len, ctx->buf, sizeof(ctx->len));
- ctx->len = ntohl(ctx->len);
- ctx->buflen = sizeof(ctx->buf);
- ctx->state = DATA;
- } else if (ctx->state == DATA && ctx->len == 0) {
- CC_MD5_Final(ctx->md5, &ctx->md5ctx);
- ctx->state = CKSUM;
- ctx->len = CC_MD5_DIGEST_LENGTH;
- ctx->buflen = ctx->len;
- } else if (ctx->state == DATA) {
- ctx->buflen = sizeof(ctx->buf);
- } else if (ctx->state == CKSUM) {
- ctx->state = DONE;
- res = memcmp(ctx->buf, ctx->md5, sizeof(ctx->md5));
- if (res != 0) {
- DEBUG("%s: MD5 FAILURE\n", ctx->label);
- }
- assert(res == 0);
- }
- ctx->offset = 0;
- }
- } else if (res == 0) {
- assert(ctx->state == DONE);
- DEBUG("%s: EOF\n", ctx->label);
- dispatch_source_cancel(ds);
- } else {
- assert_errno("read", res >= 0);
- }
- });
- dispatch_source_set_cancel_handler(ds, ^{
- DEBUG("%s: close(%d)\n", ctx->label, rfd);
- int res = close(rfd);
- assert_errno("close", res == 0);
- completion();
- dispatch_release(ds);
- free(ctx);
- });
- dispatch_resume(ds);
- return ds;
-}
-
-void
-set_nonblock(int fd)
-{
- int res, flags;
- flags = fcntl(fd, F_GETFL);
-
- flags |= O_NONBLOCK;
- res = fcntl(fd, F_SETFL, flags);
- assert_errno("fcntl(F_SETFL,O_NONBLOCK)", res == 0);
-}
-
-void
-create_fifo(int *rfd, int *wfd)
-{
- int res;
- char *name;
-
- char path[MAXPATHLEN];
- strlcpy(path, "/tmp/fd_stress.fifo.XXXXXX", sizeof(path));
- name = mktemp(path);
-
- res = unlink(name);
-
- res = mkfifo(name, 0700);
- assert_errno(name, res == 0);
-
- *rfd = open(name, O_RDONLY | O_NONBLOCK);
- assert_errno(name, *rfd >= 0);
-
- *wfd = open(name, O_WRONLY | O_NONBLOCK);
- assert_errno(name, *wfd >= 0);
-}
-
-void
-create_pipe(int *rfd, int *wfd)
-{
- int res;
- int fildes[2];
-
- res = pipe(fildes);
- assert_errno("pipe", res == 0);
-
- *rfd = fildes[0];
- *wfd = fildes[1];
-
- set_nonblock(*rfd);
- set_nonblock(*wfd);
-}
-
-void
-create_server_socket(int *rfd, struct sockaddr_in *sa)
-{
- int res;
- int value;
- socklen_t salen = sizeof(*sa);
-
- memset(sa, 0, salen);
- sa->sin_len = salen;
- sa->sin_family = AF_INET;
- sa->sin_port = htons(12345);
- sa->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
-
- *rfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- assert_errno("socket", *rfd >= 0);
-
- value = 1;
- res = setsockopt(*rfd, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value));
- assert_errno("setsockopt(SO_REUSEADDR)", res == 0);
-
- value = 1;
- res = setsockopt(*rfd, SOL_SOCKET, SO_REUSEPORT, &value, sizeof(value));
- assert_errno("setsockopt(SO_REUSEPORT)", res == 0);
-
- res = bind(*rfd, (const struct sockaddr *)sa, salen);
- assert_errno("bind", res == 0);
-
- res = listen(*rfd, 128);
- assert_errno("listen", res == 0);
-}
-
-void
-create_client_socket(int *wfd, const struct sockaddr_in *sa)
-{
- int res;
-
- *wfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
- assert_errno("socket", *wfd >= 0);
-
- set_nonblock(*wfd);
-
- res = connect(*wfd, (const struct sockaddr *)sa, sa->sin_len);
- assert_errno("connect", res == 0 || errno == EINPROGRESS);
-}
-
-extern int optind;
-
-void
-usage(void)
-{
- fprintf(stderr, "usage: fd_stress [-d] iterations width\n");
- exit(1);
-}
-
-int
-main(int argc, char* argv[])
-{
- int serverfd;
- struct sockaddr_in sa;
- create_server_socket(&serverfd, &sa);
-
- int ch;
-
- while ((ch = getopt(argc, argv, "d")) != -1) {
- switch (ch) {
- case 'd':
- debug = 1;
- break;
- case '?':
- default:
- usage();
- break;
- }
- }
- argc -= optind;
- argv += optind;
-
- if (argc != 2) {
- usage();
- }
-
- size_t iterations = strtol(argv[0], NULL, 10);
- size_t width = strtol(argv[1], NULL, 10);
-
- if (iterations == 0 || width == 0) {
- usage();
- }
-
- fprintf(stdout, "pid %d\n", getpid());
-
- dispatch_group_t group;
- group = dispatch_group_create();
- assert(group);
-
-#if 0
- dispatch_queue_t queue = dispatch_queue_create("server", NULL);
-
- dispatch_source_t ds;
- ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, serverfd, 0, queue);
- assert(ds);
- dispatch_source_set_event_handler(ds, ^{
- int res;
- int fd;
- struct sockaddr peer;
- socklen_t peerlen;
-
- fd = accept(serverfd, &peer, &peerlen);
- assert_errno("accept", fd >= 0);
-
- set_nonblock(fd);
-
- char host[NI_MAXHOST], serv[NI_MAXSERV];
- host[0] = 0;
- serv[0] = 0;
- res = getnameinfo(&peer, peerlen, host, sizeof(host), serv, sizeof(serv), NI_NUMERICHOST|NI_NUMERICSERV);
- DEBUG("ACCEPTED %d (%s:%s)\n", fd, host, serv);
-
- create_reader(fd, ^{ dispatch_group_leave(group); });
- });
- dispatch_resume(ds);
-#endif
-
- size_t i;
- for (i = 1; i < iterations; ++i) {
- fprintf(stderr, "iteration %ld\n", i);
-
- size_t j;
- for (j = 0; j < width; ++j) {
- int rfd, wfd;
- dispatch_group_enter(group);
- create_pipe(&rfd, &wfd);
- DEBUG("PIPE %d %d\n", rfd, wfd);
- dispatch_source_t reader;
- reader = create_reader(rfd, ^{ dispatch_group_leave(group); });
- create_writer(wfd, ^{});
- }
-
-#if 0
- int clientfd;
- dispatch_group_enter(group);
- create_client_socket(&clientfd, &sa);
- DEBUG("CLIENT %d\n", clientfd);
- create_writer(clientfd, ^{});
-
- dispatch_group_enter(group);
- create_fifo(&rfd, &wfd);
- DEBUG("FIFO %d %d\n", rfd, wfd);
- create_writer(wfd, ^{});
- create_reader(rfd, ^{ dispatch_group_leave(group); });
-#endif
-
- dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
- }
- fprintf(stdout, "pid %d\n", getpid());
- dispatch_main();
-
- return 0;
-}
Deleted: branches/Lion/testing/func.c
===================================================================
--- branches/Lion/testing/func.c 2011-08-18 20:57:32 UTC (rev 204)
+++ branches/Lion/testing/func.c 2011-08-19 04:26:23 UTC (rev 205)
@@ -1,9 +0,0 @@
-extern "C" {
-void
-func(void)
-{
-}
-#ifdef __BLOCKS__
-void (^block)(void) = ^{ };
-#endif
-};
Deleted: branches/Lion/testing/harness.c
===================================================================
--- branches/Lion/testing/harness.c 2011-08-18 20:57:32 UTC (rev 204)
+++ branches/Lion/testing/harness.c 2011-08-19 04:26:23 UTC (rev 205)
@@ -1,130 +0,0 @@
-/*
- * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
- *
- * @APPLE_APACHE_LICENSE_HEADER_START@
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * @APPLE_APACHE_LICENSE_HEADER_END@
- */
-
-#include "config/config.h"
-
-#include <dispatch/dispatch.h>
-#include <assert.h>
-#include <spawn.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <signal.h>
-#include <mach/clock_types.h>
-
-#include "dispatch_test.h"
-
-extern char **environ;
-
-int
-main(int argc, char *argv[])
-{
- dispatch_source_t proc;
- dispatch_source_t sig;
- int res;
- pid_t pid;
-
- if (argc < 2) {
- fprintf(stderr, "usage: harness [...]\n");
- exit(1);
- }
-
- //
- // Start the test in a suspended state. Will send SIGCONT once ready.
- //
-
- posix_spawnattr_t attr;
- res = posix_spawnattr_init(&attr);
- assert(res == 0);
- res = posix_spawnattr_setflags(&attr, POSIX_SPAWN_START_SUSPENDED);
- assert(res == 0);
-
- int i;
- char** newargv = calloc(argc, sizeof(void*));
- for (i = 1; i < argc; ++i) {
- newargv[i-1] = argv[i];
- }
- newargv[i-1] = NULL;
-
- res = posix_spawnp(&pid, newargv[0], NULL, &attr, newargv, environ);
- if (res) {
- errno = res;
- perror(newargv[0]);
- exit(EXIT_FAILURE);
- }
- //fprintf(stderr, "pid = %d\n", pid);
- assert(pid > 0);
-
- //
- // Monitor the test process exit status.
- //
-
- dispatch_queue_t main_q = dispatch_get_main_queue();
-
- proc = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid, DISPATCH_PROC_EXIT, main_q);
- assert(proc);
-
- dispatch_source_set_event_handler(proc, ^{
- int status;
- int res2 = waitpid(pid, &status, 0);
- assert(res2 != -1);
- test_long("Process exited", WEXITSTATUS(status) | WTERMSIG(status), 0);
- exit(0);
- });
-
- dispatch_resume(proc);
-
- //
- // Forcibly stop currently running test after 30 second timeout.
- //
-
- uint64_t timeout = 30LL * NSEC_PER_SEC;
-
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeout), main_q, ^{
- kill(pid, SIGKILL);
- fprintf(stderr, "Terminating unresponsive process (%0.1lfs)\n", (double)timeout/NSEC_PER_SEC);
- });
-
- //
- // Control-c forcibly stops currently running test.
- //
-
- // Ignore the default signal handler so that dispatch can handle it.
- signal(SIGINT, SIG_IGN);
-
- sig = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGINT, 0, main_q);
- assert(sig);
-
- dispatch_source_set_event_handler(sig, ^{
- fprintf(stderr, "Terminating process due to signal\n");
- kill(pid, SIGKILL);
- });
- dispatch_resume(sig);
-
- //
- // Start the test.
- //
-
- kill(pid, SIGCONT);
-
- dispatch_main();
-
- return 0;
-}
Deleted: branches/Lion/testing/os_shims.h
===================================================================
--- branches/Lion/testing/os_shims.h 2011-08-18 20:57:32 UTC (rev 204)
+++ branches/Lion/testing/os_shims.h 2011-08-19 04:26:23 UTC (rev 205)
@@ -1,30 +0,0 @@
-/*
- * @APPLE_APACHE_LICENSE_HEADER_START@
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * @APPLE_APACHE_LICENSE_HEADER_END@
- */
-
-
-#ifndef __DISPATCH_TESTS_OS_SHIMS__
-#define __DISPATCH_TESTS_OS_SHIMS__
-
-#include "config/config.h"
-
-#include "shims/asprintf.h"
-#include "shims/arc4random.h"
-#include "shims/fgetln.h"
-
-#endif /* __DISPATCH_TESTS_OS_SHIMS__ */
-
Deleted: branches/Lion/testing/queue_finalizer.c
===================================================================
--- branches/Lion/testing/queue_finalizer.c 2011-08-18 20:57:32 UTC (rev 204)
+++ branches/Lion/testing/queue_finalizer.c 2011-08-19 04:26:23 UTC (rev 205)
@@ -1,64 +0,0 @@
-/*
- * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
- *
- * @APPLE_APACHE_LICENSE_HEADER_START@
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * @APPLE_APACHE_LICENSE_HEADER_END@
- */
-
-#include "config/config.h"
-
-#include <dispatch/dispatch.h>
-
-#include <unistd.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
-
-#include "dispatch_test.h"
-
-void *ctxt_magic;
-
-static void
-finalizer(void *ctxt)
-{
- test_ptr("finalizer ran", ctxt, ctxt_magic);
- test_stop();
-}
-
-int
-main(void) {
- test_start("Dispatch Queue Finalizer");
-
-#ifdef __LP64__
- ctxt_magic = (void*)((uintptr_t)arc4random() << 32 | arc4random());
-#else
- ctxt_magic = (void*)arc4random();
-#endif
-
- dispatch_queue_t q = dispatch_queue_create(NULL, NULL);
- test_ptr_notnull("dispatch_queue_new", q);
-
- dispatch_set_context(q, ctxt_magic);
-
- dispatch_set_finalizer_f(q, finalizer);
-
- dispatch_release(q);
-
- dispatch_main();
-
- return 0;
-}
Copied: branches/Lion/testing/shims.h (from rev 200, branches/Lion/testing/os_shims.h)
===================================================================
--- branches/Lion/testing/shims.h (rev 0)
+++ branches/Lion/testing/shims.h 2011-08-19 04:26:23 UTC (rev 205)
@@ -0,0 +1,30 @@
+/*
+ * @APPLE_APACHE_LICENSE_HEADER_START@
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * @APPLE_APACHE_LICENSE_HEADER_END@
+ */
+
+
+#ifndef __DISPATCH_TESTS_OS_SHIMS__
+#define __DISPATCH_TESTS_OS_SHIMS__
+
+#include "config/config.h"
+
+#include "shims/asprintf.h"
+#include "shims/arc4random.h"
+#include "shims/fgetln.h"
+
+#endif /* __DISPATCH_TESTS_OS_SHIMS__ */
+
Deleted: branches/Lion/testing/summarize.c
===================================================================
--- branches/Lion/testing/summarize.c 2011-08-18 20:57:32 UTC (rev 204)
+++ branches/Lion/testing/summarize.c 2011-08-19 04:26:23 UTC (rev 205)
@@ -1,98 +0,0 @@
-/*
- * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
- *
- * @APPLE_APACHE_LICENSE_HEADER_START@
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * @APPLE_APACHE_LICENSE_HEADER_END@
- */
-
-#include "os_shims.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-int
-has_prefix(const char* str, const char* prefix) {
- return (strncmp(str, prefix, strlen(prefix)) == 0);
-}
-
-int
-print_summary(FILE* f, long total, long pass, long fail) {
- fprintf(f, "Total: %ld\n", total);
- fprintf(f, "Passed: %ld (%0.0lf%%)\n", pass, ((double)pass / (double)total) * (double)100.0);
- fprintf(f, "Failed: %ld (%0.0lf%%)\n", fail, ((double)fail / (double)total) * (double)100.0);
- fprintf(f, "\n");
- return 0;
-}
-
-int main(int argc, char* argv[]) {
- if (argc > 1) {
- fprintf(stderr, "%s: usage: summarize\n", argv[0]);
- exit(1);
- }
-
- /*
- FILE* f = fopen(argv[1], "w");
- if (f == NULL) {
- perror(argv[1]);
- exit(1);
- }
- */
- FILE* f = stdout;
-
- fprintf(f, "\n==================================================\n");
- fprintf(f, "[SUMMARY] Test Summary\n");
- fprintf(f, "==================================================\n\n");
-
- size_t len;
- char* ln;
- long total = 0;
- long pass = 0;
- long fail = 0;
- long total_total = 0;
- long total_pass = 0;
- long total_fail = 0;
- for(;;) {
- ln = fgetln(stdin, &len);
- //if (ln) fprintf(stdout, "%.*s", (int)len, ln);
- if (ln == NULL || has_prefix(ln, "[TEST]")) {
- if (total) {
- print_summary(f, total, pass, fail);
- }
- total_total += total;
- total_pass += pass;
- total_fail += fail;
- total = 0;
- pass = 0;
- fail = 0;
- if (ln) {
- fprintf(f, "%.*s", (int)len, ln);
- } else {
- fprintf(f, "[TOTAL]\n");
- print_summary(f, total_total, total_pass, total_fail);
- break;
- }
- } else if (has_prefix(ln, "[PASS]")) {
- ++total;
- ++pass;
- } else if (has_prefix(ln, "[FAIL]")) {
- ++total;
- ++fail;
- }
- }
-
- return (total_fail ? EXIT_FAILURE : EXIT_SUCCESS);
-}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/libdispatch-changes/attachments/20110818/8fc8840e/attachment-0001.html>
More information about the libdispatch-changes
mailing list