Skip to content

Commit

Permalink
fix concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
irgendwr committed Feb 8, 2019
1 parent 78271a6 commit 2516d10
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
12 changes: 11 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"regexp"
"strings"
"sync"
"time"
)

Expand Down Expand Up @@ -57,6 +58,7 @@ type Client struct {
notify chan Notification
disconnect chan struct{}
res []string
mutex sync.Mutex

Server *ServerMethods
}
Expand Down Expand Up @@ -234,7 +236,15 @@ func (c *Client) ExecCmd(cmd *Cmd) ([]string, error) {
return nil, ErrNotConnected
}

c.work <- cmd.String()
c.mutex.Lock()
defer c.mutex.Unlock()

select {
case c.work <- cmd.String():
// continue
case <-time.After(c.timeout):
return nil, ErrTimeout
}

select {
case err := <-c.err:
Expand Down
33 changes: 33 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,36 @@ func TestClientBadHeader(t *testing.T) {
// Should never get here
assert.NoError(t, c.Close())
}

func TestConcurrency(t *testing.T) {
s := newServer(t)
if s == nil {
return
}
defer func() {
assert.NoError(t, s.Close())
}()

c, err := NewClient(s.Addr, Timeout(time.Millisecond*100))
if !assert.NoError(t, err) {
return
}

wait := make(chan struct{})

go func() {
for i := 0; i <= 10; i++ {
_, err = c.Server.GroupList()
assert.NoError(t, err)
}
wait <- struct{}{}
}()

for i := 0; i <= 10; i++ {
_, err = c.Server.GroupList()
assert.NoError(t, err)
}

// wait for go routine to finish
<-wait
}

0 comments on commit 2516d10

Please sign in to comment.