gecko/snow/networking/sender/sender_test.go

78 lines
1.7 KiB
Go
Raw Normal View History

2020-03-10 12:20:34 -07:00
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package sender
import (
2020-04-08 16:36:26 -07:00
"reflect"
2020-03-10 12:20:34 -07:00
"sync"
"testing"
"time"
"github.com/ava-labs/gecko/ids"
"github.com/ava-labs/gecko/snow"
"github.com/ava-labs/gecko/snow/engine/common"
"github.com/ava-labs/gecko/snow/networking/router"
"github.com/ava-labs/gecko/snow/networking/timeout"
"github.com/ava-labs/gecko/utils/logging"
)
2020-04-08 16:36:26 -07:00
func TestSenderContext(t *testing.T) {
context := snow.DefaultContextTest()
sender := Sender{}
sender.Initialize(
context,
&ExternalSenderTest{},
&router.ChainRouter{},
&timeout.Manager{},
)
if res := sender.Context(); !reflect.DeepEqual(res, context) {
t.Fatalf("Got %#v, expected %#v", res, context)
}
}
2020-03-10 12:20:34 -07:00
func TestTimeout(t *testing.T) {
tm := timeout.Manager{}
tm.Initialize(time.Millisecond)
go tm.Dispatch()
2020-05-28 20:48:08 -07:00
chainRouter := router.ChainRouter{}
2020-05-29 15:32:17 -07:00
chainRouter.Initialize(logging.NoLog{}, &tm, time.Hour, time.Second)
2020-03-10 12:20:34 -07:00
sender := Sender{}
2020-05-28 20:48:08 -07:00
sender.Initialize(snow.DefaultContextTest(), &ExternalSenderTest{}, &chainRouter, &tm)
2020-03-10 12:20:34 -07:00
engine := common.EngineTest{T: t}
engine.Default(true)
engine.ContextF = snow.DefaultContextTest
wg := sync.WaitGroup{}
wg.Add(2)
failedVDRs := ids.ShortSet{}
2020-05-28 20:48:08 -07:00
engine.QueryFailedF = func(validatorID ids.ShortID, _ uint32) error {
2020-03-10 12:20:34 -07:00
failedVDRs.Add(validatorID)
wg.Done()
2020-05-28 20:48:08 -07:00
return nil
2020-03-10 12:20:34 -07:00
}
2020-05-28 20:48:08 -07:00
handler := router.Handler{}
2020-03-10 12:20:34 -07:00
handler.Initialize(&engine, nil, 1)
go handler.Dispatch()
2020-05-28 20:48:08 -07:00
chainRouter.AddChain(&handler)
2020-03-10 12:20:34 -07:00
vdrIDs := ids.ShortSet{}
vdrIDs.Add(ids.NewShortID([20]byte{255}))
vdrIDs.Add(ids.NewShortID([20]byte{254}))
sender.PullQuery(vdrIDs, 0, ids.Empty)
wg.Wait()
if !failedVDRs.Equals(vdrIDs) {
t.Fatalf("Timeouts should have fired")
}
}