Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 65c8297d10ced05b4e4bea088027acbb40dcb28d 1 parent f02bc8f
@nathanoehlman authored
Showing with 105 additions and 1 deletion.
  1. +1 −1  README.md
  2. +61 −0 mockstream.js
  3. +18 −0 package.json
  4. +25 −0 test/mockstream.js
View
2  README.md
@@ -1,4 +1,4 @@
mockstream
==========
-Mock stream is a really simply utility stream for generating data for test streams
+Mockstream is a very simple utility library for testing streams.
View
61 mockstream.js
@@ -0,0 +1,61 @@
+var stream = require('stream'),
+ util = require('util');
+
+/**
+ The MockDataStream merely emits blocks of data in chunks of a given size
+ up until a stream length is reached
+ **/
+function MockDataStream(opts) {
+ opts = opts || {};
+ this.chunkSize = opts.chunkSize || 1024; // Default 1K chunk size
+ this.streamLength = opts.streamLength || 1048576; // Default 1MB stream length
+ this.written = 0;
+ this.paused = false;
+}
+util.inherits(MockDataStream, stream);
+
+/**
+ Starts pumping on mock data
+ **/
+MockDataStream.prototype.start = function() {
+ process.nextTick(this._writeData.bind(this));
+ return this;
+}
+
+/**
+ Pause the stream
+ **/
+MockDataStream.prototype.pause = function() {
+ this.paused = true;
+}
+
+/**
+ Resume the stream
+ **/
+MockDataStream.prototype.resume = function() {
+ this.start();
+}
+
+MockDataStream.prototype._writeData = function() {
+
+ if (this.paused) return;
+
+ var remainder = this.streamLength - this.written,
+ dataLength = (remainder > this.chunkSize ? this.chunkSize : remainder),
+ data = new Array(dataLength + 1).join("0"),
+ buf = new Buffer(data);
+
+ this.emit('data', buf);
+
+ this.written += dataLength;
+
+ if (this.written >= this.streamLength) {
+ this.emit('end');
+ } else {
+ process.nextTick(this._writeData.bind(this));
+ }
+}
+
+module.exports = {
+ MockDataStream: MockDataStream
+}
View
18 package.json
@@ -0,0 +1,18 @@
+{
+ "name": "mockstream",
+ "version": "0.0.0",
+ "description": "A very simple test library for working with streams",
+ "main": "mockstream.js",
+ "scripts": {
+ "test": "mocha"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/nathanoehlman/mockstream.git"
+ },
+ "author": "Nathan Oehlman",
+ "license": "BSD",
+ "devDependencies": {
+ "mocha": "~1.6.0"
+ }
+}
View
25 test/mockstream.js
@@ -0,0 +1,25 @@
+var assert = require('assert'),
+ mockstream = require('..');
+
+describe('Mock data stream', function() {
+
+ it('should emit mock data', function(done) {
+ var chunkSize = 1024,
+ streamLength = 2048,
+ stream = new mockstream.MockDataStream({
+ chunkSize: chunkSize,
+ streamLength: streamLength
+ }),
+ read = 0;
+
+ stream
+ .start()
+ .on('data', function(data) {
+ read += data.length;
+ })
+ .on('end', function() {
+ assert.equal(streamLength, read);
+ done();
+ });
+ });
+});
Please sign in to comment.
Something went wrong with that request. Please try again.