From 067eeb5f497179a9edbfe83bc7f1fb790dd2a9cc Mon Sep 17 00:00:00 2001 Message-Id: <067eeb5f497179a9edbfe83bc7f1fb790dd2a9cc.1730415415.git.mdw@distorted.org.uk> From: Mark Wooding Date: Sat, 24 May 2008 18:13:47 +0100 Subject: [PATCH] Fix random_id(). Oops. Organization: Straylight/Edgeware From: Richard Kettlewell Slightly more verbose tests. --- lib/random.c | 2 +- server/schedule.c | 13 ++++++------- server/schedule.h | 4 ++-- server/server.c | 2 +- tests/schedule.py | 10 ++++++++++ 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/random.c b/lib/random.c index 21d0b09..69e1409 100644 --- a/lib/random.c +++ b/lib/random.c @@ -83,7 +83,7 @@ char *random_id(void) { char id[128]; random_get(words, sizeof words); - basen(words, 4, id, sizeof id, 62); + basen(words, sizeof words / sizeof *words, id, sizeof id, 62); return xstrdup(id); } diff --git a/server/schedule.c b/server/schedule.c index 7776c3c..c2cdb6d 100644 --- a/server/schedule.c +++ b/server/schedule.c @@ -249,8 +249,6 @@ void schedule_init(ev_source *ev) { /** @brief Create a scheduled event * @param ev Event loop * @param actiondata Action data - * - * The caller should set the timeout themselves. */ static int schedule_add_tid(const char *id, struct kvp *actiondata, @@ -262,7 +260,8 @@ static int schedule_add_tid(const char *id, k.data = (void *)id; k.size = strlen(id); switch(err = trackdb_scheduledb->put(trackdb_scheduledb, tid, &k, - encode_data(&d, actiondata), 0)) { + encode_data(&d, actiondata), + DB_NOOVERWRITE)) { case 0: break; case DB_LOCK_DEADLOCK: @@ -285,10 +284,10 @@ static int schedule_add_tid(const char *id, * is not allowed to perform them or if they are scheduled for a time * in the past. */ -char *schedule_add(ev_source *ev, - struct kvp *actiondata) { +const char *schedule_add(ev_source *ev, + struct kvp *actiondata) { int e, n; - char *id; + const char *id; struct timeval when; /* TODO: handle recurring events */ @@ -314,7 +313,7 @@ char *schedule_add(ev_source *ev, id = random_id(); WITH_TRANSACTION(schedule_add_tid(id, actiondata, tid)); } while(e == DB_KEYEXIST); - ev_timeout(ev, 0/*handlep*/, &when, schedule_trigger, id); + ev_timeout(ev, 0/*handlep*/, &when, schedule_trigger, (void *)id); return id; } diff --git a/server/schedule.h b/server/schedule.h index d68bafc..0c7efa1 100644 --- a/server/schedule.h +++ b/server/schedule.h @@ -22,8 +22,8 @@ #define SCHEDULE_H void schedule_init(ev_source *ev); -char *schedule_add(ev_source *ev, - struct kvp *actiondata); +const char *schedule_add(ev_source *ev, + struct kvp *actiondata); int schedule_del(const char *id); struct kvp *schedule_get(const char *id); char **schedule_list(int *neventsp); diff --git a/server/server.c b/server/server.c index e8e6a68..5df2d33 100644 --- a/server/server.c +++ b/server/server.c @@ -1567,7 +1567,7 @@ static int c_schedule_add(struct conn *c, char **vec, int nvec) { struct kvp *actiondata = 0; - char *id; + const char *id; /* Standard fields */ kvp_set(&actiondata, "who", c->who); diff --git a/tests/schedule.py b/tests/schedule.py index a3feaa8..ac0b438 100755 --- a/tests/schedule.py +++ b/tests/schedule.py @@ -31,6 +31,7 @@ def test(): print " waiting for nothing to be playing" while c.playing() is not None: time.sleep(1) + print " ." now = int(time.time()) track = "%s/Joe Bloggs/First Album/05:Fifth track.ogg" % dtest.tracks print " scheduling a track for the future" @@ -45,6 +46,7 @@ def test(): p = c.playing() while p is None and waited < 10: time.sleep(1) + print " ." waited += 1 p = c.playing() assert waited < 10, "checking track played within a reasonable period" @@ -54,6 +56,7 @@ def test(): print " waiting for nothing to be playing" while c.playing() is not None: time.sleep(1) + print " ." print " scheduling an enable-random for the future" now = int(time.time()) c.schedule_add(now + 4, "normal", "set-global", "random-play", "yes") @@ -67,6 +70,7 @@ def test(): p = c.playing() while p is None and waited < 10: time.sleep(1) + print " ." waited += 1 p = c.playing() assert waited < 10, "checking a track played within a reasonable period" @@ -76,6 +80,7 @@ def test(): print " waiting for nothing to be playing" while c.playing() is not None: time.sleep(1) + print " ." print " scheduling track to play later via command line" now = int(time.time()) dtest.command(["disorder", @@ -96,6 +101,7 @@ def test(): p = c.playing() while p is None and waited < 10: time.sleep(1) + print " ." waited += 1 p = c.playing() assert waited < 10, "checking track played within a reasonable period" @@ -105,6 +111,7 @@ def test(): print " waiting for nothing to be playing" while c.playing() is not None: time.sleep(1) + print " ." print " scheduling an enable-random for later via command line" now = int(time.time()) dtest.command(["disorder", @@ -126,6 +133,7 @@ def test(): p = c.playing() while p is None and waited < 10: time.sleep(1) + print " ." waited += 1 p = c.playing() assert waited < 10, "checking a track played within a reasonable period" @@ -135,6 +143,7 @@ def test(): print " waiting for nothing to be playing" while c.playing() is not None: time.sleep(1) + print " ." print " scheduling a track for the future" now = int(time.time()) c.schedule_add(now + 4, "normal", "play", track) @@ -152,6 +161,7 @@ def test(): p = c.playing() while p is None and waited < 10: time.sleep(1) + print " ." waited += 1 p = c.playing() assert p is None, "checking deleted scheduled event did not run" -- [mdw]