mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
beacon/light/sync: test Server.Fail calls
This commit is contained in:
parent
426c1fb010
commit
c2b8cb226e
2 changed files with 36 additions and 3 deletions
|
|
@ -24,9 +24,14 @@ import (
|
||||||
"github.com/ethereum/go-ethereum/beacon/types"
|
"github.com/ethereum/go-ethereum/beacon/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TestServer struct{ ID int }
|
type TestServer struct {
|
||||||
|
ts *TestScheduler
|
||||||
|
ID int
|
||||||
|
}
|
||||||
|
|
||||||
func (ts *TestServer) Fail(desc string) {}
|
func (s *TestServer) Fail(desc string) {
|
||||||
|
s.ts.serverFail(s)
|
||||||
|
}
|
||||||
|
|
||||||
type TestScheduler struct {
|
type TestScheduler struct {
|
||||||
t *testing.T
|
t *testing.T
|
||||||
|
|
@ -35,6 +40,8 @@ type TestScheduler struct {
|
||||||
servers []request.Server
|
servers []request.Server
|
||||||
allowance map[request.Server]int
|
allowance map[request.Server]int
|
||||||
sent map[int]request.RequestWithID
|
sent map[int]request.RequestWithID
|
||||||
|
testIndex int
|
||||||
|
expFail map[request.Server]int // expected Server.Fail calls during next Run
|
||||||
lastId request.ID
|
lastId request.ID
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -43,13 +50,24 @@ func NewTestScheduler(t *testing.T, module request.Module) *TestScheduler {
|
||||||
t: t,
|
t: t,
|
||||||
module: module,
|
module: module,
|
||||||
allowance: make(map[request.Server]int),
|
allowance: make(map[request.Server]int),
|
||||||
|
expFail: make(map[request.Server]int),
|
||||||
sent: make(map[int]request.RequestWithID),
|
sent: make(map[int]request.RequestWithID),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ts *TestScheduler) Run(testIndex int, expServer request.Server, expReq request.Request) {
|
func (ts *TestScheduler) Run(testIndex int, expServer request.Server, expReq request.Request) {
|
||||||
|
ts.testIndex = testIndex
|
||||||
ts.module.Process(ts.events)
|
ts.module.Process(ts.events)
|
||||||
ts.events = nil
|
ts.events = nil
|
||||||
|
|
||||||
|
for server, count := range ts.expFail {
|
||||||
|
delete(ts.expFail, server)
|
||||||
|
if count == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ts.t.Errorf("Missing %d Server.Fail(s) from server %d in test case #%d", count, server.(*TestServer).ID, testIndex)
|
||||||
|
}
|
||||||
|
|
||||||
expReqWithID := request.RequestWithID{
|
expReqWithID := request.RequestWithID{
|
||||||
ServerAndID: request.ServerAndID{Server: expServer, ID: ts.lastId + 1},
|
ServerAndID: request.ServerAndID{Server: expServer, ID: ts.lastId + 1},
|
||||||
Request: expReq,
|
Request: expReq,
|
||||||
|
|
@ -100,6 +118,7 @@ func (ts *TestScheduler) RequestEvent(evType *request.EventType, testIndex int,
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ts *TestScheduler) AddServer(server request.Server, allowance int) {
|
func (ts *TestScheduler) AddServer(server request.Server, allowance int) {
|
||||||
|
server.(*TestServer).ts = ts
|
||||||
ts.servers = append(ts.servers, server)
|
ts.servers = append(ts.servers, server)
|
||||||
ts.allowance[server] = allowance
|
ts.allowance[server] = allowance
|
||||||
ts.ServerEvent(request.EvRegistered, server, nil)
|
ts.ServerEvent(request.EvRegistered, server, nil)
|
||||||
|
|
@ -122,6 +141,18 @@ func (ts *TestScheduler) AddAllowance(server request.Server, allowance int) {
|
||||||
ts.allowance[server] += allowance
|
ts.allowance[server] += allowance
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ts *TestScheduler) ExpFail(server request.Server) {
|
||||||
|
ts.expFail[server]++
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts *TestScheduler) serverFail(server request.Server) {
|
||||||
|
if ts.expFail[server] == 0 {
|
||||||
|
ts.t.Errorf("Unexpected Server.Fail from server %d in test case #%d", server.(*TestServer).ID, ts.testIndex)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ts.expFail[server]--
|
||||||
|
}
|
||||||
|
|
||||||
func (ts *TestScheduler) tryRequest(testIndex int, requestFn func(server request.Server) (request.Request, float32)) (request.RequestWithID, bool) {
|
func (ts *TestScheduler) tryRequest(testIndex int, requestFn func(server request.Server) (request.Request, float32)) (request.RequestWithID, bool) {
|
||||||
var (
|
var (
|
||||||
bestServer request.Server
|
bestServer request.Server
|
||||||
|
|
|
||||||
|
|
@ -40,8 +40,9 @@ func TestCheckpointInit(t *testing.T) {
|
||||||
ts.RequestEvent(request.EvTimeout, 1, nil)
|
ts.RequestEvent(request.EvTimeout, 1, nil)
|
||||||
ts.Run(2, testServer2, ReqCheckpointData(checkpointHash))
|
ts.Run(2, testServer2, ReqCheckpointData(checkpointHash))
|
||||||
|
|
||||||
// invalid response to server 2; expect init state to still be false
|
// invalid response from server 2; expect init state to still be false
|
||||||
ts.RequestEvent(request.EvResponse, 2, &types.BootstrapData{Header: types.Header{Slot: 123456}})
|
ts.RequestEvent(request.EvResponse, 2, &types.BootstrapData{Header: types.Header{Slot: 123456}})
|
||||||
|
ts.ExpFail(testServer2)
|
||||||
ts.Run(3, nil, nil)
|
ts.Run(3, nil, nil)
|
||||||
chain.ExpInit(t, false)
|
chain.ExpInit(t, false)
|
||||||
|
|
||||||
|
|
@ -172,6 +173,7 @@ func TestUpdateSyncDifferentHeads(t *testing.T) {
|
||||||
req1x := ts.Request(1)
|
req1x := ts.Request(1)
|
||||||
req1x.Request = ReqUpdates{FirstPeriod: 10, Count: 5}
|
req1x.Request = ReqUpdates{FirstPeriod: 10, Count: 5}
|
||||||
ts.RequestEvent(request.EvResponse, 1, testRespUpdate(req1x))
|
ts.RequestEvent(request.EvResponse, 1, testRespUpdate(req1x))
|
||||||
|
ts.ExpFail(testServer3)
|
||||||
ts.Run(5, nil, nil)
|
ts.Run(5, nil, nil)
|
||||||
// expect no progress of chain head
|
// expect no progress of chain head
|
||||||
chain.ExpNextSyncPeriod(t, 15)
|
chain.ExpNextSyncPeriod(t, 15)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue