tdb2: test lock timeout plugin code.
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 21 Apr 2011 04:04:13 +0000 (13:34 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 21 Apr 2011 04:04:13 +0000 (13:34 +0930)
ccan/tdb2/test/external-agent.c
ccan/tdb2/test/external-agent.h
ccan/tdb2/test/run-locktimeout.c [new file with mode: 0644]

index 081b8500a0fb206ab2ec239e94d9bb333f399516..055b5de736f6f04334fd990dc2409d1630650ff6 100644 (file)
@@ -124,6 +124,10 @@ static enum agent_return do_operation(enum operation op, const char *name)
                ret = tdb_close(tdb) == 0 ? SUCCESS : OTHER_FAILURE;
                tdb = NULL;
                break;
+       case SEND_SIGNAL:
+               /* We do this async */
+               ret = SUCCESS;
+               break;
        default:
                ret = OTHER_FAILURE;
        }
@@ -175,6 +179,13 @@ struct agent *prepare_external_agent(void)
                if (write(response[1], &result, sizeof(result))
                    != sizeof(result))
                        err(1, "Writing response");
+               if (name[0] == SEND_SIGNAL) {
+                       struct timeval ten_ms;
+                       ten_ms.tv_sec = 0;
+                       ten_ms.tv_usec = 10000;
+                       select(0, NULL, NULL, NULL, &ten_ms);
+                       kill(getppid(), SIGUSR1);
+               }
        }
        exit(0);
 }
@@ -225,6 +236,7 @@ const char *operation_name(enum operation op)
        case TRANSACTION_START: return "TRANSACTION_START";
        case TRANSACTION_COMMIT: return "TRANSACTION_COMMIT";
        case NEEDS_RECOVERY: return "NEEDS_RECOVERY";
+       case SEND_SIGNAL: return "SEND_SIGNAL";
        case CLOSE: return "CLOSE";
        }
        return "**INVALID**";
index a2f630adf5dc80bbd79ce7097ac70b87eb20c2e4..27316dde22ded3ec856cac317db4f045c3ef5836 100644 (file)
@@ -12,6 +12,7 @@ enum operation {
        TRANSACTION_COMMIT,
        NEEDS_RECOVERY,
        CHECK,
+       SEND_SIGNAL,
        CLOSE,
 };
 
diff --git a/ccan/tdb2/test/run-locktimeout.c b/ccan/tdb2/test/run-locktimeout.c
new file mode 100644 (file)
index 0000000..d44713f
--- /dev/null
@@ -0,0 +1,192 @@
+#include <ccan/tdb2/tdb.c>
+#include <ccan/tdb2/open.c>
+#include <ccan/tdb2/free.c>
+#include <ccan/tdb2/lock.c>
+#include <ccan/tdb2/io.c>
+#include <ccan/tdb2/hash.c>
+#include <ccan/tdb2/transaction.c>
+#include <ccan/tdb2/check.c>
+#include <ccan/tap/tap.h>
+#include "logging.h"
+#include "external-agent.h"
+
+#undef alarm
+#define alarm fast_alarm
+
+/* Speed things up by doing things in milliseconds. */
+static unsigned int fast_alarm(unsigned int milli_seconds)
+{
+       struct itimerval it;
+
+       it.it_interval.tv_sec = it.it_interval.tv_usec = 0;
+       it.it_value.tv_sec = milli_seconds / 1000;
+       it.it_value.tv_usec = milli_seconds * 1000;
+       setitimer(ITIMER_REAL, &it, NULL);
+       return 0;
+}
+
+#define CatchSignal(sig, handler) signal((sig), (handler))
+
+static void do_nothing(int signum)
+{
+}
+
+/* This example code is taken from SAMBA, so try not to change it. */
+static struct flock flock_struct;
+
+/* Return a value which is none of v1, v2 or v3. */
+static inline short int invalid_value(short int v1, short int v2, short int v3)
+{
+       short int try = (v1+v2+v3)^((v1+v2+v3) << 16);
+       while (try == v1 || try == v2 || try == v3)
+               try++;
+       return try;
+}
+
+/* We invalidate in as many ways as we can, so the OS rejects it */
+static void invalidate_flock_struct(int signum)
+{
+       flock_struct.l_type = invalid_value(F_RDLCK, F_WRLCK, F_UNLCK);
+       flock_struct.l_whence = invalid_value(SEEK_SET, SEEK_CUR, SEEK_END);
+       flock_struct.l_start = -1;
+       /* A large negative. */
+       flock_struct.l_len = (((off_t)1 << (sizeof(off_t)*CHAR_BIT - 1)) + 1);
+}
+
+static int timeout_lock(int fd, int rw, off_t off, off_t len, bool waitflag,
+                       void *_timeout)
+{
+       int ret, saved_errno = errno;
+       unsigned int timeout = *(unsigned int *)_timeout;
+
+       flock_struct.l_type = rw;
+       flock_struct.l_whence = SEEK_SET;
+       flock_struct.l_start = off;
+       flock_struct.l_len = len;
+
+       CatchSignal(SIGALRM, invalidate_flock_struct);
+       alarm(timeout);
+
+       for (;;) {
+               if (waitflag)
+                       ret = fcntl(fd, F_SETLKW, &flock_struct);
+               else
+                       ret = fcntl(fd, F_SETLK, &flock_struct);
+
+               if (ret == 0)
+                       break;
+
+               /* Not signalled?  Something else went wrong. */
+               if (flock_struct.l_len == len) {
+                       if (errno == EAGAIN || errno == EINTR)
+                               continue;
+                       saved_errno = errno;
+                       break;
+               } else {
+                       saved_errno = EINTR;
+                       break;
+               }
+       }
+
+       alarm(0);
+       errno = saved_errno;
+       return ret;
+}
+
+static int tdb_chainlock_with_timeout_internal(struct tdb_context *tdb,
+                                              TDB_DATA key,
+                                              unsigned int timeout,
+                                              int rw_type)
+{
+       union tdb_attribute locking;
+       enum TDB_ERROR ecode;
+
+       if (timeout) {
+               locking.base.attr = TDB_ATTRIBUTE_FLOCK;
+               ecode = tdb_get_attribute(tdb, &locking);
+               if (ecode != TDB_SUCCESS)
+                       return ecode;
+
+               /* Replace locking function with our own. */
+               locking.flock.data = &timeout;
+               locking.flock.lock = timeout_lock;
+
+               ecode = tdb_set_attribute(tdb, &locking);
+               if (ecode != TDB_SUCCESS)
+                       return ecode;
+       }
+       if (rw_type == F_RDLCK)
+               ecode = tdb_chainlock_read(tdb, key);
+       else
+               ecode = tdb_chainlock(tdb, key);
+
+       if (timeout) {
+               tdb_unset_attribute(tdb, TDB_ATTRIBUTE_FLOCK);
+       }
+       return ecode;
+}
+
+int main(int argc, char *argv[])
+{
+       unsigned int i;
+       struct tdb_context *tdb;
+       TDB_DATA key = tdb_mkdata("hello", 5);
+       int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
+                       TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
+       struct agent *agent;
+
+       plan_tests(sizeof(flags) / sizeof(flags[0]) * 15);
+
+       agent = prepare_external_agent();
+
+       for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
+               enum TDB_ERROR ecode;
+               tdb = tdb_open("run-locktimeout.tdb", flags[i],
+                              O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
+               if (!ok1(tdb))
+                       break;
+
+               /* Simple cases: should succeed. */ 
+               ecode = tdb_chainlock_with_timeout_internal(tdb, key, 20,
+                                                           F_RDLCK);
+               ok1(ecode == TDB_SUCCESS);
+               ok1(tap_log_messages == 0);
+
+               tdb_chainunlock_read(tdb, key);
+               ok1(tap_log_messages == 0);
+
+               ecode = tdb_chainlock_with_timeout_internal(tdb, key, 20,
+                                                           F_WRLCK);
+               ok1(ecode == TDB_SUCCESS);
+               ok1(tap_log_messages == 0);
+
+               tdb_chainunlock(tdb, key);
+               ok1(tap_log_messages == 0);
+
+               /* OK, get agent to start transaction, then we should time out. */
+               ok1(external_agent_operation(agent, OPEN, "run-locktimeout.tdb")
+                   == SUCCESS);
+               ok1(external_agent_operation(agent, TRANSACTION_START, "") 
+                   == SUCCESS);
+               ecode = tdb_chainlock_with_timeout_internal(tdb, key, 20,
+                                                           F_WRLCK);
+               ok1(ecode == TDB_ERR_LOCK);
+               ok1(tap_log_messages == 0);
+
+               /* Even if we get a different signal, should be fine. */
+               CatchSignal(SIGUSR1, do_nothing);
+               external_agent_operation(agent, SEND_SIGNAL, "");
+               ecode = tdb_chainlock_with_timeout_internal(tdb, key, 20,
+                                                           F_WRLCK);
+               ok1(ecode == TDB_ERR_LOCK);
+               ok1(tap_log_messages == 0);
+
+               ok1(external_agent_operation(agent, TRANSACTION_COMMIT, "")
+                   == SUCCESS);
+               ok1(external_agent_operation(agent, CLOSE, "")
+                   == SUCCESS);
+               tdb_close(tdb);
+       }
+       free_external_agent(agent);
+       return exit_status();
+}