Skip to content

Commit

Permalink
Fixing all the last tests issues
Browse files Browse the repository at this point in the history
  • Loading branch information
AnomalRoil committed Jul 23, 2024
1 parent bef8466 commit 790f78b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
4 changes: 2 additions & 2 deletions client/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func New(ctx context.Context, l log.Logger, url string, chainHash []byte, transp

chainInfo, err := c.FetchChainInfo(ctx, chainHash)
if err != nil {
return nil, err
return nil, fmt.Errorf("FetchChainInfo err: %w", err)
}
c.chainInfo = chainInfo

Expand Down Expand Up @@ -243,7 +243,7 @@ func (h *httpClient) FetchChainInfo(ctx context.Context, chainHash []byte) (*cha

chainInfo, err := chain2.InfoFromJSON(infoBody.Body)
if err != nil {
resC <- httpInfoResponse{nil, fmt.Errorf("decoding response [chain2.InfoFromJSON]: %w", err)}
resC <- httpInfoResponse{nil, fmt.Errorf("decoding response [InfoFromJSON]: %w", err)}
return
}

Expand Down
7 changes: 6 additions & 1 deletion client/test/http/mock/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,26 @@ func NewMockHTTPPublicServer(t *testing.T, badSecondRound bool, sch *crypto.Sche
for i := 0; i < 3; i++ {
protoInfo, err := server.ChainInfo(ctx, &drand.ChainInfoRequest{})
if err != nil {
t.Error("MockServer.ChainInfo error:", err)
time.Sleep(10 * time.Millisecond)
continue
}
chainInfo, err = chain.InfoFromProto(protoInfo)
if err != nil {
t.Error("MockServer.InfoFromProto error:", err)
time.Sleep(10 * time.Millisecond)
continue
}

break
}
if chainInfo == nil {
t.Fatal("could not use server after 3 attempts.")
}

handler.RegisterNewBeaconHandler(client, chainInfo.HashString())
t.Log("MockServer.ChainInfo:", chainInfo)

handler.RegisterDefaultBeaconHandler(handler.RegisterNewBeaconHandler(client, chainInfo.HashString()))

listener, err := net.Listen("tcp", ":0")
if err != nil {
Expand Down
23 changes: 20 additions & 3 deletions internal/lib/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func Create(c *cli.Context, withInstrumentation bool, opts ...pubClient.Option)
var err error
var hash []byte
if groupPath := c.Path(GroupConfFlag.Name); groupPath != "" {
l.Debugw("parsing group-conf file")
info, err = chainInfoFromGroupTOML(groupPath)
if err != nil {
l.Infow("Got a group conf file that is not a toml file. Trying it as a ChainInfo json file.", "path", groupPath)
Expand All @@ -140,6 +141,8 @@ func Create(c *cli.Context, withInstrumentation bool, opts ...pubClient.Option)
return nil, fmt.Errorf("failed to decode group (%s) : %w", groupPath, err)
}
}
l.Debugw("parsing group-conf file, successful")

opts = append(opts, pubClient.WithChainInfo(info))
}

Expand All @@ -154,6 +157,7 @@ func Create(c *cli.Context, withInstrumentation bool, opts ...pubClient.Option)
if len(grc) > 0 {
clients = append(clients, grc...)
}
l.Debugw("built GRPC Client", "successful", len(grc))

if c.String(HashFlag.Name) != "" {
hash, err = hex.DecodeString(c.String(HashFlag.Name))
Expand Down Expand Up @@ -183,6 +187,8 @@ func Create(c *cli.Context, withInstrumentation bool, opts ...pubClient.Option)
if len(gc) > 0 {
clients = append(clients, gc...)
}
l.Debugw("built HTTP Client", "successful", len(gc))

if info != nil && hash != nil && !bytes.Equal(hash, info.Hash()) {
return nil, fmt.Errorf(
"%w for beacon %s : expected %v != info %v",
Expand Down Expand Up @@ -244,9 +250,17 @@ func buildHTTPClients(c *cli.Context, l log.Logger, hash []byte, withInstrumenta
var hc client.Client
var info *chainCommon.Info

l.Infow("Building HTTP clients", "hash", len(hash), "urls", c.StringSlice(URLFlag.Name))
urls := c.StringSlice(URLFlag.Name)

l.Infow("Building HTTP clients", "hash", len(hash), "urls", len(urls))

// we return an empty list if no URLs were provided
if len(urls) == 0 {
return clients, nil, nil
}

for _, url := range c.StringSlice(URLFlag.Name) {
for _, url := range urls {
l.Debugw("trying to instantiate http client", "url", url)
hc, err = http2.New(ctx, l, url, hash, nhttp.DefaultTransport)
if err != nil {
l.Warnw("", "client", "failed to load URL", "url", url, "err", err)
Expand All @@ -262,6 +276,7 @@ func buildHTTPClients(c *cli.Context, l log.Logger, hash []byte, withInstrumenta
clients = append(clients, hc)
}

// do we want to error out or not if all provided URL failed to instantiate a client?
if len(skipped) == len(c.StringSlice(URLFlag.Name)) {
return nil, nil, errors.New("all URLs failed to be used for creating a http client")
}
Expand All @@ -272,10 +287,12 @@ func buildHTTPClients(c *cli.Context, l log.Logger, hash []byte, withInstrumenta
return nil, nil, errors.New("mismatch between retrieved chain info and provided hash")
}

// we re-try dialing the skipped remotes, just in case, but that's the last time, we won't be dialing these again
// later in case they fail.
for _, url := range skipped {
hc, err = http2.NewWithInfo(l, url, info, nhttp.DefaultTransport)
if err != nil {
l.Warnw("", "client", "failed to load URL", "url", url, "err", err)
l.Warnw("", "client", "failed to load URL again", "url", url, "err", err)
continue
}
clients = append(clients, hc)
Expand Down
9 changes: 1 addition & 8 deletions internal/lib/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lib

import (
"bytes"
"context"
"encoding/hex"
"errors"
"os"
Expand All @@ -11,8 +10,6 @@ import (
"testing"
"time"

"github.com/drand/drand/v2/test/mock"

clock "github.com/jonboulle/clockwork"
"github.com/stretchr/testify/require"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -64,11 +61,7 @@ func TestClientLib(t *testing.T) {
addr, info, cancel, _ := httpmock.NewMockHTTPPublicServer(t, false, sch, clk)
defer cancel()

time.Sleep(time.Second)

grpcLis, _ := mock.NewMockGRPCPublicServer(t, lg, ":0", false, sch, clk)
go grpcLis.Start()
defer grpcLis.Stop(context.Background())
t.Log("Started mockserver at", addr)

args := []string{"mock-client", "--url", "http://" + addr, "--insecure"}
err = run(lg, args)
Expand Down

0 comments on commit 790f78b

Please sign in to comment.