[memcached] bradfitz, r331: more tests,
and remove duplicate code in...
commits at code.sixapart.com
commits at code.sixapart.com
Mon Sep 4 02:33:14 UTC 2006
more tests, and remove duplicate code in memcached.c dealing with
ignore the result of an assoc_find if the item has expired.
move that all to get_item
U trunk/server/Makefile.am
U trunk/server/memcached.c
A trunk/server/test/expirations.t
U trunk/server/test/getset.t
U trunk/server/test/lib/MemcachedTest.pm
Modified: trunk/server/Makefile.am
===================================================================
--- trunk/server/Makefile.am 2006-09-04 02:28:11 UTC (rev 330)
+++ trunk/server/Makefile.am 2006-09-04 02:33:13 UTC (rev 331)
@@ -5,8 +5,11 @@
SUBDIRS = doc
DIST_DIRS = scripts
-EXTRA_DIST = doc scripts TODO
+EXTRA_DIST = doc scripts TODO test
+# FIXME: how to run Perl tests from automake?
+#TESTS = test/getset.t
+
AM_CFLAGS=-DNDEBUG
dist-hook:
Modified: trunk/server/memcached.c
===================================================================
--- trunk/server/memcached.c 2006-09-04 02:28:11 UTC (rev 330)
+++ trunk/server/memcached.c 2006-09-04 02:33:13 UTC (rev 331)
@@ -96,6 +96,23 @@
settings.factor = 1.25;
settings.chunk_size = 48; /* space for a modest key and value */
}
+/* wrapper around assoc_find which does the lazy expiration/deletion logic */
+item *get_item(char *key) {
+ item *it = assoc_find(key);
+ if (it && (it->it_flags & ITEM_DELETED)) {
+ it = 0;
+ }
+ if (it && settings.oldest_live && settings.oldest_live <= current_time &&
+ it->time <= settings.oldest_live) {
+ item_unlink(it);
+ it = 0;
+ }
+ if (it && it->exptime && it->exptime <= current_time) {
+ item_unlink(it);
+ it = 0;
+ }
+ return it;
+}
/*
* Adds a message header to a connection.
*
@@ -722,7 +739,6 @@
char key[251];
int res;
char *ptr;
- rel_time_t now = current_time;
res = sscanf(command, "%*s %250s %u\n", key, &delta);
if (res!=2 || strlen(key)==0 ) {
out_string(c, "CLIENT_ERROR bad command line format");
@@ -740,14 +756,7 @@
return;
}
}
- it = assoc_find(key);
- if (it && (it->it_flags & ITEM_DELETED)) {
- it = 0;
- }
- if (it && it->exptime && it->exptime < now) {
- item_unlink(it);
- it = 0;
- }
+ it = get_item(key);
if (!it) {
out_string(c, "NOT_FOUND");
return;
@@ -809,19 +818,7 @@
while(sscanf(start, " %250s%n", key, &next) >= 1) {
start+=next;
stats.get_cmds++;
- it = assoc_find(key);
- if (it && (it->it_flags & ITEM_DELETED)) {
- it = 0;
- }
- if (settings.oldest_live && settings.oldest_live <= now &&
- it && it->time <= settings.oldest_live) {
- item_unlink(it);
- it = 0;
- }
- if (it && it->exptime && it->exptime < now) {
- item_unlink(it);
- it = 0;
- }
+ it = get_item(key);
if (it) {
if (i >= c->isize) {
item **new_list = realloc(c->ilist, sizeof(item *)*c->isize*2);
@@ -885,7 +882,7 @@
}
}
res = sscanf(command, "%*s %250s %ld", key, &exptime);
- it = assoc_find(key);
+ it = get_item(key);
if (!it) {
out_string(c, "NOT_FOUND");
return;
@@ -1986,4 +1983,4 @@
if (daemonize)
remove_pidfile(pid_file);
return 0;
-}
\ No newline at end of file
+}
Added: trunk/server/test/expirations.t
===================================================================
--- trunk/server/test/expirations.t 2006-09-04 02:28:11 UTC (rev 330)
+++ trunk/server/test/expirations.t 2006-09-04 02:33:13 UTC (rev 331)
@@ -0,0 +1,28 @@
+#!/usr/bin/perl
+
+use strict;
+use Test::More tests => 5;
+use FindBin qw($Bin);
+use lib "$Bin/lib";
+use MemcachedTest;
+
+my $server = new_memcached();
+my $sock = $server->sock;
+
+print $sock "set foo 123 1 6\r\nfooval\r\n";
+is(scalar <$sock>, "STORED\r\n", "stored foo");
+
+print $sock "get foo\n";
+is(scalar <$sock>, "VALUE foo 123 6\r\n", "got FOO value");
+is(scalar <$sock>, "fooval\r\n", "got fooval");
+is(scalar <$sock>, "END\r\n", "got END");
+sleep(1.2);
+print $sock "get foo\n";
+is(scalar <$sock>, "END\r\n", "got END (no value)");
+
+
+
+
+
+
+
Property changes on: trunk/server/test/expirations.t
___________________________________________________________________
Name: svn:executable
+ *
Modified: trunk/server/test/getset.t
===================================================================
--- trunk/server/test/getset.t 2006-09-04 02:28:11 UTC (rev 330)
+++ trunk/server/test/getset.t 2006-09-04 02:33:13 UTC (rev 331)
@@ -1,7 +1,7 @@
#!/usr/bin/perl
use strict;
-use Test::More tests => 1;
+use Test::More tests => 20;
use FindBin qw($Bin);
use lib "$Bin/lib";
use MemcachedTest;
Modified: trunk/server/test/lib/MemcachedTest.pm
===================================================================
--- trunk/server/test/lib/MemcachedTest.pm 2006-09-04 02:28:11 UTC (rev 330)
+++ trunk/server/test/lib/MemcachedTest.pm 2006-09-04 02:33:13 UTC (rev 331)
@@ -6,8 +6,13 @@
use Carp qw(croak);
use vars qw(@EXPORT);
- at EXPORT = qw(new_memcached);
+ at EXPORT = qw(new_memcached sleep);
+sub sleep {
+ my $n = shift;
+ select undef, undef, undef, $n;
+}
+
sub free_port {
my $sock;
my $port;
More information about the memcached-commits
mailing list