github.com/m3db/m3@v1.5.0/src/dbnode/integration/run_custom_background_process_test.go (about) 1 // +build integration 2 3 // Copyright (c) 2020 Uber Technologies, Inc. 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in 13 // all copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 // THE SOFTWARE. 22 23 package integration 24 25 import ( 26 "testing" 27 "time" 28 29 "github.com/m3db/m3/src/dbnode/storage" 30 xclock "github.com/m3db/m3/src/x/clock" 31 32 "github.com/stretchr/testify/assert" 33 "github.com/stretchr/testify/require" 34 "go.uber.org/atomic" 35 ) 36 37 type testBackgroundProcess struct { 38 executed, reported, stopped atomic.Int32 39 } 40 41 func (p *testBackgroundProcess) run() { 42 xclock.WaitUntil(func() bool { 43 return p.reported.Load() > 0 44 }, time.Minute) 45 p.executed.Inc() 46 xclock.WaitUntil(func() bool { 47 return p.stopped.Load() > 0 48 }, time.Minute) 49 } 50 51 func (p *testBackgroundProcess) Start() { 52 go p.run() 53 } 54 55 func (p *testBackgroundProcess) Stop() { 56 p.stopped.Inc() 57 return 58 } 59 60 func (p *testBackgroundProcess) Report() { 61 p.reported.Inc() 62 return 63 } 64 65 func TestRunCustomBackgroundProcess(t *testing.T) { 66 testOpts := NewTestOptions(t).SetReportInterval(time.Millisecond) 67 testSetup := newTestSetupWithCommitLogAndFilesystemBootstrapper(t, testOpts) 68 defer testSetup.Close() 69 70 backgroundProcess := &testBackgroundProcess{} 71 72 storageOpts := testSetup.StorageOpts(). 73 SetBackgroundProcessFns([]storage.NewBackgroundProcessFn{ 74 func(storage.Database, storage.Options) (storage.BackgroundProcess, error) { 75 return backgroundProcess, nil 76 }}) 77 testSetup.SetStorageOpts(storageOpts) 78 79 log := storageOpts.InstrumentOptions().Logger() 80 81 // Start the server. 82 require.NoError(t, testSetup.StartServer()) 83 84 // Stop the server. 85 defer func() { 86 require.NoError(t, testSetup.StopServer()) 87 log.Debug("server is now down") 88 assert.Equal(t, int32(1), backgroundProcess.stopped.Load(), "failed to stop") 89 }() 90 91 log.Info("waiting for the custom background process to execute") 92 xclock.WaitUntil(func() bool { 93 return backgroundProcess.executed.Load() > 0 94 }, time.Minute) 95 96 assert.Equal(t, int32(1), backgroundProcess.executed.Load(), "failed to execute") 97 assert.True(t, backgroundProcess.reported.Load() > 0, "failed to report") 98 }