go.temporal.io/server@v1.23.0/common/quotas/cluster_aware_quota_calculator_test.go (about) 1 // The MIT License 2 // 3 // Copyright (c) 2020 Temporal Technologies Inc. All rights reserved. 4 // 5 // Copyright (c) 2020 Uber Technologies, Inc. 6 // 7 // Permission is hereby granted, free of charge, to any person obtaining a copy 8 // of this software and associated documentation files (the "Software"), to deal 9 // in the Software without restriction, including without limitation the rights 10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 // copies of the Software, and to permit persons to whom the Software is 12 // furnished to do so, subject to the following conditions: 13 // 14 // The above copyright notice and this permission notice shall be included in 15 // all copies or substantial portions of the Software. 16 // 17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 // THE SOFTWARE. 24 25 package quotas_test 26 27 import ( 28 "testing" 29 30 "github.com/stretchr/testify/assert" 31 "go.temporal.io/server/common/dynamicconfig" 32 "go.temporal.io/server/common/quotas" 33 "go.temporal.io/server/common/quotas/quotastest" 34 ) 35 36 type quotaCalculatorTestCase struct { 37 name string 38 memberCounter quotas.MemberCounter 39 instanceLimit int 40 clusterLimit int 41 expected float64 42 } 43 44 var quotaCalculatorTestCases = []quotaCalculatorTestCase{ 45 { 46 name: "both limits set", 47 memberCounter: quotastest.NewFakeMemberCounter(4), 48 instanceLimit: 10, 49 clusterLimit: 20, 50 expected: 5.0, 51 }, 52 { 53 name: "no per cluster limit", 54 memberCounter: quotastest.NewFakeMemberCounter(4), 55 instanceLimit: 10, 56 clusterLimit: 0, 57 expected: 10.0, 58 }, 59 { 60 name: "no hosts", 61 memberCounter: quotastest.NewFakeMemberCounter(0), 62 instanceLimit: 10, 63 clusterLimit: 20, 64 expected: 10.0, 65 }, 66 { 67 name: "nil member counter", 68 memberCounter: nil, 69 instanceLimit: 10, 70 clusterLimit: 20, 71 expected: 10.0, 72 }, 73 } 74 75 func TestClusterAwareQuotaCalculator_GetQuota(t *testing.T) { 76 t.Parallel() 77 78 for _, tc := range quotaCalculatorTestCases { 79 tc := tc 80 t.Run(tc.name, func(t *testing.T) { 81 t.Parallel() 82 83 assert.Equal(t, tc.expected, quotas.ClusterAwareQuotaCalculator{ 84 MemberCounter: tc.memberCounter, 85 PerInstanceQuota: dynamicconfig.GetIntPropertyFn(tc.instanceLimit), 86 GlobalQuota: dynamicconfig.GetIntPropertyFn(tc.clusterLimit), 87 }.GetQuota()) 88 89 }) 90 } 91 } 92 93 type perNamespaceQuota struct { 94 t *testing.T 95 quota int 96 } 97 98 func (l perNamespaceQuota) getQuota(ns string) int { 99 if ns != "test-namespace" { 100 l.t.Errorf("unexpected namespace: %s", ns) 101 } 102 return l.quota 103 } 104 105 func TestClusterAwareNamespaceSpecificQuotaCalculator_GetQuota(t *testing.T) { 106 t.Parallel() 107 108 for _, tc := range quotaCalculatorTestCases { 109 tc := tc 110 t.Run(tc.name, func(t *testing.T) { 111 t.Parallel() 112 113 instanceLimit := perNamespaceQuota{t: t, quota: tc.instanceLimit} 114 115 clusterLimit := perNamespaceQuota{t: t, quota: tc.clusterLimit} 116 117 assert.Equal(t, tc.expected, quotas.ClusterAwareNamespaceSpecificQuotaCalculator{ 118 MemberCounter: tc.memberCounter, 119 PerInstanceQuota: instanceLimit.getQuota, 120 GlobalQuota: clusterLimit.getQuota, 121 }.GetQuota("test-namespace")) 122 }) 123 } 124 }