2016-03-17 14:48:04 +03:00
|
|
|
package pool_test
|
|
|
|
|
|
|
|
import (
|
2019-06-04 14:05:29 +03:00
|
|
|
"context"
|
2021-07-20 12:23:48 +03:00
|
|
|
"fmt"
|
2016-03-17 14:48:04 +03:00
|
|
|
"net"
|
|
|
|
"sync"
|
2021-07-20 12:23:48 +03:00
|
|
|
"syscall"
|
2016-03-17 14:48:04 +03:00
|
|
|
"testing"
|
2021-07-20 12:23:48 +03:00
|
|
|
"time"
|
2016-03-17 14:48:04 +03:00
|
|
|
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGinkgoSuite(t *testing.T) {
|
|
|
|
RegisterFailHandler(Fail)
|
|
|
|
RunSpecs(t, "pool")
|
|
|
|
}
|
|
|
|
|
|
|
|
func perform(n int, cbs ...func(int)) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, cb := range cbs {
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(cb func(int), i int) {
|
|
|
|
defer GinkgoRecover()
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
cb(i)
|
|
|
|
}(cb, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2019-06-04 14:05:29 +03:00
|
|
|
func dummyDialer(context.Context) (net.Conn, error) {
|
2021-07-20 12:23:48 +03:00
|
|
|
// return &net.TCPConn{}, nil
|
|
|
|
return newDummyConn(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDummyConn() net.Conn {
|
|
|
|
return &dummyConn{
|
|
|
|
rawConn: &dummyRawConn{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ net.Conn = (*dummyConn)(nil)
|
|
|
|
var _ syscall.Conn = (*dummyConn)(nil)
|
|
|
|
|
|
|
|
type dummyConn struct {
|
|
|
|
rawConn *dummyRawConn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dummyConn) SyscallConn() (syscall.RawConn, error) {
|
|
|
|
return d.rawConn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var errDummy = fmt.Errorf("dummyConn err")
|
|
|
|
|
|
|
|
func (d *dummyConn) Read(b []byte) (n int, err error) {
|
|
|
|
return 0, errDummy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dummyConn) Write(b []byte) (n int, err error) {
|
|
|
|
return 0, errDummy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dummyConn) Close() error {
|
|
|
|
d.rawConn.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dummyConn) LocalAddr() net.Addr {
|
|
|
|
return &net.TCPAddr{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dummyConn) RemoteAddr() net.Addr {
|
|
|
|
return &net.TCPAddr{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dummyConn) SetDeadline(t time.Time) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dummyConn) SetReadDeadline(t time.Time) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dummyConn) SetWriteDeadline(t time.Time) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ syscall.RawConn = (*dummyRawConn)(nil)
|
|
|
|
|
|
|
|
type dummyRawConn struct {
|
|
|
|
closed bool
|
|
|
|
mux sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dummyRawConn) Control(f func(fd uintptr)) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dummyRawConn) Read(f func(fd uintptr) (done bool)) error {
|
|
|
|
d.mux.Lock()
|
|
|
|
defer d.mux.Unlock()
|
|
|
|
if d.closed {
|
|
|
|
return fmt.Errorf("dummyRawConn closed")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dummyRawConn) Write(f func(fd uintptr) (done bool)) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (d *dummyRawConn) Close() {
|
|
|
|
d.mux.Lock()
|
|
|
|
d.closed = true
|
|
|
|
d.mux.Unlock()
|
2016-03-17 14:48:04 +03:00
|
|
|
}
|