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++ {
77 time.Sleep(time.Second)
79 assert.Equal(t, int32(size), max)
82 func getPutInstance() {
83 inst := poolGlob.Get().(instance)
85 time.Sleep(time.Millisecond*10)
90 func TestNewPool(t *testing.T){
92 pool := NewPool(size, func() interface{} {
96 func(obj interface{}) {
99 assert.NotNil(t, pool)
100 assert.NotNil(t, pool.New)
101 assert.NotNil(t, pool.Destroy)
102 assert.NotNil(t, pool.pool)
103 assert.Equal(t, cap(pool.pool), size, "the capacity of the pool should be " + string(size))
106 func TestGetCreated(t *testing.T) {
107 pool := NewPool(1, func() interface{} {
111 func(obj interface{}) {
115 available, created := pool.Stats()
116 assert.Equal(t, 0, available, "number of available objects in the pool should be 0")
117 assert.Equal(t, 1, created, "number of created objects in the pool should be 1")
121 func TestGetAndPut(t *testing.T) {
122 pool := NewPool(1, func() interface{} {
126 func(obj interface{}) {
130 available, created := pool.Stats()
131 assert.Equal(t, 1, available, "number of available objects in the pool should be 1")
132 assert.Equal(t, 1, created, "number of created objects in the pool should be 1")
136 func TestPutOutOfCapacity(t *testing.T) {
137 pool := NewPool(1, func() interface{} {
141 func(obj interface{}) {
145 pool.Put(new(instance))
146 available, created := pool.Stats()
147 assert.Equal(t, 1, available, "number of available objects in the pool should be 1")
148 assert.Equal(t, 1, created, "number of created objects in the pool should be 1")
152 func TestNotInitializedPut(t *testing.T) {
154 poolEmpty.Put(new(instance))
155 available, created := poolEmpty.Stats()
156 assert.Equal(t, 0, available, "number of available objects in the pool should be 0")
157 assert.Equal(t, 0, created, "number of created objects in the pool should be 0")
160 func TestPutNilObject(t *testing.T) {
163 available, created := poolEmpty.Stats()
164 assert.Equal(t, 0, available, "number of available objects in the pool should be 0")
165 assert.Equal(t, 0, created, "number of created objects in the pool should be 0")
168 func TestGet(t *testing.T) {
169 pool := NewPool(2, func() interface{} {
173 func(obj interface{}) {
178 available, created := pool.Stats()
179 assert.Equal(t, 0, available, "number of available objects in the pool should be 0")
180 assert.Equal(t, 2, created, "number of created objects in the pool should be 2")
183 pool.Put(new(instance))
184 available, created = pool.Stats()
185 assert.Equal(t, 2, available, "number of available objects in the pool should be 2")
186 assert.Equal(t, 2, created, "number of created objects in the pool should be 2")
189 func TestClose(t *testing.T) {
190 pool := NewPool(3, func() interface{} {
194 func(obj interface{}) {
200 available, created := pool.Stats()
201 assert.Equal(t, 0, available, "number of available objects in the pool should be 0")
202 assert.Equal(t, 3, created, "number of created objects in the pool should be 3")
206 available, created = pool.Stats()
207 assert.Equal(t, 3, available, "number of available objects in the pool should be 3")
208 assert.Equal(t, 3, created, "number of created objects in the pool should be 3")
212 available, created = pool.Stats()
213 assert.Equal(t, 0, available, "number of available objects in the pool should be 0")
214 assert.Equal(t, 0, created, "number of created objects in the pool should be 0")
217 func TestPoolPutPanicsOnClosedChannel(t *testing.T){
218 pool := NewPool(1, func() interface{} {
222 func(obj interface{}) {
226 assert.Panics(t, func(){pool.Put(instance{})})
229 func TestPoolClosePanicsOnClosedChannel(t *testing.T) {
230 pool := NewPool(1, func() interface{} {
234 func(obj interface{}) {
238 assert.Panics(t, func(){pool.Close()})