Browse Source

fixes from Bucky's review

pull/820/head
Anton Kaliaev 7 years ago
parent
commit
e0daca5693
No known key found for this signature in database GPG Key ID: 7B6881D965918214
4 changed files with 56 additions and 51 deletions
  1. +6
    -4
      consensus/mempool_test.go
  2. +10
    -7
      node/node_test.go
  3. +20
    -18
      p2p/pex_reactor_test.go
  4. +20
    -22
      p2p/switch_test.go

+ 6
- 4
consensus/mempool_test.go View File

@ -141,10 +141,12 @@ func TestRmBadTx(t *testing.T) {
}
// check for the tx
txs := cs.mempool.Reap(1)
if len(txs) == 0 {
emptyMempoolCh <- struct{}{}
return
for {
txs := cs.mempool.Reap(1)
if len(txs) == 0 {
emptyMempoolCh <- struct{}{}
}
time.Sleep(10 * time.Millisecond)
}
}()


+ 10
- 7
node/node_test.go View File

@ -9,27 +9,30 @@ import (
"github.com/tendermint/tmlibs/log"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/types"
)
func TestNodeStartStop(t *testing.T) {
config := cfg.ResetTestRoot("node_node_test")
// Create & start node
// create & start node
n, err := DefaultNewNode(config, log.TestingLogger())
assert.NoError(t, err, "expected no err on DefaultNewNode")
n.Start()
t.Logf("Started node %v", n.sw.NodeInfo())
ticker := time.NewTicker(10 * time.Millisecond)
// wait for the node to produce a block
blockCh := make(chan struct{})
types.AddListenerForEvent(n.EventSwitch(), "node_test", types.EventStringNewBlock(), func(types.TMEventData) {
blockCh <- struct{}{}
})
select {
case <-ticker.C:
if n.IsRunning() {
return
}
case <-blockCh:
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for start")
t.Fatal("timed out waiting for the node to produce a block")
}
// stop the node
go func() {
n.Stop()
}()


+ 20
- 18
p2p/pex_reactor_test.go View File

@ -109,26 +109,28 @@ func TestPEXReactorRunning(t *testing.T) {
func assertSomePeersWithTimeout(t *testing.T, switches []*Switch, checkPeriod, timeout time.Duration) {
ticker := time.NewTicker(checkPeriod)
select {
case <-ticker.C:
// check peers are connected
allGood := true
for _, s := range switches {
outbound, inbound, _ := s.NumPeers()
if outbound+inbound == 0 {
allGood = false
for {
select {
case <-ticker.C:
// check peers are connected
allGood := true
for _, s := range switches {
outbound, inbound, _ := s.NumPeers()
if outbound+inbound == 0 {
allGood = false
}
}
if allGood {
return
}
case <-time.After(timeout):
numPeersStr := ""
for i, s := range switches {
outbound, inbound, _ := s.NumPeers()
numPeersStr += fmt.Sprintf("%d => {outbound: %d, inbound: %d}, ", i, outbound, inbound)
}
t.Errorf("expected all switches to be connected to at least one peer (switches: %s)", numPeersStr)
}
if allGood {
return
}
case <-time.After(timeout):
numPeersStr := ""
for i, s := range switches {
outbound, inbound, _ := s.NumPeers()
numPeersStr += fmt.Sprintf("%d => {outbound: %d, inbound: %d}, ", i, outbound, inbound)
}
t.Errorf("expected all switches to be connected to at least one peer (switches: %s)", numPeersStr)
}
}


+ 20
- 22
p2p/switch_test.go View File

@ -138,16 +138,19 @@ func TestSwitches(t *testing.T) {
func assertMsgReceivedWithTimeout(t *testing.T, msg string, channel byte, reactor *TestReactor, checkPeriod, timeout time.Duration) {
ticker := time.NewTicker(checkPeriod)
select {
case <-ticker.C:
msgs := reactor.getMsgs(channel)
if len(msgs) > 0 {
if !bytes.Equal(msgs[0].Bytes, wire.BinaryBytes(msg)) {
t.Fatalf("Unexpected message bytes. Wanted: %X, Got: %X", wire.BinaryBytes(msg), msgs[0].Bytes)
for {
select {
case <-ticker.C:
msgs := reactor.getMsgs(channel)
if len(msgs) > 0 {
if !bytes.Equal(msgs[0].Bytes, wire.BinaryBytes(msg)) {
t.Fatalf("Unexpected message bytes. Wanted: %X, Got: %X", wire.BinaryBytes(msg), msgs[0].Bytes)
}
return
}
case <-time.After(timeout):
t.Fatalf("Expected to have received 1 message in channel #%v, got zero", channel)
}
case <-time.After(timeout):
t.Fatalf("Expected to have received 1 message in channel #%v, got zero", channel)
}
}
@ -174,19 +177,14 @@ func TestConnAddrFilter(t *testing.T) {
s2.addPeerWithConnection(c2)
}()
assertNoPeersWithTimeout(t, s1, 100*time.Millisecond, 400*time.Millisecond)
assertNoPeersWithTimeout(t, s2, 100*time.Millisecond, 400*time.Millisecond)
assertNoPeersAfterTimeout(t, s1, 400*time.Millisecond)
assertNoPeersAfterTimeout(t, s2, 400*time.Millisecond)
}
func assertNoPeersWithTimeout(t *testing.T, sw *Switch, checkPeriod, timeout time.Duration) {
ticker := time.NewTicker(checkPeriod)
select {
case <-ticker.C:
if sw.Peers().Size() != 0 {
t.Fatalf("Expected %v to not connect to some peers, got %d", sw, sw.Peers().Size())
}
case <-time.After(timeout):
return
func assertNoPeersAfterTimeout(t *testing.T, sw *Switch, timeout time.Duration) {
time.Sleep(timeout)
if sw.Peers().Size() != 0 {
t.Fatalf("Expected %v to not connect to some peers, got %d", sw, sw.Peers().Size())
}
}
@ -214,8 +212,8 @@ func TestConnPubKeyFilter(t *testing.T) {
s2.addPeerWithConnection(c2)
}()
assertNoPeersWithTimeout(t, s1, 100*time.Millisecond, 400*time.Millisecond)
assertNoPeersWithTimeout(t, s2, 100*time.Millisecond, 400*time.Millisecond)
assertNoPeersAfterTimeout(t, s1, 400*time.Millisecond)
assertNoPeersAfterTimeout(t, s2, 400*time.Millisecond)
}
func TestSwitchStopsNonPersistentPeerOnError(t *testing.T) {
@ -238,7 +236,7 @@ func TestSwitchStopsNonPersistentPeerOnError(t *testing.T) {
// simulate failure by closing connection
peer.CloseConn()
assertNoPeersWithTimeout(t, sw, 100*time.Millisecond, 100*time.Millisecond)
assertNoPeersAfterTimeout(t, sw, 100*time.Millisecond)
assert.False(peer.IsRunning())
}


Loading…
Cancel
Save