2018-05-28 15:18:14 +00:00
|
|
|
package proposer
|
|
|
|
|
|
|
|
import (
|
2018-08-12 17:58:02 +00:00
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
2018-05-28 15:18:14 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-08-12 17:58:02 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
|
|
"github.com/sirupsen/logrus"
|
2018-07-27 00:29:28 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
2018-05-28 15:18:14 +00:00
|
|
|
)
|
|
|
|
|
2018-08-12 17:58:02 +00:00
|
|
|
func init() {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
logrus.SetOutput(ioutil.Discard)
|
2018-07-27 00:29:28 +00:00
|
|
|
}
|
2018-07-23 16:29:00 +00:00
|
|
|
|
2018-08-12 17:58:02 +00:00
|
|
|
type mockBeaconService struct {
|
|
|
|
proposerChan chan bool
|
|
|
|
attesterChan chan bool
|
2018-07-23 16:29:00 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 17:58:02 +00:00
|
|
|
func (m *mockBeaconService) AttesterAssignment() <-chan bool {
|
|
|
|
return m.attesterChan
|
|
|
|
}
|
2018-07-23 16:29:00 +00:00
|
|
|
|
2018-08-12 17:58:02 +00:00
|
|
|
func (m *mockBeaconService) ProposerAssignment() <-chan bool {
|
|
|
|
return m.proposerChan
|
2018-07-23 16:29:00 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 17:58:02 +00:00
|
|
|
func TestLifecycle(t *testing.T) {
|
2018-07-23 16:29:00 +00:00
|
|
|
hook := logTest.NewGlobal()
|
2018-08-12 17:58:02 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
b := &mockBeaconService{
|
|
|
|
proposerChan: make(chan bool),
|
|
|
|
}
|
|
|
|
p := NewProposer(context.Background(), b)
|
|
|
|
p.Start()
|
|
|
|
testutil.AssertLogsContain(t, hook, "Starting service")
|
|
|
|
p.Stop()
|
|
|
|
testutil.AssertLogsContain(t, hook, "Stopping service")
|
2018-07-23 16:29:00 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 17:58:02 +00:00
|
|
|
func TestProposerLoop(t *testing.T) {
|
2018-07-23 16:29:00 +00:00
|
|
|
hook := logTest.NewGlobal()
|
2018-08-12 17:58:02 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
|
|
|
b := &mockBeaconService{
|
|
|
|
proposerChan: make(chan bool),
|
|
|
|
}
|
|
|
|
p := NewProposer(context.Background(), b)
|
|
|
|
|
|
|
|
doneChan := make(chan struct{})
|
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
p.run(doneChan)
|
|
|
|
<-exitRoutine
|
2018-07-27 00:29:28 +00:00
|
|
|
}()
|
2018-08-12 17:58:02 +00:00
|
|
|
b.proposerChan <- true
|
|
|
|
testutil.AssertLogsContain(t, hook, "Performing proposer responsibility")
|
|
|
|
doneChan <- struct{}{}
|
|
|
|
exitRoutine <- true
|
|
|
|
testutil.AssertLogsContain(t, hook, "Proposer context closed")
|
2018-05-28 15:18:14 +00:00
|
|
|
}
|