github.com/m3db/m3@v1.5.0/src/dbnode/storage/cluster/cluster_test.go (about) 1 // Copyright (c) 2016 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 cluster 22 23 import ( 24 "fmt" 25 "testing" 26 27 "github.com/m3db/m3/src/dbnode/sharding" 28 "github.com/m3db/m3/src/dbnode/storage" 29 "github.com/m3db/m3/src/dbnode/topology" 30 "github.com/m3db/m3/src/dbnode/topology/testutil" 31 xwatch "github.com/m3db/m3/src/x/watch" 32 33 "github.com/golang/mock/gomock" 34 "github.com/stretchr/testify/require" 35 ) 36 37 type restoreFn func() 38 39 func mockNewStorageDatabase( 40 ctrl *gomock.Controller, 41 ) (*storage.MockDatabase, restoreFn) { 42 var mock *storage.MockDatabase 43 if ctrl != nil { 44 mock = storage.NewMockDatabase(ctrl) 45 } 46 restore := setNewStorageDatabase(func( 47 shardSet sharding.ShardSet, 48 opts storage.Options, 49 ) (storage.Database, error) { 50 if mock == nil { 51 return nil, fmt.Errorf("no injected storage database") 52 } 53 return mock, nil 54 }) 55 return mock, restore 56 } 57 58 func setNewStorageDatabase(fn newStorageDatabaseFn) restoreFn { 59 prevNewStorageDatabase := newStorageDatabase 60 newStorageDatabase = fn 61 return func() { 62 newStorageDatabase = prevNewStorageDatabase 63 } 64 } 65 66 type mockTopoInitProperties struct { 67 topology *topology.MockDynamicTopology 68 propogateViewsCh chan struct{} 69 } 70 71 func newMockTopoInit( 72 t *testing.T, 73 ctrl *gomock.Controller, 74 viewsCh <-chan testutil.TopologyView, 75 ) ( 76 *topology.MockInitializer, 77 mockTopoInitProperties, 78 ) { 79 init := topology.NewMockInitializer(ctrl) 80 81 watch := xwatch.NewWatchable() 82 83 // Make the propagate views channel large so it never blocks 84 propogateViewsCh := make(chan struct{}, 128) 85 86 go func() { 87 for { 88 v, ok := <-viewsCh 89 if !ok { 90 break 91 } 92 93 m, err := v.Map() 94 require.NoError(t, err) 95 watch.Update(m) 96 97 propogateViewsCh <- struct{}{} 98 } 99 }() 100 101 _, w, err := watch.Watch() 102 require.NoError(t, err) 103 104 topo := topology.NewMockDynamicTopology(ctrl) 105 topo.EXPECT().Watch().Return(topology.NewMapWatch(w), nil) 106 107 init.EXPECT().Init().Return(topo, nil) 108 109 return init, mockTopoInitProperties{ 110 topology: topo, 111 propogateViewsCh: propogateViewsCh, 112 } 113 }