Browse Source

zlib: Fix test so that it's not trivially passing, then pass it.

Regression from the refactor to move more things into JS.
isaacs 13 years ago
parent
commit
d104bfd5a6
  1. 18
      lib/zlib.js
  2. 4
      src/node_zlib.cc
  3. 10
      test/simple/test-zlib.js

18
lib/zlib.js

@ -22,6 +22,7 @@
var binding = process.binding('zlib'); var binding = process.binding('zlib');
var util = require('util'); var util = require('util');
var stream = require('stream'); var stream = require('stream');
var assert = require('assert').ok;
// zlib doesn't provide these, so kludge them in following the same // zlib doesn't provide these, so kludge them in following the same
// const naming scheme zlib uses. // const naming scheme zlib uses.
@ -176,14 +177,6 @@ function Zlib(opts, Binding) {
this._buffer = new Buffer(this._chunkSize); this._buffer = new Buffer(this._chunkSize);
this._offset = 0; this._offset = 0;
var self = this; var self = this;
this._binding.onData = function(c) {
self.emit('data', c);
};
this._binding.onEnd = function() {
self.emit('end');
};
} }
util.inherits(Zlib, stream.Stream); util.inherits(Zlib, stream.Stream);
@ -269,16 +262,21 @@ Zlib.prototype._process = function() {
req.callback = callback; req.callback = callback;
this._processing = req; this._processing = req;
function callback(availInAfter, availOutAfter) { function callback(availInAfter, availOutAfter, buffer) {
var have = self.chunkSize - availOutAfter; var have = availOutBefore - availOutAfter;
assert(have >= 0, 'have should not go down');
if (have > 0) { if (have > 0) {
var out = self._buffer.slice(self._offset, self._offset + have); var out = self._buffer.slice(self._offset, self._offset + have);
self._offset += have; self._offset += have;
self.emit('data', out);
} }
// XXX Maybe have a 'min buffer' size so we don't dip into the // XXX Maybe have a 'min buffer' size so we don't dip into the
// thread pool with only 1 byte available or something? // thread pool with only 1 byte available or something?
if (availOutAfter === 0 || self._offset >= self._chunkSize) { if (availOutAfter === 0 || self._offset >= self._chunkSize) {
availOutBefore = self._chunkSize;
self._offset = 0; self._offset = 0;
self._buffer = new Buffer(self._chunkSize); self._buffer = new Buffer(self._chunkSize);
} }

4
src/node_zlib.cc

@ -39,7 +39,6 @@ using namespace v8;
// write() returns one of these, and then calls the cb() when it's done. // write() returns one of these, and then calls the cb() when it's done.
typedef ReqWrap<uv_work_t> WorkReqWrap; typedef ReqWrap<uv_work_t> WorkReqWrap;
static Persistent<String> ondata_sym;
static Persistent<String> callback_sym; static Persistent<String> callback_sym;
enum node_zlib_mode { enum node_zlib_mode {
@ -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) // write(flush, in, in_off, in_len, out, out_off, out_len)
static Handle<Value> static Handle<Value>
Write(const Arguments& args) { Write(const Arguments& args) {
HandleScope scope;
assert(args.Length() == 7); assert(args.Length() == 7);
ZCtx<mode> *ctx = ObjectWrap::Unwrap< ZCtx<mode> >(args.This()); ZCtx<mode> *ctx = ObjectWrap::Unwrap< ZCtx<mode> >(args.This());
@ -177,6 +177,7 @@ template <node_zlib_mode mode> class ZCtx : public ObjectWrap {
// v8 land! // v8 land!
static void static void
After(uv_work_t* work_req) { After(uv_work_t* work_req) {
HandleScope scope;
WorkReqWrap *req_wrap = reinterpret_cast<WorkReqWrap *>(work_req->data); WorkReqWrap *req_wrap = reinterpret_cast<WorkReqWrap *>(work_req->data);
ZCtx<mode> *ctx = (ZCtx<mode> *)req_wrap->data_; ZCtx<mode> *ctx = (ZCtx<mode> *)req_wrap->data_;
Local<Integer> avail_out = Integer::New(ctx->strm_.avail_out); Local<Integer> avail_out = Integer::New(ctx->strm_.avail_out);
@ -322,7 +323,6 @@ void InitZlib(Handle<Object> target) {
NODE_ZLIB_CLASS(GUNZIP, "Gunzip") NODE_ZLIB_CLASS(GUNZIP, "Gunzip")
NODE_ZLIB_CLASS(UNZIP, "Unzip") NODE_ZLIB_CLASS(UNZIP, "Unzip")
ondata_sym = NODE_PSYMBOL("onData");
callback_sym = NODE_PSYMBOL("callback"); callback_sym = NODE_PSYMBOL("callback");
NODE_DEFINE_CONSTANT(target, Z_NO_FLUSH); NODE_DEFINE_CONSTANT(target, Z_NO_FLUSH);

10
test/simple/test-zlib.js

@ -59,6 +59,12 @@ if (!process.env.PUMMEL) {
var fs = require('fs'); var fs = require('fs');
var testFiles = [ 'person.jpg', 'elipses.txt', 'empty.txt' ]; var testFiles = [ 'person.jpg', 'elipses.txt', 'empty.txt' ];
if (process.env.FAST) {
zlibPairs = [ [zlib.Gzip, zlib.Unzip] ];
var testFiles = [ 'person.jpg' ];
}
var tests = {} var tests = {}
testFiles.forEach(function(file) { testFiles.forEach(function(file) {
tests[file] = fs.readFileSync(path.resolve(common.fixturesDir, file)); tests[file] = fs.readFileSync(path.resolve(common.fixturesDir, file));
@ -81,6 +87,7 @@ util.inherits(BufferStream, stream.Stream);
BufferStream.prototype.write = function(c) { BufferStream.prototype.write = function(c) {
this.chunks.push(c); this.chunks.push(c);
this.length += c.length; this.length += c.length;
return true;
}; };
BufferStream.prototype.end = function(c) { BufferStream.prototype.end = function(c) {
@ -94,6 +101,7 @@ BufferStream.prototype.end = function(c) {
}); });
this.emit('data', buf); this.emit('data', buf);
this.emit('end'); this.emit('end');
return true;
}; };
@ -184,7 +192,7 @@ Object.keys(tests).forEach(function(file) {
Def.name + ' -> ' + Inf.name; Def.name + ' -> ' + Inf.name;
var ok = true; var ok = true;
var testNum = ++done; 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]) { if (c[i] !== test[i]) {
ok = false; ok = false;
failures ++; failures ++;

Loading…
Cancel
Save