profit_control_threshold_helpers_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package service
  2. import (
  3. "math"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. )
  7. // TestClampProfitControlThreshold 钉死阈值清洗口径:非有限或为负一律按 0
  8. // (等价于只放行免费上游),合法值原样保留。
  9. func TestClampProfitControlThreshold(t *testing.T) {
  10. require.Equal(t, 0.0, clampProfitControlThreshold(math.NaN()))
  11. require.Equal(t, 0.0, clampProfitControlThreshold(math.Inf(1)))
  12. require.Equal(t, 0.0, clampProfitControlThreshold(math.Inf(-1)))
  13. require.Equal(t, 0.0, clampProfitControlThreshold(-0.5))
  14. require.Equal(t, 0.0, clampProfitControlThreshold(0))
  15. require.Equal(t, 0.7, clampProfitControlThreshold(0.7))
  16. }
  17. // TestProfitControlOverThreshold 钉死「越线」的唯一定义:相对 epsilon 吸收
  18. // decimal(10,4) 落库与浮点乘法的边界误差,U == 阈值 判定为合格。
  19. //
  20. // 线上否决点(openAIProfitControlVetoReason)与 profit-preview 现在共用这个
  21. // 函数与 clampProfitControlThreshold;此前各复制了一份,会随线上口径漂移,
  22. // 导致预演结果与真实调度不一致。
  23. func TestProfitControlOverThreshold(t *testing.T) {
  24. require.False(t, profitControlOverThreshold(0.7, 0.7), "U == 阈值 必须判定为合格")
  25. require.False(t, profitControlOverThreshold(0.6999, 0.7))
  26. require.True(t, profitControlOverThreshold(0.7001, 0.7))
  27. // 0.3*3 的浮点尾数不得被判成越线。
  28. require.False(t, profitControlOverThreshold(0.1*3, 0.3))
  29. // 阈值为 0 时只放行 0 成本上游。
  30. require.False(t, profitControlOverThreshold(0, 0))
  31. require.True(t, profitControlOverThreshold(0.0001, 0))
  32. }