File: pool_test.go

package info (click to toggle)
golang-github-bsm-pool 0.8.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 88 kB
  • sloc: makefile: 14
file content (184 lines) | stat: -rw-r--r-- 3,804 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package pool_test

import (
	"net"
	"net/http"
	"net/http/httptest"
	"strings"
	"sync"
	"testing"
	"time"

	"github.com/bsm/pool"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("Pool", func() {
	var (
		subject *pool.Pool
		server  *httptest.Server
		factory pool.Factory
	)

	BeforeEach(func() {
		server, factory = mockServer()

		var err error
		subject, err = pool.New(&pool.Options{
			InitialSize: 3,
			MaxCap:      5,
		}, factory)
		Expect(err).NotTo(HaveOccurred())
	})

	AfterEach(func() {
		Expect(subject.Close()).To(Succeed())
		server.Close()
	})

	It("should check len", func() {
		Expect(subject.Len()).To(Equal(3))
	})

	It("should get/put", func() {
		cn1, err := subject.Get()
		Expect(err).NotTo(HaveOccurred())
		Expect(subject.Len()).To(Equal(2))

		cn2, err := subject.Get()
		Expect(err).NotTo(HaveOccurred())
		Expect(subject.Len()).To(Equal(1))

		cn3, err := subject.Get()
		Expect(err).NotTo(HaveOccurred())
		Expect(subject.Len()).To(Equal(0))

		cn4, err := subject.Get()
		Expect(err).NotTo(HaveOccurred())
		Expect(subject.Len()).To(Equal(0))

		cn5, err := subject.Get()
		Expect(err).NotTo(HaveOccurred())

		cn6, err := subject.Get()
		Expect(err).NotTo(HaveOccurred())

		Expect(subject.Put(cn1)).To(BeTrue())
		Expect(subject.Put(cn2)).To(BeTrue())
		Expect(subject.Put(cn3)).To(BeTrue())
		Expect(subject.Put(cn4)).To(BeTrue())
		Expect(subject.Len()).To(Equal(4))

		Expect(subject.Put(cn5)).To(BeTrue())
		Expect(subject.Len()).To(Equal(5))

		Expect(subject.Put(cn6)).To(BeFalse())
		Expect(subject.Len()).To(Equal(5))

		_, err = cn6.Write([]byte("x"))
		Expect(err).To(HaveOccurred())
		Expect(err.(*net.OpError).Err).To(MatchError("use of closed network connection"))

		last, err := subject.Get()
		Expect(err).NotTo(HaveOccurred())
		Expect(last).NotTo(BeNil())
		Expect(last).NotTo(Equal(cn1))
		Expect(last).NotTo(Equal(cn2))
		Expect(last).NotTo(Equal(cn3))
		Expect(last).NotTo(Equal(cn4))
		Expect(last).To(Equal(cn5))
	})

	It("should be thread-safe", func() {
		n := 10000
		if testing.Short() {
			n = 100
		}

		wg := new(sync.WaitGroup)
		for c := 0; c < 10; c++ {
			wg.Add(1)
			go func() {
				defer GinkgoRecover()
				defer wg.Done()

				for i := 0; i < n; i++ {
					cn, err := subject.Get()
					Expect(err).NotTo(HaveOccurred())

					subject.Put(cn)
				}
			}()
		}
		wg.Wait()

		Expect(subject.Len()).To(Equal(5))
	})

	It("should reap idle connections", func() {
		p, err := pool.New(&pool.Options{
			InitialSize:  3,
			MaxCap:       5,
			ReapInterval: 200 * time.Millisecond,
			IdleTimeout:  300 * time.Millisecond,
		}, factory)
		Expect(err).NotTo(HaveOccurred())
		defer p.Close()

		Expect(p.Len()).To(Equal(3))
		Eventually(func() (int, error) {
			cn, err := p.Get()
			if err != nil {
				return 0, err
			}
			if err != nil {
				return 0, err
			}

			p.Put(cn)
			return p.Len(), nil
		}).Should(Equal(1))
	})

})

// --------------------------------------------------------------------

func mockServer() (*httptest.Server, pool.Factory) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNoContent)
	}))
	factory := func() (net.Conn, error) {
		return net.Dial("tcp", strings.Replace(server.URL, "http://", "", -1))
	}
	return server, factory
}

// --------------------------------------------------------------------

func TestSuite(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "pool")
}

func BenchmarkPool(b *testing.B) {
	srv, factory := mockServer()
	defer srv.Close()

	p, err := pool.New(nil, factory)
	if err != nil {
		b.Fatal(err)
	}
	defer p.Close()

	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			cn, err := p.Get()
			if err != nil {
				b.Fatal(err)
			}
			p.Put(cn)
		}
	})
}