restore listeners and statements on connection reset
[m6w6/ext-pq] / tests / gh-issue015_listeners.phpt
1 --TEST--
2 restore listeners on reset
3 --SKIPIF--
4 <?php
5 include "_skipif.inc";
6 ?>
7 --INI--
8 date.timezone=UTC
9 --FILE--
10 <?php
11 echo "Test\n";
12
13 include "_setup.inc";
14
15 $c = new pq\Connection(PQ_DSN);
16
17 $c->listen("notify", function($channel, $message) {
18 printf("%s: %s\n", $channel, $message);
19 });
20 $c->on(pq\Connection::EVENT_RESET, function($conn) {
21 printf("Connection was reset\n");
22 });
23 $c->notify("notify", "Gotcha!");
24 $c->resetAsync();
25
26 // wait until the stream becomes writable
27 $w = array($c->socket);
28 $r = $e = null;
29
30 if (stream_select($r, $w, $e, null)) {
31
32 // loop until the connection is established
33 while (true) {
34
35 switch ($c->poll()) {
36
37 case pq\Connection::POLLING_READING:
38 // we should wait for the stream to be read-ready
39 $r = array($c->socket);
40 stream_select($r, $w, $e, NULL);
41 break;
42
43 case pq\Connection::POLLING_WRITING:
44 // we should wait for the stream to be write-ready
45 $w = array($c->socket);
46 $r = $e = null;
47 stream_select($r, $w, $e, null);
48 break;
49
50 case pq\Connection::POLLING_FAILED:
51 printf("Connection failed: %s\n", $c->errorMessage);
52 break 2;
53
54 case pq\Connection::POLLING_OK:
55 printf("Connection completed\n");
56 break 2;
57 }
58 }
59 }
60 $c->notify("notify", "Do you miss me?");
61 $c->exec("");
62 ?>
63 ===DONE===
64 --EXPECT--
65 Test
66 notify: Gotcha!
67 Connection was reset
68 Connection completed
69 notify: Do you miss me?
70 ===DONE===