Skip to content

Commit 5ccf9b0

Browse files
committed
Always close connections in connector tests
1 parent cb22768 commit 5ccf9b0

File tree

2 files changed

+41
-24
lines changed

2 files changed

+41
-24
lines changed

src/v1/internal/ch-dummy.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
import {CombinedBuffer} from './buf';
21+
2122
const observer = {
2223
instance: null,
2324
updateInstance: (instance) => {
@@ -55,6 +56,13 @@ class DummyChannel {
5556
toBuffer () {
5657
return new CombinedBuffer( this.written );
5758
}
59+
60+
close(cb) {
61+
this.written = [];
62+
if (cb) {
63+
return cb();
64+
}
65+
}
5866
}
5967

6068
const channel = DummyChannel;

test/internal/connector.test.js

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,63 +28,72 @@ import {ServerVersion} from '../../src/v1/internal/server-version';
2828

2929
describe('connector', () => {
3030

31+
let connection;
32+
33+
afterEach(done => {
34+
const usedConnection = connection;
35+
connection = null;
36+
if (usedConnection) {
37+
usedConnection.close();
38+
}
39+
done();
40+
});
41+
3142
it('should read/write basic messages', done => {
3243
// Given
33-
const conn = connect("bolt://localhost");
44+
connection = connect("bolt://localhost");
3445

3546
// When
36-
conn.initialize("mydriver/0.0.0", basicAuthToken(), {
47+
connection.initialize("mydriver/0.0.0", basicAuthToken(), {
3748
onCompleted: msg => {
3849
expect(msg).not.toBeNull();
39-
conn.close();
4050
done();
4151
},
4252
onError: console.log
4353
});
44-
conn.sync();
54+
connection.sync();
4555

4656
});
4757

4858
it('should retrieve stream', done => {
4959
// Given
50-
const conn = connect("bolt://localhost");
60+
connection = connect("bolt://localhost");
5161

5262
// When
5363
const records = [];
54-
conn.initialize("mydriver/0.0.0", basicAuthToken());
55-
conn.run("RETURN 1.0", {});
56-
conn.pullAll({
64+
connection.initialize("mydriver/0.0.0", basicAuthToken());
65+
connection.run("RETURN 1.0", {});
66+
connection.pullAll({
5767
onNext: record => {
5868
records.push(record);
5969
},
6070
onCompleted: () => {
6171
expect(records[0][0]).toBe(1);
62-
conn.close();
6372
done();
6473
}
6574
});
66-
conn.sync();
75+
connection.sync();
6776
});
6877

6978
it('should use DummyChannel to read what gets written', done => {
7079
// Given
7180
const observer = DummyChannel.observer;
72-
const conn = connect("bolt://localhost", {channel: DummyChannel.channel});
81+
connection = connect("bolt://localhost", {channel: DummyChannel.channel});
7382

7483
// When
75-
conn.initialize("mydriver/0.0.0", basicAuthToken());
76-
conn.run("RETURN 1", {});
77-
conn.sync();
84+
connection.initialize("mydriver/0.0.0", basicAuthToken());
85+
connection.run("RETURN 1", {});
86+
connection.sync();
7887
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 44 b2 01 8e 6d 79 64 72 69 76 65 72 2f 30 2e 30 2e 30 a3 86 73 63 68 65 6d 65 85 62 61 73 69 63 89 70 72 69 6e 63 69 70 61 6c 85 6e 65 6f 34 6a 8b 63 72 65 64 65 6e 74 69 61 6c 73 88 70 61 73 73 77 6f 72 64 00 00 00 0c b2 10 88 52 45 54 55 52 4e 20 31 a0 00 00 ');
7988
done();
8089
});
8190

8291
it('should provide error message when connecting to http-port', done => {
8392
// Given
84-
const conn = connect("bolt://localhost:7474", {encrypted: false});
93+
connection = connect("bolt://localhost:7474", {encrypted: false});
8594

8695
// When
87-
conn.initialize("mydriver/0.0.0", basicAuthToken(), {
96+
connection.initialize("mydriver/0.0.0", basicAuthToken(), {
8897
onCompleted: msg => {
8998
},
9099
onError: err => {
@@ -96,13 +105,13 @@ describe('connector', () => {
96105
done();
97106
}
98107
});
99-
conn.sync();
108+
connection.sync();
100109

101110
});
102111

103112
it('should convert failure messages to errors', done => {
104113
const channel = new DummyChannel.channel;
105-
const connection = new Connection(channel, 'bolt://localhost');
114+
connection = new Connection(channel, 'bolt://localhost');
106115

107116
const errorCode = 'Neo.ClientError.Schema.ConstraintValidationFailed';
108117
const errorMessage = 'Node 0 already exists with label User and property "email"=[john@doe.com]';
@@ -119,7 +128,7 @@ describe('connector', () => {
119128
});
120129

121130
it('should notify when connection initialization completes', done => {
122-
const connection = connect('bolt://localhost');
131+
connection = connect('bolt://localhost');
123132

124133
connection.initializationCompleted().then(initializedConnection => {
125134
expect(initializedConnection).toBe(connection);
@@ -130,7 +139,7 @@ describe('connector', () => {
130139
});
131140

132141
it('should notify when connection initialization fails', done => {
133-
const connection = connect('bolt://localhost:7474'); // wrong port
142+
connection = connect('bolt://localhost:7474'); // wrong port
134143

135144
connection.initializationCompleted().catch(error => {
136145
expect(error).toBeDefined();
@@ -141,7 +150,7 @@ describe('connector', () => {
141150
});
142151

143152
it('should notify provided observer when connection initialization completes', done => {
144-
const connection = connect('bolt://localhost');
153+
connection = connect('bolt://localhost');
145154

146155
connection.initialize('mydriver/0.0.0', basicAuthToken(), {
147156
onCompleted: metaData => {
@@ -153,7 +162,7 @@ describe('connector', () => {
153162
});
154163

155164
it('should notify provided observer when connection initialization fails', done => {
156-
const connection = connect('bolt://localhost:7474'); // wrong port
165+
connection = connect('bolt://localhost:7474'); // wrong port
157166

158167
connection.initialize('mydriver/0.0.0', basicAuthToken(), {
159168
onError: error => {
@@ -165,7 +174,7 @@ describe('connector', () => {
165174
});
166175

167176
it('should have server version after connection initialization completed', done => {
168-
const connection = connect('bolt://localhost');
177+
connection = connect('bolt://localhost');
169178

170179
connection.initializationCompleted().then(initializedConnection => {
171180
const serverVersion = ServerVersion.fromString(initializedConnection.server.version);
@@ -177,7 +186,7 @@ describe('connector', () => {
177186
});
178187

179188
it('should fail all new observers after initialization error', done => {
180-
const connection = connect('bolt://localhost:7474'); // wrong port
189+
connection = connect('bolt://localhost:7474'); // wrong port
181190

182191
connection.initialize('mydriver/0.0.0', basicAuthToken(), {
183192
onError: initialError => {

0 commit comments

Comments
 (0)