|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2018 "Neo Technology," |
| 3 | + * Network Engine for Objects in Lund AB [http://neotechnology.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +import sharedNeo4j from '../../internal/shared-neo4j'; |
| 20 | +import HttpSession from '../../../src/v1/internal/http/http-session'; |
| 21 | +import urlUtil from '../../../src/v1/internal/url-util'; |
| 22 | +import HttpSessionTracker from '../../../src/v1/internal/http/http-session-tracker'; |
| 23 | + |
| 24 | +describe('http session tracker', () => { |
| 25 | + |
| 26 | + it('should close open sessions', done => { |
| 27 | + const tracker = new HttpSessionTracker(); |
| 28 | + |
| 29 | + const session1 = new FakeHttpSession(tracker); |
| 30 | + const session2 = new FakeHttpSession(tracker); |
| 31 | + const session3 = new FakeHttpSession(tracker); |
| 32 | + |
| 33 | + tracker.sessionOpened(session1); |
| 34 | + tracker.sessionOpened(session2); |
| 35 | + tracker.sessionOpened(session3); |
| 36 | + |
| 37 | + tracker.close().then(() => { |
| 38 | + expect(session1.timesClosed).toEqual(1); |
| 39 | + expect(session2.timesClosed).toEqual(1); |
| 40 | + expect(session3.timesClosed).toEqual(1); |
| 41 | + done(); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should not close closed sessions', done => { |
| 46 | + const tracker = new HttpSessionTracker(); |
| 47 | + |
| 48 | + const session1 = new FakeHttpSession(tracker); |
| 49 | + const session2 = new FakeHttpSession(tracker); |
| 50 | + const session3 = new FakeHttpSession(tracker); |
| 51 | + const session4 = new FakeHttpSession(tracker); |
| 52 | + |
| 53 | + tracker.sessionOpened(session1); |
| 54 | + tracker.sessionOpened(session2); |
| 55 | + tracker.sessionOpened(session3); |
| 56 | + tracker.sessionOpened(session4); |
| 57 | + |
| 58 | + tracker.sessionClosed(session2); |
| 59 | + tracker.sessionClosed(session4); |
| 60 | + |
| 61 | + tracker.close().then(() => { |
| 62 | + expect(session1.timesClosed).toEqual(1); |
| 63 | + expect(session2.timesClosed).toEqual(0); |
| 64 | + expect(session3.timesClosed).toEqual(1); |
| 65 | + expect(session4.timesClosed).toEqual(0); |
| 66 | + done(); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | +}); |
| 71 | + |
| 72 | +class FakeHttpSession extends HttpSession { |
| 73 | + |
| 74 | + constructor(sessionTracker) { |
| 75 | + super(urlUtil.parseDatabaseUrl('http://localhost:7474'), sharedNeo4j.authToken, {}, sessionTracker); |
| 76 | + this.timesClosed = 0; |
| 77 | + } |
| 78 | + |
| 79 | + close(callback) { |
| 80 | + this.timesClosed++; |
| 81 | + callback(); |
| 82 | + } |
| 83 | +} |
0 commit comments