Skip to content

Commit

Permalink
fix(bft): correctly drain channels events (gnolang#1515)
Browse files Browse the repository at this point in the history
Co-authored-by: Manfred Touron <94029+moul@users.noreply.github.com>
  • Loading branch information
2 people authored and leohhhn committed May 21, 2024
1 parent 4bbf429 commit 9635a95
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 25 deletions.
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) {
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)
}
}

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

panic(r)
}
Loading

0 comments on commit 9635a95

Please sign in to comment.