Skip to content

Commit

Permalink
fix(bft): correctly drain channels events (#1515)
Browse files Browse the repository at this point in the history
Co-authored-by: Manfred Touron <[email protected]>
  • Loading branch information
gfanton and moul authored May 16, 2024
1 parent 3c37507 commit 0c9849a
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 0c9849a

Please sign in to comment.