Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 45 additions & 58 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ func TestAPIUpdateRule(t *testing.T) {
defer engine.Close()

// Add dimension configurations
regionConfig := NewDimensionConfig("region", 0, false, 5.0)
regionConfig := NewDimensionConfig("region", 0, false)
regionConfig.SetWeight(MatchTypeEqual, 10.0)
err = engine.AddDimension(regionConfig)
if err != nil {
t.Fatalf("Failed to add region dimension: %v", err)
}

envConfig := NewDimensionConfig("env", 1, false, 3.0)
envConfig := NewDimensionConfig("env", 1, false)
envConfig.SetWeight(MatchTypeEqual, 8.0)
err = engine.AddDimension(envConfig)
if err != nil {
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestAPIUpdateRuleStatus(t *testing.T) {
defer engine.Close()

// Add dimension configuration
regionConfig := NewDimensionConfig("region", 0, false, 5.0)
regionConfig := NewDimensionConfig("region", 0, false)
regionConfig.SetWeight(MatchTypeEqual, 10.0)
err = engine.AddDimension(regionConfig)
if err != nil {
Expand Down Expand Up @@ -241,7 +241,7 @@ func TestAPIUpdateRuleMetadata(t *testing.T) {
defer engine.Close()

// Add dimension configuration
regionConfig := NewDimensionConfig("region", 0, false, 5.0)
regionConfig := NewDimensionConfig("region", 0, false)
regionConfig.SetWeight(MatchTypeEqual, 10.0)
err = engine.AddDimension(regionConfig)
if err != nil {
Expand Down Expand Up @@ -321,7 +321,7 @@ func TestAPIGetRule(t *testing.T) {
defer engine.Close()

// Add dimension configuration
regionConfig := NewDimensionConfig("region", 0, false, 5.0)
regionConfig := NewDimensionConfig("region", 0, false)
regionConfig.SetWeight(MatchTypeEqual, 10.0)
err = engine.AddDimension(regionConfig)
if err != nil {
Expand Down Expand Up @@ -439,7 +439,7 @@ func TestAPIAddDimension(t *testing.T) {
defer engine.Close()

// Add a dimension
dim := NewDimensionConfig("api-test-dim", 100, false, 1.0)
dim := NewDimensionConfig("api-test-dim", 100, false)
if err := engine.AddDimension(dim); err != nil {
t.Errorf("AddDimension failed: %v", err)
}
Expand Down Expand Up @@ -494,7 +494,7 @@ func TestAPIFindAllMatches(t *testing.T) {
defer engine.Close()

// Add dimension config to control weights
config := NewDimensionConfig("region", 0, false, 5.0)
config := NewDimensionConfig("region", 0, false)
if err := engine.AddDimension(config); err != nil {
t.Fatalf("Failed to add dimension config: %v", err)
}
Expand Down Expand Up @@ -538,7 +538,7 @@ func TestAPIBatchAddRules(t *testing.T) {
defer engine.Close()

// Add dimension config to control weights
config := NewDimensionConfig("region", 0, false, 3.0)
config := NewDimensionConfig("region", 0, false)
if err := engine.AddDimension(config); err != nil {
t.Fatalf("Failed to add dimension config: %v", err)
}
Expand Down Expand Up @@ -706,7 +706,7 @@ func TestGenerateDefaultNodeID(t *testing.T) {

func TestDimensionConfig_GetWeight(t *testing.T) {
// Test case 1: GetWeight with defined match type weights
config := NewDimensionConfig("test", 0, false, 5.0)
config := NewDimensionConfig("test", 0, false)
config.SetWeight(MatchTypeEqual, 10.0)
config.SetWeight(MatchTypePrefix, 7.0)
config.SetWeight(MatchTypeAny, 2.0)
Expand All @@ -724,23 +724,23 @@ func TestDimensionConfig_GetWeight(t *testing.T) {
t.Errorf("Expected weight 2.0 for Any match type, got %.1f", weight)
}

// Test getting undefined weight (should return default)
if weight := config.GetWeight(MatchTypeSuffix); weight != 5.0 {
t.Errorf("Expected default weight 5.0 for undefined Suffix match type, got %.1f", weight)
// Test getting undefined weight (should return 0.0, no default weight anymore)
if weight := config.GetWeight(MatchTypeSuffix); weight != 0.0 {
t.Errorf("Expected 0.0 for undefined Suffix match type, got %.1f", weight)
}

// Test case 2: GetWeight with no defined weights (all should return default)
emptyConfig := NewDimensionConfig("empty", 1, true, 15.0)
// Test case 2: GetWeight with no defined weights (all should return 0.0)
emptyConfig := NewDimensionConfig("empty", 1, true)

matchTypes := []MatchType{MatchTypeEqual, MatchTypePrefix, MatchTypeSuffix, MatchTypeAny}
for _, mt := range matchTypes {
if weight := emptyConfig.GetWeight(mt); weight != 15.0 {
t.Errorf("Expected default weight 15.0 for %s match type, got %.1f", mt, weight)
if weight := emptyConfig.GetWeight(mt); weight != 0.0 {
t.Errorf("Expected 0.0 for %s match type, got %.1f", mt, weight)
}
}

// Test case 3: GetWeight after setting and overriding weights
overrideConfig := NewDimensionConfig("override", 2, false, 3.0)
overrideConfig := NewDimensionConfig("override", 2, false)

// Set initial weight
overrideConfig.SetWeight(MatchTypeEqual, 8.0)
Expand All @@ -754,14 +754,14 @@ func TestDimensionConfig_GetWeight(t *testing.T) {
t.Errorf("Expected weight 12.0 after override, got %.1f", weight)
}

// Other match types should still return default
if weight := overrideConfig.GetWeight(MatchTypePrefix); weight != 3.0 {
t.Errorf("Expected default weight 3.0 for Prefix after override, got %.1f", weight)
// Other match types should still return 0.0 (no default weight anymore)
if weight := overrideConfig.GetWeight(MatchTypePrefix); weight != 0.0 {
t.Errorf("Expected 0.0 for Prefix after override, got %.1f", weight)
}
}

func TestDimensionConfig_SetWeightFunction(t *testing.T) {
config := NewDimensionConfig("test", 0, false, 1.0)
config := NewDimensionConfig("test", 0, false)

// Test setting weights for all match types
weights := map[MatchType]float64{
Expand Down Expand Up @@ -801,7 +801,7 @@ func TestNewDimensionConfigWithWeightsFunction(t *testing.T) {
MatchTypeAny: 3.0,
}

config := NewDimensionConfigWithWeights("weighted", 1, true, weights, 5.0)
config := NewDimensionConfigWithWeights("weighted", 1, true, weights)

if config.Name != "weighted" {
t.Errorf("Expected name 'weighted', got '%s'", config.Name)
Expand All @@ -815,10 +815,16 @@ func TestNewDimensionConfigWithWeightsFunction(t *testing.T) {
t.Error("Expected required to be true")
}

if config.DefaultWeight != 5.0 {
t.Errorf("Expected default weight 5.0, got %.1f", config.DefaultWeight)
// Test that unset match types return 0.0 (no DefaultWeight anymore)
// Note: All match types in the weights map above are set, so we need to test a different approach
// or verify that GetWeight returns the correct explicit values
if config.GetWeight(MatchTypePrefix) != 10.0 {
t.Errorf("Expected MatchTypePrefix weight 10.0 from explicit weights, got %.1f", config.GetWeight(MatchTypePrefix))
}

// Test what happens if we query a type that was explicitly set
// Since all match types are covered in weights map, this test verifies explicit weights work

// Test that all weights were set correctly
for matchType, expectedWeight := range weights {
actualWeight := config.GetWeight(matchType)
Expand Down Expand Up @@ -857,7 +863,7 @@ func TestInitializeDimensionFunction(t *testing.T) {

// Since InitializeDimension is a no-op, we just verify it doesn't crash
// and we can still add dimensions normally
config := NewDimensionConfig("test_dimension", 0, false, 10.0)
config := NewDimensionConfig("test_dimension", 0, false)
config.SetWeight(MatchTypeEqual, 20.0)
config.SetWeight(MatchTypePrefix, 15.0)

Expand Down Expand Up @@ -909,7 +915,7 @@ func TestDeleteDimensionFunction(t *testing.T) {
defer engine.Close()

// Add a dimension
config := NewDimensionConfig("removable", 0, false, 5.0)
config := NewDimensionConfig("removable", 0, false)
err = engine.AddDimension(config)
if err != nil {
t.Fatalf("Failed to add dimension: %v", err)
Expand Down Expand Up @@ -1048,7 +1054,7 @@ func TestCreateQueryWithDynamicConfigs(t *testing.T) {
}

dynamicConfigs := map[string]*DimensionConfig{
"region": NewDimensionConfig("region", 0, false, 10.0),
"region": NewDimensionConfig("region", 0, false),
}

query := CreateQueryWithDynamicConfigs(values, dynamicConfigs)
Expand Down Expand Up @@ -1087,8 +1093,8 @@ func TestCreateQueryWithTenantAndDynamicConfigs(t *testing.T) {
}

dynamicConfigs := map[string]*DimensionConfig{
"category": NewDimensionConfig("category", 0, true, 15.0),
"priority": NewDimensionConfig("priority", 1, false, 8.0),
"category": NewDimensionConfig("category", 0, true),
"priority": NewDimensionConfig("priority", 1, false),
}

query := CreateQueryWithTenantAndDynamicConfigs(tenantID, applicationID, values, dynamicConfigs)
Expand Down Expand Up @@ -1120,16 +1126,6 @@ func TestCreateQueryWithTenantAndDynamicConfigs(t *testing.T) {
if len(query.DynamicDimensionConfigs) != 2 {
t.Errorf("Expected 2 dynamic configs, got %d", len(query.DynamicDimensionConfigs))
}

categoryConfig := query.DynamicDimensionConfigs["category"]
if categoryConfig.Name != "category" || categoryConfig.DefaultWeight != 15.0 {
t.Errorf("Category config mismatch: name=%s, weight=%.1f", categoryConfig.Name, categoryConfig.DefaultWeight)
}

priorityConfig := query.DynamicDimensionConfigs["priority"]
if priorityConfig.Name != "priority" || priorityConfig.DefaultWeight != 8.0 {
t.Errorf("Priority config mismatch: name=%s, weight=%.1f", priorityConfig.Name, priorityConfig.DefaultWeight)
}
}

func TestCreateQueryWithAllRulesAndDynamicConfigs(t *testing.T) {
Expand All @@ -1139,7 +1135,7 @@ func TestCreateQueryWithAllRulesAndDynamicConfigs(t *testing.T) {
}

dynamicConfigs := map[string]*DimensionConfig{
"service": NewDimensionConfig("service", 0, true, 20.0),
"service": NewDimensionConfig("service", 0, true),
}

query := CreateQueryWithAllRulesAndDynamicConfigs(values, dynamicConfigs)
Expand All @@ -1163,11 +1159,6 @@ func TestCreateQueryWithAllRulesAndDynamicConfigs(t *testing.T) {
if len(query.DynamicDimensionConfigs) != 1 {
t.Errorf("Expected 1 dynamic config, got %d", len(query.DynamicDimensionConfigs))
}

serviceConfig := query.DynamicDimensionConfigs["service"]
if serviceConfig.Name != "service" || serviceConfig.DefaultWeight != 20.0 {
t.Errorf("Service config mismatch: name=%s, weight=%.1f", serviceConfig.Name, serviceConfig.DefaultWeight)
}
}

func TestCreateQueryWithAllRulesTenantAndDynamicConfigs(t *testing.T) {
Expand All @@ -1180,9 +1171,9 @@ func TestCreateQueryWithAllRulesTenantAndDynamicConfigs(t *testing.T) {
}

dynamicConfigs := map[string]*DimensionConfig{
"user_type": NewDimensionConfig("user_type", 0, true, 25.0),
"action": NewDimensionConfig("action", 1, true, 20.0),
"resource": NewDimensionConfig("resource", 2, false, 10.0),
"user_type": NewDimensionConfig("user_type", 0, true),
"action": NewDimensionConfig("action", 1, true),
"resource": NewDimensionConfig("resource", 2, false),
}

query := CreateQueryWithAllRulesTenantAndDynamicConfigs(tenantID, applicationID, values, dynamicConfigs)
Expand Down Expand Up @@ -1240,10 +1231,6 @@ func TestCreateQueryWithAllRulesTenantAndDynamicConfigs(t *testing.T) {
t.Errorf("Config %s: expected name '%s', got '%s'", name, name, config.Name)
}

if config.DefaultWeight != expected.weight {
t.Errorf("Config %s: expected weight %.1f, got %.1f", name, expected.weight, config.DefaultWeight)
}

if config.Required != expected.required {
t.Errorf("Config %s: expected required %v, got %v", name, expected.required, config.Required)
}
Expand All @@ -1262,7 +1249,7 @@ func TestDynamicConfigsIntegration(t *testing.T) {
defer engine.Close()

// Add basic dimension configurations
regionConfig := NewDimensionConfig("region", 0, false, 5.0)
regionConfig := NewDimensionConfig("region", 0, false)
regionConfig.SetWeight(MatchTypeEqual, 10.0)
regionConfig.SetWeight(MatchTypePrefix, 7.0)

Expand Down Expand Up @@ -1300,7 +1287,7 @@ func TestDynamicConfigsIntegration(t *testing.T) {
{
name: "CreateQueryWithDynamicConfigs",
createQuery: func() *QueryRule {
dynamicConfig := NewDimensionConfig("region", 0, false, 50.0)
dynamicConfig := NewDimensionConfig("region", 0, false)
dynamicConfig.SetWeight(MatchTypeEqual, 100.0)

return CreateQueryWithDynamicConfigs(
Expand All @@ -1313,7 +1300,7 @@ func TestDynamicConfigsIntegration(t *testing.T) {
{
name: "CreateQueryWithTenantAndDynamicConfigs",
createQuery: func() *QueryRule {
dynamicConfig := NewDimensionConfig("region", 0, false, 75.0)
dynamicConfig := NewDimensionConfig("region", 0, false)
dynamicConfig.SetWeight(MatchTypeEqual, 150.0)

return CreateQueryWithTenantAndDynamicConfigs(
Expand All @@ -1327,7 +1314,7 @@ func TestDynamicConfigsIntegration(t *testing.T) {
{
name: "CreateQueryWithAllRulesAndDynamicConfigs",
createQuery: func() *QueryRule {
dynamicConfig := NewDimensionConfig("region", 0, false, 30.0)
dynamicConfig := NewDimensionConfig("region", 0, false)
dynamicConfig.SetWeight(MatchTypeEqual, 60.0)

return CreateQueryWithAllRulesAndDynamicConfigs(
Expand All @@ -1340,7 +1327,7 @@ func TestDynamicConfigsIntegration(t *testing.T) {
{
name: "CreateQueryWithAllRulesTenantAndDynamicConfigs",
createQuery: func() *QueryRule {
dynamicConfig := NewDimensionConfig("region", 0, false, 25.0)
dynamicConfig := NewDimensionConfig("region", 0, false)
dynamicConfig.SetWeight(MatchTypeEqual, 50.0)

return CreateQueryWithAllRulesTenantAndDynamicConfigs(
Expand Down Expand Up @@ -1388,7 +1375,7 @@ func TestDeleteDimensionCoverage(t *testing.T) {
defer engine.Close()

// Add a dimension first
dimConfig := NewDimensionConfig("test_dim_delete", 0, false, 5.0)
dimConfig := NewDimensionConfig("test_dim_delete", 0, false)
err = engine.AddDimension(dimConfig)
if err != nil {
t.Fatalf("Failed to add dimension: %v", err)
Expand Down
6 changes: 3 additions & 3 deletions atomic_update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func TestAtomicRuleUpdateFix(t *testing.T) {
engine.SetAllowDuplicateWeights(true)

// Add dimension configurations
regionConfig := NewDimensionConfig("region", 0, false, 5.0)
regionConfig := NewDimensionConfig("region", 0, false)
regionConfig.SetWeight(MatchTypeEqual, 10.0)
envConfig := NewDimensionConfig("env", 1, false, 3.0)
envConfig := NewDimensionConfig("env", 1, false)
envConfig.SetWeight(MatchTypeEqual, 8.0)
serviceConfig := NewDimensionConfig("service", 2, false, 2.0)
serviceConfig := NewDimensionConfig("service", 2, false)
serviceConfig.SetWeight(MatchTypeEqual, 6.0)

engine.AddDimension(regionConfig)
Expand Down
6 changes: 3 additions & 3 deletions cmd/debug_matching/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func main() {
defer engine.Close()

// Configure 3 simple dimensions
engine.AddDimension(matcher.NewDimensionConfig("product", 0, true, 10.0))
engine.AddDimension(matcher.NewDimensionConfig("environment", 1, true, 5.0))
engine.AddDimension(matcher.NewDimensionConfig("region", 2, false, 3.0))
engine.AddDimension(matcher.NewDimensionConfig("product", 0, true))
engine.AddDimension(matcher.NewDimensionConfig("environment", 1, true))
engine.AddDimension(matcher.NewDimensionConfig("region", 2, false))

// Add a simple test rule
rule := matcher.NewRule("test_rule").
Expand Down
3 changes: 1 addition & 2 deletions cmd/performance_benchmark/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,7 @@ func generateRealisticDimensions(count int) []*matcher.DimensionConfig {
dimensions[i] = matcher.NewDimensionConfig(
name,
i,
i < 3, // First 3 dimensions required
float64(20-(i%20)), // Weights from 20 down to 1, cycling if needed
i < 3, // First 3 dimensions required
)
}
return dimensions
Expand Down
3 changes: 1 addition & 2 deletions cmd/target_performance/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ func main() {
dim := matcher.NewDimensionConfig(
fmt.Sprintf("dim_%02d", i),
i,
i < 3, // First 3 required
float64(21-i), // 20, 19, 18, ... 1
i < 3, // First 3 required
)
if err := engine.AddDimension(dim); err != nil {
slog.Error("Failed to add dimension", "error", err)
Expand Down
10 changes: 5 additions & 5 deletions concurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ func TestConcurrentRuleOperationsNoPartialRules(t *testing.T) {
engine.SetAllowDuplicateWeights(true)

// Add dimension configurations
regionConfig := NewDimensionConfig("region", 0, false, 5.0)
regionConfig := NewDimensionConfig("region", 0, false)
regionConfig.SetWeight(MatchTypeEqual, 10.0)
envConfig := NewDimensionConfig("env", 1, false, 3.0)
envConfig := NewDimensionConfig("env", 1, false)
envConfig.SetWeight(MatchTypeEqual, 8.0)
serviceConfig := NewDimensionConfig("service", 2, false, 2.0)
serviceConfig := NewDimensionConfig("service", 2, false)
serviceConfig.SetWeight(MatchTypeEqual, 6.0)

err = engine.AddDimension(regionConfig)
Expand Down Expand Up @@ -311,7 +311,7 @@ func TestConcurrentRuleStatusUpdatesNoPartialRules(t *testing.T) {
defer engine.Close()

// Add dimension configuration
regionConfig := NewDimensionConfig("region", 0, false, 5.0)
regionConfig := NewDimensionConfig("region", 0, false)
regionConfig.SetWeight(MatchTypeEqual, 10.0)
err = engine.AddDimension(regionConfig)
if err != nil {
Expand Down Expand Up @@ -436,7 +436,7 @@ func TestConcurrentMetadataUpdatesNoPartialRules(t *testing.T) {
defer engine.Close()

// Add dimension configuration
regionConfig := NewDimensionConfig("region", 0, false, 5.0)
regionConfig := NewDimensionConfig("region", 0, false)
regionConfig.SetWeight(MatchTypeEqual, 10.0)
err = engine.AddDimension(regionConfig)
if err != nil {
Expand Down
Loading