Skip to content

Commit

Permalink
Test improvements (#814)
Browse files Browse the repository at this point in the history
* v1.2.0

* add drand status values on Ping response

* add new fields for Pong on protobuf definition

* improve tests readability, remove sleeps from tests

* avoiding duplication on grpc prometheus metrics

* create a new service called Status, leaving Ping as it was before

* improvements on more tests

* improve lifecycle flags on beacon handler

* apply fmt on the project

* improve beacon lifecycle flags usability

* refactor Status service in a more flexible way

* improve readability on more tests, remove more sleeps

* fix issues reported by linter

* fix more issues reported by linter

* update protoc-gen-go module on CI job

* apply go mod tidy

* add a new lifecycle flag for beacon handler, use it on tests

* apply readability improvements on node tests, remove more sleeps

* fix lint issues

* fix flaky test on HTTP Mock server

Co-authored-by: Will Scott <will@cypherpunk.email>
Co-authored-by: Will Scott <will.scott@protocol.ai>
  • Loading branch information
3 people committed Sep 28, 2021
1 parent eeaae0a commit b4378a5
Show file tree
Hide file tree
Showing 34 changed files with 1,879 additions and 766 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/generate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
version: '3.14.0'
- name: Install Protoc-gen-go
run: |
go get github.com/golang/protobuf/protoc-gen-go@v1.4.3
go get github.com/golang/protobuf/protoc-gen-go@v1.5.2
go get google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1.0
- name: Generate
run: go generate ./...&& go mod tidy
Expand Down
74 changes: 74 additions & 0 deletions chain/beacon/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type Handler struct {
close chan bool
addr string
started bool
running bool
serving bool
stopped bool
l log.Logger
}
Expand Down Expand Up @@ -153,9 +155,15 @@ func (h *Handler) Start() error {
h.l.Error("genesis_time", "past", "call", "catchup")
return errors.New("beacon: genesis time already passed. Call Catchup()")
}

h.Lock()
h.started = true
h.Unlock()

_, tTime := chain.NextRound(h.conf.Clock.Now().Unix(), h.conf.Group.Period, h.conf.Group.GenesisTime)
h.l.Info("beacon", "start")
go h.run(tTime)

return nil
}

Expand All @@ -165,6 +173,10 @@ func (h *Handler) Start() error {
// it sync its local chain with other nodes to be able to participate in the
// next upcoming round.
func (h *Handler) Catchup() {
h.Lock()
h.started = true
h.Unlock()

nRound, tTime := chain.NextRound(h.conf.Clock.Now().Unix(), h.conf.Group.Period, h.conf.Group.GenesisTime)
go h.run(tTime)
h.chain.RunSync(context.Background(), nRound, nil)
Expand All @@ -182,10 +194,17 @@ func (h *Handler) Transition(prevGroup *key.Group) error {
h.l.Fatal("transition_time", "invalid_offset", "expected_time", tTime, "got_time", targetTime)
return nil
}

h.Lock()
h.started = true
h.Unlock()

go h.run(targetTime)

// we run the sync up until (inclusive) one round before the transition
h.l.Debug("new_node", "following chain", "to_round", tRound-1)
h.chain.RunSync(context.Background(), tRound-1, toPeers(prevGroup.Nodes))

return nil
}

Expand All @@ -211,14 +230,66 @@ func (h *Handler) TransitionNewGroup(newShare *key.Share, newGroup *key.Group) {
})
}

func (h *Handler) IsStarted() bool {
h.Lock()
defer h.Unlock()

return h.started
}

func (h *Handler) IsServing() bool {
h.Lock()
defer h.Unlock()

return h.serving
}

func (h *Handler) IsRunning() bool {
h.Lock()
defer h.Unlock()

return h.running
}

func (h *Handler) IsStopped() bool {
h.Lock()
defer h.Unlock()

return h.stopped
}

func (h *Handler) Reset() {
h.Lock()
defer h.Unlock()

h.stopped = false
h.started = false
h.running = false
h.serving = false
}

// run will wait until it is supposed to start
func (h *Handler) run(startTime int64) {
chanTick := h.ticker.ChannelAt(startTime)
h.l.Debug("run_round", "wait", "until", startTime)

var current roundInfo
setRunnig := sync.Once{}

h.Lock()
h.running = true
h.Unlock()

for {
select {
case current = <-chanTick:

setRunnig.Do(func() {
h.Lock()
h.serving = true
h.Unlock()
})

lastBeacon, err := h.chain.Last()
if err != nil {
h.l.Error("beacon_loop", "loading_last", "err", err)
Expand Down Expand Up @@ -316,9 +387,12 @@ func (h *Handler) Stop() {
return
}
close(h.close)

h.chain.Stop()
h.ticker.Stop()

h.stopped = true
h.running = false
h.l.Info("beacon", "stop")
}

Expand Down
Loading

0 comments on commit b4378a5

Please sign in to comment.