|
| 1 | +package proxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "sync/atomic" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/thushan/olla/internal/adapter/proxy/olla" |
| 12 | + "github.com/thushan/olla/internal/core/domain" |
| 13 | +) |
| 14 | + |
| 15 | +// countingEndpointSelector tracks the number of Increment and Decrement calls |
| 16 | +// using atomic counters so the test is safe under concurrent execution. |
| 17 | +type countingEndpointSelector struct { |
| 18 | + incrementCalls atomic.Int64 |
| 19 | + decrementCalls atomic.Int64 |
| 20 | + endpoint *domain.Endpoint |
| 21 | +} |
| 22 | + |
| 23 | +func (c *countingEndpointSelector) Select(_ context.Context, endpoints []*domain.Endpoint) (*domain.Endpoint, error) { |
| 24 | + if c.endpoint != nil { |
| 25 | + return c.endpoint, nil |
| 26 | + } |
| 27 | + if len(endpoints) > 0 { |
| 28 | + return endpoints[0], nil |
| 29 | + } |
| 30 | + return nil, nil |
| 31 | +} |
| 32 | + |
| 33 | +func (c *countingEndpointSelector) Name() string { return "counting" } |
| 34 | + |
| 35 | +func (c *countingEndpointSelector) IncrementConnections(_ *domain.Endpoint) { |
| 36 | + c.incrementCalls.Add(1) |
| 37 | +} |
| 38 | + |
| 39 | +func (c *countingEndpointSelector) DecrementConnections(_ *domain.Endpoint) { |
| 40 | + c.decrementCalls.Add(1) |
| 41 | +} |
| 42 | + |
| 43 | +// TestOllaProxy_ConnectionCountingNoDuplication verifies that a single successful proxy |
| 44 | +// attempt results in exactly one IncrementConnections call and one DecrementConnections |
| 45 | +// call. Before the fix, proxyToSingleEndpoint also incremented/decremented, producing |
| 46 | +// counts of two each. |
| 47 | +func TestOllaProxy_ConnectionCountingNoDuplication(t *testing.T) { |
| 48 | + t.Parallel() |
| 49 | + |
| 50 | + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 51 | + w.WriteHeader(http.StatusOK) |
| 52 | + w.Write([]byte(`{"ok":true}`)) |
| 53 | + })) |
| 54 | + defer upstream.Close() |
| 55 | + |
| 56 | + endpoint := createTestEndpoint("test-endpoint", upstream.URL, domain.StatusHealthy) |
| 57 | + |
| 58 | + selector := &countingEndpointSelector{endpoint: endpoint} |
| 59 | + |
| 60 | + config := &olla.Configuration{} |
| 61 | + config.ResponseTimeout = 5 * time.Second |
| 62 | + config.ReadTimeout = 2 * time.Second |
| 63 | + config.StreamBufferSize = 8192 |
| 64 | + config.MaxIdleConns = 10 |
| 65 | + config.IdleConnTimeout = 30 * time.Second |
| 66 | + config.MaxConnsPerHost = 5 |
| 67 | + |
| 68 | + proxy, err := olla.NewService( |
| 69 | + &mockDiscoveryService{endpoints: []*domain.Endpoint{endpoint}}, |
| 70 | + selector, |
| 71 | + config, |
| 72 | + createTestStatsCollector(), |
| 73 | + nil, |
| 74 | + createTestLogger(), |
| 75 | + ) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("failed to create Olla proxy: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + req, stats, rlog := createTestRequestWithStats("POST", "/v1/chat/completions", `{"model":"test"}`) |
| 81 | + w := httptest.NewRecorder() |
| 82 | + |
| 83 | + if err := proxy.ProxyRequestToEndpoints(req.Context(), w, req, []*domain.Endpoint{endpoint}, stats, rlog); err != nil { |
| 84 | + t.Fatalf("proxy request failed: %v", err) |
| 85 | + } |
| 86 | + |
| 87 | + if got := selector.incrementCalls.Load(); got != 1 { |
| 88 | + t.Errorf("IncrementConnections called %d times; want exactly 1", got) |
| 89 | + } |
| 90 | + if got := selector.decrementCalls.Load(); got != 1 { |
| 91 | + t.Errorf("DecrementConnections called %d times; want exactly 1", got) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// TestOllaProxy_ConnectionCountReturnsToZero verifies that after a completed request |
| 96 | +// the net connection delta is zero — i.e. every increment is paired with a decrement. |
| 97 | +func TestOllaProxy_ConnectionCountReturnsToZero(t *testing.T) { |
| 98 | + t.Parallel() |
| 99 | + |
| 100 | + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 101 | + w.WriteHeader(http.StatusOK) |
| 102 | + w.Write([]byte(`{"ok":true}`)) |
| 103 | + })) |
| 104 | + defer upstream.Close() |
| 105 | + |
| 106 | + endpoint := createTestEndpoint("test-endpoint", upstream.URL, domain.StatusHealthy) |
| 107 | + selector := &countingEndpointSelector{endpoint: endpoint} |
| 108 | + |
| 109 | + config := &olla.Configuration{} |
| 110 | + config.ResponseTimeout = 5 * time.Second |
| 111 | + config.ReadTimeout = 2 * time.Second |
| 112 | + config.StreamBufferSize = 8192 |
| 113 | + config.MaxIdleConns = 10 |
| 114 | + config.IdleConnTimeout = 30 * time.Second |
| 115 | + config.MaxConnsPerHost = 5 |
| 116 | + |
| 117 | + proxy, err := olla.NewService( |
| 118 | + &mockDiscoveryService{endpoints: []*domain.Endpoint{endpoint}}, |
| 119 | + selector, |
| 120 | + config, |
| 121 | + createTestStatsCollector(), |
| 122 | + nil, |
| 123 | + createTestLogger(), |
| 124 | + ) |
| 125 | + if err != nil { |
| 126 | + t.Fatalf("failed to create Olla proxy: %v", err) |
| 127 | + } |
| 128 | + |
| 129 | + const requests = 5 |
| 130 | + for i := 0; i < requests; i++ { |
| 131 | + req, stats, rlog := createTestRequestWithStats("POST", "/v1/chat/completions", `{"model":"test"}`) |
| 132 | + w := httptest.NewRecorder() |
| 133 | + if err := proxy.ProxyRequestToEndpoints(req.Context(), w, req, []*domain.Endpoint{endpoint}, stats, rlog); err != nil { |
| 134 | + t.Fatalf("request %d failed: %v", i+1, err) |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + inc := selector.incrementCalls.Load() |
| 139 | + dec := selector.decrementCalls.Load() |
| 140 | + |
| 141 | + if inc != requests { |
| 142 | + t.Errorf("IncrementConnections called %d times; want %d", inc, requests) |
| 143 | + } |
| 144 | + if dec != requests { |
| 145 | + t.Errorf("DecrementConnections called %d times; want %d", dec, requests) |
| 146 | + } |
| 147 | + if net := inc - dec; net != 0 { |
| 148 | + t.Errorf("net connection delta is %d after all requests completed; want 0", net) |
| 149 | + } |
| 150 | +} |
0 commit comments