Skip to content

Commit 97e842a

Browse files
committed
Remove onCompleted callback and add verifyConnectivity method
1 parent 5ace101 commit 97e842a

File tree

7 files changed

+42
-62
lines changed

7 files changed

+42
-62
lines changed

src/driver.js

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ class Driver {
9393
*/
9494
this._connectionProvider = null;
9595

96-
this._onCompleted = null;
97-
9896
this._afterConstruction();
9997
}
10098

@@ -106,26 +104,13 @@ class Driver {
106104
}
107105

108106
/**
109-
* Get the installed connectivity verification callback.
110-
* @return {null|function}
111-
* @deprecated driver can be used directly once instantiated, use of this callback is not required.
107+
* Verifies connectivity of this driver by trying to open a connection with the provided driver options.
108+
* @returns {Promise<object>} promise resolved with server info or rejected with error.
112109
*/
113-
get onCompleted() {
114-
return this._onCompleted;
115-
}
116-
117-
/**
118-
* Install a connectivity verification callback.
119-
* @param {null|function} callback the new function to be notified about successful connection.
120-
* @deprecated driver can be used directly once instantiated, use of this callback is not required.
121-
*/
122-
set onCompleted(callback) {
123-
this._onCompleted = callback;
124-
if (this._onCompleted) {
125-
const connectionProvider = this._getOrCreateConnectionProvider();
126-
const connectivityVerifier = new ConnectivityVerifier(connectionProvider, this._onCompleted);
127-
connectivityVerifier.verify();
128-
}
110+
verifyConnectivity() {
111+
const connectionProvider = this._getOrCreateConnectionProvider();
112+
const connectivityVerifier = new ConnectivityVerifier(connectionProvider);
113+
return connectivityVerifier.verify();
129114
}
130115

131116
/**

src/internal/connectivity-verifier.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,17 @@ export default class ConnectivityVerifier {
2929
/**
3030
* @constructor
3131
* @param {ConnectionProvider} connectionProvider the provider to obtain connections from.
32-
* @param {function} successCallback a callback to invoke when verification succeeds.
3332
*/
34-
constructor(connectionProvider, successCallback) {
33+
constructor(connectionProvider) {
3534
this._connectionProvider = connectionProvider;
36-
this._successCallback = successCallback;
3735
}
3836

37+
/**
38+
* Try to obtain a working connection from the connection provider.
39+
* @returns {Promise<object>} promise resolved with server info or rejected with error.
40+
*/
3941
verify() {
40-
acquireAndReleaseDummyConnection(this._connectionProvider).then(serverInfo => {
41-
if (this._successCallback) {
42-
this._successCallback(serverInfo);
43-
}
44-
}).catch(ignoredError => {
45-
});
42+
return acquireAndReleaseDummyConnection(this._connectionProvider);
4643
}
4744
}
4845

test/driver.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,43 +147,43 @@ describe('driver', () => {
147147
driver = neo4j.driver("bolt://localhost", sharedNeo4j.authToken);
148148

149149
// Expect
150-
driver.onCompleted = server => {
150+
driver.verifyConnectivity().then(server => {
151151
expect(server.address).toBeDefined();
152152
done();
153-
};
153+
});
154154
});
155155

156156
it('should be possible to pass a realm with basic auth tokens', done => {
157157
// Given
158158
driver = neo4j.driver("bolt://localhost", neo4j.auth.basic(sharedNeo4j.username, sharedNeo4j.password, "native"));
159159

160160
// Expect
161-
driver.onCompleted = server => {
161+
driver.verifyConnectivity().then(server => {
162162
expect(server.address).toBeDefined();
163163
done();
164-
};
164+
});
165165
});
166166

167167
it('should be possible to create custom auth tokens', done => {
168168
// Given
169169
driver = neo4j.driver("bolt://localhost", neo4j.auth.custom(sharedNeo4j.username, sharedNeo4j.password, "native", "basic"));
170170

171171
// Expect
172-
driver.onCompleted = server => {
172+
driver.verifyConnectivity().then(server => {
173173
expect(server.address).toBeDefined();
174174
done();
175-
};
175+
});
176176
});
177177

178178
it('should be possible to create custom auth tokens with additional parameters', done => {
179179
// Given
180180
driver = neo4j.driver("bolt://localhost", neo4j.auth.custom(sharedNeo4j.username, sharedNeo4j.password, "native", "basic", {secret: 42}));
181181

182182
// Expect
183-
driver.onCompleted = server => {
183+
driver.verifyConnectivity().then(server => {
184184
expect(server.address).toBeDefined();
185185
done();
186-
};
186+
});
187187
});
188188

189189
it('should fail nicely when connecting with routing to standalone server', done => {

test/examples.test.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ describe('examples', () => {
9999
const driver = neo4j.driver(uri, neo4j.auth.basic(user, password));
100100
// end::basic-auth[]
101101

102-
driver.onCompleted = () => {
102+
driver.verifyConnectivity().then(() => {
103103
driver.close();
104104
done();
105-
};
105+
});
106106
});
107107

108108
it('config connection pool example', done => {
@@ -116,10 +116,10 @@ describe('examples', () => {
116116
);
117117
// end::config-connection-pool[]
118118

119-
driver.onCompleted = () => {
119+
driver.verifyConnectivity().then(() => {
120120
driver.close();
121121
done();
122-
};
122+
});
123123
});
124124

125125
it('config connection timeout example', done => {
@@ -131,10 +131,10 @@ describe('examples', () => {
131131
);
132132
// end::config-connection-timeout[]
133133

134-
driver.onCompleted = () => {
134+
driver.verifyConnectivity().then(() => {
135135
driver.close();
136136
done();
137-
};
137+
});
138138
});
139139

140140
it('config max retry time example', done => {
@@ -147,10 +147,10 @@ describe('examples', () => {
147147
);
148148
// end::config-max-retry-time[]
149149

150-
driver.onCompleted = () => {
150+
driver.verifyConnectivity().then(() => {
151151
driver.close();
152152
done();
153-
};
153+
});
154154
});
155155

156156
it('config trust example', done => {
@@ -163,10 +163,10 @@ describe('examples', () => {
163163
);
164164
// end::config-trust[]
165165

166-
driver.onCompleted = () => {
166+
driver.verifyConnectivity().then(() => {
167167
driver.close();
168168
done();
169-
};
169+
});
170170
});
171171

172172
it('config unencrypted example', done => {
@@ -178,10 +178,10 @@ describe('examples', () => {
178178
);
179179
// end::config-unencrypted[]
180180

181-
driver.onCompleted = () => {
181+
driver.verifyConnectivity().then(() => {
182182
driver.close();
183183
done();
184-
};
184+
});
185185
});
186186

187187
it('config custom resolver example', done => {
@@ -217,10 +217,10 @@ describe('examples', () => {
217217
const driver = neo4j.driver(uri, neo4j.auth.custom(principal, credentials, realm, scheme, parameters));
218218
// end::custom-auth[]
219219

220-
driver.onCompleted = () => {
220+
driver.verifyConnectivity().then(() => {
221221
driver.close();
222222
done();
223-
};
223+
});
224224
});
225225

226226
it('kerberos auth example', () => {
@@ -261,9 +261,9 @@ describe('examples', () => {
261261
// tag::driver-lifecycle[]
262262
const driver = neo4j.driver(uri, neo4j.auth.basic(user, password));
263263

264-
driver.onCompleted = () => {
264+
driver.verifyConnectivity().then(() => {
265265
console.log('Driver created');
266-
};
266+
});
267267

268268
driver.onError = error => {
269269
console.log(error);

test/internal/connectivity-verifier.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ describe('ConnectivityVerifier', () => {
2626
it('should call success callback when able to acquire and release a connection', done => {
2727
const connectionPromise = Promise.resolve(new FakeConnection());
2828
const connectionProvider = new SingleConnectionProvider(connectionPromise);
29+
const verifier = new ConnectivityVerifier(connectionProvider)
2930

30-
const verifier = new ConnectivityVerifier(connectionProvider, () => {
31+
verifier.verify().then(() => {
3132
done();
3233
});
33-
34-
verifier.verify();
3534
});
3635

3736
});

test/types/driver.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,10 @@ session1.run("RETURN 1").then(result => {
7979

8080
const close: void = driver.close();
8181

82-
driver.onCompleted = (serverInfo: ServerInfo) => {
82+
driver.verifyConnectivity().then((serverInfo: ServerInfo) => {
8383
console.log(serverInfo.version);
8484
console.log(serverInfo.address);
85-
};
86-
87-
driver.onCompleted({version: "Neo4j/3.2.0", address: "localhost:7687"});
85+
});
8886

8987
driver.onError = (error: Neo4jError) => {
9088
console.log(error);

types/driver.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ declare interface Driver {
6666

6767
close(): void;
6868

69-
onCompleted?: (serverInfo: ServerInfo) => void;
69+
verifyConnectivity(): Promise<ServerInfo>;
70+
7071
onError?: (error: Neo4jError) => void;
7172
}
7273

0 commit comments

Comments
 (0)