|
| 1 | +/* |
| 2 | +Copyright 2026 The llm-d Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package loader |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 24 | + |
| 25 | + logutil "github.com/llm-d/llm-d-inference-payload-processor/pkg/common/observability/logging" |
| 26 | + "github.com/llm-d/llm-d-inference-payload-processor/pkg/framework/interface/plugin" |
| 27 | + "github.com/llm-d/llm-d-inference-payload-processor/pkg/framework/interface/requesthandling" |
| 28 | +) |
| 29 | + |
| 30 | +type fakeResponsePlugin struct { |
| 31 | + name string |
| 32 | + mode *requesthandling.ResponseBodyMode |
| 33 | +} |
| 34 | + |
| 35 | +func (p *fakeResponsePlugin) TypedName() plugin.TypedName { |
| 36 | + return plugin.TypedName{Type: "fake", Name: p.name} |
| 37 | +} |
| 38 | + |
| 39 | +func (p *fakeResponsePlugin) ProcessResponse(_ context.Context, _ *plugin.CycleState, _ *requesthandling.InferenceResponse) error { |
| 40 | + return nil |
| 41 | +} |
| 42 | + |
| 43 | +func (p *fakeResponsePlugin) ResponseBodyMode() requesthandling.ResponseBodyMode { |
| 44 | + if p.mode != nil { |
| 45 | + return *p.mode |
| 46 | + } |
| 47 | + return requesthandling.BodyFull |
| 48 | +} |
| 49 | + |
| 50 | +var ( |
| 51 | + _ requesthandling.ResponseProcessor = &fakeResponsePlugin{} |
| 52 | + _ requesthandling.ResponseBodyRequirement = &fakeResponsePlugin{} |
| 53 | +) |
| 54 | + |
| 55 | +type legacyResponsePlugin struct { |
| 56 | + name string |
| 57 | +} |
| 58 | + |
| 59 | +func (p *legacyResponsePlugin) TypedName() plugin.TypedName { |
| 60 | + return plugin.TypedName{Type: "legacy", Name: p.name} |
| 61 | +} |
| 62 | + |
| 63 | +func (p *legacyResponsePlugin) ProcessResponse(_ context.Context, _ *plugin.CycleState, _ *requesthandling.InferenceResponse) error { |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +var _ requesthandling.ResponseProcessor = &legacyResponsePlugin{} |
| 68 | + |
| 69 | +func modePtr(m requesthandling.ResponseBodyMode) *requesthandling.ResponseBodyMode { return &m } |
| 70 | + |
| 71 | +func TestComputeResponseBuffering(t *testing.T) { |
| 72 | + logger := log.FromContext(logutil.NewTestLoggerIntoContext(context.Background())) |
| 73 | + |
| 74 | + tests := []struct { |
| 75 | + name string |
| 76 | + plugins []requesthandling.ResponseProcessor |
| 77 | + wantBuffering bool |
| 78 | + }{ |
| 79 | + { |
| 80 | + name: "no response plugins", |
| 81 | + plugins: nil, |
| 82 | + wantBuffering: false, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "all BodyNotNeeded", |
| 86 | + plugins: []requesthandling.ResponseProcessor{ |
| 87 | + &fakeResponsePlugin{name: "a", mode: modePtr(requesthandling.BodyNotNeeded)}, |
| 88 | + &fakeResponsePlugin{name: "b", mode: modePtr(requesthandling.BodyNotNeeded)}, |
| 89 | + }, |
| 90 | + wantBuffering: false, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "all BodyChunked", |
| 94 | + plugins: []requesthandling.ResponseProcessor{ |
| 95 | + &fakeResponsePlugin{name: "a", mode: modePtr(requesthandling.BodyChunked)}, |
| 96 | + }, |
| 97 | + wantBuffering: false, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "one BodyFull forces buffering", |
| 101 | + plugins: []requesthandling.ResponseProcessor{ |
| 102 | + &fakeResponsePlugin{name: "a", mode: modePtr(requesthandling.BodyNotNeeded)}, |
| 103 | + &fakeResponsePlugin{name: "b", mode: modePtr(requesthandling.BodyFull)}, |
| 104 | + }, |
| 105 | + wantBuffering: true, |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "legacy plugin without ResponseBodyRequirement defaults to BodyFull", |
| 109 | + plugins: []requesthandling.ResponseProcessor{ |
| 110 | + &legacyResponsePlugin{name: "old-plugin"}, |
| 111 | + }, |
| 112 | + wantBuffering: true, |
| 113 | + }, |
| 114 | + { |
| 115 | + name: "mixed: BodyChunked + legacy forces buffering", |
| 116 | + plugins: []requesthandling.ResponseProcessor{ |
| 117 | + &fakeResponsePlugin{name: "a", mode: modePtr(requesthandling.BodyChunked)}, |
| 118 | + &legacyResponsePlugin{name: "legacy"}, |
| 119 | + }, |
| 120 | + wantBuffering: true, |
| 121 | + }, |
| 122 | + { |
| 123 | + name: "mixed: BodyNotNeeded + BodyChunked — no buffering", |
| 124 | + plugins: []requesthandling.ResponseProcessor{ |
| 125 | + &fakeResponsePlugin{name: "a", mode: modePtr(requesthandling.BodyNotNeeded)}, |
| 126 | + &fakeResponsePlugin{name: "b", mode: modePtr(requesthandling.BodyChunked)}, |
| 127 | + }, |
| 128 | + wantBuffering: false, |
| 129 | + }, |
| 130 | + } |
| 131 | + |
| 132 | + for _, tc := range tests { |
| 133 | + t.Run(tc.name, func(t *testing.T) { |
| 134 | + profiles := map[string]*requesthandling.Profile{ |
| 135 | + "test": { |
| 136 | + ResponsePlugins: tc.plugins, |
| 137 | + }, |
| 138 | + } |
| 139 | + computeResponseBuffering(profiles, logger) |
| 140 | + if profiles["test"].NeedsResponseBuffering != tc.wantBuffering { |
| 141 | + t.Errorf("NeedsResponseBuffering = %v, want %v", profiles["test"].NeedsResponseBuffering, tc.wantBuffering) |
| 142 | + } |
| 143 | + }) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func TestComputeResponseBuffering_MultipleProfiles(t *testing.T) { |
| 148 | + logger := log.FromContext(logutil.NewTestLoggerIntoContext(context.Background())) |
| 149 | + |
| 150 | + profiles := map[string]*requesthandling.Profile{ |
| 151 | + "streaming": { |
| 152 | + ResponsePlugins: []requesthandling.ResponseProcessor{ |
| 153 | + &fakeResponsePlugin{name: "headers-only", mode: modePtr(requesthandling.BodyNotNeeded)}, |
| 154 | + }, |
| 155 | + }, |
| 156 | + "full-body": { |
| 157 | + ResponsePlugins: []requesthandling.ResponseProcessor{ |
| 158 | + &fakeResponsePlugin{name: "translator", mode: modePtr(requesthandling.BodyFull)}, |
| 159 | + }, |
| 160 | + }, |
| 161 | + } |
| 162 | + |
| 163 | + computeResponseBuffering(profiles, logger) |
| 164 | + |
| 165 | + if profiles["streaming"].NeedsResponseBuffering { |
| 166 | + t.Error("streaming profile should not need buffering") |
| 167 | + } |
| 168 | + if !profiles["full-body"].NeedsResponseBuffering { |
| 169 | + t.Error("full-body profile should need buffering") |
| 170 | + } |
| 171 | +} |
0 commit comments