<commit_before><?php
/**
 * Created by <NAME> <<EMAIL>>
 * Date: 23/10/14
 * Time: 16:53
 */
namespace Keboola\Syrup\Tests\Command;

use Keboola\Syrup\Test\Job\Executor\ErrorExecutor;
use Keboola\Syrup\Test\Job\Executor\HookExecutor;
use Keboola\Syrup\Test\Job\Executor\SignaledExectuor;
use Keboola\Syrup\Test\Job\Executor\SuccessExecutor;
use Keboola\Syrup\Test\Job\Executor\WarningExecutor;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Keboola\Syrup\Test\WebTestCase;
use Keboola\Syrup\Command\JobCommand;
use Keboola\Syrup\Job\Metadata\Job;
use Keboola\Syrup\Tests\Job as TestExecutor;
use Keboola\Syrup\Elasticsearch\JobMapper;
use Keboola\StorageApi\Client as StorageApiClient;
use Symfony\Component\Process\Process;

class JobCommandTest extends WebTestCase
{
    /**
     * @var Application
     */
    protected $application;
    protected $storageApiToken;
    /**
     * @var StorageApiClient;
     */
    protected $storageApiClient;


    protected function setUp()
    {
        $this->bootKernel();

        $this->application = new Application(self::$kernel);
        $this->application->add(new JobCommand());

        $this->storageApiToken = self::$kernel->getContainer()->getParameter('storage_api.test.token');
        $this->storageApiClient = new StorageApiClient([
            'token' => $this->storageApiToken,
            'url' => self::$kernel->getContainer()->getParameter('storage_api.test.url')
        ]);
    }

    public function testSignalJob()
    {
        /** @var JobMapper $jobMapper */
        $jobMapper = self::$kernel->getContainer()->get('syrup.elasticsearch.current_component_job_mapper');
        $encryptedToken = self::$kernel->getContainer()->get('syrup.encryptor')->encrypt($this->storageApiToken);

        // job execution test
        $jobId = $jobMapper->create($this->createJob($encryptedToken));

        $process = new Process(self::$kernel->getRootDir() . '/console syrup:run-job ' . $jobId . ' --env=test');
        $process->setTimeout(60);
        $process->setIdleTimeout(60);
        $process->start();

        // let it run for a while
        sleep(10);

        // terminate the job
        $process->signal(SIGTERM);

        while ($process->isRunning()) {
            // waiting for process to finish
            sleep(2);
        }

        $job = $jobMapper->get($jobId);

        $i = 0;
        while ($job->getVersion() < 3 && $i<5) {
            $job = $jobMapper->get($jobId);
            sleep(1 + pow(2, $i)/2);
            $i++;
        }

        $this->assertEquals(Job::STATUS_TERMINATED, $job->getStatus(), "job version: " . $job->getVersion());
    }

    public function testRunjob()
    {
        /** @var JobMapper $jobMapper */
        $jobMapper = self::$kernel->getContainer()->get('syrup.elasticsearch.current_component_job_mapper');
        $encryptedToken = self::$kernel->getContainer()->get('syrup.encryptor')->encrypt($this->storageApiToken);

        // job execution test
        $jobId = $jobMapper->create($this->createJob($encryptedToken));

        $command = $this->application->find('syrup:run-job');
        $commandTester = new CommandTester($command);
        $commandTester->execute([
            'jobId'   => $jobId
        ]);

        $this->assertEquals(0, $commandTester->getStatusCode());

        $job = $jobMapper->get($jobId);
        $this->assertEquals($job->getStatus(), Job::STATUS_SUCCESS);

        // replace executor with warning executor
        self::$kernel->getContainer()->set('syrup.job_executor', new WarningExecutor());<commit_msg>test: added some dumps to figure out whats goin on<commit_after><?php
/**
 * Created by <NAME> <<EMAIL>>
 * Date: 23/10/14
 * Time: 16:53
 */
namespace Keboola\Syrup\Tests\Command;

use Keboola\Syrup\Test\Job\Executor\ErrorExecutor;
use Keboola\Syrup\Test\Job\Executor\HookExecutor;
use Keboola\Syrup\Test\Job\Executor\SignaledExectuor;
use Keboola\Syrup\Test\Job\Executor\SuccessExecutor;
use Keboola\Syrup\Test\Job\Executor\WarningExecutor;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Keboola\Syrup\Test\WebTestCase;
use Keboola\Syrup\Command\JobCommand;
use Keboola\Syrup\Job\Metadata\Job;
use Keboola\Syrup\Tests\Job as TestExecutor;
use Keboola\Syrup\Elasticsearch\JobMapper;
use Keboola\StorageApi\Client as StorageApiClient;
use Symfony\Component\Process\Process;

class JobCommandTest extends WebTestCase
{
    /**
     * @var Application
     */
    protected $application;
    protected $storageApiToken;
    /**
     * @var StorageApiClient;
     */
    protected $storageApiClient;


    protected function setUp()
    {
        $this->bootKernel();

        $this->application = new Application(self::$kernel);
        $this->application->add(new JobCommand());

        $this->storageApiToken = self::$kernel->getContainer()->getParameter('storage_api.test.token');
        $this->storageApiClient = new StorageApiClient([
            'token' => $this->storageApiToken,
            'url' => self::$kernel->getContainer()->getParameter('storage_api.test.url')
        ]);
    }

    public function testSignalJob()
    {
        /** @var JobMapper $jobMapper */
        $jobMapper = self::$kernel->getContainer()->get('syrup.elasticsearch.current_component_job_mapper');
        $encryptedToken = self::$kernel->getContainer()->get('syrup.encryptor')->encrypt($this->storageApiToken);

        // job execution test
        $jobId = $jobMapper->create($this->createJob($encryptedToken));

        $process = new Process(self::$kernel->getRootDir() . '/console syrup:run-job ' . $jobId . ' --env=test');
        $process->setTimeout(60);
        $process->setIdleTimeout(60);
        $process->start();

        // let it run for a while
        sleep(10);

        // terminate the job
        $process->signal(SIGTERM);

        while ($process->isRunning()) {
            // waiting for process to finish
            sleep(2);
        }

        $job = $jobMapper->get($jobId);

        $i = 0;
        while ($job->getVersion() < 3 && $i<5) {
            $job = $jobMapper->get($jobId);
            sleep(1 + pow(2, $i)/2);
            $i++;
        }

        var_dump($process->getOutput());

        var_dump($process->getErrorOutput());

        $this->assertEquals(0, $process->getExitCode());

        $this->assertEquals(Job::STATUS_TERMINATED, $job->getStatus(), "job version: " . $job->getVersion() . " data " . var_export($job->getData()));
    }

    public function testRunjob()
    {
        /** @var JobMapper $jobMapper */
        $jobMapper = self::$kernel->getContainer()->get('syrup.elasticsearch.current_component_job_mapper');
        $encryptedToken = self::$kernel->getContainer()->get('syrup.encryptor')->encrypt($this->storageApiToken);

        // job execution test
        $jobId = $jobMapper->create($this->createJob($encryptedToken));

        $command = $this->application->find('syrup:run-job');
        $commandTester = new CommandTester($command);
        $commandTester->execute([
            'jobId'   => $jobId
        ]);

        $this->assertEquals(0, $commandTester->getStatusCode());

        $job = $jobMapper->get($jobId);
        $this->assertEquals($job->getStatus(), Job::STATUS_SUCCESS);

        // replace executor with warning executor
        self::$kernel->getContainer()->set('syrup.job_executor', new WarningExecutor());
{'fraction_non_alphanumeric': 0.158056334267788, 'fraction_numerical': 0.006541182752636497, 'mean_word_length': 3.032292787944026, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 118, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '2359471', 'n_tokens_mistral': 2283, 'n_tokens_neox': 2273, 'n_words': 364}