@@ -1032,6 +1032,93 @@ describe("FairDequeuingStrategy", () => {
1032
1032
expect ( selectionPercentages [ "env-4" ] || 0 ) . toBeLessThan ( 20 ) ;
1033
1033
}
1034
1034
) ;
1035
+
1036
+ redisTest (
1037
+ "should respect maximumQueuePerEnvCount when distributing queues" ,
1038
+ async ( { redisOptions } ) => {
1039
+ const redis = createRedisClient ( redisOptions ) ;
1040
+
1041
+ const keyProducer = createKeyProducer ( "test" ) ;
1042
+ const strategy = new FairDequeuingStrategy ( {
1043
+ tracer,
1044
+ redis,
1045
+ keys : keyProducer ,
1046
+ defaultEnvConcurrency : 5 ,
1047
+ parentQueueLimit : 100 ,
1048
+ seed : "test-seed-max-queues" ,
1049
+ maximumQueuePerEnvCount : 2 , // Only take 2 queues per env
1050
+ } ) ;
1051
+
1052
+ const now = Date . now ( ) ;
1053
+
1054
+ // Setup two environments with different numbers of queues
1055
+ const envSetups = [
1056
+ {
1057
+ envId : "env-1" ,
1058
+ queues : [
1059
+ { age : 5000 } , // Oldest
1060
+ { age : 4000 } ,
1061
+ { age : 3000 } , // This should be excluded due to maximumQueuePerEnvCount
1062
+ ] ,
1063
+ } ,
1064
+ {
1065
+ envId : "env-2" ,
1066
+ queues : [
1067
+ { age : 2000 } ,
1068
+ { age : 1000 } , // Newest
1069
+ ] ,
1070
+ } ,
1071
+ ] ;
1072
+
1073
+ // Setup queues and concurrency for each env
1074
+ for ( const setup of envSetups ) {
1075
+ await setupConcurrency ( {
1076
+ redis,
1077
+ keyProducer,
1078
+ env : { id : setup . envId , currentConcurrency : 0 , limit : 5 } ,
1079
+ } ) ;
1080
+
1081
+ for ( let i = 0 ; i < setup . queues . length ; i ++ ) {
1082
+ await setupQueue ( {
1083
+ redis,
1084
+ keyProducer,
1085
+ parentQueue : "parent-queue" ,
1086
+ score : now - setup . queues [ i ] . age ,
1087
+ queueId : `queue-${ setup . envId } -${ i } ` ,
1088
+ orgId : `org-${ setup . envId } ` ,
1089
+ envId : setup . envId ,
1090
+ } ) ;
1091
+ }
1092
+ }
1093
+
1094
+ const result = await strategy . distributeFairQueuesFromParentQueue (
1095
+ "parent-queue" ,
1096
+ "consumer-1"
1097
+ ) ;
1098
+
1099
+ // Verify that each environment has at most 2 queues
1100
+ for ( const envQueues of result ) {
1101
+ expect ( envQueues . queues . length ) . toBeLessThanOrEqual ( 2 ) ;
1102
+ }
1103
+
1104
+ // Get queues for env-1 (which had 3 queues originally)
1105
+ const env1Queues = result . find ( ( eq ) => eq . envId === "env-1" ) ?. queues ?? [ ] ;
1106
+
1107
+ // Should have exactly 2 queues
1108
+ expect ( env1Queues . length ) . toBe ( 2 ) ;
1109
+
1110
+ // The queues should be the two oldest ones (queue-env-1-0 and queue-env-1-1)
1111
+ expect ( env1Queues ) . toContain ( keyProducer . queueKey ( "org-env-1" , "env-1" , "queue-env-1-0" ) ) ;
1112
+ expect ( env1Queues ) . toContain ( keyProducer . queueKey ( "org-env-1" , "env-1" , "queue-env-1-1" ) ) ;
1113
+ expect ( env1Queues ) . not . toContain ( keyProducer . queueKey ( "org-env-1" , "env-1" , "queue-env-1-2" ) ) ;
1114
+
1115
+ // Get queues for env-2 (which had 2 queues originally)
1116
+ const env2Queues = result . find ( ( eq ) => eq . envId === "env-2" ) ?. queues ?? [ ] ;
1117
+
1118
+ // Should still have both queues since it was within the limit
1119
+ expect ( env2Queues . length ) . toBe ( 2 ) ;
1120
+ }
1121
+ ) ;
1035
1122
} ) ;
1036
1123
1037
1124
// Helper function to flatten results for counting
0 commit comments