Skip to content

Commit

Permalink
Add DummyChannel to inspect what gets written
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarhane committed Feb 2, 2016
1 parent 4a5c7e7 commit 795de56
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 35 deletions.
51 changes: 51 additions & 0 deletions src/v1/internal/ch-dummy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {CombinedBuffer} from './buf';
const observer = {
instance: null,
updateInstance: (instance) => {
observer.instance = instance
}
}

class DummyChannel {
constructor(opts) {
this.written = [];
}
write( buf ) {
this.written.push(buf);
observer.updateInstance(this);
}
toHex() {
var out = "";
for( var i=0; i<this.written.length; i++ ) {
out += this.written[i].toHex();
}
return out;
}
toBuffer () {
return new CombinedBuffer( this.written );
}
}

export default {
channel: DummyChannel,
observer: observer
}
5 changes: 3 additions & 2 deletions src/v1/internal/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,9 @@ class Connection {
* @param {string} url - 'neo4j'-prefixed URL to Neo4j Bolt endpoint
* @return {Connection} - New connection
*/
function connect( url ) {
return new Connection( new Channel({
function connect( url, channel = null) {
channel = channel || Channel;
return new Connection( new channel({
host: host(url),
port: port(url)
}));
Expand Down
37 changes: 5 additions & 32 deletions test/internal/chunking.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ var Chunker = require('../../lib/v1/internal/chunking').Chunker;
var Dechunker = require('../../lib/v1/internal/chunking').Dechunker;
var alloc = require('../../lib/v1/internal/buf').alloc;
var CombinedBuffer = require('../../lib/v1/internal/buf').CombinedBuffer;
var DummyChannel = require('../../lib/v1/internal/ch-dummy.js').channel;

describe('Chunker', function() {
it('should chunk simple data', function() {
// Given
var ch = new TestChannel();
var ch = new DummyChannel();
var chunker = new Chunker(ch);

// When
Expand All @@ -38,7 +39,7 @@ describe('Chunker', function() {
});
it('should chunk blobs larger than the output buffer', function() {
// Given
var ch = new TestChannel();
var ch = new DummyChannel();
var chunker = new Chunker(ch, 4);

// When
Expand All @@ -50,7 +51,7 @@ describe('Chunker', function() {
});
it('should include message boundaries', function() {
// Given
var ch = new TestChannel();
var ch = new DummyChannel();
var chunker = new Chunker(ch);

// When
Expand Down Expand Up @@ -87,7 +88,7 @@ describe('Dechunker', function() {

it('should handle message split at any point', function() {
// Given
var ch = new TestChannel();
var ch = new DummyChannel();
var chunker = new Chunker(ch);

// And given the following message
Expand Down Expand Up @@ -121,34 +122,6 @@ describe('Dechunker', function() {
});
});

function TestChannel() {
this._written = [];
}

TestChannel.prototype.write = function( buf ) {
this._written.push(buf);
};

TestChannel.prototype.toHex = function() {
var out = "";
for( var i=0; i<this._written.length; i++ ) {
out += this._written[i].toHex();
}
return out;
};

TestChannel.prototype.toBuffer = function() {
return new CombinedBuffer( this._written );
};

TestChannel.prototype.toHex = function() {
var out = "";
for( var i=0; i<this._written.length; i++ ) {
out += this._written[i].toHex();
}
return out;
};

function bytes() {
var b = alloc( arguments.length );
for( var i=0; i<arguments.length; i++ ) {
Expand Down
16 changes: 15 additions & 1 deletion test/internal/connector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var DummyChannel = require('../../lib/v1/internal/ch-dummy.js');
var connect = require("../../lib/v1/internal/connector.js").connect;

describe('connector', function() {
Expand Down Expand Up @@ -57,6 +57,20 @@ describe('connector', function() {
}
});
conn.sync();
});

it('should use DummyChannel to read what gets written', function(done) {
// Given
var observer = DummyChannel.observer;
var conn = connect("bolt://localhost", DummyChannel.channel)

// When
var records = [];
conn.initialize( "mydriver/0.0.0" );
conn.run( "RETURN 1", {} );
conn.sync();
expect( observer.instance.toHex() ).toBe( '60 60 b0 17 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 11 b1 01 8e 6d 79 64 72 69 76 65 72 2f 30 2e 30 2e 30 00 00 00 0c b2 10 88 52 45 54 55 52 4e 20 31 a0 00 00 ' );
done();
});

});

0 comments on commit 795de56

Please sign in to comment.