extesion deps
[m6w6/ext-http] / tests / requestpool001.phpt
1 --TEST--
2 HttpRequestPool chain
3 --SKIPIF--
4 <?php
5 include 'skipif.inc';
6 ?>
7 --FILE--
8 <?php
9
10 echo "-TEST\n";
11
12 set_time_limit(0);
13 ini_set('error_reporting', E_ALL);
14 ini_set('html_errors', 0);
15
16 class Pool extends \http\Curl\Client\Pool
17 {
18 private $all;
19 private $rem;
20 private $dir;
21
22 private $factory;
23
24 public final function run($factory, $urls_file = 'data/urls.txt', $cache_dir = 'HttpRequestPool_cache')
25 {
26 $this->factory = $factory;
27 $this->dir = (is_dir($cache_dir) or @mkdir($cache_dir)) ? $cache_dir : null;
28
29 foreach (array_map('trim', file($urls_file)) as $url) {
30 $this->all[$url] = $this->dir ? $this->dir .'/'. md5($url) : null;
31 }
32
33 $this->send();
34 }
35
36 public final function send()
37 {
38 if (RMAX) {
39 $now = array_slice($this->all, 0, RMAX);
40 $this->rem = array_slice($this->all, RMAX);
41 } else {
42 $now = $urls;
43 $this->rem = array();
44 }
45
46 foreach ($now as $url => $file) {
47 $this->attach(
48 $this->factory->createClient(
49 array(
50 'redirect' => 5,
51 'compress' => GZIP,
52 'timeout' => TOUT,
53 'connecttimeout' => TOUT,
54 'lastmodified' => is_file($file)?filemtime($file):0
55 )
56 )->setRequest(new http\Client\Request("GET", $url))
57 );
58 }
59
60 while ($this->once()) {
61 if (!$this->wait()) {
62 throw new http\Exception;
63 }
64 }
65 }
66
67 protected final function once()
68 {
69 try {
70 $rc = parent::once();
71 } catch (http\Exception $x) {
72 // a request may have thrown an exception,
73 // but it is still save to continue
74 echo $x->getMessage(), "\n";
75 }
76
77 foreach ($this->getFinished() as $r) {
78 $this->detach($r);
79
80 $u = $r->getRequest()->getRequestUrl();
81 $c = $r->getResponseMessage()->getResponseCode();
82 try {
83 $b = $r->getResponseMessage()->getBody();
84 } catch (\Exception $e) {
85 echo $e->getMessage(), "\n";
86 $b = "";
87 }
88
89 printf("%d %s %d\n", $c, $u, strlen($b));
90
91 if ($c == 200 && $this->dir) {
92 file_put_contents($this->all[$u], $b);
93 }
94
95 if ($a = each($this->rem)) {
96 list($url, $file) = $a;
97 $this->attach(
98 $this->factory->createClient(
99 array(
100 'redirect' => 5,
101 'compress' => GZIP,
102 'timeout' => TOUT,
103 'connecttimeout' => TOUT,
104 'lastmodified' => is_file($file)?filemtime($file):0
105 )
106 )->setRequest(new http\Client\Request("GET", $url))
107 );
108 }
109 }
110 return $rc;
111 }
112 }
113
114 define('GZIP', true);
115 define('TOUT', 300);
116 define('RMAX', 10);
117 chdir(__DIR__);
118
119 $time = microtime(true);
120 $factory = new http\Client\Factory(array("driver" => "curl", "clientPoolClass" => "Pool"));
121 $factory->createPool()->run($factory);
122 printf("Elapsed: %0.3fs\n", microtime(true)-$time);
123
124 echo "Done\n";
125 ?>
126 --EXPECTF--
127 %aTEST
128 %d %s %d
129 %d %s %d
130 %d %s %d
131 %d %s %d
132 %d %s %d
133 %d %s %d
134 %d %s %d
135 %d %s %d
136 %d %s %d
137 %d %s %d
138 %d %s %d
139 %d %s %d
140 %d %s %d
141 %d %s %d
142 %d %s %d
143 %d %s %d
144 %d %s %d
145 %d %s %d
146 %d %s %d
147 %d %s %d
148 %d %s %d
149 %d %s %d
150 %d %s %d
151 %d %s %d
152 %d %s %d
153 %d %s %d
154 %d %s %d
155 %d %s %d
156 %d %s %d
157 %d %s %d
158 %d %s %d
159 %d %s %d
160 %d %s %d
161 %d %s %d
162 %d %s %d
163 %d %s %d
164 %d %s %d
165 %d %s %d
166 %d %s %d
167 %d %s %d
168 %d %s %d
169 %d %s %d
170 %d %s %d
171 %d %s %d
172 %d %s %d
173 %d %s %d
174 %d %s %d
175 %d %s %d
176 %d %s %d
177 Elapsed: %fs
178 Done