@@ -24,15 +24,14 @@ import {Chunker} from '../../src/v1/internal/chunking';
24
24
import { alloc } from '../../src/v1/internal/node' ;
25
25
import { Neo4jError , newError , SERVICE_UNAVAILABLE } from '../../src/v1/error' ;
26
26
import sharedNeo4j from '../internal/shared-neo4j' ;
27
- import { ServerVersion } from '../../src/v1/internal/server-version' ;
27
+ import { ServerVersion , VERSION_3_5_0 } from '../../src/v1/internal/server-version' ;
28
28
import lolex from 'lolex' ;
29
29
import Logger from '../../src/v1/internal/logger' ;
30
30
import StreamObserver from '../../src/v1/internal/stream-observer' ;
31
31
import ConnectionErrorHandler from '../../src/v1/internal/connection-error-handler' ;
32
32
import testUtils from '../internal/test-utils' ;
33
33
import Bookmark from '../../src/v1/internal/bookmark' ;
34
34
import TxConfig from '../../src/v1/internal/tx-config' ;
35
- import boltStub from '../internal/bolt-stub' ;
36
35
37
36
const ILLEGAL_MESSAGE = { signature : 42 , fields : [ ] } ;
38
37
const SUCCESS_MESSAGE = { signature : 0x70 , fields : [ { } ] } ;
@@ -347,27 +346,46 @@ describe('Connection', () => {
347
346
connection . _handleFatalError ( newError ( 'Hello' , SERVICE_UNAVAILABLE ) ) ;
348
347
} ) ;
349
348
350
- it ( 'should send hello and goodbye messages' , done => {
351
- if ( ! boltStub . supported ) {
352
- done ( ) ;
353
- return ;
354
- }
349
+ it ( 'should send INIT/HELLO and GOODBYE messages' , done => {
350
+ const messages = [ ] ;
351
+ connection = createConnection ( 'bolt://localhost' ) ;
352
+ recordWrittenMessages ( connection , messages ) ;
355
353
356
- const server = boltStub . start ( './test/resources/boltstub/hello_goodbye.script' , 9001 ) ;
357
-
358
- boltStub . run ( ( ) => {
359
- connection = createConnection ( 'bolt://127.0.0.1:9001' , { encrypted : false } ) ;
360
- connection . connect ( 'single-connection/1.2.3' , basicAuthToken ( ) )
361
- . then ( ( ) => {
362
- connection . close ( ( ) => {
363
- server . exit ( code => {
364
- expect ( code ) . toEqual ( 0 ) ;
365
- done ( ) ;
366
- } ) ;
367
- } ) ;
368
- } )
369
- . catch ( error => done . fail ( error ) ) ;
370
- } ) ;
354
+ connection . connect ( 'mydriver/0.0.0' , basicAuthToken ( ) )
355
+ . then ( ( ) => {
356
+ expect ( connection . isOpen ( ) ) . toBeTruthy ( ) ;
357
+ connection . close ( ( ) => {
358
+ expect ( messages . length ) . toBeGreaterThan ( 0 ) ;
359
+ expect ( messages [ 0 ] . signature ) . toEqual ( 0x01 ) ; // first message is either INIT or HELLO
360
+
361
+ const serverVersion = ServerVersion . fromString ( connection . server . version ) ;
362
+ if ( serverVersion . compareTo ( VERSION_3_5_0 ) >= 0 ) {
363
+ expect ( messages [ messages . length - 1 ] . signature ) . toEqual ( 0x02 ) ; // last message is GOODBYE in V3
364
+ }
365
+ done ( ) ;
366
+ } ) ;
367
+ } ) . catch ( done . fail ) ;
368
+ } ) ;
369
+
370
+ it ( 'should not prepare broken connection to close' , done => {
371
+ connection = createConnection ( 'bolt://localhost' ) ;
372
+
373
+ connection . connect ( 'my-connection/9.9.9' , basicAuthToken ( ) )
374
+ . then ( ( ) => {
375
+ expect ( connection . _protocol ) . toBeDefined ( ) ;
376
+ expect ( connection . _protocol ) . not . toBeNull ( ) ;
377
+
378
+ // make connection seem broken
379
+ connection . _isBroken = true ;
380
+ expect ( connection . isOpen ( ) ) . toBeFalsy ( ) ;
381
+
382
+ connection . _protocol . prepareToClose = ( ) => {
383
+ throw new Error ( 'Not supposed to be called' ) ;
384
+ } ;
385
+
386
+ connection . close ( ( ) => done ( ) ) ;
387
+ } )
388
+ . catch ( error => done . fail ( error ) ) ;
371
389
} ) ;
372
390
373
391
function packedHandshakeMessage ( ) {
@@ -446,4 +464,12 @@ describe('Connection', () => {
446
464
return Connection . create ( url , config || { } , new ConnectionErrorHandler ( errorCode || SERVICE_UNAVAILABLE ) , Logger . noOp ( ) ) ;
447
465
}
448
466
467
+ function recordWrittenMessages ( connection , messages ) {
468
+ const originalWrite = connection . write . bind ( connection ) ;
469
+ connection . write = ( message , observer , flush ) => {
470
+ messages . push ( message ) ;
471
+ originalWrite ( message , observer , flush ) ;
472
+ } ;
473
+ }
474
+
449
475
} ) ;
0 commit comments