Skip to content

Commit df7702a

Browse files
committed
Fix socket leak in types test
1 parent f477ee4 commit df7702a

File tree

1 file changed

+60
-36
lines changed

1 file changed

+60
-36
lines changed

test/v1/types.test.js

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -160,24 +160,52 @@ describe('byte arrays', () => {
160160
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
161161
});
162162

163-
it('should support returning empty byte array', done => {
164-
if(!serverSupportsByteArrays) {
163+
it('should support returning empty byte array if server supports byte arrays', done => {
164+
if (!serverSupportsByteArrays) {
165165
done();
166166
return;
167167
}
168168

169169
testValue(new Int8Array(0))(done);
170170
});
171171

172-
it('should support returning empty byte array', conditionalTestValues(serverSupportsByteArrays, new Int8Array(0)));
172+
it('should support returning empty byte array if server supports byte arrays', done => {
173+
if (!serverSupportsByteArrays) {
174+
done();
175+
return;
176+
}
177+
178+
testValues([new Int8Array(0)])(done);
179+
});
180+
181+
it('should support returning short byte arrays if server supports byte arrays', done => {
182+
if (!serverSupportsByteArrays) {
183+
done();
184+
return;
185+
}
186+
187+
testValues(randomByteArrays(100, 1, 255))(done);
188+
});
189+
190+
it('should support returning medium byte arrays if server supports byte arrays', done => {
191+
if (!serverSupportsByteArrays) {
192+
done();
193+
return;
194+
}
173195

174-
it('should support returning short byte arrays', conditionalTestValues(serverSupportsByteArrays, randomByteArrays(100, 1, 255)));
196+
testValues(randomByteArrays(50, 256, 65535))(done);
197+
});
175198

176-
it('should support returning medium byte arrays', conditionalTestValues(serverSupportsByteArrays, randomByteArrays(50, 256, 65535)));
199+
it('should support returning long byte arrays if server supports byte arrays', done => {
200+
if (!serverSupportsByteArrays) {
201+
done();
202+
return;
203+
}
177204

178-
it('should support returning long byte arrays', conditionalTestValues(serverSupportsByteArrays, randomByteArrays(10, 65536, 2 * 65536)));
205+
testValues(randomByteArrays(10, 65536, 2 * 65536))(done);
206+
});
179207

180-
it('should fail to return byte array', done => {
208+
it('should fail to return byte array if server does not support byte arrays', done => {
181209
if (serverSupportsByteArrays) {
182210
done();
183211
return;
@@ -193,28 +221,35 @@ describe('byte arrays', () => {
193221
});
194222
});
195223

196-
function conditionalTestValues(condition, values) {
197-
if (!condition) {
198-
return done => done();
199-
}
200-
201-
const driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken);
202-
const queriesPromise = values.reduce((acc, value) =>
203-
acc.then(() => runReturnQuery(driver, value)), Promise.resolve());
204-
return asTestFunction(queriesPromise, driver);
205-
}
206-
207224
function testValue(actual, expected) {
208-
const driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken);
209-
const queryPromise = runReturnQuery(driver, actual, expected);
210-
return asTestFunction(queryPromise, driver);
225+
return done => {
226+
const driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken);
227+
const queryPromise = runReturnQuery(driver, actual, expected);
228+
229+
queryPromise.then(() => {
230+
driver.close();
231+
done();
232+
}).catch(error => {
233+
driver.close();
234+
console.log(error);
235+
});
236+
};
211237
}
212238

213239
function testValues(values) {
214-
const driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken);
215-
const queriesPromise = values.reduce((acc, value) =>
216-
acc.then(() => runReturnQuery(driver, value)), Promise.resolve());
217-
return asTestFunction(queriesPromise, driver);
240+
return done => {
241+
const driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken);
242+
const queriesPromise = values.reduce((acc, value) =>
243+
acc.then(() => runReturnQuery(driver, value)), Promise.resolve());
244+
245+
queriesPromise.then(() => {
246+
driver.close();
247+
done();
248+
}).catch(error => {
249+
driver.close();
250+
console.log(error);
251+
});
252+
};
218253
}
219254

220255
function runReturnQuery(driver, actual, expected) {
@@ -230,17 +265,6 @@ function runReturnQuery(driver, actual, expected) {
230265
});
231266
}
232267

233-
function asTestFunction(promise, driver) {
234-
return done =>
235-
promise.then(() => {
236-
driver.close();
237-
done();
238-
}).catch(error => {
239-
driver.close();
240-
console.log(error);
241-
});
242-
}
243-
244268
function randomByteArrays(count, minLength, maxLength) {
245269
return _.range(count).map(() => {
246270
const length = _.random(minLength, maxLength);

0 commit comments

Comments
 (0)