public function providerPromise() { $promise = new \ION\ResolvablePromise(); $promise->done("promise_result"); return array(["result", "result"], [$promise, "promise_result"], [function () { return "cb_result"; }, "cb_result"], [function () { (yield ION::await(0.1)); return "yield_result"; }, "yield_result"], [function () { return ION::await(0.1); }, true]); }
/** * @memcheck */ public function testSignals() { Process::signal(SIGHUP)->then(function ($signo) { $this->data["signal"] = $signo; })->setName("test"); $this->promise(function () { (yield \ION::await(0.01)); Process::kill(SIGHUP, getmypid()); (yield \ION::await(0.01)); }); $this->loop(); Process::signal(SIGHUP)->forget('test'); $this->assertEquals(["signal" => SIGHUP], $this->data); }
/** * @memcheck */ public function testWatch() { $file = ION_VAR . "/iddqd"; @unlink($file); file_put_contents($file, "a1"); $expected = realpath($file); FS::watch($file)->then(function ($files) { $this->data["changed"] = $files; })->setName('test'); $this->promise(function () use($file) { (yield \ION::await(0.02)); @unlink($file); // file_put_contents('/tmp/iddqd', "a2", FILE_APPEND); (yield \ION::await(0.1)); }); $this->loop(); FS::unwatchAll(); // FS::watch($file)->forget('test'); $this->assertCount(1, $this->data["changed"]); $this->assertArrayHasKey($expected, $this->data["changed"]); }
/** * @memcheck */ public function testDisconnect() { list($one, $this->tmp) = IPC::create((string) "one1", (string) "two2"); /* @var IPC $one */ /* @var IPC $two */ $one->whenIncoming()->then(function ($data) { $this->data[] = $data; }); $one->whenDisconnected()->then(function ($ctx) { $this->data[] = $ctx; }); \ION::await(0.02)->then(function () { $this->tmp->send("test1"); }); \ION::await(0.03)->then(function () { unset($this->tmp); }); \ION::await(0.04)->then(function () { $this->stop(); }); \ION::dispatch(); $this->assertEquals([$this->message("test1", "one1"), "one1"], $this->data); }
/** * @memcheck */ public function testAwaitFailedPromise() { $promise = new ResolvablePromise(); $promise->then(function ($x) { $this->data["x1"] = $x; return \ION::await(0.1)->then(function ($result) use($x) { $this->data["await"] = $result; throw new \RuntimeException("problem description"); }); })->onDone(function ($result) { $this->data["result"] = $result; $this->stop(); })->onFail(function ($error) { /* @var \Exception $error */ $this->data["error"] = $this->exception2array($error); $this->stop(); }); $promise->done(1); $this->loop(); $this->assertEquals(['x1' => 1, 'await' => true, 'error' => ['exception' => 'RuntimeException', 'message' => 'problem description', 'code' => 0]], $this->data); }