diff --git a/agent/consul/state/catalog_test.go b/agent/consul/state/catalog_test.go index c21b203db4..07bcc49377 100644 --- a/agent/consul/state/catalog_test.go +++ b/agent/consul/state/catalog_test.go @@ -1709,8 +1709,10 @@ func TestStateStore_ServicesByNodeMeta(t *testing.T) { }) t.Run("Uses watchLimit to limit the number of watches", func(t *testing.T) { + patchWatchLimit(t, 10) + var idx uint64 = 6 - for i := 0; i < 2*watchLimit; i++ { + for i := 0; i < watchLimit+2; i++ { node := fmt.Sprintf("many%d", i) testRegisterNodeWithMeta(t, s, idx, node, map[string]string{"common": "1"}) idx++ @@ -1731,6 +1733,16 @@ func TestStateStore_ServicesByNodeMeta(t *testing.T) { }) } +// patchWatchLimit package variable. Not safe for concurrent use. Do not use +// with t.Parallel. +func patchWatchLimit(t *testing.T, limit int) { + oldLimit := watchLimit + watchLimit = limit + t.Cleanup(func() { + watchLimit = oldLimit + }) +} + func TestStateStore_ServiceNodes(t *testing.T) { s := testStateStore(t) @@ -1847,7 +1859,8 @@ func TestStateStore_ServiceNodes(t *testing.T) { // Overwhelm the node tracking. idx = 19 - for i := 0; i < 2*watchLimit; i++ { + patchWatchLimit(t, 10) + for i := 0; i < watchLimit+2; i++ { node := fmt.Sprintf("many%d", i) if err := s.EnsureNode(idx, &structs.Node{Node: node, Address: "127.0.0.1"}); err != nil { t.Fatalf("err: %v", err) @@ -2616,7 +2629,8 @@ func TestStateStore_ServiceChecksByNodeMeta(t *testing.T) { } // Overwhelm the node tracking. - for i := 0; i < 2*watchLimit; i++ { + patchWatchLimit(t, 10) + for i := 0; i < watchLimit+2; i++ { node := fmt.Sprintf("many%d", idx) testRegisterNodeWithMeta(t, s, idx, node, map[string]string{"common": "1"}) idx++ @@ -2796,7 +2810,8 @@ func TestStateStore_ChecksInStateByNodeMeta(t *testing.T) { } // Overwhelm the node tracking. - for i := 0; i < 2*watchLimit; i++ { + patchWatchLimit(t, 10) + for i := 0; i < watchLimit+2; i++ { node := fmt.Sprintf("many%d", idx) testRegisterNodeWithMeta(t, s, idx, node, map[string]string{"common": "1"}) idx++ diff --git a/agent/consul/state/state_store.go b/agent/consul/state/state_store.go index 3692deb9dc..bab348fefa 100644 --- a/agent/consul/state/state_store.go +++ b/agent/consul/state/state_store.go @@ -82,7 +82,7 @@ var ( ErrMissingIntentionID = errors.New("Missing Intention ID") ) -const ( +var ( // watchLimit is used as a soft limit to cap how many watches we allow // for a given blocking query. If this is exceeded, then we will use a // higher-level watch that's less fine-grained. Choosing the perfect