Files
tasks/service/matching/version_rule_helper_test.go
zeekay 5a601d081b rebrand: go.temporal.io/server -> github.com/hanzoai/tasks
Full module path rebrand from Temporal to Hanzo Tasks:
- go.mod module: github.com/hanzoai/tasks
- All internal Go imports updated (1906 files)
- cmd/server renamed to cmd/tasksd
- Binary name: tasksd (was temporal-server)
- Docker images: hanzoai/tasks (was temporalio/server)
- README rewritten with Hanzo Tasks branding
- LLM.md created with integration notes
- Makefile, goreleaser, CI workflows updated
- go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos)

Fork of Temporal.io (MIT license).
2026-03-19 10:36:40 -07:00

97 lines
2.8 KiB
Go

package matching
import (
"strconv"
"testing"
"github.com/stretchr/testify/assert"
taskqueuepb "go.temporal.io/api/taskqueue/v1"
persistencespb "github.com/hanzoai/tasks/api/persistence/v1"
)
func TestFindAssignmentBuildId_NoRules(t *testing.T) {
b := FindAssignmentBuildId(nil, "")
assert.Equal(t, "", b)
}
func TestFindAssignmentBuildId_OneFullRule(t *testing.T) {
buildId := "bld"
b := FindAssignmentBuildId([]*persistencespb.AssignmentRule{createAssignmentRuleWithoutRamp(buildId)}, "")
assert.Equal(t, buildId, b)
}
func TestFindAssignmentBuildId_TwoFullRules(t *testing.T) {
buildId := "bld"
buildId2 := "bld2"
b := FindAssignmentBuildId(
[]*persistencespb.AssignmentRule{
createAssignmentRuleWithoutRamp(buildId),
createAssignmentRuleWithoutRamp(buildId2),
},
"",
)
assert.Equal(t, buildId, b)
}
func TestFindAssignmentBuildId_WithRamp(t *testing.T) {
buildId1 := "bld1"
buildId2 := "bld2"
buildId3 := "bld3"
buildId4 := "bld4"
buildId5 := "bld5"
rules := []*persistencespb.AssignmentRule{
createAssignmentRuleWithRamp(buildId1, 0),
createAssignmentRuleWithRamp(buildId2, 20),
createAssignmentRuleWithRamp(buildId3, 70),
createAssignmentRuleWithoutRamp(buildId4),
createAssignmentRuleWithRamp(buildId5, 90),
}
histogram := make(map[string]int)
runs := 1000000
for i := range runs {
b := FindAssignmentBuildId(rules, "run-"+strconv.Itoa(i))
histogram[b]++
}
assert.Equal(t, 0, histogram[buildId1])
assert.InEpsilon(t, .2*float64(runs), histogram[buildId2], .02)
// 20% has gone to build 2, so 70%-20%=50% should go to build 3
assert.InEpsilon(t, .5*float64(runs), histogram[buildId3], .02)
assert.InEpsilon(t, .3*float64(runs), histogram[buildId4], .02)
assert.Equal(t, 0, histogram[buildId5])
}
func TestCalcRampThresholdUniform(t *testing.T) {
buildPref := "bldXYZ-"
histogram := [100]int{}
for i := range 1000000 {
v := calcRampThreshold(buildPref + strconv.Itoa(i))
histogram[int32(v)]++
}
for i := range 100 {
assert.InEpsilon(t, 10000, histogram[i], 0.1)
}
}
func TestCalcRampThresholdDeterministic(t *testing.T) {
// same input should always may to the same output
assert.Equal(t, calcRampThreshold("A"), calcRampThreshold("A"))
// unless it's an empty string which maps randomly each time
assert.NotEqual(t, calcRampThreshold(""), calcRampThreshold(""))
}
func createAssignmentRuleWithoutRamp(buildId string) *persistencespb.AssignmentRule {
return &persistencespb.AssignmentRule{Rule: &taskqueuepb.BuildIdAssignmentRule{TargetBuildId: buildId}}
}
func createAssignmentRuleWithRamp(buildId string, ramp float32) *persistencespb.AssignmentRule {
return &persistencespb.AssignmentRule{Rule: &taskqueuepb.BuildIdAssignmentRule{
TargetBuildId: buildId,
Ramp: mkNewAssignmentPercentageRamp(ramp),
}}
}