Skip to content

Commit 795de56

Browse files
committed
Add DummyChannel to inspect what gets written
1 parent 4a5c7e7 commit 795de56

File tree

4 files changed

+74
-35
lines changed

4 files changed

+74
-35
lines changed

src/v1/internal/ch-dummy.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright (c) 2002-2016 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
import {CombinedBuffer} from './buf';
21+
const observer = {
22+
instance: null,
23+
updateInstance: (instance) => {
24+
observer.instance = instance
25+
}
26+
}
27+
28+
class DummyChannel {
29+
constructor(opts) {
30+
this.written = [];
31+
}
32+
write( buf ) {
33+
this.written.push(buf);
34+
observer.updateInstance(this);
35+
}
36+
toHex() {
37+
var out = "";
38+
for( var i=0; i<this.written.length; i++ ) {
39+
out += this.written[i].toHex();
40+
}
41+
return out;
42+
}
43+
toBuffer () {
44+
return new CombinedBuffer( this.written );
45+
}
46+
}
47+
48+
export default {
49+
channel: DummyChannel,
50+
observer: observer
51+
}

src/v1/internal/connector.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,9 @@ class Connection {
339339
* @param {string} url - 'neo4j'-prefixed URL to Neo4j Bolt endpoint
340340
* @return {Connection} - New connection
341341
*/
342-
function connect( url ) {
343-
return new Connection( new Channel({
342+
function connect( url, channel = null) {
343+
channel = channel || Channel;
344+
return new Connection( new channel({
344345
host: host(url),
345346
port: port(url)
346347
}));

test/internal/chunking.test.js

+5-32
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ var Chunker = require('../../lib/v1/internal/chunking').Chunker;
2121
var Dechunker = require('../../lib/v1/internal/chunking').Dechunker;
2222
var alloc = require('../../lib/v1/internal/buf').alloc;
2323
var CombinedBuffer = require('../../lib/v1/internal/buf').CombinedBuffer;
24+
var DummyChannel = require('../../lib/v1/internal/ch-dummy.js').channel;
2425

2526
describe('Chunker', function() {
2627
it('should chunk simple data', function() {
2728
// Given
28-
var ch = new TestChannel();
29+
var ch = new DummyChannel();
2930
var chunker = new Chunker(ch);
3031

3132
// When
@@ -38,7 +39,7 @@ describe('Chunker', function() {
3839
});
3940
it('should chunk blobs larger than the output buffer', function() {
4041
// Given
41-
var ch = new TestChannel();
42+
var ch = new DummyChannel();
4243
var chunker = new Chunker(ch, 4);
4344

4445
// When
@@ -50,7 +51,7 @@ describe('Chunker', function() {
5051
});
5152
it('should include message boundaries', function() {
5253
// Given
53-
var ch = new TestChannel();
54+
var ch = new DummyChannel();
5455
var chunker = new Chunker(ch);
5556

5657
// When
@@ -87,7 +88,7 @@ describe('Dechunker', function() {
8788

8889
it('should handle message split at any point', function() {
8990
// Given
90-
var ch = new TestChannel();
91+
var ch = new DummyChannel();
9192
var chunker = new Chunker(ch);
9293

9394
// And given the following message
@@ -121,34 +122,6 @@ describe('Dechunker', function() {
121122
});
122123
});
123124

124-
function TestChannel() {
125-
this._written = [];
126-
}
127-
128-
TestChannel.prototype.write = function( buf ) {
129-
this._written.push(buf);
130-
};
131-
132-
TestChannel.prototype.toHex = function() {
133-
var out = "";
134-
for( var i=0; i<this._written.length; i++ ) {
135-
out += this._written[i].toHex();
136-
}
137-
return out;
138-
};
139-
140-
TestChannel.prototype.toBuffer = function() {
141-
return new CombinedBuffer( this._written );
142-
};
143-
144-
TestChannel.prototype.toHex = function() {
145-
var out = "";
146-
for( var i=0; i<this._written.length; i++ ) {
147-
out += this._written[i].toHex();
148-
}
149-
return out;
150-
};
151-
152125
function bytes() {
153126
var b = alloc( arguments.length );
154127
for( var i=0; i<arguments.length; i++ ) {

test/internal/connector.test.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* See the License for the specific language governing permissions and
1717
* limitations under the License.
1818
*/
19-
19+
var DummyChannel = require('../../lib/v1/internal/ch-dummy.js');
2020
var connect = require("../../lib/v1/internal/connector.js").connect;
2121

2222
describe('connector', function() {
@@ -57,6 +57,20 @@ describe('connector', function() {
5757
}
5858
});
5959
conn.sync();
60+
});
61+
62+
it('should use DummyChannel to read what gets written', function(done) {
63+
// Given
64+
var observer = DummyChannel.observer;
65+
var conn = connect("bolt://localhost", DummyChannel.channel)
6066

67+
// When
68+
var records = [];
69+
conn.initialize( "mydriver/0.0.0" );
70+
conn.run( "RETURN 1", {} );
71+
conn.sync();
72+
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 ' );
73+
done();
6174
});
75+
6276
});

0 commit comments

Comments
 (0)