github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/query/storage/m3/cluster_namespaces_watcher_test.go (about) 1 // Copyright (c) 2020 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package m3 22 23 import ( 24 "testing" 25 "time" 26 27 "github.com/m3db/m3/src/dbnode/client" 28 "github.com/m3db/m3/src/x/ident" 29 xtest "github.com/m3db/m3/src/x/test" 30 xwatch "github.com/m3db/m3/src/x/watch" 31 32 "github.com/golang/mock/gomock" 33 "github.com/stretchr/testify/require" 34 ) 35 36 func TestClusterNamespacesWatcherGet(t *testing.T) { 37 ctrl := xtest.NewController(t) 38 defer ctrl.Finish() 39 40 watchable := xwatch.NewWatchable() 41 var watcher ClusterNamespacesWatcher = &clusterNamespacesWatcher{watchable: watchable} 42 defer watcher.Close() 43 44 // Get from empty watch. 45 namespaces := watcher.Get() 46 require.Nil(t, namespaces) 47 48 // Get when watch is set. 49 namespaces = createClusterNamespaces(t, ctrl) 50 err := watchable.Update(namespaces) 51 require.NoError(t, err) 52 53 require.Equal(t, namespaces, watcher.Get()) 54 } 55 56 func TestClusterNamespacesWatcherUpdater(t *testing.T) { 57 ctrl := xtest.NewController(t) 58 defer ctrl.Finish() 59 60 watcher := NewClusterNamespacesWatcher() 61 defer watcher.Close() 62 63 require.Nil(t, watcher.Get()) 64 65 namespaces := createClusterNamespaces(t, ctrl) 66 err := watcher.Update(namespaces) 67 require.NoError(t, err) 68 69 require.Equal(t, namespaces, watcher.Get()) 70 } 71 72 func TestClusterNamespacesWatcherRegisterListener(t *testing.T) { 73 ctrl := xtest.NewController(t) 74 defer ctrl.Finish() 75 76 watcher := NewClusterNamespacesWatcher() 77 defer watcher.Close() 78 79 namespaces := createClusterNamespaces(t, ctrl) 80 81 updateCh := make(chan bool) 82 listener := &testListener{ 83 expected: namespaces, 84 updateCh: updateCh, 85 t: t, 86 } 87 watcher.RegisterListener(listener) 88 89 err := watcher.Update(namespaces) 90 require.NoError(t, err) 91 92 <-updateCh 93 } 94 95 func TestClusterNamespacesWatcherRegisterListenerNamespacesAlreadySet(t *testing.T) { 96 ctrl := xtest.NewController(t) 97 defer ctrl.Finish() 98 99 watcher := NewClusterNamespacesWatcher() 100 defer watcher.Close() 101 102 namespaces := createClusterNamespaces(t, ctrl) 103 104 err := watcher.Update(namespaces) 105 require.NoError(t, err) 106 107 updateCh := make(chan bool, 1) 108 listener := &testListener{ 109 expected: namespaces, 110 updateCh: updateCh, 111 t: t, 112 } 113 watcher.RegisterListener(listener) 114 115 <-updateCh 116 117 // Add a short sleep to ensure we don't receive a duplicate (and undesired) call 118 time.Sleep(250 * time.Millisecond) 119 120 require.Equal(t, 1, listener.callCount) 121 } 122 123 func createClusterNamespaces(t *testing.T, ctrl *gomock.Controller) ClusterNamespaces { 124 session := client.NewMockSession(ctrl) 125 namespace, err := newAggregatedClusterNamespace(AggregatedClusterNamespaceDefinition{ 126 NamespaceID: ident.StringID("2s:1d"), 127 Resolution: 2 * time.Second, 128 Retention: 24 * time.Hour, 129 Session: session, 130 }) 131 require.NoError(t, err) 132 133 return ClusterNamespaces{namespace} 134 } 135 136 type testListener struct { 137 updateCh chan bool 138 expected ClusterNamespaces 139 t *testing.T 140 callCount int 141 } 142 143 func (l *testListener) OnUpdate(namespaces ClusterNamespaces) { 144 require.Equal(l.t, l.expected, namespaces) 145 l.updateCh <- true 146 l.callCount++ 147 }