Completed
Push — master ( 2813c1...14a1cb )
by Maxence
01:59
created

TestController::testAsyncReset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Controller;
28
29
use Exception;
30
use OCA\Circles\AppInfo\Application;
31
use OCA\Circles\Service\ConfigService;
32
use OCA\Circles\Service\MiscService;
33
use OCP\AppFramework\Controller;
34
use OCP\AppFramework\Http\DataResponse;
35
use OCP\Http\Client\IClientService;
36
use OCP\IRequest;
37
38
39
class TestController extends Controller {
40
41
42
	const TEST_DURATION = 120;
43
44
	/** @var ConfigService */
45
	private $configService;
46
47
	/** @var IClientService */
48
	private $clientService;
49
50
	/** @var MiscService */
51
	private $miscService;
52
53
	public function __construct(
54
		$appName, IRequest $request, ConfigService $configService, IClientService $clientService,
55
		MiscService $miscService
56
	) {
57
		parent::__construct($appName, $request);
58
		$this->configService = $configService;
59
		$this->clientService = $clientService;
60
		$this->miscService = $miscService;
61
	}
62
63
64
	/**
65
	 * @NoCSRFRequired
66
	 * @PublicPage
67
	 */
68
	public function testAsyncRun($data) {
69
		$lock = $data['lock'];
70
		if ($this->configService->getAppValue(ConfigService::CIRCLES_TEST_ASYNC_LOCK) !== $lock) {
71
			return;
72
		}
73
74
		$this->configService->setAppValue(ConfigService::CIRCLES_TEST_ASYNC_COUNT, 0);
75
		$this->miscService->asyncAndLeaveClientOutOfThis($this->testAsyncStatus());
76
77
		$t = 0;
78
		while ($t <= self::TEST_DURATION) {
79
			$t++;
80
81
			$this->configService->setAppValue(ConfigService::CIRCLES_TEST_ASYNC_COUNT, $t);
82
			sleep(1);
83
		}
84
85
	}
86
87
88
	/**
89
	 * @param string $remote
90
	 *
91
	 * @return string
92
	 */
93
	private function generateTestAsyncURL($remote) {
94
		return $this->configService->generateRemoteHost($remote) . Application::TEST_URL_ASYNC;
95
	}
96
97
98
	public function testAsyncStart() {
99
		try {
100
101
			$lock = $this->testAsyncInitiate();
102
			$this->configService->setAppValue(ConfigService::CIRCLES_TEST_ASYNC_HAND, 1);
103
			$this->configService->setAppValue(ConfigService::CIRCLES_TEST_ASYNC_INIT, time());
104
105
			$client = $this->clientService->newClient();
106
			$url = $this->generateTestAsyncURL($this->configService->getLocalAddress());
107
108
			$client->put($url, MiscService::generateClientBodyData(['lock' => $lock]));
109
110
			$this->configService->setAppValue(ConfigService::CIRCLES_TEST_ASYNC_HAND, 0);
111
		} catch (Exception $e) {
112
			return $this->miscService->fail(['error' => $e->getMessage()]);
113
		}
114
115
		return $this->testAsyncStatus();
116
	}
117
118
119
	public function testAsyncReset() {
120
		try {
121
			$this->testAsyncInitiate();
122
		} catch (Exception $e) {
123
			return $this->miscService->fail(['error' => $e->getMessage()]);
124
		}
125
126
		return $this->testAsyncStatus();
127
	}
128
129
130
	/**
131
	 * @return DataResponse
132
	 */
133
	public function testAsyncStatus() {
134
		return $this->miscService->success(
135
			[
136
				'test'  => $this->getTestStatus(),
137
				'init'  => $this->configService->getAppValue(ConfigService::CIRCLES_TEST_ASYNC_INIT),
138
				'hand'  => $this->configService->getAppValue(ConfigService::CIRCLES_TEST_ASYNC_HAND),
139
				'lock'  => $this->configService->getAppValue(ConfigService::CIRCLES_TEST_ASYNC_LOCK),
140
				'count' => $this->configService->getAppValue(ConfigService::CIRCLES_TEST_ASYNC_COUNT)
141
			]
142
		);
143
	}
144
145
146
	/**
147
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,integer|null>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
148
	 */
149
	private function getTestStatus() {
150
		return [
151
			'running'  => (($this->isTestRunning()) ? 1 : 0),
152
			'counting' => (($this->isTestStillCounting()) ? 1 : 0),
153
			'note'     => $this->getTestNote()
154
		];
155
	}
156
157
158
	/**
159
	 * @return bool
160
	 */
161
	private function isTestRunning() {
162
		return ($this->configService->getAppValue(ConfigService::CIRCLES_TEST_ASYNC_INIT) > (time()
163
																							 - self::TEST_DURATION));
164
	}
165
166
167
	/**
168
	 * @return bool
169
	 */
170
	private function isTestStillCounting() {
171
		$shouldBe = (time() - $this->configService->getAppValue(ConfigService::CIRCLES_TEST_ASYNC_INIT));
172
		$current = $this->configService->getAppValue(ConfigService::CIRCLES_TEST_ASYNC_COUNT);
173
174
		return ($current >= ($shouldBe - 3));
175
	}
176
177
178
	private function getTestNote() {
179
		if ($this->configService->getAppValue(ConfigService::CIRCLES_TEST_ASYNC_HAND) === 1) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $this->configService->ge...IRCLES_TEST_ASYNC_HAND) (string) and 1 (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
180
			return 0;
181
		}
182
		if ($this->configService->getAppValue(ConfigService::CIRCLES_TEST_ASYNC_COUNT) > 100) {
183
			return 5;
184
		}
185
		if ($this->configService->getAppValue(ConfigService::CIRCLES_TEST_ASYNC_COUNT) > 50) {
186
			return 4;
187
		}
188
		if ($this->configService->getAppValue(ConfigService::CIRCLES_TEST_ASYNC_COUNT) > 25) {
189
			$note = 3;
0 ignored issues
show
Unused Code introduced by
$note is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
190
		}
191
		if ($this->configService->getAppValue(ConfigService::CIRCLES_TEST_ASYNC_COUNT) > 15) {
192
			$note = 2;
0 ignored issues
show
Unused Code introduced by
$note is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
193
		}
194
		if ($this->configService->getAppValue(ConfigService::CIRCLES_TEST_ASYNC_COUNT) > 5) {
195
			return 1;
196
		}
197
198
	}
199
200
201
	/**
202
	 * @return string
203
	 * @throws Exception
204
	 */
205
	private function testAsyncInitiate() {
206
		if ($this->isTestRunning()) {
207
			throw new Exception('Wait for previous test to finish');
208
		}
209
210
		$lock = bin2hex(openssl_random_pseudo_bytes(24));
211
212
		$this->configService->setAppValue(ConfigService::CIRCLES_TEST_ASYNC_LOCK, $lock);
213
		$this->configService->setAppValue(ConfigService::CIRCLES_TEST_ASYNC_INIT, '0');
214
		$this->configService->setAppValue(ConfigService::CIRCLES_TEST_ASYNC_COUNT, 0);
215
216
		return $lock;
217
	}
218
219
220
}