Skip to content

Commit

Permalink
php version depends with promise/coroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
moolex committed Jul 26, 2019
1 parent be22c9a commit 3ca7f2e
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
1 change: 1 addition & 0 deletions .travis.yml
@@ -1,6 +1,7 @@
language: php

php:
- '7.1'
- '7.2'
- '7.3'

Expand Down
1 change: 0 additions & 1 deletion composer.json
Expand Up @@ -2,7 +2,6 @@
"name": "carno-php/channel",
"license": "MIT",
"require": {
"php": "^7.2",
"carno-php/coroutine": "^1.0",
"carno-php/promise": "^1.0"
},
Expand Down
14 changes: 7 additions & 7 deletions src/Channel.php
Expand Up @@ -53,9 +53,9 @@ class Channel implements Chan
public function __construct(int $cap = 1)
{
$this->cap = $cap;
$this->qData = new SplQueue;
$this->qSend = new SplQueue;
$this->qRecv = new SplQueue;
$this->qData = new SplQueue();
$this->qSend = new SplQueue();
$this->qRecv = new SplQueue();
}

/**
Expand All @@ -74,7 +74,7 @@ public function cap() : int
public function send($data = null, Context $ctx = null) : Promised
{
if ($this->closing) {
throw new ChannelClosingException;
throw new ChannelClosingException();
}

/**
Expand Down Expand Up @@ -104,7 +104,7 @@ public function send($data = null, Context $ctx = null) : Promised
public function recv() : Promised
{
if ($this->closing) {
throw new ChannelClosingException;
throw new ChannelClosingException();
}

/**
Expand Down Expand Up @@ -143,12 +143,12 @@ public function close() : void

while ($this->qSend->count() > 0) {
$send = $this->qSend->dequeue();
$send->throw(new ChannelClosingException);
$send->throw(new ChannelClosingException());
}

while ($this->qRecv->count() > 0) {
$recv = $this->qRecv->dequeue();
$recv->throw(new ChannelClosingException);
$recv->throw(new ChannelClosingException());
}

$this->closed()->resolve();
Expand Down
2 changes: 1 addition & 1 deletion src/Worker.php
Expand Up @@ -68,7 +68,7 @@ public function __construct(Chan $chan, Closure $program, Closure $failure = nul
$this->failure = $failure;

$this->processor = function ($data, Context $ctx = null) {
async($this->program, $ctx ?? new Context, $data)->then($this->done, $this->done);
async($this->program, $ctx ?? new Context(), $data)->then($this->done, $this->done);
};

$this->done = function ($e = null) {
Expand Down
26 changes: 13 additions & 13 deletions tests/ChannelTest.php
Expand Up @@ -15,18 +15,18 @@ class ChannelTest extends TestCase
{
public function testSR1()
{
$chain = new Channel;
$chain = new Channel();

$cs = 0;

$chain->recv()->then(function () use (&$cs) {
$cs ++;
$cs++;
});

$this->assertEquals('0|0|1', (string)$chain);

$chain->recv()->then(function () use (&$cs) {
$cs ++;
$cs++;
});

$this->assertEquals('0|0|2', (string)$chain);
Expand Down Expand Up @@ -56,7 +56,7 @@ public function testSR1()
$this->assertEquals(2, $cs);

$chain->recv()->then(function () use (&$cs) {
$cs ++;
$cs++;
});

$this->assertEquals('1|0|0', (string)$chain);
Expand All @@ -70,25 +70,25 @@ public function testSR2()
{
$chan = new Channel(1024);

for ($i = 0; $i < 2048; $i ++) {
for ($i = 0; $i < 2048; $i++) {
$chan->send($i);
}

$this->assertEquals('2048|1024|0', (string)$chan);

$r = 0;

for ($j = 0; $j < 2048 + 512; $j ++) {
for ($j = 0; $j < 2048 + 512; $j++) {
$chan->recv()->then(function () use (&$r) {
$r ++;
$r++;
});
}

$this->assertEquals(2048, $r);

$this->assertEquals('0|0|512', (string)$chan);

for ($k = 0; $k < 512; $k ++) {
for ($k = 0; $k < 512; $k++) {
$chan->send($k);
}

Expand All @@ -99,29 +99,29 @@ public function testSR2()

public function testSR3()
{
$chan = new Channel;
$chan = new Channel();

$r = 0;

for ($i = 0; $i < 1024; $i ++) {
for ($i = 0; $i < 1024; $i++) {
$chan->recv()->then(function () use (&$r) {
$r ++;
$r++;
});
}

$this->assertEquals(0, $r);

$this->assertEquals('0|0|1024', (string)$chan);

for ($j = 0; $j < 512; $j ++) {
for ($j = 0; $j < 512; $j++) {
$chan->send($j);
}

$this->assertEquals(512, $r);

$this->assertEquals('0|0|512', (string)$chan);

for ($k = 0; $k < 512; $k ++) {
for ($k = 0; $k < 512; $k++) {
$chan->send($k);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/WorkerTest.php
Expand Up @@ -26,7 +26,7 @@ public function testRun1()
$sum += $got;
});

for ($i = 1; $i <= 100; $i ++) {
for ($i = 1; $i <= 100; $i++) {
$chan->send($i);
}

Expand All @@ -46,7 +46,7 @@ public function testRun2()
throw new Exception('test1');
});

for ($i = 0; $i < 100; $i ++) {
for ($i = 0; $i < 100; $i++) {
$chan->send($i);
}

Expand All @@ -55,7 +55,7 @@ public function testRun2()

public function testFailure()
{
$chan = new Channel;
$chan = new Channel();

$em = null;
new Worker($chan, function (string $em) {
Expand Down

0 comments on commit 3ca7f2e

Please sign in to comment.