1
+ /**
2
+ * Copyright (c) 2002-2016 "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
+
20
+ var Pool = require ( '../../lib/v1/internal/pool' ) . Pool ;
21
+
22
+ fdescribe ( 'Pool' , function ( ) {
23
+ it ( 'allocates if pool is empty' , function ( ) {
24
+ // Given
25
+ var counter = 0 ;
26
+ var pool = new Pool ( function ( release ) { return new Resource ( counter ++ , release ) } ) ;
27
+
28
+ // When
29
+ var r0 = pool . acquire ( ) ;
30
+ var r1 = pool . acquire ( ) ;
31
+
32
+ // Then
33
+ expect ( r0 . id ) . toBe ( 0 ) ;
34
+ expect ( r1 . id ) . toBe ( 1 ) ;
35
+ } ) ;
36
+
37
+ it ( 'pools if resources are returned' , function ( ) {
38
+ // Given a pool that allocates
39
+ var counter = 0 ;
40
+ var pool = new Pool ( function ( release ) { return new Resource ( counter ++ , release ) } ) ;
41
+
42
+ // When
43
+ var r0 = pool . acquire ( ) ;
44
+ r0 . close ( ) ;
45
+ var r1 = pool . acquire ( ) ;
46
+
47
+ // Then
48
+ expect ( r0 . id ) . toBe ( 0 ) ;
49
+ expect ( r1 . id ) . toBe ( 0 ) ;
50
+ } ) ;
51
+
52
+ it ( 'frees if pool reaches max size' , function ( ) {
53
+ // Given a pool that tracks destroyed resources
54
+ var counter = 0 ,
55
+ destroyed = [ ] ;
56
+ var pool = new Pool (
57
+ function ( release ) { return new Resource ( counter ++ , release ) } ,
58
+ function ( resource ) { destroyed . push ( resource ) ; } ,
59
+ function ( resource ) { return true ; } ,
60
+ 2 // maxIdle
61
+ ) ;
62
+
63
+ // When
64
+ var r0 = pool . acquire ( ) ;
65
+ var r1 = pool . acquire ( ) ;
66
+ var r2 = pool . acquire ( ) ;
67
+ r0 . close ( ) ;
68
+ r1 . close ( ) ;
69
+ r2 . close ( ) ;
70
+
71
+ // Then
72
+ expect ( destroyed . length ) . toBe ( 1 ) ;
73
+ expect ( destroyed [ 0 ] . id ) . toBe ( r2 . id ) ;
74
+ } ) ;
75
+
76
+ it ( 'frees if validate returns false' , function ( ) {
77
+ // Given a pool that allocates
78
+ var counter = 0 ,
79
+ destroyed = [ ] ;
80
+ var pool = new Pool (
81
+ function ( release ) { return new Resource ( counter ++ , release ) } ,
82
+ function ( resource ) { destroyed . push ( resource ) ; } ,
83
+ function ( resource ) { return false ; } ,
84
+ 1000 // maxIdle
85
+ ) ;
86
+
87
+ // When
88
+ var r0 = pool . acquire ( ) ;
89
+ var r1 = pool . acquire ( ) ;
90
+ r0 . close ( ) ;
91
+ r1 . close ( ) ;
92
+
93
+ // Then
94
+ expect ( destroyed . length ) . toBe ( 2 ) ;
95
+ expect ( destroyed [ 0 ] . id ) . toBe ( r0 . id ) ;
96
+ expect ( destroyed [ 1 ] . id ) . toBe ( r1 . id ) ;
97
+ } ) ;
98
+ } ) ;
99
+
100
+ function Resource ( id , release ) {
101
+ var self = this ;
102
+ this . id = id ;
103
+ this . close = function ( ) { release ( self ) ; } ;
104
+ }
0 commit comments