Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avalonche committed Aug 20, 2024
1 parent 950030c commit e181099
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 308 deletions.
3 changes: 1 addition & 2 deletions builder/beacon_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ type IBeaconClient interface {

type NilBeaconClient struct{}

func (b *NilBeaconClient) SubscribeToPayloadAttributesEvents(payloadAttrC chan BuilderPayloadAttributes) {
}
func (b *NilBeaconClient) SubscribeToPayloadAttributesEvents(payloadAttrC chan BuilderPayloadAttributes) {}

func (b *NilBeaconClient) Start() error { return nil }

Expand Down
41 changes: 25 additions & 16 deletions builder/beacon_client_test.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
package builder

import (
"net/http/httptest"
"testing"
"time"

"github.com/gorilla/mux"
"gotest.tools/assert"
)

type testBeaconClient struct {
slot uint64
type mockBeaconNode struct {
payloadAttributes BuilderPayloadAttributes
}

func (b *testBeaconClient) Stop() {}
func (b *mockBeaconNode) Stop() {}

func (b *testBeaconClient) SubscribeToPayloadAttributesEvents(payloadAttrC chan BuilderPayloadAttributes) {
func (b *mockBeaconNode) SubscribeToPayloadAttributesEvents(payloadAttrC chan BuilderPayloadAttributes) {
go func() {
payloadAttrC <- b.payloadAttributes
}()
}

func (b *testBeaconClient) Start() error { return nil }
func (b *mockBeaconNode) Start() error { return nil }

type mockBeaconNode struct {
srv *httptest.Server
func newMockBeaconNode(payloadAttributes BuilderPayloadAttributes) *mockBeaconNode {
return &mockBeaconNode{
payloadAttributes: payloadAttributes,
}
}

func newMockBeaconNode() *mockBeaconNode {
r := mux.NewRouter()
srv := httptest.NewServer(r)
func TestMockBeaconNode(t *testing.T) {
mockBeaconNode := newMockBeaconNode(BuilderPayloadAttributes{Slot: 123})
payloadAttrC := make(chan BuilderPayloadAttributes, 1) // Use buffered channel

mbn := &mockBeaconNode{
srv: srv,
}
mockBeaconNode.SubscribeToPayloadAttributesEvents(payloadAttrC)

return mbn
select {
case payloadAttributes := <-payloadAttrC:
assert.Equal(t, uint64(123), payloadAttributes.Slot)
case <-time.After(2 * time.Second):
t.Fatal("Test timed out waiting for payload attributes")
}
}
Loading

0 comments on commit e181099

Please sign in to comment.