Skip to content

Commit 98c7202

Browse files
committed
rm commented section + start test
1 parent 751d473 commit 98c7202

2 files changed

Lines changed: 94 additions & 37 deletions

File tree

operator/pkg/s3.go

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ func RequestBatch(req *http.Request, ctx context.Context) func() (*http.Response
1818
req_func := func() (*http.Response, error) {
1919
resp, err := http.DefaultClient.Do(req)
2020
if err != nil {
21-
// Handle Permanent Error cases here
22-
23-
// From my understanding upon erroring, we close the response body to free the resources before trying???
21+
// We close the response body to free the resources before re-trying
2422
if resp != nil {
2523
err = resp.Body.Close()
2624
resp = nil
@@ -50,40 +48,6 @@ func (o *Operator) getBatchFromDataService(ctx context.Context, batchURL string,
5048
return nil, err
5149
}
5250

53-
/*
54-
for attempt := 0; attempt < maxRetries; attempt++ {
55-
if attempt > 0 {
56-
o.Logger.Infof("Waiting for %s before retrying data fetch (attempt %d of %d)", retryDelay, attempt+1, maxRetries)
57-
select {
58-
case <-time.After(retryDelay):
59-
// Wait before retrying
60-
case <-ctx.Done():
61-
return nil, ctx.Err()
62-
}
63-
retryDelay *= 2 // Exponential backoff. Ex: 5s, 10s, 20s
64-
}
65-
66-
req, err = http.NewRequestWithContext(ctx, "GET", batchURL, nil)
67-
if err != nil {
68-
return nil, err
69-
}
70-
71-
resp, err = http.DefaultClient.Do(req)
72-
if err == nil && resp.StatusCode == http.StatusOK {
73-
break // Successful request, exit retry loop
74-
}
75-
76-
if resp != nil {
77-
err := resp.Body.Close()
78-
if err != nil {
79-
return nil, err
80-
}
81-
}
82-
83-
o.Logger.Warnf("Error fetching batch from data service - (attempt %d): %v", attempt+1, err)
84-
}
85-
*/
86-
8751
// At this point, the HTTP request was successfull.
8852

8953
defer func(Body io.ReadCloser) {

operator/pkg/s3_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package operator
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"net/http/httptest"
7+
"os/exec"
8+
"strings"
9+
"syscall"
10+
"testing"
11+
"time"
12+
13+
"github.com/ethereum/go-ethereum/accounts/abi/bind"
14+
"github.com/ethereum/go-ethereum/common"
15+
"github.com/stretchr/testify/assert"
16+
retry "github.com/yetanotherco/aligned_layer/core"
17+
"github.com/yetanotherco/aligned_layer/core/chainio"
18+
s3 "github.com/yetanotherco/aligned_layer/operator/pkg/s3"
19+
)
20+
21+
// Function wrapper around `make run_storage`
22+
func RunStorage() (*exec.Cmd, error) {
23+
24+
// Create a command
25+
cmd := exec.Command("make", "run_storage")
26+
cmd.SysProcAttr = &syscall.SysProcAttr{
27+
Setpgid: true,
28+
}
29+
30+
// Run the command
31+
err := cmd.Start()
32+
if err != nil {
33+
fmt.Printf("Error: %s\n", err)
34+
}
35+
36+
// Delay needed for anvil to start
37+
time.Sleep(750 * time.Millisecond)
38+
39+
return cmd, nil
40+
}
41+
42+
func TestBatchersBalances(t *testing.T) {
43+
cmd, err := RunStorage()
44+
if err != nil {
45+
t.Errorf("Error setting up Anvil: %s\n", err)
46+
}
47+
48+
// To Simulate Retrieving information from S3 we create a mock http server.
49+
expected := "dummy data"
50+
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
51+
fmt.Fprintf(w, expected)
52+
}))
53+
defer svr.Close()
54+
if err != nil {
55+
return
56+
}
57+
senderAddress := common.HexToAddress("0x0")
58+
59+
batcher_func := s3.RequestBatch(avsWriter, &bind.CallOpts{}, senderAddress)
60+
_, err = batcher_func()
61+
assert.Nil(t, err)
62+
63+
if err := cmd.Process.Kill(); err != nil {
64+
t.Errorf("Error killing process: %v\n", err)
65+
return
66+
}
67+
68+
batcher_func = chainio.BatcherBalances(avsWriter, &bind.CallOpts{}, senderAddress)
69+
_, err = batcher_func()
70+
assert.NotNil(t, err)
71+
if _, ok := err.(retry.PermanentError); ok {
72+
t.Errorf("BatchersBalances Emitted non-Transient error: %s\n", err)
73+
return
74+
}
75+
if !strings.Contains(err.Error(), "connect: connection refused") {
76+
t.Errorf("BatchersBalances did not return expected error: %s\n", err)
77+
return
78+
}
79+
80+
cmd, _, err = retry_test.SetupAnvil(8545)
81+
if err != nil {
82+
t.Errorf("Error setting up Anvil: %s\n", err)
83+
}
84+
85+
batcher_func = chainio.BatcherBalances(avsWriter, &bind.CallOpts{}, senderAddress)
86+
_, err = batcher_func()
87+
assert.Nil(t, err)
88+
89+
if err := cmd.Process.Kill(); err != nil {
90+
t.Errorf("Error killing process: %v\n", err)
91+
return
92+
}
93+
}

0 commit comments

Comments
 (0)