add tests for proxy.Process

This commit is contained in:
Benson Wong
2024-11-17 20:49:14 -08:00
parent 36a31f450f
commit e5c909ddf7
5 changed files with 115 additions and 8 deletions

View File

@@ -38,7 +38,6 @@ func (pm *ProxyManager) HandleFunc(w http.ResponseWriter, r *http.Request) {
} else {
http.Error(w, "no strategy to handle request", http.StatusBadRequest)
}
}
}

View File

@@ -21,6 +21,10 @@ type Process struct {
config ModelConfig
cmd *exec.Cmd
logMonitor *LogMonitor
// Only useful for go testing the proxy functionality
// will leave the start/stop of process to go code
overrideProxyFunc *func(w http.ResponseWriter, r *http.Request)
}
func NewProcess(ID string, config ModelConfig, logMonitor *LogMonitor) *Process {
@@ -115,6 +119,7 @@ func (p *Process) checkHealthEndpoint(cmdCtx context.Context, healthCheckTimeout
startTime := time.Now()
for {
time.Sleep(time.Second)
req, err := http.NewRequest("GET", healthURL, nil)
if err != nil {
return err
@@ -141,7 +146,6 @@ func (p *Process) checkHealthEndpoint(cmdCtx context.Context, healthCheckTimeout
// wait a bit longer for TCP connection issues
if strings.Contains(err.Error(), "connection refused") {
fmt.Fprintf(p.logMonitor, "Connection refused on %s, ttl %.0fs\n", healthURL, ttl)
time.Sleep(5 * time.Second)
} else {
time.Sleep(time.Second)
@@ -162,13 +166,23 @@ func (p *Process) checkHealthEndpoint(cmdCtx context.Context, healthCheckTimeout
if ttl < 0 {
return fmt.Errorf("failed to check health from: %s", healthURL)
}
time.Sleep(time.Second)
}
}
// sends the request to the upstream process
func (p *Process) ProxyRequest(w http.ResponseWriter, r *http.Request) {
if p.overrideProxyFunc != nil {
(*p.overrideProxyFunc)(w, r)
} else {
p.defaultProxyHandler(w, r)
}
}
func (p *Process) SetOverride(f func(http.ResponseWriter, *http.Request)) {
p.overrideProxyFunc = &f
}
// sends the request to the upstream process
func (p *Process) defaultProxyHandler(w http.ResponseWriter, r *http.Request) {
if p.cmd == nil {
http.Error(w, "process not started", http.StatusInternalServerError)
return

93
proxy/process_test.go Normal file
View File

@@ -0,0 +1,93 @@
package proxy
import (
"fmt"
"math/rand"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
// Check if the binary exists
func TestMain(m *testing.M) {
binaryPath := getBinaryPath()
if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
fmt.Printf("simple-responder not found at %s, did you `make simple-responder`?\n", binaryPath)
os.Exit(1)
}
m.Run()
}
// Helper function to get the binary path
func getBinaryPath() string {
goos := runtime.GOOS
goarch := runtime.GOARCH
return filepath.Join("..", "build", fmt.Sprintf("simple-responder_%s_%s", goos, goarch))
}
func TestProcess_StartProxyStop(t *testing.T) {
// Define the range
min := 12000
max := 13000
// Generate a random number between 12000 and 13000
randomPort := rand.Intn(max-min+1) + min
binaryPath := getBinaryPath()
// Create a log monitor
logMonitor := NewLogMonitor()
expectedMessage := "testing91931"
// Create a process configuration
config := ModelConfig{
Cmd: fmt.Sprintf("%s --port %d --respond '%s'", binaryPath, randomPort, expectedMessage),
Proxy: fmt.Sprintf("http://127.0.0.1:%d", randomPort),
CheckEndpoint: "/health",
}
// Create a process
process := NewProcess("test-process", config, logMonitor)
// Start the process
t.Logf("Starting %s on port %d", binaryPath, randomPort)
err := process.Start(5)
if err != nil {
t.Fatalf("Failed to start process: %v", err)
}
// Create a test request
req := httptest.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
// Proxy the request
process.ProxyRequest(w, req)
// Check the response
if w.Code != http.StatusOK {
t.Errorf("Expected status code %d, got %d", http.StatusOK, w.Code)
}
if !strings.Contains(w.Body.String(), expectedMessage) {
t.Errorf("Expected body to contain '%s', got %q", expectedMessage, w.Body.String())
}
// Stop the process
process.Stop()
req = httptest.NewRequest("GET", "/", nil)
w = httptest.NewRecorder()
// Proxy the request
process.ProxyRequest(w, req)
// Check the response
if w.Code == http.StatusInternalServerError {
t.Errorf("Expected status code %d, got %d", http.StatusInternalServerError, w.Code)
}
}