File: BagOStuffStatsStoreTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (105 lines) | stat: -rw-r--r-- 2,599 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php

namespace Wikimedia\Tests\WRStats;

use PHPUnit\Framework\TestCase;
use Wikimedia\ObjectCache\HashBagOStuff;
use Wikimedia\WRStats\BagOStuffStatsStore;
use Wikimedia\WRStats\EntityKey;
use Wikimedia\WRStats\GlobalEntityKey;
use Wikimedia\WRStats\LocalEntityKey;

/**
 * @covers \Wikimedia\WRStats\BagOStuffStatsStore
 */
class BagOStuffStatsStoreTest extends TestCase {

	/**
	 * @var HashBagOStuff
	 */
	private $cache;

	/**
	 * @var float
	 */
	private $mockTime = 1000000.0;

	public function setUp(): void {
		parent::setUp();
		$this->cache = new HashBagOStuff();
		$this->cache->setMockTime( $this->mockTime );
	}

	private function tickMockTime( $time ) {
		$this->mockTime += $time;
		$this->cache->setMockTime( $this->mockTime );
	}

	private function getStatsStore() {
		return new BagOStuffStatsStore( $this->cache );
	}

	public static function provideMakeKey() {
		yield [ [ 'prefix' ], [ 'internals' ], new LocalEntityKey( [ 'key' ] ), 'local:prefix:internals:key' ];
		yield [ [ 'prefix' ], [ 'internals' ], new GlobalEntityKey( [ 'key' ] ), 'global:prefix:internals:key' ];
		yield [ [ 'p', 'q' ], [ 'i', 'j' ], new GlobalEntityKey( [ 'k', 'h' ] ), 'global:p:q:i:j:k:h' ];
	}

	/**
	 * @param array $prefix
	 * @param array $internals
	 * @param EntityKey $entity
	 * @param string $expected
	 *
	 * @dataProvider provideMakeKey
	 */
	public function testMakeKey( $prefix, $internals, $entity, $expected ) {
		$store = $this->getStatsStore();
		$this->assertSame(
			$expected,
			$store->makeKey(
				$prefix,
				$internals,
				$entity
			)
		);
	}

	public function testIncrAndExpiry() {
		$store = $this->getStatsStore();

		$store->incr( [ 'a' => 1, 'b' => 2 ], 10 );

		$this->tickMockTime( 2 );

		$store->incr( [ 'b' => 1, 'c' => 1 ], 10 );

		$values = $store->query( [ 'a', 'b', 'c' ] );
		$this->assertSame( 1, $values['a'] );
		$this->assertSame( 3, $values['b'] );
		$this->assertSame( 1, $values['c'] );

		$this->tickMockTime( 9 );

		// The TTL is counted from the time the value was first set,
		// not the time it was last updated. So the entries
		// for a and b should have expired now.
		$values = $store->query( [ 'a', 'b', 'c' ] );
		$this->assertArrayNotHasKey( 'a', $values );
		$this->assertArrayNotHasKey( 'b', $values );
		$this->assertSame( 1, $values['c'] );
	}

	public function testDelete() {
		$store = $this->getStatsStore();

		$store->incr( [ 'a' => 1, 'b' => 2 ], 10 );

		$store->delete( [ 'b' ] );

		$values = $store->query( [ 'a', 'b' ] );
		$this->assertSame( 1, $values['a'] );
		$this->assertArrayNotHasKey( 'b', $values );
	}

}