Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 0eec353

Browse files
fcabralpachecoOcramius
authored andcommitted
Fix the way to check memory usage (now is HHVM compatible) and minor bugs/improvements.
1 parent 6a65323 commit 0eec353

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

test/Response/SapiStreamEmitterTest.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ public function testEmitMemoryUsage($seekable, $maxBufferLength, $sizeBlocks, $m
142142
$position = 0;
143143
}
144144

145+
$closureTrackMemoryUsage = function () use (&$peakMemoryUsage) {
146+
$memoryUsage = memory_get_usage();
147+
148+
if ($memoryUsage > $peakMemoryUsage) {
149+
$peakMemoryUsage = $memoryUsage;
150+
}
151+
};
152+
145153
$closureFullContents = function () use (&$sizeBytes) {
146154
return str_repeat('0', $sizeBytes);
147155
};
@@ -151,7 +159,7 @@ public function testEmitMemoryUsage($seekable, $maxBufferLength, $sizeBlocks, $m
151159
$stream->isSeekable()->willReturn($seekable);
152160
$stream->isReadable()->willReturn(true);
153161
$stream->__toString()->will($closureFullContents);
154-
$stream->getContents()->willReturn($closureFullContents);
162+
$stream->getContents()->will($closureFullContents);
155163
$stream->rewind()->willReturn(true);
156164

157165
$stream->seek(Argument::type('integer'), Argument::any())->will(function ($args) use (&$position) {
@@ -163,12 +171,17 @@ public function testEmitMemoryUsage($seekable, $maxBufferLength, $sizeBlocks, $m
163171
return ($position >= $sizeBytes);
164172
});
165173

174+
$stream->tell()->will(function () use (&$position) {
175+
return $position;
176+
});
177+
166178
$stream->read(Argument::type('integer'))->will(function ($args) use (&$position, &$peakBufferLength) {
167179
if ($args[0] > $peakBufferLength) {
168180
$peakBufferLength = $args[0];
169181
}
170182

171183
$position += $args[0];
184+
172185
return str_repeat('0', $args[0]);
173186
});
174187

@@ -178,28 +191,17 @@ public function testEmitMemoryUsage($seekable, $maxBufferLength, $sizeBlocks, $m
178191

179192

180193
if ($rangeBlocks) {
181-
$response->withHeader('Content-Range', "bytes $first-$last/*");
194+
$response = $response->withHeader('Content-Range', "bytes $first-$last/*");
182195
}
183196

184-
ob_start(function ($output) {
197+
ob_start(function ($output) use (&$closureTrackMemoryUsage) {
198+
call_user_func($closureTrackMemoryUsage);
185199
return "";
186200
}, $maxBufferLength);
187201

188-
$closureTrackMemoryUsage = function () use (&$peakMemoryUsage) {
189-
$memoryUsage = memory_get_usage();
190-
191-
if ($memoryUsage > $peakMemoryUsage) {
192-
$peakMemoryUsage = $memoryUsage;
193-
}
194-
};
195-
196-
register_tick_function($closureTrackMemoryUsage);
202+
gc_collect_cycles();
197203

198-
declare (ticks = 1) {
199-
$this->emitter->emit($response, $maxBufferLength);
200-
}
201-
202-
unregister_tick_function($closureTrackMemoryUsage);
204+
$this->emitter->emit($response, $maxBufferLength);
203205

204206
ob_end_flush();
205207

0 commit comments

Comments
 (0)