2018-07-26 01:57:19 +00:00
|
|
|
package attester
|
|
|
|
|
|
|
|
import (
|
2018-08-09 22:54:59 +00:00
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
2018-07-26 01:57:19 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-08-12 17:58:02 +00:00
|
|
|
"github.com/golang/mock/gomock"
|
2018-08-09 22:54:59 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
2018-07-26 01:57:19 +00:00
|
|
|
)
|
|
|
|
|
2018-08-09 22:54:59 +00:00
|
|
|
func init() {
|
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
logrus.SetOutput(ioutil.Discard)
|
|
|
|
}
|
2018-07-26 01:57:19 +00:00
|
|
|
|
2018-08-12 17:58:02 +00:00
|
|
|
type mockBeaconService struct {
|
|
|
|
proposerChan chan bool
|
|
|
|
attesterChan chan bool
|
2018-08-09 22:54:59 +00:00
|
|
|
}
|
2018-07-26 01:57:19 +00:00
|
|
|
|
2018-08-12 17:58:02 +00:00
|
|
|
func (m *mockBeaconService) AttesterAssignment() <-chan bool {
|
|
|
|
return m.attesterChan
|
|
|
|
}
|
2018-08-09 22:54:59 +00:00
|
|
|
|
2018-08-12 17:58:02 +00:00
|
|
|
func (m *mockBeaconService) ProposerAssignment() <-chan bool {
|
|
|
|
return m.proposerChan
|
2018-08-09 22:54:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLifecycle(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
2018-08-12 17:58:02 +00:00
|
|
|
b := &mockBeaconService{
|
|
|
|
attesterChan: make(chan bool),
|
|
|
|
}
|
|
|
|
at := NewAttester(context.Background(), b)
|
2018-08-09 22:54:59 +00:00
|
|
|
at.Start()
|
|
|
|
testutil.AssertLogsContain(t, hook, "Starting service")
|
|
|
|
at.Stop()
|
|
|
|
testutil.AssertLogsContain(t, hook, "Stopping service")
|
2018-07-26 01:57:19 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 17:58:02 +00:00
|
|
|
func TestAttesterLoop(t *testing.T) {
|
2018-08-09 22:54:59 +00:00
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
defer ctrl.Finish()
|
2018-08-14 00:58:37 +00:00
|
|
|
|
2018-08-12 17:58:02 +00:00
|
|
|
b := &mockBeaconService{
|
|
|
|
attesterChan: make(chan bool),
|
2018-07-26 01:57:19 +00:00
|
|
|
}
|
2018-08-12 17:58:02 +00:00
|
|
|
at := NewAttester(context.Background(), b)
|
|
|
|
|
|
|
|
doneChan := make(chan struct{})
|
|
|
|
exitRoutine := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
at.run(doneChan)
|
|
|
|
<-exitRoutine
|
|
|
|
}()
|
|
|
|
b.attesterChan <- true
|
|
|
|
testutil.AssertLogsContain(t, hook, "Performing attestation responsibility")
|
|
|
|
doneChan <- struct{}{}
|
|
|
|
exitRoutine <- true
|
|
|
|
testutil.AssertLogsContain(t, hook, "Attester context closed")
|
2018-07-26 01:57:19 +00:00
|
|
|
}
|