Make WordPress Core

Changeset 5 in tests


Ignore:
Timestamp:
09/20/2007 01:06:28 AM (18 years ago)
Author:
tellyworth
Message:

fix cache tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wp-testcase/test_includes_cache.php

    r1 r5  
    22
    33class TestObjectCache extends WPTestCase {
     4    var $cache = NULL;
    45
    56    function setUp() {
    6         $this->init_cache();
     7        // create two cache objects with a shared cache dir
     8        // this simulates a typical cache situation, two separate requests interacting
     9        $this->cache1 =& $this->init_cache();
     10        $this->cache2 =& $this->init_cache();
    711    }
    812
     
    1115    }
    1216
    13     function init_cache() {
    14         wp_cache_init();
    15         $GLOBALS['wp_object_cache']->cache_enabled = true;
    16         $GLOBALS['wp_object_cache']->cache_dir = '/tmp/';
     17    function &init_cache() {
     18        $cache = new WP_Object_cache();
     19        $cache->cache_enabled = true;
     20        $cache->cache_dir = '/tmp/';
     21        return $cache;
    1722    }
    1823
    1924    function test_miss() {
    20         $this->assertEquals(NULL, wp_cache_get(rand_str()));
     25        $this->assertEquals(NULL, $this->cache1->get(rand_str()));
    2126    }
    2227
     
    2530        $val = rand_str();
    2631
    27         wp_cache_add($key, $val);
    28         $this->assertEquals($val, wp_cache_get($key));
     32        $this->cache1->add($key, $val);
     33        $this->cache1->save();
     34        $this->assertEquals($val, $this->cache2->get($key));
     35        $this->assertEquals($val, $this->cache1->get($key));
    2936    }
    3037
     
    3340        $val = rand_str();
    3441
    35         wp_cache_add($key, $val);
    36         wp_cache_flush();
    37         $this->assertEquals(NULL, wp_cache_get($key));
     42        $this->cache1->add($key, $val);
     43        $this->cache1->save();
     44        // item is visible to both cache objects
     45        $this->assertEquals($val, $this->cache1->get($key));
     46        $this->cache1->flush();
     47        // flush affects both cache objects
     48        $this->assertEquals(NULL, $this->cache1->get($key));
     49        $this->assertEquals(NULL, $this->cache2->get($key));
    3850    }
    3951
    4052    function test_not_expired() {
    41 #       $this->markTestSkipped();
    4253        // this fails - looks like the object cache doesn't expire
    4354        $key = rand_str();
     
    4556
    4657        // expires in 1 second
    47         wp_cache_add($key, $val, '', 1);
    48         wp_cache_close();
    49         $this->init_cache();
     58        // nb: add() accepts an $expire parameter, but it doesn't work
     59        // so we have to set the expiration value for the whole cache
     60        // http://trac.wordpress.org/ticket/4179
     61        $this->cache1->expiration_time = 3; # seconds
     62        $this->cache2->expiration_time = 3;
     63        $this->assertTrue($this->cache1->add($key, $val));
     64        $this->assertTrue($this->cache1->save());
     65
    5066        // not yet expired
    51         $this->assertEquals($val, wp_cache_get($key));
     67        $this->assertEquals($val, $this->cache1->get($key));
     68        $this->assertEquals($val, $this->cache2->get($key));
    5269    }
    5370
     
    5875
    5976        // expires in 1 second
    60         wp_cache_add($key, $val, '', 1);
    61         wp_cache_close();
    62         $this->init_cache();
     77        $this->cache1->expiration_time = 1; # second
     78        $this->cache2->expiration_time = 1;
     79        $this->assertTrue($this->cache1->add($key, $val));
     80        $this->assertTrue($this->cache1->save());
     81
     82        // 3 seconds later, key should have expired
    6383        sleep(3);
    64         $this->assertEquals(NULL, wp_cache_get($key));
     84        $this->assertEquals(NULL, $this->cache2->get($key));
     85        // cache1's key is still cached in memory, which doesn't expire
     86        $this->assertEquals($val, $this->cache1->get($key));
    6587    }
    6688
Note: See TracChangeset for help on using the changeset viewer.