File size: 1,496 Bytes
ea775bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package proxy

import (
	"context"
	"errors"
	"net/http/httptest"
	"testing"
)

func TestShouldRecordForwardCopyFailure(t *testing.T) {
	t.Run("NilError", func(t *testing.T) {
		req := httptest.NewRequest("GET", "http://example.com", nil)
		if shouldRecordForwardCopyFailure(req, nil) {
			t.Fatal("nil copy error should not be recorded as failure")
		}
	})

	t.Run("ClientCanceledContext", func(t *testing.T) {
		ctx, cancel := context.WithCancel(context.Background())
		cancel()
		req := httptest.NewRequest("GET", "http://example.com", nil).WithContext(ctx)
		if shouldRecordForwardCopyFailure(req, genericErr{}) {
			t.Fatal("client-canceled request should not penalize node health")
		}
	})

	t.Run("ContextCanceledError", func(t *testing.T) {
		req := httptest.NewRequest("GET", "http://example.com", nil)
		if shouldRecordForwardCopyFailure(req, canceledErr{}) {
			t.Fatal("context canceled errors should not penalize node health")
		}
	})

	t.Run("UpstreamTimeout", func(t *testing.T) {
		req := httptest.NewRequest("GET", "http://example.com", nil)
		if !shouldRecordForwardCopyFailure(req, deadlineExceededErr{}) {
			t.Fatal("upstream timeout should be recorded as node failure")
		}
	})

	t.Run("GenericUpstreamError", func(t *testing.T) {
		req := httptest.NewRequest("GET", "http://example.com", nil)
		err := errors.New("upstream read failed")
		if !shouldRecordForwardCopyFailure(req, err) {
			t.Fatal("generic upstream read error should be recorded as node failure")
		}
	})
}