@@ -18,109 +18,13 @@ package runner
1818
1919import (
2020 "context"
21- "encoding/json"
2221 "testing"
2322
24- "github.com/go-logr/logr"
2523 "github.com/stretchr/testify/assert"
26- "github.com/stretchr/testify/require"
2724
28- fc "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/flowcontrol"
2925 "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/flowcontrol/framework/plugins/usagelimits"
30- "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/framework/interface/flowcontrol"
31- fwkplugin "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/framework/interface/plugin"
3226)
3327
34- // TestSetupFlowControlPlugins verifies that setupFlowControlPlugins resolves a registered
35- // UsageLimitPolicy from the plugin registry and that the resolved policy returns the expected limit.
36- func TestSetupFlowControlPlugins (t * testing.T ) {
37- // Register the factory before any parallel sub-test starts so the write
38- // to the global registry map does not race with reads in sub-tests.
39- const pluginType = "func-plugin"
40- fwkplugin .Register (pluginType , func (name string , _ json.RawMessage , _ fwkplugin.Handle ) (fwkplugin.Plugin , error ) {
41- return usagelimits .NewPolicyFunc (name , func (_ context.Context , _ float64 , priorities []int ) []float64 {
42- result := make ([]float64 , len (priorities ))
43- for i := range result {
44- result [i ] = 0.8
45- }
46- return result
47- }), nil
48- })
49- t .Cleanup (func () {
50- delete (fwkplugin .Registry , pluginType )
51- })
52-
53- r := & Runner {}
54-
55- t .Run ("resolves registered plugin and returns 0.8" , func (t * testing.T ) {
56- t .Parallel ()
57-
58- policy := r .setupFlowControlPlugins (pluginType , logr .Discard ())
59- require .NotNil (t , policy )
60-
61- ctx := context .Background ()
62- for _ , tc := range []struct {
63- name string
64- priority int
65- saturation float64
66- }{
67- {"zero saturation" , 0 , 0.0 },
68- {"half saturation" , 1 , 0.5 },
69- {"full saturation" , 5 , 1.0 },
70- } {
71- t .Run (tc .name , func (t * testing.T ) {
72- t .Parallel ()
73- assert .Equal (t , []float64 {0.8 }, policy .ComputeLimit (ctx , tc .saturation , []int {tc .priority }))
74- })
75- }
76- })
77-
78- t .Run ("falls back to default policy when type is empty" , func (t * testing.T ) {
79- t .Parallel ()
80-
81- policy := r .setupFlowControlPlugins ("" , logr .Discard ())
82- require .NotNil (t , policy )
83-
84- ctx := context .Background ()
85- assert .Equal (t , []float64 {1.0 }, policy .ComputeLimit (ctx , 0.5 , []int {0 }))
86- })
87-
88- t .Run ("falls back to default policy when type is unknown" , func (t * testing.T ) {
89- t .Parallel ()
90-
91- policy := r .setupFlowControlPlugins ("no-such-policy-type" , logr .Discard ())
92- require .NotNil (t , policy )
93-
94- ctx := context .Background ()
95- assert .Equal (t , []float64 {1.0 }, policy .ComputeLimit (ctx , 0.5 , []int {0 }))
96- })
97- }
98-
99- const constantPointEightStructPolicyType = "test-constant-point-eight-struct-usage-limit-policy"
100-
101- // constantPointEightPolicy is a hand-rolled UsageLimitPolicy implementation that always returns 0.8.
102- // It exists to show that any struct satisfying the interface can be registered and resolved,
103- // without relying on the usagelimits.NewPolicyFunc helper.
104- type constantPointEightPolicy struct {}
105-
106- func (p * constantPointEightPolicy ) TypedName () fwkplugin.TypedName {
107- return fwkplugin.TypedName {
108- Type : constantPointEightStructPolicyType + "-type" ,
109- Name : constantPointEightStructPolicyType ,
110- }
111- }
112-
113- func (p * constantPointEightPolicy ) ComputeLimit (_ context.Context , _ float64 , priorities []int ) []float64 {
114- result := make ([]float64 , len (priorities ))
115- for i := range result {
116- result [i ] = 0.8
117- }
118- return result
119- }
120-
121- // compile-time check that constantPointEightPolicy satisfies the interface.
122- var _ flowcontrol.UsageLimitPolicy = (* constantPointEightPolicy )(nil )
123-
12428// TestLinearSpacingPolicy demonstrates a stateless UsageLimitPolicy that dynamically spaces
12529// ceilings based on the active priority domain. The highest-active priority always gets ceiling
12630// 1.0, and each subsequent tier drops by a fixed step (0.2). When priorities go idle and the
@@ -154,58 +58,3 @@ func TestLinearSpacingPolicy(t *testing.T) {
15458 got = linearSpacing .ComputeLimit (ctx , 0.5 , []int {0 })
15559 assert .Equal (t , []float64 {1.0 }, got , "single active priority should get full ceiling" )
15660}
157-
158- // TestSetupFlowControlPlugins_WithUsageLimitPolicyType verifies that UsageLimitPolicyType on
159- // flowcontrol.Config (the field a downstream project would set from a config file) is read by
160- // setupFlowControlPlugins to resolve and return the correct registered plugin.
161- func TestSetupFlowControlPlugins_WithUsageLimitPolicyType (t * testing.T ) {
162- const pluginType = "struct-plugin-via-config-option"
163-
164- fwkplugin .Register (pluginType , func (name string , _ json.RawMessage , _ fwkplugin.Handle ) (fwkplugin.Plugin , error ) {
165- return & constantPointEightPolicy {}, nil
166- })
167- t .Cleanup (func () {
168- delete (fwkplugin .Registry , pluginType )
169- })
170-
171- cfg := & fc.Config {UsageLimitPolicyType : pluginType }
172-
173- policy := (& Runner {}).setupFlowControlPlugins (cfg .UsageLimitPolicyType , logr .Discard ())
174- require .NotNil (t , policy )
175-
176- ctx := context .Background ()
177- assert .Equal (t , []float64 {0.8 }, policy .ComputeLimit (ctx , 0.5 , []int {0 }))
178- }
179-
180- // TestSetupFlowControlPlugins_StructPlugin verifies that a hand-rolled struct implementing
181- // UsageLimitPolicy (without using usagelimits.NewPolicyFunc) can be registered and resolved.
182- func TestSetupFlowControlPlugins_StructPlugin (t * testing.T ) {
183-
184- fwkplugin .Register (constantPointEightStructPolicyType , func (name string , _ json.RawMessage , _ fwkplugin.Handle ) (fwkplugin.Plugin , error ) {
185- return & constantPointEightPolicy {}, nil
186- })
187- t .Cleanup (func () {
188- delete (fwkplugin .Registry , constantPointEightStructPolicyType )
189- })
190-
191- r := & Runner {}
192-
193- policy := r .setupFlowControlPlugins (constantPointEightStructPolicyType , logr .Discard ())
194- require .NotNil (t , policy )
195-
196- ctx := context .Background ()
197- for _ , tc := range []struct {
198- name string
199- priority int
200- saturation float64
201- }{
202- {"zero saturation" , 0 , 0.0 },
203- {"half saturation" , 1 , 0.5 },
204- {"full saturation" , 5 , 1.0 },
205- } {
206- t .Run (tc .name , func (t * testing.T ) {
207- t .Parallel ()
208- assert .Equal (t , []float64 {0.8 }, policy .ComputeLimit (ctx , tc .saturation , []int {tc .priority }))
209- })
210- }
211- }
0 commit comments