The core protocol of WoopChain
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
woop/p2p/stream/common/streammanager/streammanager_test.go

244 lines
5.8 KiB

package streammanager
import (
"errors"
"fmt"
"strings"
"sync"
"testing"
"time"
sttypes "github.com/harmony-one/harmony/p2p/stream/types"
Release Candidate: dev -> main (#4319) * Rebase dev branch to current main branch (#4318) * add openssl compatibility on m2 chips using darwin (#4302) Adds support for OpenSSL on MacOS Ventura using m2 chips. * [dumpdb] ensure each cross link is dumped (#4311) * bump libp2p to version 0.24.0 and update its dependencies and relevant tests (#4315) * Removed legacy syncing peer provider. (#4260) * Removed legacy syncing peer provider. * Fix localnet. * Fix migrate version. * Rebased on main. * Fix formatting. * Remove blockchain dependency from engine. (#4310) * Consensus doesn't require anymore `Node` as a circular dependency. * Rebased upon main. * Removed engine beacon chain dependency. * Fixed nil error. * Fixed error. * bump libp2p to version 0.24.0 and update its dependencies and relevant tests * fix format, remove wrongly added configs * add back wrongly deleted comment * fix travis go checker Co-authored-by: Konstantin <355847+Frozen@users.noreply.github.com> Co-authored-by: “GheisMohammadi” <“Gheis.Mohammadi@gmail.com”> * bump libp2p to version 0.24.0 and update its dependencies and relevant tests (#4315) * Removed legacy syncing peer provider. (#4260) * Removed legacy syncing peer provider. * Fix localnet. * Fix migrate version. * Rebased on main. * Fix formatting. * Remove blockchain dependency from engine. (#4310) * Consensus doesn't require anymore `Node` as a circular dependency. * Rebased upon main. * Removed engine beacon chain dependency. * Fixed nil error. * Fixed error. * bump libp2p to version 0.24.0 and update its dependencies and relevant tests * fix format, remove wrongly added configs * add back wrongly deleted comment * fix travis go checker Co-authored-by: Konstantin <355847+Frozen@users.noreply.github.com> Co-authored-by: “GheisMohammadi” <“Gheis.Mohammadi@gmail.com”> * Fix for consensus stuck. (#4307) * Added check for block validity. * Starts new view change if block invalid. * Revert "Starts new view change if block invalid." This reverts commit e889fa5da2e0780f087ab7dae5106b96287706db. * staged dns sync v1.0 (#4316) * staged dns sync v1.0 * enabled stream downloader for localnet * fix code review issues * remove extra lock Co-authored-by: “GheisMohammadi” <“Gheis.Mohammadi@gmail.com”> * add description for closing client and change randomize process to ma… (#4276) * add description for closing client and change randomize process to make sure only online nodes are added to sync config * fix sync test * fix legacy limitNumPeers test * add WaitForEachPeerToConnect to node configs to make parallel peer connection optional Co-authored-by: “GheisMohammadi” <“Gheis.Mohammadi@gmail.com”> * Small fixes and code cleanup for network stack. (#4320) * staged dns sync v1.0 * enabled stream downloader for localnet * fix code review issues * remove extra lock * staged dns sync v1.0 * Fixed, code clean up and other. * Fixed, code clean up and other. * Fixed, code clean up and other. * Fix config. Co-authored-by: “GheisMohammadi” <“Gheis.Mohammadi@gmail.com”> * Fix not disable cache in archival mode (#4322) * Feature registry (#4324) * Registry for services. * Test. * Reverted comment. * Fix. * Slash fix (#4284) * Implementation of new slashing rate calculation * Write tests for then new slashing rate calculation * Add engine.applySlashing tests * fix #4059 Co-authored-by: Alex Brezas <abresas@gmail.com> Co-authored-by: Dimitris Lamprinos <pkakelas@gmail.com> * Bump github.com/aws/aws-sdk-go from 1.30.1 to 1.33.0 (#4325) (#4328) Bumps [github.com/aws/aws-sdk-go](https://github.com/aws/aws-sdk-go) from 1.30.1 to 1.33.0. - [Release notes](https://github.com/aws/aws-sdk-go/releases) - [Changelog](https://github.com/aws/aws-sdk-go/blob/v1.33.0/CHANGELOG.md) - [Commits](https://github.com/aws/aws-sdk-go/compare/v1.30.1...v1.33.0) --- updated-dependencies: - dependency-name: github.com/aws/aws-sdk-go dependency-type: direct:production ... Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Bump github.com/btcsuite/btcd from 0.21.0-beta to 0.23.2 (#4327) (#4329) Bumps [github.com/btcsuite/btcd](https://github.com/btcsuite/btcd) from 0.21.0-beta to 0.23.2. - [Release notes](https://github.com/btcsuite/btcd/releases) - [Changelog](https://github.com/btcsuite/btcd/blob/master/CHANGES) - [Commits](https://github.com/btcsuite/btcd/compare/v0.21.0-beta...v0.23.2) --- updated-dependencies: - dependency-name: github.com/btcsuite/btcd dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Max <82761650+MaxMustermann2@users.noreply.github.com> Co-authored-by: Gheis <36589218+GheisMohammadi@users.noreply.github.com> Co-authored-by: Konstantin <355847+Frozen@users.noreply.github.com> Co-authored-by: “GheisMohammadi” <“Gheis.Mohammadi@gmail.com”> Co-authored-by: Danny Willis <102543677+dannyposi@users.noreply.github.com> Co-authored-by: PeekPI <894646171@QQ.COM> Co-authored-by: Alex Brezas <abresas@gmail.com> Co-authored-by: Dimitris Lamprinos <pkakelas@gmail.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2 years ago
libp2p_peer "github.com/libp2p/go-libp2p/core/peer"
)
const (
defTestWait = 100 * time.Millisecond
)
// When started, discover will be run at bootstrap
func TestStreamManager_BootstrapDisc(t *testing.T) {
sm := newTestStreamManager()
sm.host.(*testHost).errHook = func(id sttypes.StreamID, err error) {
t.Errorf("%s stream error: %v", id, err)
}
// After bootstrap, stream manager shall discover streams and setup connection
// Note host will mock the upper code logic to call sm.NewStream in this case
sm.Start()
time.Sleep(defTestWait)
if gotSize := sm.streams.size(); gotSize != sm.config.DiscBatch {
t.Errorf("unexpected stream size: %v != %v", gotSize, sm.config.DiscBatch)
}
}
// After close, all stream shall be closed and removed
func TestStreamManager_Close(t *testing.T) {
sm := newTestStreamManager()
// Bootstrap
sm.Start()
time.Sleep(defTestWait)
// Close stream manager, all stream shall be closed and removed
closeDone := make(chan struct{})
go func() {
sm.Close()
closeDone <- struct{}{}
}()
select {
case <-time.After(defTestWait):
t.Errorf("still not closed")
case <-closeDone:
}
// Check stream been removed from stream manager and all streams to be closed
if sm.streams.size() != 0 {
t.Errorf("after close, stream not removed from stream manager")
}
host := sm.host.(*testHost)
for _, st := range host.streams {
if !st.closed {
t.Errorf("after close, stream still not closed")
}
}
}
// Close shall terminate the current discover at once
func TestStreamManager_CloseDisc(t *testing.T) {
sm := newTestStreamManager()
// discover will be blocked forever
sm.pf.(*testPeerFinder).fpHook = func(id libp2p_peer.ID) <-chan struct{} {
select {}
}
sm.Start()
time.Sleep(defTestWait)
// Close stream manager, all stream shall be closed and removed
closeDone := make(chan struct{})
go func() {
sm.Close()
closeDone <- struct{}{}
}()
select {
case <-time.After(defTestWait):
t.Errorf("close shall unblock the current discovery")
case <-closeDone:
}
}
// Each time discTicker ticks, it will cancel last discovery, and start a new one
func TestStreamManager_refreshDisc(t *testing.T) {
sm := newTestStreamManager()
// discover will be blocked for the first time but good for second time
var once sync.Once
sm.pf.(*testPeerFinder).fpHook = func(id libp2p_peer.ID) <-chan struct{} {
var sendSig = true
once.Do(func() {
sendSig = false
})
c := make(chan struct{}, 1)
if sendSig {
c <- struct{}{}
}
return c
}
sm.Start()
time.Sleep(defTestWait)
sm.discCh <- struct{}{}
time.Sleep(defTestWait)
// We shall now have non-zero streams setup
if sm.streams.size() == 0 {
t.Errorf("stream size still zero after refresh")
}
}
func TestStreamManager_HandleNewStream(t *testing.T) {
tests := []struct {
stream sttypes.Stream
expSize int
expErr error
}{
{
stream: newTestStream(makeStreamID(100), testProtoID),
expSize: defDiscBatch + 1,
expErr: nil,
},
{
stream: newTestStream(makeStreamID(1), testProtoID),
expSize: defDiscBatch,
expErr: errors.New("stream already exist"),
},
}
for i, test := range tests {
sm := newTestStreamManager()
sm.Start()
time.Sleep(defTestWait)
err := sm.NewStream(test.stream)
if assErr := assertError(err, test.expErr); assErr != nil {
t.Errorf("Test %v: %v", i, assErr)
}
if sm.streams.size() != test.expSize {
t.Errorf("Test %v: unexpected stream size: %v / %v", i, sm.streams.size(),
test.expSize)
}
}
}
func TestStreamManager_HandleRemoveStream(t *testing.T) {
tests := []struct {
id sttypes.StreamID
expSize int
expErr error
}{
{
id: makeStreamID(1),
expSize: defDiscBatch - 1,
expErr: nil,
},
{
id: makeStreamID(100),
expSize: defDiscBatch,
expErr: errors.New("stream already removed"),
},
}
for i, test := range tests {
sm := newTestStreamManager()
sm.Start()
time.Sleep(defTestWait)
err := sm.RemoveStream(test.id)
if assErr := assertError(err, test.expErr); assErr != nil {
t.Errorf("Test %v: %v", i, assErr)
}
if sm.streams.size() != test.expSize {
t.Errorf("Test %v: unexpected stream size: %v / %v", i, sm.streams.size(),
test.expSize)
}
}
}
// When number of streams is smaller than hard low limit, discover will be triggered
func TestStreamManager_HandleRemoveStream_Disc(t *testing.T) {
sm := newTestStreamManager()
sm.Start()
time.Sleep(defTestWait)
// Remove DiscBatch - HardLoCap + 1 streams
num := 0
for _, st := range sm.streams.slice() {
if err := sm.RemoveStream(st.ID()); err != nil {
t.Error(err)
}
num++
if num == sm.config.DiscBatch-sm.config.HardLoCap+1 {
break
}
}
// Last remove stream will also trigger discover
time.Sleep(defTestWait)
if sm.streams.size() != sm.config.HardLoCap+sm.config.DiscBatch-1 {
t.Errorf("unexpected stream number %v / %v", sm.streams.size(), sm.config.HardLoCap+sm.config.DiscBatch-1)
}
}
func TestStreamSet_numStreamsWithMinProtoID(t *testing.T) {
var (
pid1 = testProtoID
numPid1 = 5
pid2 = sttypes.ProtoID("harmony/sync/unitest/0/1.0.1")
numPid2 = 10
)
ss := newStreamSet()
for i := 0; i != numPid1; i++ {
ss.addStream(newTestStream(makeStreamID(i), pid1))
}
for i := 0; i != numPid2; i++ {
ss.addStream(newTestStream(makeStreamID(i), pid2))
}
minSpec, _ := sttypes.ProtoIDToProtoSpec(pid2)
num := ss.numStreamsWithMinProtoSpec(minSpec)
if num != numPid2 {
t.Errorf("unexpected result: %v/%v", num, numPid2)
}
}
func assertError(got, exp error) error {
if (got == nil) != (exp == nil) {
return fmt.Errorf("unexpected error: %v / %v", got, exp)
}
if got == nil {
return nil
}
if !strings.Contains(got.Error(), exp.Error()) {
return fmt.Errorf("unexpected error: %v / %v", got, exp)
}
return nil
}