Code Monkey home page Code Monkey logo

Comments (16)

kelunik avatar kelunik commented on May 25, 2024

We're now at 64%, progress!

from byte-stream.

sebdesign avatar sebdesign commented on May 25, 2024

I'd be happy to work on this one!

from byte-stream.

kelunik avatar kelunik commented on May 25, 2024

Just submit a PR. :-)

from byte-stream.

sebdesign avatar sebdesign commented on May 25, 2024

I'm having trouble while testing the ZlibOutputStream.

Here are my steps:

  1. I create a IteratorStream with the fixtures/foobar.txt contents, like in the ZlibInputStreamTest.

  2. In the ZlibOutputStream instance I don't know what to pass as to the first parameter. What kind of OutputStream should I use? I tried to use a new ResourceOutputStream(fopen("php://memory", "wb+")). I also tried to create a ResourceInputStream with the same resource stream, but I can't read it back.

  3. I'm writing the contents of the IteratorStream to the ZlibOutputStream.

  4. Now I need to assert that the contents of the OutputStream instance match fixtures/foobar.txt.gz. I know I can't read from that interface, but I'm not sure how to proceed. Maybe a stream_socket_pair like in the ResourceStreamTest?

from byte-stream.

kelunik avatar kelunik commented on May 25, 2024

I guess we could add an OutputBuffer that implements Promise and OutputStream and resolves the promise once the stream has ended or fails it if there's an error.

Would you be interested in working on that?

Another solution is to use a stream pair, as you suggested.

from byte-stream.

sebdesign avatar sebdesign commented on May 25, 2024

Yes absolutely. In my mind this would complement the InMemoryStream.

from byte-stream.

sebdesign avatar sebdesign commented on May 25, 2024

I don't have a really good grasp of the Amp framework, so I'm not sure I can make this test pass.
Here's what I've tried so far.

I will try to increase coverage on easier parts.
If you can point me to some direction I will give it another shot, otherwise I can leave that part.
So far I've achieved a 90% coverage!

https://gist.github.com/sebdesign/b2ea5b9dc1bedb3f9614fa8f44031ab8

from byte-stream.

kelunik avatar kelunik commented on May 25, 2024

@sebdesign The issue is your output buffer implementation, it needs to be something like this:

<?php

namespace Amp\ByteStream;

use Amp\ByteStream\ClosedException;
use Amp\Deferred;
use Amp\Promise;
use Amp\Success;

class OutputBuffer implements OutputStream, Promise {
    private $deferred;
    private $contents;
    private $closed = false;
    
    public function __construct() {
        $this->deferred = new Deferred;
    }

    public function write(string $data): Promise {
        if ($this->closed) {
            throw new ClosedException("The stream has already been closed.");
        }
    
        $this->contents .= $data;
        
        return new Success(\strlen($data));
    }
    
    public function end(string $finalData = ""): Promise {
        if ($this->closed) {
            throw new ClosedException("The stream has already been closed.");
        }
        
        $this->contents .= $finalData;
        $this->closed = true;
        
        $this->deferred->resolve($this->contents);
        $this->contents = "";
        
        return new Success(\strlen($finalData));
    }
    
    public function onResolve(callable $onResolve) {
        $this->deferred->promise()->onResolve($onResolve);
    }
}

It needs to collect the contents and resolve the promise (async placeholder) once the stream is complete.

from byte-stream.

sebdesign avatar sebdesign commented on May 25, 2024

Thanks for your feedback!

But I'm still getting the same failure when comparing the compressed/uncompressed files.

from byte-stream.

kelunik avatar kelunik commented on May 25, 2024

Sorry, didn't have a closer look. The issue is that the stream is written in single byte chunks, so there are other compression blocks generated as when generating the complete file in one chunk. I guess it's best to test the input stream and then test the output stream by writing the output and then reading the contents again and see whether it's the same content (uncompressed).

from byte-stream.

sebdesign avatar sebdesign commented on May 25, 2024

Oh now I get it. That means we won't do any assertions against the compressed file?

from byte-stream.

kelunik avatar kelunik commented on May 25, 2024

That's probably best. Another option is to compress the full file in one chunk with the same compression level as the pre-generated file.

from byte-stream.

sebdesign avatar sebdesign commented on May 25, 2024

I got it passing by writing a $fileStream = new ResourceInputStream(fopen($file1, 'r')); to the OutputBuffer through the ZlibOutputStream.

Then I used an InMemoryStream with the contents of the OutputBuffer through the ZlibInputStream to decompress the data and assert against the uncompressed file.

from byte-stream.

sebdesign avatar sebdesign commented on May 25, 2024

I changed the code in the end method of the ZlibOutputStream to call the end method on the destination.
Then in the test I'm calling yield $gzStream->end() otherwise nothing works.

One thing I noticed is that the close() method on the ZlipOutputStream is not being called.

from byte-stream.

kelunik avatar kelunik commented on May 25, 2024

Indeed, the if ($error) looks wrong in the onResolve call.

from byte-stream.

kelunik avatar kelunik commented on May 25, 2024

We're at 94% now, think I can close this issue. 🎉

from byte-stream.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.