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\Exception as HttpRequestException;
14 use http\Exception 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 try {
91 $b = $r->getResponseBody();
92 } catch (\Exception $e) {
93 echo $e->getMessage(), "\n";
94 $b = "";
95 }
96
97 printf("%d %s %d\n", $c, $u, strlen($b));
98
99 if ($c == 200 && $this->dir) {
100 file_put_contents($this->all[$u], $b);
101 }
102
103 if ($a = each($this->rem)) {
104 list($url, $file) = $a;
105 $this->attach(
106 $this->factory->createRequest(
107 $url,
108 HttpRequestMethod::GET,
109 array(
110 'redirect' => 5,
111 'compress' => GZIP,
112 'timeout' => TOUT,
113 'connecttimeout' => TOUT,
114 'lastmodified' => is_file($file)?filemtime($file):0
115 )
116 )
117 );
118 }
119 }
120 return $rc;
121 }
122 }
123
124 define('GZIP', true);
125 define('TOUT', 300);
126 define('RMAX', 10);
127 chdir(__DIR__);
128
129 $time = microtime(true);
130 $factory = new HttpRequestFactory(array("driver" => "curl", "requestPoolClass" => "Pool"));
131 $factory->createPool()->run($factory);
132 printf("Elapsed: %0.3fs\n", microtime(true)-$time);
133
134 echo "Done\n";
135 ?>
136 --EXPECTF--
137 %aTEST
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 %d %s %d
183 %d %s %d
184 %d %s %d
185 %d %s %d
186 %d %s %d
187 Elapsed: %fs
188 Done