Return-Path: Delivered-To: apmail-apr-cvs-archive@www.apache.org Received: (qmail 73899 invoked from network); 13 Mar 2004 12:34:51 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 13 Mar 2004 12:34:51 -0000 Received: (qmail 37760 invoked by uid 500); 13 Mar 2004 12:34:49 -0000 Delivered-To: apmail-apr-cvs-archive@apr.apache.org Received: (qmail 37662 invoked by uid 500); 13 Mar 2004 12:34:48 -0000 Mailing-List: contact cvs-help@apr.apache.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Reply-To: dev@apr.apache.org Delivered-To: mailing list cvs@apr.apache.org Received: (qmail 37648 invoked from network); 13 Mar 2004 12:34:48 -0000 Date: 13 Mar 2004 12:34:49 -0000 Message-ID: <20040313123449.73857.qmail@minotaur.apache.org> From: rbb@apache.org To: apr-cvs@apache.org Subject: cvs commit: apr/test testflock.h tryread.c .cvsignore Makefile.in test_apr.h testall.c testflock.c X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N rbb 2004/03/13 04:34:49 Modified: test .cvsignore Makefile.in test_apr.h testall.c testflock.c Added: test testflock.h tryread.c Log: Migrate testflock to unified test framework Revision Changes Path 1.49 +1 -0 apr/test/.cvsignore Index: .cvsignore =================================================================== RCS file: /home/cvs/apr/test/.cvsignore,v retrieving revision 1.48 retrieving revision 1.49 diff -u -r1.48 -r1.49 --- .cvsignore 9 Mar 2004 18:59:39 -0000 1.48 +++ .cvsignore 13 Mar 2004 12:34:49 -0000 1.49 @@ -72,6 +72,7 @@ testregex testmutexscope testtable +tryread testall testall.ncb testall.opt 1.149 +7 -7 apr/test/Makefile.in Index: Makefile.in =================================================================== RCS file: /home/cvs/apr/test/Makefile.in,v retrieving revision 1.148 retrieving revision 1.149 diff -u -r1.148 -r1.149 --- Makefile.in 13 Mar 2004 00:28:45 -0000 1.148 +++ Makefile.in 13 Mar 2004 12:34:49 -0000 1.149 @@ -11,7 +11,6 @@ # or with special parameters STDTEST_PORTABLE = \ - testflock@EXEEXT@ \ testsock@EXEEXT@ \ testlockperf@EXEEXT@ \ testshmproducer@EXEEXT@ \ @@ -36,7 +35,7 @@ LOCAL_LIBS=../lib@APR_LIBNAME@.la CLEAN_TARGETS = testfile.tmp mod_test.slo proc_child@EXEEXT@ occhild@EXEEXT@ \ -readchild@EXEEXT@ +readchild@EXEEXT@ tryread@EXEEXT@ CLEAN_SUBDIRS = internal INCDIR=../include @@ -55,14 +54,14 @@ fi; \ done -testflock@EXEEXT@: testflock.lo $(LOCAL_LIBS) - $(LINK_PROG) testflock.lo $(LOCAL_LIBS) $(ALL_LIBS) - occhild@EXEEXT@: occhild.lo $(LOCAL_LIBS) $(LINK_PROG) occhild.lo $(LOCAL_LIBS) $(ALL_LIBS) readchild@EXEEXT@: readchild.lo $(LOCAL_LIBS) $(LINK_PROG) readchild.lo $(LOCAL_LIBS) $(ALL_LIBS) + +tryread@EXEEXT@: tryread.lo $(LOCAL_LIBS) + $(LINK_PROG) tryread.lo $(LOCAL_LIBS) $(ALL_LIBS) proc_child@EXEEXT@: proc_child.lo $(LOCAL_LIBS) $(LINK_PROG) proc_child.lo $(LOCAL_LIBS) $(ALL_LIBS) @@ -117,10 +116,11 @@ testpoll.lo testlock.lo testsockopt.lo testpipe.lo testthread.lo \ testhash.lo testargs.lo testnames.lo testuser.lo testpath.lo \ testenv.lo testprocmutex.lo testrand2.lo testfnmatch.lo \ - testatomic.lo + testatomic.lo testflock.lo testall: $(TESTS) mod_test.la libmod_test.la occhild@EXEEXT@ \ - readchild@EXEEXT@ CuTest.lo proc_child@EXEEXT@ $(LOCAL_LIBS) + readchild@EXEEXT@ CuTest.lo proc_child@EXEEXT@ \ + tryread@EXEEXT@ $(LOCAL_LIBS) $(LINK_PROG) $(TESTS) CuTest.lo $(LOCAL_LIBS) $(ALL_LIBS) 1.47 +1 -0 apr/test/test_apr.h Index: test_apr.h =================================================================== RCS file: /home/cvs/apr/test/test_apr.h,v retrieving revision 1.46 retrieving revision 1.47 diff -u -r1.46 -r1.47 --- test_apr.h 13 Mar 2004 00:28:45 -0000 1.46 +++ test_apr.h 13 Mar 2004 12:34:49 -0000 1.47 @@ -61,6 +61,7 @@ CuSuite *testenv(void); CuSuite *testfnmatch(void); CuSuite *testatomic(void); +CuSuite *testflock(void); /* Assert that RV is an APR_SUCCESS value; else fail giving strerror * for RV and CONTEXT message. */ 1.52 +1 -0 apr/test/testall.c Index: testall.c =================================================================== RCS file: /home/cvs/apr/test/testall.c,v retrieving revision 1.51 retrieving revision 1.52 diff -u -r1.51 -r1.52 --- testall.c 13 Mar 2004 00:28:45 -0000 1.51 +++ testall.c 13 Mar 2004 12:34:49 -0000 1.52 @@ -72,6 +72,7 @@ {"testenv", testenv}, {"testfnmatch", testfnmatch}, {"testatomic", testatomic}, + {"testflock", testflock}, {"LastTest", NULL} }; 1.14 +64 -113 apr/test/testflock.c Index: testflock.c =================================================================== RCS file: /home/cvs/apr/test/testflock.c,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- testflock.c 13 Feb 2004 09:38:34 -0000 1.13 +++ testflock.c 13 Mar 2004 12:34:49 -0000 1.14 @@ -13,143 +13,94 @@ * limitations under the License. */ -/* - * USAGE - * - * Start one process, no args, and place it into the background. Start a - * second process with the "-r" switch to attempt a read on the file - * created by the first process. - * - * $ ./testflock & - * ...messages... - * $ ./testflock -r - * ...messages... - * - * The first process will sleep for 30 seconds while holding a lock. The - * second process will attempt to grab it (non-blocking) and fail. It - * will then grab it with a blocking scheme. When the first process' 30 - * seconds are up, it will exit (thus releasing its lock). The second - * process will acquire the lock, then exit. - */ - +#include "testflock.h" +#include "test_apr.h" #include "apr_pools.h" +#include "apr_thread_proc.h" #include "apr_file_io.h" -#include "apr_time.h" +#include "apr_file_info.h" #include "apr_general.h" -#include "apr_getopt.h" #include "apr_strings.h" -#include -#include - -const char *testfile = "testfile.tmp"; +/* XXX I'm sure there has to be a better way to do this ... */ +#ifdef WIN32 +#define EXTENSION ".exe" +#elif NETWARE +#define EXTENSION ".nlm" +#else +#define EXTENSION +#endif -static apr_pool_t *pool = NULL; -static void errmsg(const char *msg) +static int launch_reader(CuTest *tc) { - if (pool != NULL) - apr_pool_destroy(pool); - fprintf(stderr, msg); - exit(1); -} + int i; + apr_proc_t proc = {0}; + apr_procattr_t *procattr; + const char *args[2]; + apr_status_t rv; + apr_exit_why_e why; + int exitcode; -static void errmsg2(const char *msg, apr_status_t rv) -{ - char *newmsg; - char errstr[120]; + rv = apr_procattr_create(&procattr, p); + apr_assert_success(tc, "Couldn't create procattr", rv); + + rv = apr_procattr_io_set(procattr, APR_NO_PIPE, APR_NO_PIPE, + APR_NO_PIPE); + apr_assert_success(tc, "Couldn't set io in procattr", rv); + + rv = apr_procattr_error_check_set(procattr, 1); + apr_assert_success(tc, "Couldn't set error check in procattr", rv); + + args[0] = "tryread" EXTENSION; + args[1] = NULL; + rv = apr_proc_create(&proc, "./tryread" EXTENSION, args, NULL, procattr, p); + apr_assert_success(tc, "Couldn't launch program", rv); - apr_strerror(rv, errstr, sizeof errstr); - newmsg = apr_psprintf(pool, "%s: %s (%d)\n", - msg, errstr, rv); - errmsg(newmsg); - exit(1); + CuAssert(tc, "wait for child process", + apr_proc_wait(&proc, &exitcode, &why, APR_WAIT) == APR_CHILD_DONE); + + CuAssert(tc, "child terminated normally", why == APR_PROC_EXIT); + return exitcode; } -static void do_read(void) +static void test_withlock(CuTest *tc) { apr_file_t *file; - apr_status_t status; + apr_status_t rv; + int code; + + rv = apr_file_open(&file, TESTFILE, APR_WRITE|APR_CREATE, + APR_OS_DEFAULT, p); + apr_assert_success(tc, "Could not create file.", rv); + CuAssertPtrNotNull(tc, file); + + rv = apr_file_lock(file, APR_FLOCK_EXCLUSIVE); + apr_assert_success(tc, "Could not lock the file.", rv); + CuAssertPtrNotNull(tc, file); - if (apr_file_open(&file, testfile, APR_WRITE, - APR_OS_DEFAULT, pool) != APR_SUCCESS) - errmsg("Could not open test file.\n"); - printf("Test file opened.\n"); - - status = apr_file_lock(file, APR_FLOCK_EXCLUSIVE | APR_FLOCK_NONBLOCK); - if (!APR_STATUS_IS_EAGAIN(status)) { - char msg[200]; - errmsg(apr_psprintf(pool, "Expected APR_EAGAIN. Got %d: %s.\n", - status, apr_strerror(status, msg, sizeof(msg)))); - } - printf("First attempt: we were properly locked out.\nWaiting for lock..."); - fflush(stdout); - - if (apr_file_lock(file, APR_FLOCK_EXCLUSIVE) != APR_SUCCESS) - errmsg("Could not establish lock on test file."); - printf(" got it.\n"); + code = launch_reader(tc); + CuAssertIntEquals(tc, FAILED_READ, code); (void) apr_file_close(file); - printf("Exiting.\n"); } -static void do_write(void) +static void test_withoutlock(CuTest *tc) { apr_file_t *file; apr_status_t rv; - - if (apr_file_open(&file, testfile, APR_WRITE|APR_CREATE, APR_OS_DEFAULT, - pool) != APR_SUCCESS) - errmsg("Could not create file.\n"); - printf("Test file created.\n"); - - if ((rv = apr_file_lock(file, APR_FLOCK_EXCLUSIVE)) != APR_SUCCESS) - errmsg2("Could not lock the file", rv); - printf("Lock created.\nSleeping..."); - fflush(stdout); - - apr_sleep(apr_time_from_sec(30)); - - (void) apr_file_close(file); - printf(" done.\nExiting.\n"); + int code; + + code = launch_reader(tc); + CuAssertIntEquals(tc, SUCCESSFUL_READ, code); } -int main(int argc, const char * const *argv) +CuSuite *testflock(void) { - int reader = 0; - apr_status_t status; - char optchar; - const char *optarg; - apr_getopt_t *opt; - - if (apr_initialize() != APR_SUCCESS) - errmsg("Could not initialize APR.\n"); - atexit(apr_terminate); - - if (apr_pool_create(&pool, NULL) != APR_SUCCESS) - errmsg("Could not create global pool.\n"); - - if (apr_getopt_init(&opt, pool, argc, argv) != APR_SUCCESS) - errmsg("Could not parse options.\n"); - - while ((status = apr_getopt(opt, "rf:", &optchar, &optarg)) == APR_SUCCESS) { - if (optchar == 'r') - ++reader; - else if (optchar == 'f') - testfile = optarg; - } - if (status != APR_SUCCESS && status != APR_EOF) { - char msgbuf[80]; - - fprintf(stderr, "error: %s\n", - apr_strerror(status, msgbuf, sizeof msgbuf)); - exit(1); - } - - if (reader) - do_read(); - else - do_write(); + CuSuite *suite = CuSuiteNew("Flock"); + + SUITE_ADD_TEST(suite, test_withlock); + SUITE_ADD_TEST(suite, test_withoutlock); - return 0; + return suite; } 1.1 apr/test/testflock.h Index: testflock.h =================================================================== /* Copyright 2000-2004 The Apache Software Foundation * * 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. */ #ifndef TESTFLOCK #define TESTFLOCK #define TESTFILE "data/testfile.lock" #define FAILED_READ 0 #define SUCCESSFUL_READ 1 #define UNEXPECTED_ERROR 2 #endif 1.1 apr/test/tryread.c Index: tryread.c =================================================================== /* Copyright 2000-2004 The Apache Software Foundation * * 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. */ /* * USAGE * * Start one process, no args, and place it into the background. Start a * second process with the "-r" switch to attempt a read on the file * created by the first process. * * $ ./testflock & * ...messages... * $ ./testflock -r * ...messages... * * The first process will sleep for 30 seconds while holding a lock. The * second process will attempt to grab it (non-blocking) and fail. It * will then grab it with a blocking scheme. When the first process' 30 * seconds are up, it will exit (thus releasing its lock). The second * process will acquire the lock, then exit. */ #include "testflock.h" #include "apr_pools.h" #include "apr_file_io.h" #include "apr_general.h" int main(int argc, const char * const *argv) { apr_file_t *file; apr_status_t status; apr_pool_t *p; apr_initialize(); apr_pool_create(&p, NULL); if (apr_file_open(&file, TESTFILE, APR_WRITE, APR_OS_DEFAULT, p) != APR_SUCCESS) { exit(UNEXPECTED_ERROR); } status = apr_file_lock(file, APR_FLOCK_EXCLUSIVE | APR_FLOCK_NONBLOCK); if (status == APR_SUCCESS) { exit(SUCCESSFUL_READ); } if (APR_STATUS_IS_EAGAIN(status)) { exit(FAILED_READ); } exit(UNEXPECTED_ERROR); }