Skip to content

Commit

Permalink
test: error codes
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 committed Aug 11, 2024
1 parent b34648e commit 017b079
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
29 changes: 23 additions & 6 deletions internal/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -40,14 +44,17 @@ func TestProxy(t *testing.T) {
Address: srv2.URL,
Provider: "srv2",
},
{
Address: srv3.URL,
Provider: "srv3",
},
},
},
}

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)
}))
Expand All @@ -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)
Expand All @@ -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))
}
Expand All @@ -98,18 +108,25 @@ 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
}
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)
Expand Down
4 changes: 3 additions & 1 deletion internal/proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 017b079

Please sign in to comment.