Fixed tests.

pull/4509/head
frozen 2 years ago committed by Casey Gardiner
parent d1a0e9cbf2
commit 69ae68c744
  1. 4
      consensus/consensus_service.go
  2. 17
      internal/utils/singleton.go

@ -442,7 +442,11 @@ func (consensus *Consensus) UpdateConsensusInformation() Mode {
// IsLeader check if the node is a leader or not by comparing the public key of // IsLeader check if the node is a leader or not by comparing the public key of
// the node with the leader public key // the node with the leader public key
func (consensus *Consensus) IsLeader() bool { func (consensus *Consensus) IsLeader() bool {
_ = utils.AssertNoLongerThan0(5*time.Second, func() error {
consensus.mutex.RLock() consensus.mutex.RLock()
return nil
})
defer consensus.mutex.RUnlock() defer consensus.mutex.RUnlock()
return consensus.isLeader() return consensus.isLeader()

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"os" "os"
"path" "path"
"runtime/debug"
"strconv" "strconv"
"sync" "sync"
"time" "time"
@ -213,3 +214,19 @@ func GetPort() int {
} }
return 0 return 0
} }
func AssertNoLongerThan0[E any](t time.Duration, f func() E) E {
ch := make(chan E)
defer close(ch)
stack := debug.Stack()
go func() {
select {
case <-time.After(t):
panic("AssertNoLongerThan0: " + string(stack))
case <-ch:
return
}
}()
return f()
}

Loading…
Cancel
Save