From: Stephen D. <sd...@us...> - 2005-12-30 11:07:44
|
Update of /cvsroot/naviserver/naviserver/tests In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32584/tests Added Files: ns_cache.test Log Message: 2005-12-30 Stephen Deasey <sd...@us...> * include/ns.h: * nsd/nsd.h: * nsd/init.c: * nsd/proc.c: * nsd/cache.c: Global hash of cache names removed; access no longer available via Ns_CacheFind() or ns_cache_names. Caches have unique locking requirements and lifetimes which could be violated by e.g. running ns_cache_stats on a thread local cache as the thread is exiting. New Ns_CacheCreateEx() allows creating a cache which has both a size and a time limit. Caches with time limits are calculated differently. Previous behaviour was to periodically check the last access time of each cache entry and flush those which had expired. A busy cache could grow without bound. New behaviour is to check for expirey each time an entry is retrieved from the cache. All caches can (and should) be size limited. Timeouts may be specified per cache and per entry using new Ns_CacheSetValueExpires(). Ns_CacheFlush() now returns the number of entries flushed. New Ns_CacheStats() takes over from ns_cache_stats for non-Tcl caches. Stats are returned in Tcl "array get" format. Removed Ns_CacheMalloc(), Ns_CacheFree() and Ns_CacheName(). Move Tcl commands to tclcache.c. * nsd/Makefile: * nsd/tclcmds.c: * nsd/tclcache.c: * tests/ns_cache.test: Add new Tcl commands ns_cache_create, _eval, _append, _lappend, and _incr. (RFE# 1119257) Old commands ns_cache_names, _keys, _flush and _stats now only work on Tcl caches. ns_cache_size has been removed; the size is now reported along with other statistics. These commands now only work on Tcl caches. * tcl/cache.tcl: New ns_memoize and related commands which act just like ns_cache_eval but use the script as a key into the memoize cache. --- NEW FILE: ns_cache.test --- # # $Header: /cvsroot/naviserver/naviserver/tests/ns_cache.test,v 1.1 2005/12/30 11:07:34 sdeasey Exp $ # package require tcltest 2.2 namespace import -force ::tcltest::* eval ::tcltest::configure $argv ns_cache_create c1 1024 ns_cache_create c2 [expr 1024 * 1024] test cache-1.1 {basic syntax} -body { ns_cache_keys } -returnCodes error -result {wrong # args: should be "ns_cache_keys cache ?pattern?"} test cache-1.2 {basic syntax} -body { ns_cache_flush } -returnCodes error -result {wrong # args: should be "ns_cache_flush ?-glob? ?--? cache ?args?"} test cache-1.3 {basic syntax} -body { ns_cache_stats } -returnCodes error -result {wrong # args: should be "ns_cache_stats cache"} test cache-1.4 {basic syntax} -body { ns_cache_create } -returnCodes error -result {wrong # args: should be "ns_cache_create ?-ttl ttl? ?--? cache size"} test cache-1.5 {basic syntax} -body { ns_cache_eval } -returnCodes error -result {wrong # args: should be "ns_cache_eval ?-timeout timeout? ?-ttl ttl? ?--? cache key args"} test cache-1.6 {basic syntax} -body { ns_cache_incr } -returnCodes error -result {wrong # args: should be "ns_cache_incr ?-timeout timeout? ?-ttl ttl? ?--? cache key ?incr?"} test cache-1.7 {basic syntax} -body { ns_cache_append } -returnCodes error -result {wrong # args: should be "ns_cache_append ?-timeout timeout? ?-ttl ttl? ?--? cache key args"} test cache-1.8 {basic syntax} -body { ns_cache_lappend } -returnCodes error -result {wrong # args: should be "ns_cache_lappend ?-timeout timeout? ?-ttl ttl? ?--? cache key args"} test cache-1.9 {basic syntax} -body { ns_memoize } -returnCodes error -result {wrong # args: should be "?-timeout timeout? ?-ttl ttl? ?--? script ?args?"} test cache-2.1 {cache names} -body { lsort [ns_cache_names] } -result {c1 c2 ns:memoize} test cache-3.1 {cache create} -body { ns_cache_create -ttl 3 -- c3 1024 } -result {} test cache-4.1 {cache keys} -body { ns_cache_keys noexist } -returnCodes error -result {no such cache: noexist} test cache-4.2 {cache keys} -body { ns_cache_keys c1 } -result {} test cache-4.3 {cache keys} -body { ns_cache_eval c1 k1 {return a} ns_cache_keys c1 } -cleanup { ns_cache_flush c1 } -result k1 test cache-4.4 {cache keys} -body { ns_cache_eval c1 k1 {return a} ns_cache_eval c1 k2 {return a} lsort [ns_cache_keys c1] } -cleanup { ns_cache_flush c1 } -result {k1 k2} test cache-5.1 {cache flush} -body { ns_cache_flush c1 ns_cache_flush c1 } -result 0 test cache-5.2 {cache flush} -body { ns_cache_eval c1 k1 {return a} ns_cache_eval c1 k2 {return a} ns_cache_flush c1 } -result 2 test cache-5.3 {cache flush} -body { ns_cache_eval c1 k1 {return a} ns_cache_eval c1 k2 {return a} ns_cache_flush c1 k1 ns_cache_keys c1 } -cleanup { ns_cache_flush c1 } -result k2 test cache-5.4 {cache flush multiple keys} -body { ns_cache_eval c1 k1 {return a} ns_cache_eval c1 k2 {return a} ns_cache_flush c1 k1 k2 ns_cache_keys c1 } -cleanup { ns_cache_flush c1 } -result {} test cache-5.5 {cache flush glob keys} -body { ns_cache_eval c1 kx1 {return a} ns_cache_eval c1 kx2 {return a} ns_cache_eval c1 ky3 {return a} ns_cache_flush -glob c1 kx* ns_cache_keys c1 } -cleanup { ns_cache_flush c1 } -result ky3 test cache-5.6 {cache flush multiple glob keys} -body { ns_cache_eval c1 kx1 {return a} ns_cache_eval c1 kx2 {return a} ns_cache_eval c1 ky3 {return a} ns_cache_flush -glob c1 kx1 kx* ns_cache_keys c1 } -cleanup { ns_cache_flush c1 } -result ky3 test cache-6.1 {eval with args} -body { ns_cache_eval c1 k1 concat a b c } -cleanup { ns_cache_flush c1 } -result {a b c} test cache-6.2 {eval timeout switch} -body { ns_cache_eval -timeout 1 -- c1 k1 {return a} } -cleanup { ns_cache_flush c1 } -result a test cache-6.2 {eval ttl} -body { ns_cache_eval -ttl 1 -- c1 k1 {return a} after 1500 ns_cache_eval c1 k1 {return b} } -cleanup { ns_cache_flush c1 } -result b test cache-7.1 {cache stats} -body { array set stats [ns_cache_stats c1] lsort [array names stats] } -cleanup { unset -nocomplain stats } -result {entries flushed hitrate hits maxsize missed size} test cache-8.1 {cache incr} -body { ns_cache_incr c1 k1 } -cleanup { ns_cache_flush c1 } -result 1 test cache-8.2 {cache incr} -body { ns_cache_incr c1 k1 ns_cache_incr c1 k1 } -cleanup { ns_cache_flush c1 } -result 2 test cache-8.3 {cache incr: negative number} -body { ns_cache_incr c1 k1 -1 } -cleanup { ns_cache_flush c1 } -result -1 test cache-8.4 {cache incr: positive multiple} -body { ns_cache_incr c1 k1 2 } -cleanup { ns_cache_flush c1 } -result 2 test cache-9.1 {cache append} -body { ns_cache_append c1 k1 a ns_cache_append c1 k1 b } -cleanup { ns_cache_flush c1 } -result ab test cache-9.2 {cache append multiple values} -body { ns_cache_append c1 k1 a ns_cache_append c1 k1 b c } -cleanup { ns_cache_flush c1 } -result {abc} test cache-9.4 {cache lappend} -body { ns_cache_lappend c1 k1 a ns_cache_lappend c1 k1 b } -cleanup { ns_cache_flush c1 } -result {a b} test cache-9.5 {cache lappend multiple values} -body { ns_cache_lappend c1 k1 a ns_cache_lappend c1 k1 b c } -cleanup { ns_cache_flush c1 } -result {a b c} test cache-10.1 {memoize} -body { ns_memoize {return a} } -cleanup { ns_memoize_flush } -result a test cache-10.2 {memoize with multiple args} -body { ns_memoize return b } -cleanup { ns_memoize_flush } -result b test cache-10.3 {memoize timeout} -body { ns_memoize -timeout 2 -- {return c} } -cleanup { ns_memoize_flush } -result c test cache-10.4 {memoize flush} -body { ns_memoize {return d} ns_memoize_flush {return d} } -cleanup { ns_memoize_flush } -result 1 test cache-10.5 {memoize flush glob} -body { ns_memoize return e1 ns_memoize return e2 ns_memoize return f1 ns_memoize_flush {return e*} } -cleanup { ns_memoize_flush } -result 2 test ns_cache-11.1 {stability} -body { for {set i 0} {$i < 100} {incr i} { ns_thread begindetached " for {set j 0} {\$j < 1000} {incr j} { for {set x 0} {\$x < 10000} {incr x} {set a b} ns_cache_eval c1 k1 {return a} ns_thread yield } " } ns_thread begindetached { for {set j 0} {$j < 1000} {incr j} { for {set x 0} {$x < 10000} {incr x} {set a b} ns_cache_flush c1 ns_thread yield } } ns_log notice [ns_cache_stats c1] } -cleanup { unset -nocomplain i j ns_cache_flush c1 } -result {} cleanupTests |