Skip to content

Commit

Permalink
fix a race on reading the called value
Browse files Browse the repository at this point in the history
  • Loading branch information
fzerorubigd committed Feb 3, 2019
1 parent 5ca08e5 commit 6a4ffbf
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type Command struct {
terminate bool
delay time.Duration

lock sync.Mutex
lock sync.RWMutex
called int
}

Expand Down Expand Up @@ -125,14 +125,14 @@ func (s *Server) Addr() *net.TCPAddr {

// ExpectationsWereMet return nil if the all expects match or error if not
func (s *Server) ExpectationsWereMet() error {
s.lock.RLock()
var all []error
for i := range s.expectList {
if err := s.expectList[i].error(); err != nil {
all = append(all, err)
}
}

s.lock.RLock()
for i := range s.unexpectedCommands {
all = append(all, fmt.Errorf(
"command %s is called but not expected",
Expand Down Expand Up @@ -246,6 +246,9 @@ func (c *Command) compare(input []string) bool {
}

func (c *Command) error() error {
c.lock.RLock()
defer c.lock.RUnlock()

if c.count < 0 || c.count == c.called {
return nil
}
Expand Down

0 comments on commit 6a4ffbf

Please sign in to comment.