go-pattern-examples/creation/04_object_pool/object_pool_test.go
2020-05-05 22:59:18 +08:00

31 lines
398 B
Go

package objectpool
import (
"fmt"
"testing"
)
func TestObjectPool(t *testing.T) {
p := newPool(3)
doc1 := <-p
doc1.surgery("tom")
doc2 := <-p
doc2.surgery("rose")
doc3 := <-p
doc3.surgery("kate")
select {
case obj := <-p:
obj.surgery("lily")
p <- obj
default:
// No more objects left — retry later or fail
fmt.Println("No more objects left, this moment")
return
}
}