Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
zlib: Fix test so that it's not trivially passing, then pass it.
Browse files Browse the repository at this point in the history
Regression from the refactor to move more things into JS.
  • Loading branch information
isaacs committed Sep 18, 2011
1 parent 5b8e1da commit d104bfd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
18 changes: 8 additions & 10 deletions lib/zlib.js
Expand Up @@ -22,6 +22,7 @@
var binding = process.binding('zlib');
var util = require('util');
var stream = require('stream');
var assert = require('assert').ok;

// zlib doesn't provide these, so kludge them in following the same
// const naming scheme zlib uses.
Expand Down Expand Up @@ -176,14 +177,6 @@ function Zlib(opts, Binding) {
this._buffer = new Buffer(this._chunkSize);
this._offset = 0;
var self = this;

this._binding.onData = function(c) {
self.emit('data', c);
};

this._binding.onEnd = function() {
self.emit('end');
};
}

util.inherits(Zlib, stream.Stream);
Expand Down Expand Up @@ -269,16 +262,21 @@ Zlib.prototype._process = function() {
req.callback = callback;
this._processing = req;

function callback(availInAfter, availOutAfter) {
var have = self.chunkSize - availOutAfter;
function callback(availInAfter, availOutAfter, buffer) {
var have = availOutBefore - availOutAfter;

assert(have >= 0, 'have should not go down');

if (have > 0) {
var out = self._buffer.slice(self._offset, self._offset + have);
self._offset += have;
self.emit('data', out);
}

// XXX Maybe have a 'min buffer' size so we don't dip into the
// thread pool with only 1 byte available or something?
if (availOutAfter === 0 || self._offset >= self._chunkSize) {
availOutBefore = self._chunkSize;
self._offset = 0;
self._buffer = new Buffer(self._chunkSize);
}
Expand Down
4 changes: 2 additions & 2 deletions src/node_zlib.cc
Expand Up @@ -39,7 +39,6 @@ using namespace v8;
// write() returns one of these, and then calls the cb() when it's done.
typedef ReqWrap<uv_work_t> WorkReqWrap;

static Persistent<String> ondata_sym;
static Persistent<String> callback_sym;

enum node_zlib_mode {
Expand Down Expand Up @@ -78,6 +77,7 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
// write(flush, in, in_off, in_len, out, out_off, out_len)
static Handle<Value>
Write(const Arguments& args) {
HandleScope scope;
assert(args.Length() == 7);

ZCtx<mode> *ctx = ObjectWrap::Unwrap< ZCtx<mode> >(args.This());
Expand Down Expand Up @@ -177,6 +177,7 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
// v8 land!
static void
After(uv_work_t* work_req) {
HandleScope scope;
WorkReqWrap *req_wrap = reinterpret_cast<WorkReqWrap *>(work_req->data);
ZCtx<mode> *ctx = (ZCtx<mode> *)req_wrap->data_;
Local<Integer> avail_out = Integer::New(ctx->strm_.avail_out);
Expand Down Expand Up @@ -322,7 +323,6 @@ void InitZlib(Handle<Object> target) {
NODE_ZLIB_CLASS(GUNZIP, "Gunzip")
NODE_ZLIB_CLASS(UNZIP, "Unzip")

ondata_sym = NODE_PSYMBOL("onData");
callback_sym = NODE_PSYMBOL("callback");

NODE_DEFINE_CONSTANT(target, Z_NO_FLUSH);
Expand Down
10 changes: 9 additions & 1 deletion test/simple/test-zlib.js
Expand Up @@ -59,6 +59,12 @@ if (!process.env.PUMMEL) {
var fs = require('fs');

var testFiles = [ 'person.jpg', 'elipses.txt', 'empty.txt' ];

if (process.env.FAST) {
zlibPairs = [ [zlib.Gzip, zlib.Unzip] ];
var testFiles = [ 'person.jpg' ];
}

var tests = {}
testFiles.forEach(function(file) {
tests[file] = fs.readFileSync(path.resolve(common.fixturesDir, file));
Expand All @@ -81,6 +87,7 @@ util.inherits(BufferStream, stream.Stream);
BufferStream.prototype.write = function(c) {
this.chunks.push(c);
this.length += c.length;
return true;
};

BufferStream.prototype.end = function(c) {
Expand All @@ -94,6 +101,7 @@ BufferStream.prototype.end = function(c) {
});
this.emit('data', buf);
this.emit('end');
return true;
};


Expand Down Expand Up @@ -184,7 +192,7 @@ Object.keys(tests).forEach(function(file) {
Def.name + ' -> ' + Inf.name;
var ok = true;
var testNum = ++done;
for (var i = 0; i < c.length; i++) {
for (var i = 0; i < Math.max(c.length, test.length); i++) {
if (c[i] !== test[i]) {
ok = false;
failures ++;
Expand Down

0 comments on commit d104bfd

Please sign in to comment.