Prevent multiple invocations of relay cancel function

This commit is contained in:
Lukasz Zimnoch 2020-05-01 10:51:23 +02:00 committed by vyzo
parent 9a0d2f5948
commit 9a0bd7ad56
2 changed files with 24 additions and 1 deletions

View File

@ -676,12 +676,22 @@ func (p *PubSub) handleAddRelay(req *addRelayReq) {
p.rt.Join(topic)
}
req.resp <- func() {
// flag used to prevent calling cancel function multiple times
isCancelled := false
relayCancelFunc := func() {
if isCancelled {
return
}
select {
case p.rmRelay <- topic:
isCancelled = true
case <-p.ctx.Done():
}
}
req.resp <- relayCancelFunc
}
// handleRemoveRelay removes one relay reference from bookkeeping.

View File

@ -629,7 +629,20 @@ func TestTopicRelayReuse(t *testing.T) {
t.Fatal("incorrect number of relays")
}
// only the first invocation should take effect
relay1Cancel()
relay1Cancel()
relay1Cancel()
pubsubs[0].eval <- func() {
res <- pubsubs[0].myRelays[topic] == 2
}
isCorrectNumber = <-res
if !isCorrectNumber {
t.Fatal("incorrect number of relays")
}
relay2Cancel()
relay3Cancel()