-
Notifications
You must be signed in to change notification settings - Fork 150
Fix Driver.onCompleted callback #298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Copyright (c) 2002-2017 "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 ConnectionHolder from './connection-holder'; | ||
import {READ} from '../driver'; | ||
import StreamObserver from './stream-observer'; | ||
|
||
/** | ||
* Verifies connectivity using the given connection provider. | ||
*/ | ||
export default class ConnectivityVerifier { | ||
|
||
/** | ||
* @constructor | ||
* @param {ConnectionProvider} connectionProvider the provider to obtain connections from. | ||
* @param {function} successCallback a callback to invoke when verification succeeds. | ||
*/ | ||
constructor(connectionProvider, successCallback) { | ||
this._connectionProvider = connectionProvider; | ||
this._successCallback = successCallback; | ||
} | ||
|
||
verify() { | ||
acquireAndReleaseDummyConnection(this._connectionProvider).then(serverInfo => { | ||
if (this._successCallback) { | ||
this._successCallback(serverInfo); | ||
} | ||
}).catch(ignoredError => { | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* @private | ||
* @param {ConnectionProvider} connectionProvider the provider to obtain connections from. | ||
* @return {Promise<object>} promise resolved with server info or rejected with error. | ||
*/ | ||
function acquireAndReleaseDummyConnection(connectionProvider) { | ||
const connectionHolder = new ConnectionHolder(READ, connectionProvider); | ||
connectionHolder.initializeConnection(); | ||
const dummyObserver = new StreamObserver(); | ||
const connectionPromise = connectionHolder.getConnection(dummyObserver); | ||
|
||
return connectionPromise.then(connection => { | ||
// able to establish a connection | ||
return connectionHolder.close().then(() => connection.server); | ||
}).catch(error => { | ||
// failed to establish a connection | ||
return connectionHolder.close().catch(ignoredError => { | ||
// ignore connection release error | ||
}).then(() => { | ||
return Promise.reject(error); | ||
}); | ||
}); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Copyright (c) 2002-2017 "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 ConnectivityVerifier from '../../src/v1/internal/connectivity-verifier'; | ||
import {SingleConnectionProvider} from '../../src/v1/internal/connection-providers'; | ||
import FakeConnection from './fake-connection'; | ||
|
||
describe('ConnectivityVerifier', () => { | ||
|
||
it('should call success callback when able to acquire and release a connection', done => { | ||
const connectionPromise = Promise.resolve(new FakeConnection()); | ||
const connectionProvider = new SingleConnectionProvider(connectionPromise); | ||
|
||
const verifier = new ConnectivityVerifier(connectionProvider, () => { | ||
done(); | ||
}); | ||
|
||
verifier.verify(); | ||
}); | ||
|
||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting an event handler invoking actual verification confused me a bit. What happens if verification fails? Maybe an explicit method
verifyConnectivity()
returning a promise would be a better option, can be elaborated for upcoming versions?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Failed verification will result in
Driver.onCompleted
not being invoked. InsteadDriver.onError
will be invoked. I agree, these callbacks are not ideal and rather ugly. That's why they are removed from README. Users can verify connectivity by running a dummy query likeRETURN 1
. So this fix is only for backwards compatibility.