Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bft): correctly drain channels events #1515

Merged
merged 16 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions tm2/pkg/bft/consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path"
"path/filepath"
"reflect"
"sort"
"sync"
"testing"
Expand Down Expand Up @@ -762,3 +763,48 @@ func newPersistentKVStore() abci.Application {
func newPersistentKVStoreWithPath(dbDir string) abci.Application {
return kvstore.NewPersistentKVStoreApplication(dbDir)
}

// ------------------------------------

func ensureDrainedChannels(t *testing.T, channels ...any) {
gfanton marked this conversation as resolved.
Show resolved Hide resolved
t.Helper()

r := recover()
if r == nil {
return
}

t.Logf("checking for drained channel")
leaks := make(map[string]int)
for _, ch := range channels {
chVal := reflect.ValueOf(ch)
if chVal.Kind() != reflect.Chan {
panic(chVal.Type().Name() + " not a channel")
}

maxExp := time.After(time.Second * 5)

// Use a select statement with reflection
cases := []reflect.SelectCase{
{Dir: reflect.SelectRecv, Chan: chVal},
{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(maxExp)},
{Dir: reflect.SelectDefault},
}

for {
chosen, recv, recvOK := reflect.Select(cases)
if chosen != 0 || !recvOK {
break
}

leaks[reflect.TypeOf(recv.Interface()).String()]++
time.Sleep(time.Millisecond * 500)
gfanton marked this conversation as resolved.
Show resolved Hide resolved
}
}

for leak, count := range leaks {
t.Logf("channel %q: %d events left\n", leak, count)
}

panic(r)
}
Loading
Loading