aboutsummaryrefslogtreecommitdiff
path: root/server_timing_test.go
diff options
context:
space:
mode:
authorGravatar Aliaksandr Valialkin <valyala@gmail.com> 2015-11-25 17:33:57 +0200
committerGravatar Aliaksandr Valialkin <valyala@gmail.com> 2015-11-25 17:33:57 +0200
commit1c4474c96fea2de75d464b158d7d73c9707daef8 (patch)
tree5b26eecff85a4fb5f266498da3bf4167288a4c1c /server_timing_test.go
parentFixed a bug in ipv4 addresses resolution if the resolved ip addresses contain... (diff)
downloadfasthttp-1c4474c96fea2de75d464b158d7d73c9707daef8.tar.gz
fasthttp-1c4474c96fea2de75d464b158d7d73c9707daef8.tar.bz2
fasthttp-1c4474c96fea2de75d464b158d7d73c9707daef8.zip
Made fakeListener.Accept() safe for concurrent use
Diffstat (limited to 'server_timing_test.go')
-rw-r--r--server_timing_test.go13
1 files changed, 12 insertions, 1 deletions
diff --git a/server_timing_test.go b/server_timing_test.go
index 739be01..08c916a 100644
--- a/server_timing_test.go
+++ b/server_timing_test.go
@@ -8,6 +8,7 @@ import (
"net"
"net/http"
"runtime"
+ "sync"
"sync/atomic"
"testing"
"time"
@@ -240,19 +241,28 @@ func (c *fakeServerConn) SetWriteDeadline(t time.Time) error {
}
type fakeListener struct {
+ lock sync.Mutex
requestsCount int
requestsPerConn int
request []byte
ch chan *fakeServerConn
done chan struct{}
+ closed bool
}
func (ln *fakeListener) Accept() (net.Conn, error) {
+ ln.lock.Lock()
if ln.requestsCount == 0 {
+ ln.lock.Unlock()
for len(ln.ch) < cap(ln.ch) {
time.Sleep(10 * time.Millisecond)
}
- close(ln.done)
+ ln.lock.Lock()
+ if !ln.closed {
+ close(ln.done)
+ ln.closed = true
+ }
+ ln.lock.Unlock()
return nil, io.EOF
}
requestsCount := ln.requestsPerConn
@@ -260,6 +270,7 @@ func (ln *fakeListener) Accept() (net.Conn, error) {
requestsCount = ln.requestsCount
}
ln.requestsCount -= requestsCount
+ ln.lock.Unlock()
c := <-ln.ch
c.requestsCount = requestsCount