Skip to content

Commit 251e42a

Browse files
authored
Merge pull request #15 from clue-labs/write-on-data
Fix processing events when input writes during data event
2 parents 4ebf673 + ada7a5b commit 251e42a

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/ControlCodeParser.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public function handleData($data)
121121
$this->buffer = substr($this->buffer, $c0);
122122

123123
$this->emit('data', array($data));
124+
continue;
124125
}
125126

126127
// C0 is now at start of buffer

tests/ControlCodeParserTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,41 @@ public function testEmitsDataAndC0()
8787
$this->input->emit('data', array("hello world\n"));
8888
}
8989

90+
public function testEmitsDataInTwoChunksWithC0InBetween()
91+
{
92+
// first data event is everything before control code
93+
$first = $this->expectCallableOnceWith('hello');
94+
$this->parser->once('data', $first);
95+
96+
// second data event will fire with remaining buffer
97+
$second = $this->expectCallableOnceWith('world');
98+
$parser = $this->parser;
99+
$this->parser->once('data', function () use ($parser, $second) {
100+
$parser->once('data', $second);
101+
});
102+
103+
$this->input->emit('data', array("hello\nworld"));
104+
}
105+
106+
public function testEmitsDataInTwoChunkWithC0InBetweenWhileAddingDuringDataEvent()
107+
{
108+
// first data event is everything before control code
109+
$first = $this->expectCallableOnceWith('hello');
110+
$this->parser->once('data', $first);
111+
112+
// second data event will fire with remaining buffer
113+
// we write to buffer while processing the first event and expect the full buffer
114+
$second = $this->expectCallableOnceWith('worldagain');
115+
$input = $this->input;
116+
$parser = $this->parser;
117+
$this->parser->once('data', function () use ($input, $parser, $second) {
118+
$parser->once('data', $second);
119+
$input->emit('data', array('again'));
120+
});
121+
122+
$this->input->emit('data', array("hello\nworld"));
123+
}
124+
90125
public function testEmitsC0AndData()
91126
{
92127
$this->parser->on('data', $this->expectCallableOnceWith("hello world"));

0 commit comments

Comments
 (0)