mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
## What changed? - Split implementation of "constrained default" settings from "plain default" settings. This is more code and the diff looks complex, but the individual paths are both simpler than the mixed version. - Add conversion cache using a weak map. - Remove GlobalCachedTypedValue. - Use "raw" values for subscription dispatch deduping to avoid unnecessary conversions. - Deep copy default values when using mapstructure, to avoid problems with merging over shared default values. ## Why? - Fixes #6756 - Performance improvement for "plain default" settings (almost all of them) - Performance improvement for settings with complex converters - Remove footgun in defaults that aren't scalar values ## How did you test it? existing+new unit tests
154 lines
3.4 KiB
Go
154 lines
3.4 KiB
Go
package dynamicconfig
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// These two tests are in a separate file in the 'dynamicconfig' package to access the private
|
|
// findMatch function. Most other tests should be in 'dynamicconfig_test' to test things as a
|
|
// client.
|
|
func TestFindMatch(t *testing.T) {
|
|
testCases := []struct {
|
|
v []ConstrainedValue
|
|
filters []Constraints
|
|
matched bool
|
|
}{
|
|
{
|
|
v: []ConstrainedValue{
|
|
{Constraints: Constraints{}},
|
|
},
|
|
filters: []Constraints{
|
|
{Namespace: "some random namespace"},
|
|
},
|
|
matched: false,
|
|
},
|
|
{
|
|
v: []ConstrainedValue{
|
|
{Constraints: Constraints{Namespace: "samples-namespace"}},
|
|
},
|
|
filters: []Constraints{
|
|
{Namespace: "some random namespace"},
|
|
},
|
|
matched: false,
|
|
},
|
|
{
|
|
v: []ConstrainedValue{
|
|
{Constraints: Constraints{Namespace: "samples-namespace", TaskQueueName: "sample-task-queue"}},
|
|
},
|
|
filters: []Constraints{
|
|
{Namespace: "samples-namespace", TaskQueueName: "sample-task-queue"},
|
|
},
|
|
matched: true,
|
|
},
|
|
{
|
|
v: []ConstrainedValue{
|
|
{Constraints: Constraints{Namespace: "samples-namespace"}},
|
|
},
|
|
filters: []Constraints{
|
|
{TaskQueueName: "sample-task-queue"},
|
|
},
|
|
matched: false,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
_, err := findMatch(tc.v, tc.filters)
|
|
assert.Equal(t, tc.matched, err == nil)
|
|
}
|
|
}
|
|
|
|
func TestFindMatchWithTyped(t *testing.T) {
|
|
testCases := []struct {
|
|
val []ConstrainedValue
|
|
tv []TypedConstrainedValue[struct{}]
|
|
filters []Constraints
|
|
valOrder int
|
|
defOrder int
|
|
}{
|
|
{
|
|
val: nil,
|
|
tv: []TypedConstrainedValue[struct{}]{
|
|
{Constraints: Constraints{}},
|
|
},
|
|
filters: []Constraints{
|
|
{Namespace: "some random namespace"},
|
|
},
|
|
valOrder: 0,
|
|
defOrder: 0,
|
|
},
|
|
{
|
|
val: nil,
|
|
tv: []TypedConstrainedValue[struct{}]{
|
|
{Constraints: Constraints{Namespace: "samples-namespace"}},
|
|
},
|
|
filters: []Constraints{
|
|
{Namespace: "some random namespace"},
|
|
},
|
|
valOrder: 0,
|
|
defOrder: 0,
|
|
},
|
|
{
|
|
val: nil,
|
|
tv: []TypedConstrainedValue[struct{}]{
|
|
{Constraints: Constraints{Namespace: "samples-namespace", TaskQueueName: "sample-task-queue"}},
|
|
},
|
|
filters: []Constraints{
|
|
{Namespace: "samples-namespace", TaskQueueName: "sample-task-queue"},
|
|
},
|
|
valOrder: 0,
|
|
defOrder: 1,
|
|
},
|
|
{
|
|
val: nil,
|
|
tv: []TypedConstrainedValue[struct{}]{
|
|
{Constraints: Constraints{Namespace: "samples-namespace"}},
|
|
},
|
|
filters: []Constraints{
|
|
{TaskQueueName: "sample-task-queue"},
|
|
},
|
|
valOrder: 0,
|
|
defOrder: 0,
|
|
},
|
|
{
|
|
val: []ConstrainedValue{
|
|
{Constraints: Constraints{Namespace: "ns"}},
|
|
},
|
|
tv: []TypedConstrainedValue[struct{}]{
|
|
{Constraints: Constraints{Namespace: "ns", TaskQueueName: "othertq"}},
|
|
{},
|
|
},
|
|
filters: []Constraints{
|
|
{Namespace: "ns", TaskQueueName: "tq"},
|
|
{Namespace: "ns"},
|
|
{},
|
|
},
|
|
valOrder: 4,
|
|
defOrder: 9,
|
|
},
|
|
{
|
|
val: []ConstrainedValue{
|
|
{Constraints: Constraints{Namespace: "ns"}},
|
|
},
|
|
tv: []TypedConstrainedValue[struct{}]{
|
|
{Constraints: Constraints{Namespace: "ns", TaskQueueName: "tq"}},
|
|
{},
|
|
},
|
|
filters: []Constraints{
|
|
{Namespace: "ns", TaskQueueName: "tq"},
|
|
{Namespace: "ns"},
|
|
{},
|
|
},
|
|
valOrder: 4,
|
|
defOrder: 2,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
_, _, valOrder, defOrder := findMatchWithConstrainedDefaults(tc.val, tc.tv, tc.filters)
|
|
assert.Equal(t, tc.valOrder, valOrder)
|
|
assert.Equal(t, tc.defOrder, defOrder)
|
|
}
|
|
}
|