Skip to content

Commit 1ece273

Browse files
committed
chore: use correct block height
1 parent 8188e91 commit 1ece273

2 files changed

Lines changed: 83 additions & 3 deletions

File tree

apps/loadgen/cmd/flags_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestStartFlags(t *testing.T) {
10+
root := NewRootCmd()
11+
startCmd := newStartCmd()
12+
root.AddCommand(startCmd)
13+
14+
err := startCmd.ParseFlags([]string{"--regular-matrix", "custom.json"})
15+
require.NoError(t, err)
16+
17+
// Since we can't easily access the cfg inside newStartCmd's closure from here
18+
// without refactoring, we'll check if the flag is registered correctly.
19+
flag := startCmd.Flags().Lookup("regular-matrix")
20+
require.NotNil(t, flag)
21+
require.Equal(t, "custom.json", flag.Value.String())
22+
}
23+
24+
func TestRunArgs(t *testing.T) {
25+
runCmd := newRunCmd()
26+
err := runCmd.Args(runCmd, []string{"matrix.json"})
27+
require.NoError(t, err)
28+
29+
err = runCmd.Args(runCmd, []string{})
30+
require.Error(t, err)
31+
}

apps/loadgen/internal/client.go

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package internal
22

33
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
"time"
9+
410
dto "github.com/prometheus/client_model/go"
511

612
"github.com/celestiaorg/tastora/framework/docker/evstack/spamoor"
@@ -18,12 +24,16 @@ type SpamoorClient interface {
1824
}
1925

2026
type spamoorAPIClient struct {
21-
api *spamoor.API
27+
api *spamoor.API
28+
client *http.Client
2229
}
2330

2431
// NewSpamoorClient creates a SpamoorClient backed by the real spamoor HTTP API.
2532
func NewSpamoorClient(baseURL string) SpamoorClient {
26-
return spamoorAPIClient{api: spamoor.NewAPI(baseURL)}
33+
return spamoorAPIClient{
34+
api: spamoor.NewAPI(baseURL),
35+
client: &http.Client{Timeout: 2 * time.Second},
36+
}
2737
}
2838

2939
func (c spamoorAPIClient) URL() string { return c.api.BaseURL }
@@ -42,4 +52,43 @@ func (c spamoorAPIClient) GetMetrics() (map[string]*dto.MetricFamily, error) {
4252
return c.api.GetMetrics()
4353
}
4454

45-
func (c spamoorAPIClient) GetClients() ([]spamoor.Client, error) { return c.api.GetClients() }
55+
// clientResponse matches the actual spamoor daemon JSON response where
56+
// the block height field is "block_height".
57+
type clientResponse struct {
58+
Index int `json:"index"`
59+
Name string `json:"name"`
60+
URL string `json:"url"`
61+
Groups []string `json:"groups"`
62+
Enabled bool `json:"enabled"`
63+
BlockHeight uint64 `json:"block_height"`
64+
}
65+
66+
// GetClients fetches clients from spamoor, correctly mapping "block_height" to Height.
67+
func (c spamoorAPIClient) GetClients() ([]spamoor.Client, error) {
68+
url := fmt.Sprintf("%s/api/clients", c.api.BaseURL)
69+
resp, err := c.client.Get(url)
70+
if err != nil {
71+
return nil, err
72+
}
73+
defer func() { _ = resp.Body.Close() }()
74+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
75+
body, _ := io.ReadAll(resp.Body)
76+
return nil, fmt.Errorf("get clients failed: %s", string(body))
77+
}
78+
var raw []clientResponse
79+
if err := json.NewDecoder(resp.Body).Decode(&raw); err != nil {
80+
return nil, err
81+
}
82+
clients := make([]spamoor.Client, len(raw))
83+
for i, r := range raw {
84+
clients[i] = spamoor.Client{
85+
Index: r.Index,
86+
Name: r.Name,
87+
URL: r.URL,
88+
Groups: r.Groups,
89+
Enabled: r.Enabled,
90+
Height: r.BlockHeight,
91+
}
92+
}
93+
return clients, nil
94+
}

0 commit comments

Comments
 (0)