aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/integration/includes/jobqueue/JobQueueDBTest.php
blob: 3d1f5006a3ec35db3908293d34cf99f446d57f0d (plain) (blame)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php

namespace MediaWiki\Tests\Integration\JobQueue;

use JobQueue;
use JobQueueDB;
use JobSpecification;
use MediaWiki\Title\Title;
use MediaWiki\WikiMap\WikiMap;
use MediaWikiIntegrationTestCase;
use Profiler;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

/**
 * @covers JobQueueDB
 * @group JobQueue
 * @group Database
 */
class JobQueueDBTest extends MediaWikiIntegrationTestCase {

	private function newJobQueue(): JobQueueDB {
		$jobQueue = JobQueue::factory( [
			'class' => JobQueueDB::class,
			'domain' => WikiMap::getCurrentWikiDbDomain()->getId(),
			'type' => 'null',
			'idGenerator' => $this->getServiceContainer()->getGlobalIdGenerator(),
			'claimTTL' => 3600,
		] );
		$this->assertInstanceOf( JobQueueDB::class, $jobQueue );
		return $jobQueue;
	}

	/**
	 * @return JobSpecification A job which has a parameter named 'id' which should be a unique enough ID to
	 *   assert that two given jobs are either the same or not the same.
	 */
	private function newJobSpecification() {
		return new JobSpecification(
			'null',
			[ 'customParameter' => null, 'id' => mt_rand( 1, 0xFFFFFF ) ],
			[],
			Title::makeTitle( NS_MAIN, 'Custom title' )
		);
	}

	private function addQueuedJobs( JobQueueDB $jobQueue, int $numOfQueuedJobs ) {
		for ( $i = 0; $i < $numOfQueuedJobs; $i++ ) {
			$jobQueue->push( $this->newJobSpecification() );
		}
	}

	private function addAcquiredJobs( JobQueueDB $jobQueue, int $numOfAcquiredJobs ) {
		for ( $i = 0; $i < $numOfAcquiredJobs; $i++ ) {
			$jobQueue->push( $this->newJobSpecification() );
			$jobQueue->pop();
		}
	}

	/** @dataProvider provideIsEmpty */
	public function testIsEmpty( $numOfQueuedJobs, $numOfAcquiredJobs, $expectedReturnValue ) {
		$jobQueue = $this->newJobQueue();
		$this->addQueuedJobs( $jobQueue, $numOfQueuedJobs );
		$this->addAcquiredJobs( $jobQueue, $numOfAcquiredJobs );
		$this->assertSame( $expectedReturnValue, $jobQueue->isEmpty() );
	}

	public static function provideIsEmpty() {
		return [
			'Job queue is empty' => [ 0, 0, true ],
			'Job queue has all acquired jobs' => [ 0, 2, true ],
			'Job queue has all queued jobs' => [ 2, 0, false ],
			'Job queue has a mix of acquired and queued jobs' => [ 2, 1, false ],
		];
	}

	/** @dataProvider provideGetSize */
	public function testGetSize( $numOfQueuedJobs, $numOfAcquiredJobs, $expectedReturnValue ) {
		$jobQueue = $this->newJobQueue();
		$this->addQueuedJobs( $jobQueue, $numOfQueuedJobs );
		$this->addAcquiredJobs( $jobQueue, $numOfAcquiredJobs );
		$this->assertSame( $expectedReturnValue, $jobQueue->getSize() );
	}

	public static function provideGetSize() {
		return [
			'Job queue is empty' => [ 0, 0, 0 ],
			'Job queue has all acquired jobs' => [ 0, 2, 0 ],
			'Job queue has all queued jobs' => [ 2, 0, 2 ],
			'Job queue has a mix of acquired and queued jobs' => [ 2, 1, 2 ],
		];
	}

	/** @dataProvider provideGetAcquiredCount */
	public function testGetAcquiredCount( $numOfQueuedJobs, $numOfAcquiredJobs, $expectedReturnValue ) {
		$jobQueue = $this->newJobQueue();
		$this->addQueuedJobs( $jobQueue, $numOfQueuedJobs );
		$this->addAcquiredJobs( $jobQueue, $numOfAcquiredJobs );
		$this->assertSame( $expectedReturnValue, $jobQueue->getAcquiredCount() );
	}

	public static function provideGetAcquiredCount() {
		return [
			'Job queue is empty' => [ 0, 0, 0 ],
			'Job queue has all acquired jobs' => [ 0, 2, 2 ],
			'Job queue has all queued jobs' => [ 2, 0, 0 ],
			'Job queue has a mix of acquired and queued jobs' => [ 2, 1, 1 ],
		];
	}

	public function testGetAllQueuedJobsWhenJobQueueEmpty() {
		$this->assertCount( 0, $this->newJobQueue()->getAllQueuedJobs() );
	}

	public function testGetAllQueuedJobs() {
		// Queue a job into the job queue
		$jobQueue = $this->newJobQueue();
		$firstJob = $this->newJobSpecification();
		$jobQueue->push( $firstJob );
		// Check that ::getAllQueuedJobs returns the job we just queued into the queue.
		$allQueuedJobs = iterator_to_array( $jobQueue->getAllQueuedJobs() );
		$this->assertCount( 1, $allQueuedJobs );
		$this->assertSame( $firstJob->getParams()['id'], $allQueuedJobs[0]->getParams()['id'] );
	}

	public function testGetAllAcquiredJobsWhenJobQueueEmpty() {
		$this->assertCount( 0, $this->newJobQueue()->getAllAcquiredJobs() );
	}

	public function testGetAllAcquiredJobs() {
		// Add a job to the queue
		$queue = $this->newJobQueue();
		$queuedJob = $this->newJobSpecification();
		$queue->push( $queuedJob );
		$this->assertCount( 0, $queue->getAllAcquiredJobs() );

		// Pop the job off the queue and check that it is the correct job
		$job = $queue->pop();
		$this->assertNotFalse( $job );
		$this->assertSame( $queuedJob->getParams()['id'], $job->getParams()['id'] );

		// Check that ::getAllAcquiredJobs returns the job we just popped off the queue
		$acquiredJobs = iterator_to_array( $queue->getAllAcquiredJobs() );
		$this->assertCount( 1, $acquiredJobs );
		$this->assertSame( $job->getParams()['id'], $acquiredJobs[0]->getParams()['id'] );
	}

	public function testPushWhenTransactionProfilerExpectsNoWrites() {
		// Narrow the expectations to be no writes and no master connections.
		$transactionProfiler = Profiler::instance()->getTransactionProfiler();
		$transactionProfiler->setExpectations(
			[ 'writes' => 0, 'masterConns' => 0 ],
			__METHOD__
		);
		// Expect no calls to the LoggerInterface used by the profiler. We can't use ::createNoOpMock because we need
		// to reset the transaction profiler to clean up after the test.
		$logCreated = false;
		$mockLogger = $this->createMock( LoggerInterface::class );
		$mockLogger->method( $this->anythingBut( [ '__debugInfo', '__destruct' ] ) )
			->willReturnCallback( static function () use ( &$logCreated ) {
				$logCreated = true;
			} );
		$transactionProfiler->setLogger( $mockLogger );

		// Push a job into the queue, which should cause a write to occur without trying to log a violated exception.
		$queue = $this->newJobQueue();
		$queue->push( $this->newJobSpecification() );
		$this->assertFalse( $logCreated );

		// Reset the logger and expectations to clean up after ourselves.
		$transactionProfiler->setLogger( new NullLogger() );
		$transactionProfiler->resetExpectations();
	}
}