<commit_before><?php

/**
 * @testCase
 */

namespace stekycz\Cronner\tests;

use Nette\Utils\FileSystem;
use stekycz\Cronner\CriticalSection;
use Tester\Assert;



require_once(__DIR__ . "/bootstrap.php");

/**
 * @author <NAME> <<EMAIL>>
 */
class CriticalSectionTest extends \TestCase
{

	const TEST_LABEL = "test";

	/**
	 * @var \stekycz\Cronner\CriticalSection
	 */
	private $criticalSection;



	protected function setUp()
	{
		parent::setUp();
		$filesDir = TEMP_DIR . "/cronner/critical-section";
		FileSystem::createDir($filesDir);
		$this->criticalSection = new CriticalSection($filesDir);
	}



	protected function tearDown()
	{
		parent::tearDown();
		if ($this->criticalSection->isEntered(self::TEST_LABEL)) {
			$this->criticalSection->leave(self::TEST_LABEL);
		}
	}



	public function testCanBeEnteredAndLeaved()
	{
		Assert::false($this->criticalSection->isEntered(self::TEST_LABEL));
		Assert::true($this-> criticalSection->enter(self::TEST_LABEL));
		Assert::true($this->criticalSection->isEntered(self::TEST_LABEL));
		Assert::true($this->criticalSection->leave(self::TEST_LABEL));
		Assert::false($this->criticalSection->isEntered(self::TEST_LABEL));
	}



	public function testCannotBeEnteredTwice()
	{
		Assert::true($this->criticalSection->enter(self::TEST_LABEL));
		Assert::true($this->criticalSection->isEntered(self::TEST_LABEL));
		Assert::false($this->criticalSection->enter(self::TEST_LABEL));
	}



	public function testCannotBeLeavedWithoutEnter()
	{
		Assert::false($this->criticalSection->isEntered(self::TEST_LABEL));
		Assert::false($this->criticalSection->leave(self::TEST_LABEL));
	}



	public function testCannotBeLeavedTwice()
	{
		Assert::true($this->criticalSection->enter(self::TEST_LABEL));
		Assert::true($this->criticalSection->isEntered(self::TEST_LABEL));
		Assert::true($this->criticalSection->leave(self::TEST_LABEL));
		Assert::false($this->criticalSection->leave(self::TEST_LABEL));
	}

}

run(new CriticalSectionTest());
<commit_msg>Added test to check correct functionality of two independent handlers<commit_after><?php

/**
 * @testCase
 */

namespace stekycz\Cronner\tests;

use Nette\Utils\FileSystem;
use stekycz\Cronner\CriticalSection;
use Tester\Assert;



require_once(__DIR__ . "/bootstrap.php");

/**
 * @author <NAME> <<EMAIL>>
 */
class CriticalSectionTest extends \TestCase
{

	const TEST_LABEL = "test";

	/**
	 * @var \stekycz\Cronner\CriticalSection
	 */
	private $criticalSection;

	/**
	 * @var string
	 */
	private $filesDir;



	protected function setUp()
	{
		parent::setUp();
		$this->filesDir = TEMP_DIR . "/cronner/critical-section";
		FileSystem::createDir($this->filesDir);
		$this->criticalSection = new CriticalSection($this->filesDir);
	}



	protected function tearDown()
	{
		parent::tearDown();
		if ($this->criticalSection->isEntered(self::TEST_LABEL)) {
			$this->criticalSection->leave(self::TEST_LABEL);
		}
	}



	public function testCanBeEnteredAndLeaved()
	{
		Assert::false($this->criticalSection->isEntered(self::TEST_LABEL));
		Assert::true($this-> criticalSection->enter(self::TEST_LABEL));
		Assert::true($this->criticalSection->isEntered(self::TEST_LABEL));
		Assert::true($this->criticalSection->leave(self::TEST_LABEL));
		Assert::false($this->criticalSection->isEntered(self::TEST_LABEL));
	}



	public function testCannotBeEnteredTwice()
	{
		Assert::true($this->criticalSection->enter(self::TEST_LABEL));
		Assert::true($this->criticalSection->isEntered(self::TEST_LABEL));
		Assert::false($this->criticalSection->enter(self::TEST_LABEL));
	}



	public function testCannotBeLeavedWithoutEnter()
	{
		Assert::false($this->criticalSection->isEntered(self::TEST_LABEL));
		Assert::false($this->criticalSection->leave(self::TEST_LABEL));
	}



	public function testCannotBeLeavedTwice()
	{
		Assert::true($this->criticalSection->enter(self::TEST_LABEL));
		Assert::true($this->criticalSection->isEntered(self::TEST_LABEL));
		Assert::true($this->criticalSection->leave(self::TEST_LABEL));
		Assert::false($this->criticalSection->leave(self::TEST_LABEL));
	}



	public function testMultipleCriticalSectionHandlers()
	{
		$criticalSection = $this->criticalSection;
		$criticalSection2 = new CriticalSection($this->filesDir);

		Assert::false($criticalSection->isEntered(self::TEST_LABEL));
		Assert::false($criticalSection2->isEntered(self::TEST_LABEL));
		Assert::true($criticalSection->enter(self::TEST_LABEL));
		Assert::false($criticalSection2->enter(self::TEST_LABEL));
		Assert::true($criticalSection->isEntered(self::TEST_LABEL));
		Assert::false($criticalSection2->isEntered(self::TEST_LABEL));
		Assert::true($criticalSection->leave(self::TEST_LABEL));
		Assert::false($criticalSection2->leave(self::TEST_LABEL));
		Assert::false($criticalSection->isEntered(self::TEST_LABEL));
		Assert::false($criticalSection2->isEntered(self::TEST_LABEL));
	}

}

run(new CriticalSectionTest());

{'fraction_non_alphanumeric': 0.17173779865223607, 'fraction_numerical': 0.001225239942822136, 'mean_word_length': 6.9, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 90, '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': '3190650', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1576, 'n_words': 177}