Skip to content

Commit

Permalink
Merge 07bebc2 into 9f9b1c4
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovic committed Oct 18, 2018
2 parents 9f9b1c4 + 07bebc2 commit e1e0ca0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 28 additions & 0 deletions stan_test.go
Expand Up @@ -2489,3 +2489,31 @@ func TestClientIDAndConnIDInPubMsg(t *testing.T) {
t.Fatal("Invalid ClientID and/or ConnID")
}
}

func TestRaceOnSubscribeAndConnClose(t *testing.T) {
s := RunServer(clusterName)
defer s.Shutdown()

cb := func(_ *Msg) {}
for i := 0; i < 100; i++ {
sc := NewDefaultConnection(t)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
i := 1
for {
if _, err := sc.Subscribe("foo", cb); err != nil {
return
}
i++
}
}()
// It's not a mistake.. not millisecond, just nano.
// The runtime.Gosched() is not helping producing the race,
// but calling Sleep() with tiny amount does.
time.Sleep(time.Duration(rand.Intn(50)))
sc.Close()
wg.Wait()
}
}
2 changes: 1 addition & 1 deletion sub.go
Expand Up @@ -253,7 +253,7 @@ func (sc *conn) subscribe(subject, qgroup string, cb MsgHandler, options ...Subs
}

b, _ := sr.Marshal()
reply, err := sc.nc.Request(sc.subRequests, b, sc.opts.ConnectTimeout)
reply, err := nc.Request(sc.subRequests, b, sc.opts.ConnectTimeout)
if err != nil {
sub.inboxSub.Unsubscribe()
if err == nats.ErrTimeout {
Expand Down

0 comments on commit e1e0ca0

Please sign in to comment.