<commit_before><?php
namespace Icicle\Tests\Concurrent\Sync;

use Icicle\Concurrent\Sync\Channel;
use Icicle\Coroutine;
use Icicle\Loop;
use Icicle\Socket\Stream\DuplexStream;
use Icicle\Tests\Concurrent\TestCase;

class ChannelTest extends TestCase
{
    public function testCreate()
    {
        list($a, $b) = Channel::createSocketPair();

        $this->assertInternalType('resource', $a);
        $this->assertInternalType('resource', $b);
    }

    public function testClose()
    {
        list($a, $b) = Channel::createSocketPair();
        $a = new Channel(new DuplexStream($a));
        $b = new Channel(new DuplexStream($b));

        // Close $a. $b should close on next read...
        $a->close();
        new Coroutine\Coroutine($b->receive());

        Loop\run();

        $this->assertFalse($a->isOpen());
        $this->assertFalse($b->isOpen());
    }

    public function testSendReceive()
    {
        Coroutine\create(function () {
            list($a, $b) = Channel::createSocketPair();
            $a = new Channel(new DuplexStream($a));
            $b = new Channel(new DuplexStream($b));

            yield $a->send('hello');
            $data = (yield $b->receive());
            $this->assertEquals('hello', $data);
        })->done();

        Loop\run();
    }

    /**
     * @group threading
     */
    public function testThreadTransfer()
    {
        list($a, $b) = Channel::createSocketPair();
        $b = new Channel(new DuplexStream($b));
<commit_msg>Close channels when finished<commit_after><?php
namespace Icicle\Tests\Concurrent\Sync;

use Icicle\Concurrent\Sync\Channel;
use Icicle\Coroutine;
use Icicle\Loop;
use Icicle\Socket\Stream\DuplexStream;
use Icicle\Tests\Concurrent\TestCase;

class ChannelTest extends TestCase
{
    public function testCreate()
    {
        list($a, $b) = Channel::createSocketPair();

        $this->assertInternalType('resource', $a);
        $this->assertInternalType('resource', $b);
    }

    public function testClose()
    {
        list($a, $b) = Channel::createSocketPair();
        $a = new Channel(new DuplexStream($a));
        $b = new Channel(new DuplexStream($b));

        // Close $a. $b should close on next read...
        $a->close();
        new Coroutine\Coroutine($b->receive());

        Loop\run();

        $this->assertFalse($a->isOpen());
        $this->assertFalse($b->isOpen());
    }

    public function testSendReceive()
    {
        Coroutine\create(function () {
            list($a, $b) = Channel::createSocketPair();
            $a = new Channel(new DuplexStream($a));
            $b = new Channel(new DuplexStream($b));

            yield $a->send('hello');
            $data = (yield $b->receive());
            $this->assertEquals('hello', $data);

            $a->close();
            $b->close();
        })->done();

        Loop\run();
    }

    /**
     * @group threading
     */
    public function testThreadTransfer()
    {
        list($a, $b) = Channel::createSocketPair();
        $b = new Channel(new DuplexStream($b));

{'fraction_non_alphanumeric': 0.1642670157068063, 'fraction_numerical': 0.0, 'mean_word_length': 2.423292273236282, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '634559', 'n_tokens_mistral': 951, 'n_tokens_neox': 919, 'n_words': 187}