-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
delta: fix goroutine leak when watches close and a node is removed (#570
- Loading branch information
1 parent
26e2ad7
commit 6efcb69
Showing
2 changed files
with
48 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package delta | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/envoyproxy/go-control-plane/pkg/cache/v3" | ||
) | ||
|
||
func TestDeltaWatches(t *testing.T) { | ||
t.Run("watches response channels are properly closed when the watches are cancelled", func(t *testing.T) { | ||
watches := newWatches() | ||
|
||
cancelCount := 0 | ||
var channels []chan cache.DeltaResponse | ||
// create a few watches, and ensure that the cancel function are called and the channels are closed | ||
for i := 0; i < 5; i++ { | ||
newWatch := watch{} | ||
if i%2 == 0 { | ||
newWatch.cancel = func() { cancelCount++ } | ||
newWatch.responses = make(chan cache.DeltaResponse) | ||
channels = append(channels, newWatch.responses) | ||
} | ||
|
||
watches.deltaWatches[strconv.Itoa(i)] = newWatch | ||
} | ||
|
||
watches.Cancel() | ||
|
||
assert.Equal(t, 3, cancelCount) | ||
for _, channel := range channels { | ||
select { | ||
case _, ok := <-channel: | ||
assert.False(t, ok, "a channel was not closed") | ||
default: | ||
assert.Fail(t, "a channel was not closed") | ||
} | ||
} | ||
}) | ||
} |