2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
20 "github.com/stretchr/testify/assert"
30 type instance struct{}
33 tmpc := atomic.AddInt32(&counter, 1)
36 tmpm := atomic.LoadInt32(&max)
38 swapped = atomic.CompareAndSwapInt32(&max, tmpm, tmpc)
45 func(instance) down(){
46 atomic.AddInt32(&counter, - 1)
49 func TestPoolMax(t *testing.T){
52 validateMaxLimit(1, 1, t)
55 validateMaxLimit(1, 2, t)
58 validateMaxLimit(5, 10, t)
61 func validateMaxLimit(size int, iterations int, t *testing.T) {
62 poolGlob = NewPool(size, func() interface{} {
66 func(obj interface{}) {
69 group := sync.WaitGroup{}
70 for i := 0; i < iterations; i++ {
71 go getPutInstance(group)
73 time.Sleep(time.Second)
75 assert.Equal(t, int32(size), max)
78 func getPutInstance(group sync.WaitGroup) {
80 inst := poolGlob.Get().(instance)
82 time.Sleep(time.Millisecond*10)
88 func TestNewPool(t *testing.T){
90 pool := NewPool(size, func() interface{} {
94 func(obj interface{}) {
97 assert.NotNil(t, pool)
98 assert.NotNil(t, pool.New)
99 assert.NotNil(t, pool.Destroy)
100 assert.NotNil(t, pool.pool)
101 assert.Equal(t, cap(pool.pool), size, "the capacity of the pool should be " + string(size))
104 func TestGetCreated(t *testing.T) {
105 pool := NewPool(1, func() interface{} {
109 func(obj interface{}) {
113 available, created := pool.Stats()
114 assert.Equal(t, 0, available, "number of available objects in the pool should be 0")
115 assert.Equal(t, 1, created, "number of created objects in the pool should be 1")
119 func TestGetAndPut(t *testing.T) {
120 pool := NewPool(1, func() interface{} {
124 func(obj interface{}) {
128 available, created := pool.Stats()
129 assert.Equal(t, 1, available, "number of available objects in the pool should be 1")
130 assert.Equal(t, 1, created, "number of created objects in the pool should be 1")
134 func TestPutOutOfCapacity(t *testing.T) {
135 pool := NewPool(1, func() interface{} {
139 func(obj interface{}) {
143 pool.Put(new(instance))
144 available, created := pool.Stats()
145 assert.Equal(t, 1, available, "number of available objects in the pool should be 1")
146 assert.Equal(t, 1, created, "number of created objects in the pool should be 1")
150 func TestNotInitializedPut(t *testing.T) {
152 poolEmpty.Put(new(instance))
153 available, created := poolEmpty.Stats()
154 assert.Equal(t, 0, available, "number of available objects in the pool should be 0")
155 assert.Equal(t, 0, created, "number of created objects in the pool should be 0")
158 func TestPutNilObject(t *testing.T) {
161 available, created := poolEmpty.Stats()
162 assert.Equal(t, 0, available, "number of available objects in the pool should be 0")
163 assert.Equal(t, 0, created, "number of created objects in the pool should be 0")
166 func TestGet(t *testing.T) {
167 pool := NewPool(2, func() interface{} {
171 func(obj interface{}) {
176 available, created := pool.Stats()
177 assert.Equal(t, 0, available, "number of available objects in the pool should be 0")
178 assert.Equal(t, 2, created, "number of created objects in the pool should be 2")
181 pool.Put(new(instance))
182 available, created = pool.Stats()
183 assert.Equal(t, 2, available, "number of available objects in the pool should be 2")
184 assert.Equal(t, 2, created, "number of created objects in the pool should be 2")
187 func TestClose(t *testing.T) {
188 pool := NewPool(3, func() interface{} {
192 func(obj interface{}) {
198 available, created := pool.Stats()
199 assert.Equal(t, 0, available, "number of available objects in the pool should be 0")
200 assert.Equal(t, 3, created, "number of created objects in the pool should be 3")
204 available, created = pool.Stats()
205 assert.Equal(t, 3, available, "number of available objects in the pool should be 3")
206 assert.Equal(t, 3, created, "number of created objects in the pool should be 3")
210 available, created = pool.Stats()
211 assert.Equal(t, 0, available, "number of available objects in the pool should be 0")
212 assert.Equal(t, 0, created, "number of created objects in the pool should be 0")
215 func TestPoolPutPanicsOnClosedChannel(t *testing.T){
216 pool := NewPool(1, func() interface{} {
220 func(obj interface{}) {
224 assert.Panics(t, func(){pool.Put(instance{})})
227 func TestPoolClosePanicsOnClosedChannel(t *testing.T) {
228 pool := NewPool(1, func() interface{} {
232 func(obj interface{}) {
236 assert.Panics(t, func(){pool.Close()})