From 017b079a68dcbe5f21aaf0fc5e6026f229bfd7e1 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 10 Aug 2024 23:28:00 -0300 Subject: [PATCH] test: error codes Signed-off-by: Carlos Alexandro Becker --- internal/proxy/proxy_test.go | 29 +++++++++++++++++++++++------ internal/proxy/server.go | 4 +++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/internal/proxy/proxy_test.go b/internal/proxy/proxy_test.go index 6842bcb..dc918bd 100644 --- a/internal/proxy/proxy_test.go +++ b/internal/proxy/proxy_test.go @@ -19,12 +19,16 @@ import ( func TestProxy(t *testing.T) { const chainID = "unittest" srv1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - io.WriteString(w, "srv1 replied") + _, _ = io.WriteString(w, "srv1 replied") })) t.Cleanup(srv1.Close) srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { time.Sleep(time.Millisecond * 500) - io.WriteString(w, "srv2 replied") + _, _ = io.WriteString(w, "srv2 replied") + })) + t.Cleanup(srv2.Close) + srv3 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusTeapot) })) t.Cleanup(srv2.Close) @@ -40,6 +44,10 @@ func TestProxy(t *testing.T) { Address: srv2.URL, Provider: "srv2", }, + { + Address: srv3.URL, + Provider: "srv3", + }, }, }, } @@ -47,7 +55,6 @@ func TestProxy(t *testing.T) { t.Logf("%+v", seed) seedSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - t.Log("AQUI") bts, _ := json.Marshal(seed) _, _ = w.Write(bts) })) @@ -60,13 +67,15 @@ func TestProxy(t *testing.T) { HealthyThreshold: 10 * time.Millisecond, ProxyRequestTimeout: time.Second, UnhealthyServerRecoverChancePct: 1, + HealthyErrorRateThreshold: 10, + HealthyErrorRateBucketTimeout: time.Second, }) ctx, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) proxy.Start(ctx) - require.Len(t, proxy.servers, 2) + require.Len(t, proxy.servers, 3) proxySrv := httptest.NewServer(proxy) t.Cleanup(proxySrv.Close) @@ -85,7 +94,8 @@ func TestProxy(t *testing.T) { return err } defer resp.Body.Close() - if resp.StatusCode != 200 { + // only two status codes accepted + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusTeapot { bts, _ := io.ReadAll(resp.Body) return fmt.Errorf("bad status code: %v: %s", resp.StatusCode, string(bts)) } @@ -98,10 +108,11 @@ func TestProxy(t *testing.T) { cancel() stats := proxy.Stats() - require.Len(t, stats, 2) + require.Len(t, stats, 3) var srv1Stats ServerStat var srv2Stats ServerStat + var srv3Stats ServerStat for _, st := range stats { if st.Name == "srv1" { srv1Stats = st @@ -109,7 +120,13 @@ func TestProxy(t *testing.T) { if st.Name == "srv2" { srv2Stats = st } + if st.Name == "srv3" { + srv3Stats = st + } } + require.Zero(t, srv1Stats.ErrorRate) + require.Zero(t, srv2Stats.ErrorRate) + require.Equal(t, float64(100), srv3Stats.ErrorRate) require.Greater(t, srv1Stats.Requests, srv2Stats.Requests) require.Greater(t, srv2Stats.Avg, srv1Stats.Avg) require.False(t, srv1Stats.Degraded) diff --git a/internal/proxy/server.go b/internal/proxy/server.go index 3394727..2c2f21a 100644 --- a/internal/proxy/server.go +++ b/internal/proxy/server.go @@ -56,11 +56,12 @@ func (s *Server) Healthy() bool { } func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { + var status int start := time.Now() defer func() { d := time.Since(start) avg := s.pings.Next(d) - slog.Info("request done", "name", s.name, "avg", avg, "last", d) + slog.Info("request done", "name", s.name, "avg", avg, "last", d, "status", status) }() proxiedURL := r.URL @@ -86,6 +87,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { defer cancel() resp, err := http.DefaultClient.Do(rr.WithContext(ctx)) + status = resp.StatusCode if err == nil { defer resp.Body.Close() for k, v := range resp.Header {