統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / Cache / Engine / MemcacheEngineTest.php @ 0b1b8047

履歴 | 表示 | アノテート | ダウンロード (13.222 KB)

1
<?php
2
/**
3
 * MemcacheEngineTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Cache.Engine
15
 * @since         CakePHP(tm) v 1.2.0.5434
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18

    
19
App::uses('Cache', 'Cache');
20
App::uses('MemcacheEngine', 'Cache/Engine');
21

    
22
/**
23
 * Class TestMemcacheEngine
24
 *
25
 * @package       Cake.Test.Case.Cache.Engine
26
 */
27
class TestMemcacheEngine extends MemcacheEngine {
28

    
29
/**
30
 * public accessor to _parseServerString
31
 *
32
 * @param string $server
33
 * @return array
34
 */
35
        public function parseServerString($server) {
36
                return $this->_parseServerString($server);
37
        }
38

    
39
        public function setMemcache($memcache) {
40
                $this->_Memcache = $memcache;
41
        }
42

    
43
}
44

    
45
/**
46
 * MemcacheEngineTest class
47
 *
48
 * @package       Cake.Test.Case.Cache.Engine
49
 */
50
class MemcacheEngineTest extends CakeTestCase {
51

    
52
/**
53
 * setUp method
54
 *
55
 * @return void
56
 */
57
        public function setUp() {
58
                parent::setUp();
59
                $this->skipIf(!class_exists('Memcache'), 'Memcache is not installed or configured properly.');
60

    
61
                $this->_cacheDisable = Configure::read('Cache.disable');
62
                Configure::write('Cache.disable', false);
63
                Cache::config('memcache', array(
64
                        'engine' => 'Memcache',
65
                        'prefix' => 'cake_',
66
                        'duration' => 3600
67
                ));
68
        }
69

    
70
/**
71
 * tearDown method
72
 *
73
 * @return void
74
 */
75
        public function tearDown() {
76
                parent::tearDown();
77
                Configure::write('Cache.disable', $this->_cacheDisable);
78
                Cache::drop('memcache');
79
                Cache::drop('memcache_groups');
80
                Cache::drop('memcache_helper');
81
                Cache::config('default');
82
        }
83

    
84
/**
85
 * testSettings method
86
 *
87
 * @return void
88
 */
89
        public function testSettings() {
90
                $settings = Cache::settings('memcache');
91
                unset($settings['serialize'], $settings['path']);
92
                $expecting = array(
93
                        'prefix' => 'cake_',
94
                        'duration' => 3600,
95
                        'probability' => 100,
96
                        'servers' => array('127.0.0.1'),
97
                        'persistent' => true,
98
                        'compress' => false,
99
                        'engine' => 'Memcache',
100
                        'groups' => array()
101
                );
102
                $this->assertEquals($expecting, $settings);
103
        }
104

    
105
/**
106
 * testSettings method
107
 *
108
 * @return void
109
 */
110
        public function testMultipleServers() {
111
                $servers = array('127.0.0.1:11211', '127.0.0.1:11222');
112
                $available = true;
113
                $Memcache = new Memcache();
114

    
115
                foreach ($servers as $server) {
116
                        list($host, $port) = explode(':', $server);
117
                        //@codingStandardsIgnoreStart
118
                        if (!@$Memcache->connect($host, $port)) {
119
                                $available = false;
120
                        }
121
                        //@codingStandardsIgnoreEnd
122
                }
123

    
124
                $this->skipIf(!$available, 'Need memcache servers at ' . implode(', ', $servers) . ' to run this test.');
125

    
126
                $Memcache = new MemcacheEngine();
127
                $Memcache->init(array('engine' => 'Memcache', 'servers' => $servers));
128

    
129
                $settings = $Memcache->settings();
130
                $this->assertEquals($settings['servers'], $servers);
131
                Cache::drop('dual_server');
132
        }
133

    
134
/**
135
 * testConnect method
136
 *
137
 * @return void
138
 */
139
        public function testConnect() {
140
                $Memcache = new MemcacheEngine();
141
                $Memcache->init(Cache::settings('memcache'));
142
                $result = $Memcache->connect('127.0.0.1');
143
                $this->assertTrue($result);
144
        }
145

    
146
/**
147
 * test connecting to an ipv6 server.
148
 *
149
 * @return void
150
 */
151
        public function testConnectIpv6() {
152
                $Memcache = new MemcacheEngine();
153
                $result = $Memcache->init(array(
154
                        'prefix' => 'cake_',
155
                        'duration' => 200,
156
                        'engine' => 'Memcache',
157
                        'servers' => array(
158
                                '[::1]:11211'
159
                        )
160
                ));
161
                $this->assertTrue($result);
162
        }
163

    
164
/**
165
 * test domain starts with u
166
 *
167
 * @return void
168
 */
169
        public function testParseServerStringWithU() {
170
                $Memcached = new TestMemcachedEngine();
171
                $result = $Memcached->parseServerString('udomain.net:13211');
172
                $this->assertEquals(array('udomain.net', '13211'), $result);
173
        }
174

    
175
/**
176
 * test non latin domains.
177
 *
178
 * @return void
179
 */
180
        public function testParseServerStringNonLatin() {
181
                $Memcache = new TestMemcacheEngine();
182
                $result = $Memcache->parseServerString('schülervz.net:13211');
183
                $this->assertEquals(array('schülervz.net', '13211'), $result);
184

    
185
                $result = $Memcache->parseServerString('sülül:1111');
186
                $this->assertEquals(array('sülül', '1111'), $result);
187
        }
188

    
189
/**
190
 * test unix sockets.
191
 *
192
 * @return void
193
 */
194
        public function testParseServerStringUnix() {
195
                $Memcache = new TestMemcacheEngine();
196
                $result = $Memcache->parseServerString('unix:///path/to/memcached.sock');
197
                $this->assertEquals(array('unix:///path/to/memcached.sock', 0), $result);
198
        }
199

    
200
/**
201
 * testReadAndWriteCache method
202
 *
203
 * @return void
204
 */
205
        public function testReadAndWriteCache() {
206
                Cache::set(array('duration' => 1), null, 'memcache');
207

    
208
                $result = Cache::read('test', 'memcache');
209
                $expecting = '';
210
                $this->assertEquals($expecting, $result);
211

    
212
                $data = 'this is a test of the emergency broadcasting system';
213
                $result = Cache::write('test', $data, 'memcache');
214
                $this->assertTrue($result);
215

    
216
                $result = Cache::read('test', 'memcache');
217
                $expecting = $data;
218
                $this->assertEquals($expecting, $result);
219

    
220
                Cache::delete('test', 'memcache');
221
        }
222

    
223
/**
224
 * testExpiry method
225
 *
226
 * @return void
227
 */
228
        public function testExpiry() {
229
                Cache::set(array('duration' => 1), 'memcache');
230

    
231
                $result = Cache::read('test', 'memcache');
232
                $this->assertFalse($result);
233

    
234
                $data = 'this is a test of the emergency broadcasting system';
235
                $result = Cache::write('other_test', $data, 'memcache');
236
                $this->assertTrue($result);
237

    
238
                sleep(2);
239
                $result = Cache::read('other_test', 'memcache');
240
                $this->assertFalse($result);
241

    
242
                Cache::set(array('duration' => "+1 second"), 'memcache');
243

    
244
                $data = 'this is a test of the emergency broadcasting system';
245
                $result = Cache::write('other_test', $data, 'memcache');
246
                $this->assertTrue($result);
247

    
248
                sleep(3);
249
                $result = Cache::read('other_test', 'memcache');
250
                $this->assertFalse($result);
251

    
252
                Cache::config('memcache', array('duration' => '+1 second'));
253

    
254
                $result = Cache::read('other_test', 'memcache');
255
                $this->assertFalse($result);
256

    
257
                Cache::config('memcache', array('duration' => '+29 days'));
258
                $data = 'this is a test of the emergency broadcasting system';
259
                $result = Cache::write('long_expiry_test', $data, 'memcache');
260
                $this->assertTrue($result);
261

    
262
                sleep(2);
263
                $result = Cache::read('long_expiry_test', 'memcache');
264
                $expecting = $data;
265
                $this->assertEquals($expecting, $result);
266

    
267
                Cache::config('memcache', array('duration' => 3600));
268
        }
269

    
270
/**
271
 * testDeleteCache method
272
 *
273
 * @return void
274
 */
275
        public function testDeleteCache() {
276
                $data = 'this is a test of the emergency broadcasting system';
277
                $result = Cache::write('delete_test', $data, 'memcache');
278
                $this->assertTrue($result);
279

    
280
                $result = Cache::delete('delete_test', 'memcache');
281
                $this->assertTrue($result);
282
        }
283

    
284
/**
285
 * testDecrement method
286
 *
287
 * @return void
288
 */
289
        public function testDecrement() {
290
                $result = Cache::write('test_decrement', 5, 'memcache');
291
                $this->assertTrue($result);
292

    
293
                $result = Cache::decrement('test_decrement', 1, 'memcache');
294
                $this->assertEquals(4, $result);
295

    
296
                $result = Cache::read('test_decrement', 'memcache');
297
                $this->assertEquals(4, $result);
298

    
299
                $result = Cache::decrement('test_decrement', 2, 'memcache');
300
                $this->assertEquals(2, $result);
301

    
302
                $result = Cache::read('test_decrement', 'memcache');
303
                $this->assertEquals(2, $result);
304
        }
305

    
306
/**
307
 * testIncrement method
308
 *
309
 * @return void
310
 */
311
        public function testIncrement() {
312
                $result = Cache::write('test_increment', 5, 'memcache');
313
                $this->assertTrue($result);
314

    
315
                $result = Cache::increment('test_increment', 1, 'memcache');
316
                $this->assertEquals(6, $result);
317

    
318
                $result = Cache::read('test_increment', 'memcache');
319
                $this->assertEquals(6, $result);
320

    
321
                $result = Cache::increment('test_increment', 2, 'memcache');
322
                $this->assertEquals(8, $result);
323

    
324
                $result = Cache::read('test_increment', 'memcache');
325
                $this->assertEquals(8, $result);
326
        }
327

    
328
/**
329
 * test that configurations don't conflict, when a file engine is declared after a memcache one.
330
 *
331
 * @return void
332
 */
333
        public function testConfigurationConflict() {
334
                Cache::config('long_memcache', array(
335
                        'engine' => 'Memcache',
336
                        'duration' => '+2 seconds',
337
                        'servers' => array('127.0.0.1:11211'),
338
                ));
339
                Cache::config('short_memcache', array(
340
                        'engine' => 'Memcache',
341
                        'duration' => '+1 seconds',
342
                        'servers' => array('127.0.0.1:11211'),
343
                ));
344
                Cache::config('some_file', array('engine' => 'File'));
345

    
346
                $this->assertTrue(Cache::write('duration_test', 'yay', 'long_memcache'));
347
                $this->assertTrue(Cache::write('short_duration_test', 'boo', 'short_memcache'));
348

    
349
                $this->assertEquals('yay', Cache::read('duration_test', 'long_memcache'), 'Value was not read %s');
350
                $this->assertEquals('boo', Cache::read('short_duration_test', 'short_memcache'), 'Value was not read %s');
351

    
352
                sleep(1);
353
                $this->assertEquals('yay', Cache::read('duration_test', 'long_memcache'), 'Value was not read %s');
354

    
355
                sleep(2);
356
                $this->assertFalse(Cache::read('short_duration_test', 'short_memcache'), 'Cache was not invalidated %s');
357
                $this->assertFalse(Cache::read('duration_test', 'long_memcache'), 'Value did not expire %s');
358

    
359
                Cache::delete('duration_test', 'long_memcache');
360
                Cache::delete('short_duration_test', 'short_memcache');
361
        }
362

    
363
/**
364
 * test clearing memcache.
365
 *
366
 * @return void
367
 */
368
        public function testClear() {
369
                Cache::config('memcache2', array(
370
                        'engine' => 'Memcache',
371
                        'prefix' => 'cake2_',
372
                        'duration' => 3600
373
                ));
374

    
375
                Cache::write('some_value', 'cache1', 'memcache');
376
                $result = Cache::clear(true, 'memcache');
377
                $this->assertTrue($result);
378
                $this->assertEquals('cache1', Cache::read('some_value', 'memcache'));
379

    
380
                Cache::write('some_value', 'cache2', 'memcache2');
381
                $result = Cache::clear(false, 'memcache');
382
                $this->assertTrue($result);
383
                $this->assertFalse(Cache::read('some_value', 'memcache'));
384
                $this->assertEquals('cache2', Cache::read('some_value', 'memcache2'));
385

    
386
                Cache::clear(false, 'memcache2');
387
        }
388

    
389
/**
390
 * test that a 0 duration can successfully write.
391
 *
392
 * @return void
393
 */
394
        public function testZeroDuration() {
395
                Cache::config('memcache', array('duration' => 0));
396
                $result = Cache::write('test_key', 'written!', 'memcache');
397

    
398
                $this->assertTrue($result);
399
                $result = Cache::read('test_key', 'memcache');
400
                $this->assertEquals('written!', $result);
401
        }
402

    
403
/**
404
 * test that durations greater than 30 days never expire
405
 *
406
 * @return void
407
 */
408
        public function testLongDurationEqualToZero() {
409
                $memcache = new TestMemcacheEngine();
410
                $memcache->settings['compress'] = false;
411

    
412
                $mock = $this->getMock('Memcache');
413
                $memcache->setMemcache($mock);
414
                $mock->expects($this->once())
415
                        ->method('set')
416
                        ->with('key', 'value', false, 0);
417

    
418
                $value = 'value';
419
                $memcache->write('key', $value, 50 * DAY);
420
        }
421

    
422
/**
423
 * Tests that configuring groups for stored keys return the correct values when read/written
424
 * Shows that altering the group value is equivalent to deleting all keys under the same
425
 * group
426
 *
427
 * @return void
428
 */
429
        public function testGroupReadWrite() {
430
                Cache::config('memcache_groups', array(
431
                        'engine' => 'Memcache',
432
                        'duration' => 3600,
433
                        'groups' => array('group_a', 'group_b'),
434
                        'prefix' => 'test_'
435
                ));
436
                Cache::config('memcache_helper', array(
437
                        'engine' => 'Memcache',
438
                        'duration' => 3600,
439
                        'prefix' => 'test_'
440
                ));
441
                $this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
442
                $this->assertEquals('value', Cache::read('test_groups', 'memcache_groups'));
443

    
444
                Cache::increment('group_a', 1, 'memcache_helper');
445
                $this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
446
                $this->assertTrue(Cache::write('test_groups', 'value2', 'memcache_groups'));
447
                $this->assertEquals('value2', Cache::read('test_groups', 'memcache_groups'));
448

    
449
                Cache::increment('group_b', 1, 'memcache_helper');
450
                $this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
451
                $this->assertTrue(Cache::write('test_groups', 'value3', 'memcache_groups'));
452
                $this->assertEquals('value3', Cache::read('test_groups', 'memcache_groups'));
453
        }
454

    
455
/**
456
 * Tests that deleteing from a groups-enabled config is possible
457
 *
458
 * @return void
459
 */
460
        public function testGroupDelete() {
461
                Cache::config('memcache_groups', array(
462
                        'engine' => 'Memcache',
463
                        'duration' => 3600,
464
                        'groups' => array('group_a', 'group_b')
465
                ));
466
                $this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
467
                $this->assertEquals('value', Cache::read('test_groups', 'memcache_groups'));
468
                $this->assertTrue(Cache::delete('test_groups', 'memcache_groups'));
469

    
470
                $this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
471
        }
472

    
473
/**
474
 * Test clearing a cache group
475
 *
476
 * @return void
477
 */
478
        public function testGroupClear() {
479
                Cache::config('memcache_groups', array(
480
                        'engine' => 'Memcache',
481
                        'duration' => 3600,
482
                        'groups' => array('group_a', 'group_b')
483
                ));
484

    
485
                $this->assertTrue(Cache::write('test_groups', 'value', 'memcache_groups'));
486
                $this->assertTrue(Cache::clearGroup('group_a', 'memcache_groups'));
487
                $this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
488

    
489
                $this->assertTrue(Cache::write('test_groups', 'value2', 'memcache_groups'));
490
                $this->assertTrue(Cache::clearGroup('group_b', 'memcache_groups'));
491
                $this->assertFalse(Cache::read('test_groups', 'memcache_groups'));
492
        }
493
}