tests
[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 use http\request\Factory as HttpRequestFactory;
11 use http\request\Pool as HttpRequestPool;
12 use http\request\Method as HttpRequestMethod;
13 use http\RequestException as HttpRequestException;
14 use http\SocketException as HttpSocketException;
15
16 echo "-TEST\n";
17
18 set_time_limit(0);
19 ini_set('error_reporting', E_ALL);
20 ini_set('html_errors', 0);
21
22 class Pool extends HttpRequestPool
23 {
24 private $all;
25 private $rem;
26 private $dir;
27
28 private $factory;
29
30 public final function run($factory, $urls_file = 'data/urls.txt', $cache_dir = 'HttpRequestPool_cache')
31 {
32 $this->factory = $factory;
33 $this->dir = (is_dir($cache_dir) or @mkdir($cache_dir)) ? $cache_dir : null;
34
35 foreach (array_map('trim', file($urls_file)) as $url) {
36 $this->all[$url] = $this->dir ? $this->dir .'/'. md5($url) : null;
37 }
38
39 $this->send();
40 }
41
42 public final function send()
43 {
44 if (RMAX) {
45 $now = array_slice($this->all, 0, RMAX);
46 $this->rem = array_slice($this->all, RMAX);
47 } else {
48 $now = $urls;
49 $this->rem = array();
50 }
51
52 foreach ($now as $url => $file) {
53 $this->attach(
54 $this->factory->createRequest(
55 $url,
56 HttpRequestMethod::GET,
57 array(
58 'redirect' => 5,
59 'compress' => GZIP,
60 'timeout' => TOUT,
61 'connecttimeout' => TOUT,
62 'lastmodified' => is_file($file)?filemtime($file):0
63 )
64 )
65 );
66 }
67
68 while ($this->once()) {
69 if (!$this->wait()) {
70 throw new HttpSocketException;
71 }
72 }
73 }
74
75 protected final function once()
76 {
77 try {
78 $rc = parent::once();
79 } catch (HttpRequestException $x) {
80 // a request may have thrown an exception,
81 // but it is still save to continue
82 echo $x->getMessage(), "\n";
83 }
84
85 foreach ($this->getFinishedRequests() as $r) {
86 $this->detach($r);
87
88 $u = $r->getUrl();
89 $c = $r->getResponseCode();
90 $b = $r->getResponseBody();
91
92 printf("%d %s %d\n", $c, $u, strlen($b));
93
94 if ($c == 200 && $this->dir) {
95 file_put_contents($this->all[$u], $b);
96 }
97
98 if ($a = each($this->rem)) {
99 list($url, $file) = $a;
100 $this->attach(
101 $this->factory->createRequest(
102 $url,
103 HttpRequestMethod::GET,
104 array(
105 'redirect' => 5,
106 'compress' => GZIP,
107 'timeout' => TOUT,
108 'connecttimeout' => TOUT,
109 'lastmodified' => is_file($file)?filemtime($file):0
110 )
111 )
112 );
113 }
114 }
115 return $rc;
116 }
117 }
118
119 define('GZIP', true);
120 define('TOUT', 50);
121 define('RMAX', 10);
122 chdir(__DIR__);
123
124 $time = microtime(true);
125 $factory = new HttpRequestFactory("curl", array("requestPoolClass" => "Pool"));
126 $factory->createPool()->run($factory);
127 printf("Elapsed: %0.3fs\n", microtime(true)-$time);
128
129 echo "Done\n";
130 ?>
131 --EXPECTF--
132 %aTEST
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 %d %s %d
178 %d %s %d
179 %d %s %d
180 %d %s %d
181 %d %s %d
182 Elapsed: %fs
183 Done