Debian Codegraph @

extensions/Scribunto/tests/phpunit/Engines/LuaStandalone/StandaloneInterpreterTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Scribunto\Tests\Engines\LuaStandalone;
4
5 if ( !wfIsCLI() ) {
6 exit;
7 }
8
9 use Exception;
10 use MediaWiki\Extension\Scribunto\Engines\LuaCommon\LuaError;
11 use MediaWiki\Extension\Scribunto\Engines\LuaStandalone\LuaStandaloneEngine;
12 use MediaWiki\Extension\Scribunto\Engines\LuaStandalone\LuaStandaloneInterpreter;
13 use MediaWiki\Extension\Scribunto\Engines\LuaStandalone\LuaStandaloneInterpreterFunction;
14 use MediaWiki\Extension\Scribunto\ScribuntoException;
15 use MediaWiki\Extension\Scribunto\Tests\Engines\LuaCommon\LuaInterpreterTestBase;
16 use MediaWiki\Title\Title;
17 use Wikimedia\TestingAccessWrapper;
18
19 /**
20 * @group Lua
21 * @group LuaStandalone
22 * @group Standalone
23 * @covers \MediaWiki\Extension\Scribunto\Engines\LuaStandalone\LuaStandaloneInterpreter
24 */
25 class StandaloneInterpreterTest extends LuaInterpreterTestBase {
26 /** @var array */
27 public $stdOpts = [
28 'errorFile' => null,
29 'luaPath' => null,
30 'memoryLimit' => 50000000,
31 'cpuLimit' => 30,
32 ];
33
34 private function getVsize( $pid ) {
35 $size = wfShellExec( wfEscapeShellArg( 'ps', '-p', $pid, '-o', 'vsz', '--no-headers' ) );
36 return trim( $size ) * 1024;
37 }
38
39 protected function newInterpreter( $opts = [] ) {
40 $opts += $this->stdOpts;
41 $engine = new LuaStandaloneEngine( $this->stdOpts + [
42 'title' => Title::makeTitle( NS_MODULE, 'StandaloneInterpreterTest' ),
43 ] );
44 return new LuaStandaloneInterpreter( $engine, $opts );
45 }
46
47 public function testIOErrorExit() {
48 $interpreter = $this->newInterpreter();
49 try {
50 $interpreter->testquit();
51 $this->fail( 'Expected exception not thrown' );
52 } catch ( ScribuntoException $ex ) {
53 $this->assertSame( 'scribunto-luastandalone-exited', $ex->getMessageName() );
54 $this->assertSame( [ '[UNKNOWN]', 42 ], $ex->messageArgs );
55 }
56 }
57
58 public function testIOErrorSignal() {
59 $interpreter = $this->newInterpreter();
60 try {
61 proc_terminate( $interpreter->proc, 15 );
62 // Some dummy protocol interaction to make it see the interpreter went away
63 $interpreter->loadString( 'return ...', 'test' );
64 $this->fail( 'Expected exception not thrown' );
65 } catch ( ScribuntoException $ex ) {
66 $this->assertSame( 'scribunto-luastandalone-signal', $ex->getMessageName() );
67 $this->assertSame( [ '[UNKNOWN]', 15 ], $ex->messageArgs );
68 }
69 }
70
71 public function testGetStatus() {
72 $startTime = microtime( true );
73 if ( php_uname( 's' ) !== 'Linux' ) {
74 $this->markTestSkipped( "getStatus() not supported on platforms other than Linux" );
75 }
76 $interpreter = $this->newInterpreter();
77 $engine = TestingAccessWrapper::newFromObject( $interpreter->engine );
78 $status = $interpreter->getStatus();
79 $pid = $status['pid'];
80 $this->assertIsInt( $status['pid'] );
81 $initialVsize = $this->getVsize( $pid );
82 $this->assertGreaterThan( 0, $initialVsize, 'Initial vsize' );
83
84 $chunk = $this->getBusyLoop( $interpreter );
85
86 while ( microtime( true ) - $startTime < 1 ) {
87 $interpreter->callFunction( $chunk, 100 );
88 }
89 $status = $interpreter->getStatus();
90 $vsize = $this->getVsize( $pid );
91 $time = $status['time'] / $engine->getClockTick();
92 $this->assertGreaterThan( 0.1, $time, 'getStatus() time usage' );
93 $this->assertLessThan( 1.5, $time, 'getStatus() time usage' );
94 $this->assertEqualsWithDelta( $vsize, $status['vsize'], $vsize * 0.1, 'vsize' );
95 }
96
97 /**
98 * @dataProvider providePhpToLuaArrayKeyConversion
99 */
100 public function testPhpToLuaArrayKeyConversion( $array, $expect ) {
101 $interpreter = $this->newInterpreter();
102
103 $ret = $interpreter->callFunction(
104 $interpreter->loadString(
105 'local t, r = ..., {}; for k, v in pairs( t ) do r[v] = type(k) end return r', 'test'
106 ),
107 $array
108 );
109 ksort( $ret[0], SORT_STRING );
110 $this->assertSame( $expect, $ret[0] );
111 }
112
113 public static function providePhpToLuaArrayKeyConversion() {
114 if ( PHP_INT_MAX > 9007199254740992 ) {
115 $a = [
116 '9007199254740992' => 'max', '9007199254740993' => 'max+1',
117 '-9007199254740992' => 'min', '-9007199254740993' => 'min-1',
118 ];
119 } else {
120 $a = [
121 '2147483647' => 'max', '2147483648' => 'max+1',
122 '-2147483648' => 'min', '-2147483649' => 'min-1',
123 ];
124 }
125
126 return [
127 'simple integers' => [
128 [ -10 => 'minus ten', 0 => 'zero', 10 => 'ten' ],
129 [ 'minus ten' => 'number', 'ten' => 'number', 'zero' => 'number' ],
130 ],
131 'maximal values' => [
132 $a,
133 [ 'max' => 'number', 'max+1' => 'string', 'min' => 'number', 'min-1' => 'string' ],
134 ],
135 ];
136 }
137
138 /**
139 * @dataProvider provideLuaToPhpArrayKeyConversion
140 */
141 public function testLuaToPhpArrayKeyConversion( $lua, $expect ) {
142 if ( $expect instanceof Exception ) {
143 $this->expectException( LuaError::class );
144 $this->expectExceptionMessage( $expect->getMessage() );
145 }
146
147 $interpreter = $this->newInterpreter();
148 $ret = $interpreter->callFunction(
149 $interpreter->loadString( "return { $lua }", 'test' )
150 );
151 if ( $expect instanceof Exception ) {
152 $this->fail( 'Expected exception not thrown' );
153 }
154 ksort( $ret[0], SORT_STRING );
155 $this->assertSame( $expect, $ret[0] );
156 }
157
158 public static function provideLuaToPhpArrayKeyConversion() {
159 if ( PHP_INT_MAX > 9007199254740992 ) {
160 $max = '9223372036854774784';
161 $max2 = '9223372036854775808';
162 $min = '-9223372036854775808';
163 $min2 = '-9223372036854775809';
164 } else {
165 $max = '2147483647';
166 $max2 = '2147483648';
167 $min = '-2147483648';
168 $min2 = '-2147483649';
169 }
170
171 return [
172 'simple integers' => [
173 '[-10] = "minus ten", [0] = "zero", [10] = "ten"',
174 [ -10 => 'minus ten', 0 => 'zero', 10 => 'ten' ],
175 ],
176 'stringified integers' => [
177 '["-10"] = "minus ten", ["0"] = "zero", ["10"] = "ten"',
178 [ -10 => 'minus ten', 0 => 'zero', 10 => 'ten' ],
179 ],
180 'maximal integers' => [
181 "['$max'] = 'near max', ['$max2'] = 'max+1', ['$min'] = 'min', ['$min2'] = 'min-1'",
182 [ $min => 'min', $min2 => 'min-1', $max => 'near max', $max2 => 'max+1' ],
183 ],
184 'collision (0)' => [
185 '[0] = "number zero", ["0"] = "string zero"',
186 new Exception( 'Collision for array key 0 when passing data from Lua to PHP.' ),
187 ],
188 'collision (float)' => [
189 '[1.5] = "number 1.5", ["1.5"] = "string 1.5"',
190 new Exception( 'Collision for array key 1.5 when passing data from Lua to PHP.' ),
191 ],
192 'collision (inf)' => [
193 '[1/0] = "number inf", ["inf"] = "string inf"',
194 new Exception( 'Collision for array key inf when passing data from Lua to PHP.' ),
195 ],
196 ];
197 }
198
199 public function testFreeFunctions() {
200 $interpreter = $this->newInterpreter();
201
202 // Test #1: Make sure freeing actually works
203 $ret = $interpreter->callFunction(
204 $interpreter->loadString( 'return function() return "testFreeFunction #1" end', 'test' )
205 );
206 $id = $ret[0]->id;
207 $interpreter->cleanupLuaChunks();
208 $this->assertEquals(
209 [ 'testFreeFunction #1' ], $interpreter->callFunction( $ret[0] ),
210 'Test that function #1 was not freed while a reference exists'
211 );
212 $ret = null;
213 $interpreter->cleanupLuaChunks();
214 $testfunc = new LuaStandaloneInterpreterFunction( $interpreter->id, $id );
215 try {
216 $interpreter->callFunction( $testfunc );
217 $this->fail( "Expected exception because function #1 should have been freed" );
218 } catch ( LuaError $e ) {
219 $this->assertEquals(
220 "function id $id does not exist", $e->messageArgs[1],
221 'Testing for expected error when calling a freed function #1'
222 );
223 }
224
225 // Test #2: Make sure constructing a new copy of the function works
226 $ret = $interpreter->callFunction(
227 $interpreter->loadString( 'return function() return "testFreeFunction #2" end', 'test' )
228 );
229 $id = $ret[0]->id;
230 $func = new LuaStandaloneInterpreterFunction( $interpreter->id, $id );
231 $ret = null;
232 $interpreter->cleanupLuaChunks();
233 $this->assertEquals(
234 [ 'testFreeFunction #2' ], $interpreter->callFunction( $func ),
235 'Test that function #2 was not freed while a reference exists'
236 );
237 $func = null;
238 $interpreter->cleanupLuaChunks();
239 $testfunc = new LuaStandaloneInterpreterFunction( $interpreter->id, $id );
240 try {
241 $interpreter->callFunction( $testfunc );
242 $this->fail( "Expected exception because function #2 should have been freed" );
243 } catch ( LuaError $e ) {
244 $this->assertEquals(
245 "function id $id does not exist", $e->messageArgs[1],
246 'Testing for expected error when calling a freed function #2'
247 );
248 }
249
250 // Test #3: Make sure cloning works
251 $ret = $interpreter->callFunction(
252 $interpreter->loadString( 'return function() return "testFreeFunction #3" end', 'test' )
253 );
254 $id = $ret[0]->id;
255 $func = clone $ret[0];
256 $ret = null;
257 $interpreter->cleanupLuaChunks();
258 $this->assertEquals(
259 [ 'testFreeFunction #3' ], $interpreter->callFunction( $func ),
260 'Test that function #3 was not freed while a reference exists'
261 );
262 $func = null;
263 $interpreter->cleanupLuaChunks();
264 $testfunc = new LuaStandaloneInterpreterFunction( $interpreter->id, $id );
265 try {
266 $interpreter->callFunction( $testfunc );
267 $this->fail( "Expected exception because function #3 should have been freed" );
268 } catch ( LuaError $e ) {
269 $this->assertEquals(
270 "function id $id does not exist", $e->messageArgs[1],
271 'Testing for expected error when calling a freed function #3'
272 );
273 }
274 }
275 }