Skip to content

Commit

Permalink
race detector enabled at the testing stage
Browse files Browse the repository at this point in the history
  • Loading branch information
kpacha committed Apr 11, 2018
1 parent f792d79 commit 5250d46
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ deps:
@echo ""

test:
go test -cover ./...
go test -cover -race ./...

benchmark:
@echo "Proxy middleware stack"
Expand Down
38 changes: 29 additions & 9 deletions proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"strings"
"sync"
"testing"
"time"
)
Expand Down Expand Up @@ -72,18 +73,22 @@ func (d dummyReadCloser) Close() error {

func TestWrapper(t *testing.T) {
expected := "supu"
closed := false
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

r := NewReadCloserWrapper(ctx, dummyRC{bytes.NewBufferString(expected), &closed})
readCloser := &dummyRC{
r: bytes.NewBufferString(expected),
mu: &sync.Mutex{},
}

r := NewReadCloserWrapper(ctx, readCloser)
var out bytes.Buffer
tot, err := out.ReadFrom(r)
if err != nil {
t.Errorf("Total bits read: %d. Err: %s", tot, err.Error())
return
}
if closed {
if readCloser.IsClosed() {
t.Error("The subject shouldn't be closed yet")
return
}
Expand All @@ -98,25 +103,40 @@ func TestWrapper(t *testing.T) {

cancel()
<-time.After(100 * time.Millisecond)
if !closed {
if !readCloser.IsClosed() {
t.Error("The subject should be already closed")
return
}
}

type dummyRC struct {
r io.Reader
closed *bool
closed bool
mu *sync.Mutex
}

func (d dummyRC) Read(b []byte) (int, error) {
if *(d.closed) {
func (d *dummyRC) Read(b []byte) (int, error) {
d.mu.Lock()
defer d.mu.Unlock()

if d.closed {
return -1, fmt.Errorf("Reading from a closed source")
}
return d.r.Read(b)
}

func (d dummyRC) Close() error {
*(d.closed) = true
func (d *dummyRC) Close() error {
d.mu.Lock()
defer d.mu.Unlock()

d.closed = true
return nil
}

func (d *dummyRC) IsClosed() bool {
d.mu.Lock()
defer d.mu.Unlock()

res := d.closed
return res
}

0 comments on commit 5250d46

Please sign in to comment.