summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Scribunto/tests/phpunit/engines/LuaStandalone/LuaStandaloneInterpreterTest.php
blob: 0c3d069be6669c52fe3499eca30a94e80968a2ce (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php

if ( !wfIsCLI() ) {
	exit;
}

require_once __DIR__ . '/../LuaCommon/LuaInterpreterTest.php';

/**
 * @group Lua
 * @group LuaStandalone
 */
// @codingStandardsIgnoreLine Squiz.Classes.ValidClassName.NotCamelCaps
class Scribunto_LuaStandaloneInterpreterTest extends Scribunto_LuaInterpreterTest {
	public $stdOpts = [
		'errorFile' => null,
		'luaPath' => null,
		'memoryLimit' => 50000000,
		'cpuLimit' => 30,
	];

	private function getVsize( $pid ) {
		$size = wfShellExec( wfEscapeShellArg( 'ps', '-p', $pid, '-o', 'vsz', '--no-headers' ) );
		return trim( $size ) * 1024;
	}

	protected function newInterpreter( $opts = [] ) {
		$opts = $opts + $this->stdOpts;
		$engine = new Scribunto_LuaStandaloneEngine( $this->stdOpts );
		return new Scribunto_LuaStandaloneInterpreter( $engine, $opts );
	}

	public function testIOErrorExit() {
		$interpreter = $this->newInterpreter();
		try {
			$interpreter->testquit();
			$this->fail( 'Expected exception not thrown' );
		} catch ( ScribuntoException $ex ) {
			$this->assertSame( 'scribunto-luastandalone-exited', $ex->getMessageName() );
			$this->assertSame( [ '[UNKNOWN]', 42 ], $ex->messageArgs );
		}
	}

	public function testIOErrorSignal() {
		$interpreter = $this->newInterpreter();
		try {
			proc_terminate( $interpreter->proc, 15 );
			// Some dummy protocol interaction to make it see the interpreter went away
			$interpreter->loadString( 'return ...', 'test' );
			$this->fail( 'Expected exception not thrown' );
		} catch ( ScribuntoException $ex ) {
			$this->assertSame( 'scribunto-luastandalone-signal', $ex->getMessageName() );
			$this->assertSame( [ '[UNKNOWN]', 15 ], $ex->messageArgs );
		}
	}

	public function testGetStatus() {
		$startTime = microtime( true );
		if ( php_uname( 's' ) !== 'Linux' ) {
			$this->markTestSkipped( "getStatus() not supported on platforms other than Linux" );
			return;
		}
		$interpreter = $this->newInterpreter();
		$status = $interpreter->getStatus();
		$pid = $status['pid'];
		$this->assertInternalType( 'integer', $status['pid'] );
		$initialVsize = $this->getVsize( $pid );
		$this->assertGreaterThan( 0, $initialVsize, 'Initial vsize' );

		$chunk = $this->getBusyLoop( $interpreter );

		while ( microtime( true ) - $startTime < 1 ) {
			$interpreter->callFunction( $chunk, 100 );
		}
		$status = $interpreter->getStatus();
		$vsize = $this->getVsize( $pid );
		$time = $status['time'] / $interpreter->engine->getClockTick();
		$this->assertGreaterThan( 0.1, $time, 'getStatus() time usage' );
		$this->assertLessThan( 1.5, $time, 'getStatus() time usage' );
		$this->assertEquals( $vsize, $status['vsize'], 'vsize', $vsize * 0.1 );
	}

	/**
	 * @dataProvider providePhpToLuaArrayKeyConversion
	 */
	public function testPhpToLuaArrayKeyConversion( $array, $expect ) {
		$interpreter = $this->newInterpreter();

		$ret = $interpreter->callFunction(
			$interpreter->loadString(
				'local t, r = ..., {}; for k, v in pairs( t ) do r[v] = type(k) end return r', 'test'
			),
			$array
		);
		ksort( $ret[0], SORT_STRING );
		$this->assertSame( $expect, $ret[0] );
	}

	public static function providePhpToLuaArrayKeyConversion() {
		if ( PHP_INT_MAX > 9007199254740992 ) {
			$a = [
				'9007199254740992' => 'max', '9007199254740993' => 'max+1',
				'-9007199254740992' => 'min', '-9007199254740993' => 'min-1',
			];
		} else {
			$a = [
				'2147483647' => 'max', '2147483648' => 'max+1',
				'-2147483648' => 'min', '-2147483649' => 'min-1',
			];
		}

		return [
			'simple integers' => [
				[ -10 => 'minus ten', 0 => 'zero', 10 => 'ten' ],
				[ 'minus ten' => 'number', 'ten' => 'number', 'zero' => 'number' ],
			],
			'maximal values' => [
				$a,
				[ 'max' => 'number', 'max+1' => 'string', 'min' => 'number', 'min-1' => 'string' ],
			],
		];
	}

	/**
	 * @dataProvider provideLuaToPhpArrayKeyConversion
	 */
	public function testLuaToPhpArrayKeyConversion( $lua, $expect ) {
		if ( $expect instanceof Exception ) {
			$this->setExpectedException( Scribunto_LuaError::class, $expect->getMessage() );
		}

		$interpreter = $this->newInterpreter();
		$ret = $interpreter->callFunction(
			$interpreter->loadString( "return { $lua }", 'test' )
		);
		if ( $expect instanceof Exception ) {
			$this->fail( 'Expected exception not thrown' );
		}
		ksort( $ret[0], SORT_STRING );
		$this->assertSame( $expect, $ret[0] );
	}

	public static function provideLuaToPhpArrayKeyConversion() {
		if ( PHP_INT_MAX > 9007199254740992 ) {
			$max = '9223372036854774784';
			$max2 = '9223372036854775808';
			$min = '-9223372036854775808';
			$min2 = '-9223372036854775809';
		} else {
			$max = '2147483647';
			$max2 = '2147483648';
			$min = '-2147483648';
			$min2 = '-2147483649';
		}

		return [
			'simple integers' => [
				'[-10] = "minus ten", [0] = "zero", [10] = "ten"',
				[ -10 => 'minus ten', 0 => 'zero', 10 => 'ten' ],
			],
			'stringified integers' => [
				'["-10"] = "minus ten", ["0"] = "zero", ["10"] = "ten"',
				[ -10 => 'minus ten', 0 => 'zero', 10 => 'ten' ],
			],
			'maximal integers' => [
				"['$max'] = 'near max', ['$max2'] = 'max+1', ['$min'] = 'min', ['$min2'] = 'min-1'",
				[ $min => 'min', $min2 => 'min-1', $max => 'near max', $max2 => 'max+1' ],
			],
			'collision (0)' => [
				'[0] = "number zero", ["0"] = "string zero"',
				new Exception( 'Collision for array key 0 when passing data from Lua to PHP.' ),
			],
			'collision (float)' => [
				'[1.5] = "number 1.5", ["1.5"] = "string 1.5"',
				new Exception( 'Collision for array key 1.5 when passing data from Lua to PHP.' ),
			],
			'collision (inf)' => [
				'[1/0] = "number inf", ["inf"] = "string inf"',
				new Exception( 'Collision for array key inf when passing data from Lua to PHP.' ),
			],
		];
	}

	public function testFreeFunctions() {
		$interpreter = $this->newInterpreter();

		// Test #1: Make sure freeing actually works
		$ret = $interpreter->callFunction(
			$interpreter->loadString( 'return function() return "testFreeFunction #1" end', 'test' )
		);
		$id = $ret[0]->id;
		$interpreter->cleanupLuaChunks();
		$this->assertEquals(
			[ 'testFreeFunction #1' ], $interpreter->callFunction( $ret[0] ),
			'Test that function #1 was not freed while a reference exists'
		);
		$ret = null;
		$interpreter->cleanupLuaChunks();
		$testfunc = new Scribunto_LuaStandaloneInterpreterFunction( $interpreter->id, $id );
		try {
			$interpreter->callFunction( $testfunc );
			$this->fail( "Expected exception because function #1 should have been freed" );
		} catch ( Scribunto_LuaError $e ) {
			$this->assertEquals(
				"function id $id does not exist", $e->messageArgs[1],
				'Testing for expected error when calling a freed function #1'
			);
		}

		// Test #2: Make sure constructing a new copy of the function works
		$ret = $interpreter->callFunction(
			$interpreter->loadString( 'return function() return "testFreeFunction #2" end', 'test' )
		);
		$id = $ret[0]->id;
		$func = new Scribunto_LuaStandaloneInterpreterFunction( $interpreter->id, $id );
		$ret = null;
		$interpreter->cleanupLuaChunks();
		$this->assertEquals(
			[ 'testFreeFunction #2' ], $interpreter->callFunction( $func ),
			'Test that function #2 was not freed while a reference exists'
		);
		$func = null;
		$interpreter->cleanupLuaChunks();
		$testfunc = new Scribunto_LuaStandaloneInterpreterFunction( $interpreter->id, $id );
		try {
			$interpreter->callFunction( $testfunc );
			$this->fail( "Expected exception because function #2 should have been freed" );
		} catch ( Scribunto_LuaError $e ) {
			$this->assertEquals(
				"function id $id does not exist", $e->messageArgs[1],
				'Testing for expected error when calling a freed function #2'
			);
		}

		// Test #3: Make sure cloning works
		$ret = $interpreter->callFunction(
			$interpreter->loadString( 'return function() return "testFreeFunction #3" end', 'test' )
		);
		$id = $ret[0]->id;
		$func = clone $ret[0];
		$ret = null;
		$interpreter->cleanupLuaChunks();
		$this->assertEquals(
			[ 'testFreeFunction #3' ], $interpreter->callFunction( $func ),
			'Test that function #3 was not freed while a reference exists'
		);
		$func = null;
		$interpreter->cleanupLuaChunks();
		$testfunc = new Scribunto_LuaStandaloneInterpreterFunction( $interpreter->id, $id );
		try {
			$interpreter->callFunction( $testfunc );
			$this->fail( "Expected exception because function #3 should have been freed" );
		} catch ( Scribunto_LuaError $e ) {
			$this->assertEquals(
				"function id $id does not exist", $e->messageArgs[1],
				'Testing for expected error when calling a freed function #3'
			);
		}
	}
}