<commit_before><?php
namespace FutureProcess;

use Symfony\Component\Process\PhpExecutableFinder;

/**
 * @author <NAME> <<EMAIL>>
 */
class IntegrationTest extends \PHPUnit_Framework_TestCase
{
    private $phpExecutablePath;
    
    public function __construct($name = null, array $data = array(), $dataName = '')
    {
        parent::__construct($name, $data, $dataName);
        
        $finder = new PhpExecutableFinder;
        $this->phpExecutablePath = $finder->find();
    }
    
    public function testLargeIO()
    {
        $shell = new Shell;
        
        $process = $shell->startProcess(sprintf('%s -r %s',
            $this->phpExecutablePath,
            escapeshellarg(
                '$stdin = fopen("php://stdin", "r");' .
                '$data = "";' .
                'while (strlen($data) < 10000000) {' .
                    '$data .= fread($stdin, 2 << 18);' .
                '}' .
                'echo $data;'
            )
        ));
        
        $process->writeToBuffer(0, str_repeat("X", 10000000));
        $process->getResult()->wait(5);
        $this->assertSame(0, $process->getResult()->getExitCode());
        $this->assertSame(10000000, strlen($process->readFromBuffer(1)));
    }
    
    public function testProcessTimeLimitExceeded()
    {
        $shell = new Shell;
        
        $command = sprintf('%s -r %s',
            $this->phpExecutablePath,
            escapeshellarg(
                'echo "Hello world!";' .
                'sleep(1);' .
                'echo "Goodbye world!";'
            )
        );
        
        $thrown = new Exception;
        
        $process = $shell->startProcess($command, array(
            'timeout' => 0.5,
            'timeout_error' => $thrown,
        ));
        
        $process->wait(1);
        
        try {
            $process->getResult()->wait(2);
            $this->fail('The expected exception was not thrown');
        } catch (Exception $caught) {
            $this->assertSame($thrown, $caught);
        }
        
        $process->wait(1);
        
        $this->assertSame('Hello world!', $process->readFromBuffer(1));
    }
    
    public function testQuietlyAbortRunningProcess()
    {
        $shell = new Shell;
        $process = $shell->startProcess($this->phpSleepCommand(0.5));
        
        $process->then(function ($process) {
            $process->abort();
        });
        
        $process->wait(0.5); // this should not error
        $process->getResult()->wait(1); // this should not error
        $process->wait(0); // this should not error
        
        $that = $this;
        $processPromiseResolved = false;
        $process->then(
            function () use (&$processPromiseResolved) {
                $processPromiseResolved = true;
            },
            function () use ($that) {
                $that->fail();
            }
        );
        $this->assertTrue($processPromiseResolved);
        
        $processPromiseResolved = false;
        $process->then(
            function () use (&$processPromiseResolved) {
                $processPromiseResolved = true;
            },
            function () use ($that) {
                $that->fail();
            }
        );
        $this->assertTrue($processPromiseResolved);
    }
    
    public function testQuietlyAbortQueuedProcess()
    {
        $shell = new Shell;
        $shell->setProcessLimit(1);
        $process1 = $shell->startProcess($this->phpSleepCommand(0.5));
        $process2 = $shell->startProcess($this->phpSleepCommand(0.5));
        
        $this->assertSame(FutureProcess::STATUS_RUNNING, $process1->getStatus());
        $this->assertSame(FutureProcess::STATUS_QUEUED, $process2->getStatus());
        
        $process2->abort();
        
        $process2->wait(0);
        $process2->getResult()->wait(0);
        
        $that = $this;
        $processPromiseResolved = false;
        $process2->then(
            function () use (&$processPromiseResolved) {
                $processPromiseResolved = true;
            },
            function () use ($that) {
                $that->fail();
            }
        );
        $this->assertTrue($processPromiseResolved);
        
        $processPromiseResolved = false;
        $process2->then(
            function () use (&$processPromiseResolved) {
                $processPromiseResolved = true;
            },
            function () use ($that) {
                $that->fail();
            }
        );
        $this->assertTrue($processPromiseResolved);
    }
    
    public function testLateAbort()
    {
        $shell = new Shell;
        
        $process = $shell->startProcess(sprintf('%s -r %s',
            $this->phpExecutablePath,
            escapeshellarg('echo "Hello world!";')
        ));
        
        $process->getResult()->wait(0.5);
        $process->abort(new Exception);
        $process->wait(0);<commit_msg>Wait longer for HHVM processes<commit_after><?php
namespace FutureProcess;

use Symfony\Component\Process\PhpExecutableFinder;

/**
 * @author <NAME> <<EMAIL>>
 */
class IntegrationTest extends \PHPUnit_Framework_TestCase
{
    private $phpExecutablePath;
    
    public function __construct($name = null, array $data = array(), $dataName = '')
    {
        parent::__construct($name, $data, $dataName);
        
        $finder = new PhpExecutableFinder;
        $this->phpExecutablePath = $finder->find();
    }
    
    public function testLargeIO()
    {
        $shell = new Shell;
        
        $process = $shell->startProcess(sprintf('%s -r %s',
            $this->phpExecutablePath,
            escapeshellarg(
                '$stdin = fopen("php://stdin", "r");' .
                '$data = "";' .
                'while (strlen($data) < 10000000) {' .
                    '$data .= fread($stdin, 2 << 18);' .
                '}' .
                'echo $data;'
            )
        ));
        
        $process->writeToBuffer(0, str_repeat("X", 10000000));
        $process->getResult()->wait(5);
        $this->assertSame(0, $process->getResult()->getExitCode());
        $this->assertSame(10000000, strlen($process->readFromBuffer(1)));
    }
    
    public function testProcessTimeLimitExceeded()
    {
        $shell = new Shell;
        
        $command = sprintf('%s -r %s',
            $this->phpExecutablePath,
            escapeshellarg(
                'echo "Hello world!";' .
                'sleep(1);' .
                'echo "Goodbye world!";'
            )
        );
        
        $thrown = new Exception;
        
        $process = $shell->startProcess($command, array(
            'timeout' => 0.5,
            'timeout_error' => $thrown,
        ));
        
        $process->wait(1);
        
        try {
            $process->getResult()->wait(2);
            $this->fail('The expected exception was not thrown');
        } catch (Exception $caught) {
            $this->assertSame($thrown, $caught);
        }
        
        $process->wait(1);
        
        $this->assertSame('Hello world!', $process->readFromBuffer(1));
    }
    
    public function testQuietlyAbortRunningProcess()
    {
        $shell = new Shell;
        $process = $shell->startProcess($this->phpSleepCommand(0.5));
        
        $process->then(function ($process) {
            $process->abort();
        });
        
        $process->wait(0.5); // this should not error
        $process->getResult()->wait(1); // this should not error
        $process->wait(0); // this should not error
        
        $that = $this;
        $processPromiseResolved = false;
        $process->then(
            function () use (&$processPromiseResolved) {
                $processPromiseResolved = true;
            },
            function () use ($that) {
                $that->fail();
            }
        );
        $this->assertTrue($processPromiseResolved);
        
        $processPromiseResolved = false;
        $process->then(
            function () use (&$processPromiseResolved) {
                $processPromiseResolved = true;
            },
            function () use ($that) {
                $that->fail();
            }
        );
        $this->assertTrue($processPromiseResolved);
    }
    
    public function testQuietlyAbortQueuedProcess()
    {
        $shell = new Shell;
        $shell->setProcessLimit(1);
        $process1 = $shell->startProcess($this->phpSleepCommand(0.5));
        $process2 = $shell->startProcess($this->phpSleepCommand(0.5));
        
        $this->assertSame(FutureProcess::STATUS_RUNNING, $process1->getStatus());
        $this->assertSame(FutureProcess::STATUS_QUEUED, $process2->getStatus());
        
        $process2->abort();
        
        $process2->wait(0);
        $process2->getResult()->wait(0);
        
        $that = $this;
        $processPromiseResolved = false;
        $process2->then(
            function () use (&$processPromiseResolved) {
                $processPromiseResolved = true;
            },
            function () use ($that) {
                $that->fail();
            }
        );
        $this->assertTrue($processPromiseResolved);
        
        $processPromiseResolved = false;
        $process2->then(
            function () use (&$processPromiseResolved) {
                $processPromiseResolved = true;
            },
            function () use ($that) {
                $that->fail();
            }
        );
        $this->assertTrue($processPromiseResolved);
    }
    
    public function testLateAbort()
    {
        $shell = new Shell;
        
        $process = $shell->startProcess(sprintf('%s -r %s',
            $this->phpExecutablePath,
            escapeshellarg('echo "Hello world!";')
        ));
        
        $process->getResult()->wait(2);
        $process->abort(new Exception);
        $process->wait(0);
{'fraction_non_alphanumeric': 0.1453682190120214, 'fraction_numerical': 0.012627538135165168, 'mean_word_length': 1.6727861771058314, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 135, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '632168', 'n_tokens_mistral': 2795, 'n_tokens_neox': 2733, 'n_words': 459}