Skip to content

Commit 9e9874b

Browse files
committed
fix test
1 parent c3051a2 commit 9e9874b

2 files changed

Lines changed: 43 additions & 47 deletions

File tree

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ aggregator_send_dummy_responses:
150150
test_go_retries:
151151
@cd core/ && \
152152
go test -v -timeout 15m
153+
@cd operator/pkg/ && \
154+
go test -v -run TestRequestBatch
153155

154156
__OPERATOR__:
155157

operator/pkg/s3_test.go

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,62 @@ package operator
33
import (
44
"context"
55
"fmt"
6+
"io"
7+
"log"
8+
"net"
69
"net/http"
710
"net/http/httptest"
8-
"os/exec"
911
"strings"
10-
"syscall"
1112
"testing"
12-
"time"
1313

1414
"github.com/stretchr/testify/assert"
1515
retry "github.com/yetanotherco/aligned_layer/core"
1616
)
1717

18-
// Function wrapper around `make run_storage`
19-
func RunStorage() (*exec.Cmd, error) {
20-
21-
// Create a command
22-
cmd := exec.Command("make", "run_storage")
23-
cmd.SysProcAttr = &syscall.SysProcAttr{
24-
Setpgid: true,
18+
// EchoHandler reads and writes the request body
19+
func EchoHandler(w http.ResponseWriter, r *http.Request) {
20+
body, err := io.ReadAll(r.Body)
21+
if err != nil {
22+
http.Error(w, "Error reading body", http.StatusBadRequest)
23+
return
2524
}
25+
defer r.Body.Close()
26+
27+
w.Header().Set("Content-Type", "text/plain")
28+
w.WriteHeader(http.StatusOK)
29+
w.Write(body)
30+
}
2631

27-
// Run the command
28-
err := cmd.Start()
32+
// Note the httptest API requires that for restarting a server its url is empty. Given ours the default address is in use.
33+
// Its abstracting creation of a task server works around these issues.
34+
// NOTE: httptest panic's if the url of the server starts without being set to "" ref: https://cs.opensource.google/go/go/+/refs/tags/go1.23.3:src/net/http/httptest/server.go;l=127
35+
func CreateTestServer() *httptest.Server {
36+
// To Simulate Retrieving information from S3 we create a mock http server.
37+
expected := "dummy data"
38+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
39+
fmt.Fprintf(w, expected)
40+
})
41+
// create a listener with the desired port.
42+
l, err := net.Listen("tcp", "127.0.0.1:7878")
2943
if err != nil {
30-
fmt.Printf("Error: %s\n", err)
44+
log.Fatal(err)
3145
}
3246

33-
// Delay needed for anvil to start
34-
time.Sleep(750 * time.Millisecond)
47+
svr := httptest.NewUnstartedServer(handler)
48+
49+
// NewUnstartedServer creates a listener. Close that listener and replace
50+
// with the one we created.
51+
svr.Listener.Close()
52+
svr.Listener = l
3553

36-
return cmd, nil
54+
return svr
3755
}
56+
3857
func TestRequestBatch(t *testing.T) {
39-
/*
40-
cmd, err := RunStorage()
41-
if err != nil {
42-
t.Errorf("Error setting up Anvil: %s\n", err)
43-
}
44-
*/
45-
// To Simulate Retrieving information from S3 we create a mock http server.
46-
expected := "dummy data"
47-
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
48-
t.Errorf("error %v %v", w, expected)
49-
}))
50-
defer svr.Close()
58+
svr := CreateTestServer()
59+
60+
// Start the server.
61+
svr.Start()
5162

5263
req, err := http.NewRequestWithContext(context.Background(), "GET", svr.URL, nil)
5364
if err != nil {
@@ -59,12 +70,6 @@ func TestRequestBatch(t *testing.T) {
5970
assert.Nil(t, err)
6071

6172
svr.Close()
62-
/*
63-
if err := cmd.Process.Kill(); err != nil {
64-
t.Errorf("Error killing process: %v\n", err)
65-
return
66-
}
67-
*/
6873

6974
batcher_func = RequestBatch(req, context.Background())
7075
_, err = batcher_func()
@@ -78,22 +83,11 @@ func TestRequestBatch(t *testing.T) {
7883
return
7984
}
8085

86+
svr = CreateTestServer()
8187
svr.Start()
82-
/*
83-
cmd, err = RunStorage()
84-
if err != nil {
85-
t.Errorf("Error setting up Anvil: %s\n", err)
86-
}
87-
*/
88+
defer svr.Close()
8889

8990
batcher_func = RequestBatch(req, context.Background())
9091
_, err = batcher_func()
9192
assert.Nil(t, err)
92-
93-
/*
94-
if err := cmd.Process.Kill(); err != nil {
95-
t.Errorf("Error killing process: %v\n", err)
96-
return
97-
}
98-
*/
9993
}

0 commit comments

Comments
 (0)