|
| 1 | +package service |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestGetBaseRPM(t *testing.T) { |
| 6 | + tests := []struct { |
| 7 | + name string |
| 8 | + extra map[string]any |
| 9 | + expected int |
| 10 | + }{ |
| 11 | + {"nil extra", nil, 0}, |
| 12 | + {"no key", map[string]any{}, 0}, |
| 13 | + {"zero", map[string]any{"base_rpm": 0}, 0}, |
| 14 | + {"int value", map[string]any{"base_rpm": 15}, 15}, |
| 15 | + {"float value", map[string]any{"base_rpm": 15.0}, 15}, |
| 16 | + } |
| 17 | + for _, tt := range tests { |
| 18 | + t.Run(tt.name, func(t *testing.T) { |
| 19 | + a := &Account{Extra: tt.extra} |
| 20 | + if got := a.GetBaseRPM(); got != tt.expected { |
| 21 | + t.Errorf("GetBaseRPM() = %d, want %d", got, tt.expected) |
| 22 | + } |
| 23 | + }) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func TestGetRPMStrategy(t *testing.T) { |
| 28 | + tests := []struct { |
| 29 | + name string |
| 30 | + extra map[string]any |
| 31 | + expected string |
| 32 | + }{ |
| 33 | + {"nil extra", nil, "tiered"}, |
| 34 | + {"no key", map[string]any{}, "tiered"}, |
| 35 | + {"tiered", map[string]any{"rpm_strategy": "tiered"}, "tiered"}, |
| 36 | + {"sticky_exempt", map[string]any{"rpm_strategy": "sticky_exempt"}, "sticky_exempt"}, |
| 37 | + {"invalid", map[string]any{"rpm_strategy": "foobar"}, "tiered"}, |
| 38 | + } |
| 39 | + for _, tt := range tests { |
| 40 | + t.Run(tt.name, func(t *testing.T) { |
| 41 | + a := &Account{Extra: tt.extra} |
| 42 | + if got := a.GetRPMStrategy(); got != tt.expected { |
| 43 | + t.Errorf("GetRPMStrategy() = %q, want %q", got, tt.expected) |
| 44 | + } |
| 45 | + }) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func TestCheckRPMSchedulability(t *testing.T) { |
| 50 | + tests := []struct { |
| 51 | + name string |
| 52 | + extra map[string]any |
| 53 | + currentRPM int |
| 54 | + expected WindowCostSchedulability |
| 55 | + }{ |
| 56 | + {"disabled", map[string]any{}, 100, WindowCostSchedulable}, |
| 57 | + {"green zone", map[string]any{"base_rpm": 15}, 10, WindowCostSchedulable}, |
| 58 | + {"yellow zone tiered", map[string]any{"base_rpm": 15}, 15, WindowCostStickyOnly}, |
| 59 | + {"red zone tiered", map[string]any{"base_rpm": 15}, 18, WindowCostNotSchedulable}, |
| 60 | + {"sticky_exempt at limit", map[string]any{"base_rpm": 15, "rpm_strategy": "sticky_exempt"}, 15, WindowCostStickyOnly}, |
| 61 | + {"sticky_exempt over limit", map[string]any{"base_rpm": 15, "rpm_strategy": "sticky_exempt"}, 100, WindowCostStickyOnly}, |
| 62 | + {"custom buffer", map[string]any{"base_rpm": 10, "rpm_sticky_buffer": 5}, 14, WindowCostStickyOnly}, |
| 63 | + {"custom buffer red", map[string]any{"base_rpm": 10, "rpm_sticky_buffer": 5}, 15, WindowCostNotSchedulable}, |
| 64 | + } |
| 65 | + for _, tt := range tests { |
| 66 | + t.Run(tt.name, func(t *testing.T) { |
| 67 | + a := &Account{Extra: tt.extra} |
| 68 | + if got := a.CheckRPMSchedulability(tt.currentRPM); got != tt.expected { |
| 69 | + t.Errorf("CheckRPMSchedulability(%d) = %d, want %d", tt.currentRPM, got, tt.expected) |
| 70 | + } |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
0 commit comments