go-pattern-examples/creation/04_object_pool/object_pool_test.go

31 lines
398 B
Go
Raw Permalink Normal View History

2020-04-24 13:09:58 +03:00
package objectpool
import (
2020-05-05 17:59:18 +03:00
"fmt"
2020-04-24 13:09:58 +03:00
"testing"
)
func TestObjectPool(t *testing.T) {
2020-05-05 17:59:18 +03:00
p := newPool(3)
doc1 := <-p
doc1.surgery("tom")
doc2 := <-p
doc2.surgery("rose")
doc3 := <-p
doc3.surgery("kate")
2020-04-24 13:09:58 +03:00
select {
case obj := <-p:
2020-05-05 17:59:18 +03:00
obj.surgery("lily")
2020-04-24 13:09:58 +03:00
p <- obj
default:
// No more objects left — retry later or fail
2020-05-05 17:59:18 +03:00
fmt.Println("No more objects left, this moment")
2020-04-24 13:09:58 +03:00
return
}
}