github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/cmd/services/m3coordinator/downsample/async_downsampler_test.go (about) 1 // Copyright (c) 2018 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 downsample 22 23 import ( 24 "errors" 25 "fmt" 26 "sync" 27 "testing" 28 29 "github.com/golang/mock/gomock" 30 "github.com/stretchr/testify/assert" 31 "github.com/stretchr/testify/require" 32 ) 33 34 func TestAsyncDownsamplerError(t *testing.T) { 35 customErr := errors.New("some error") 36 expectedErrStr := fmt.Sprintf(errNewDownsamplerFailFmt, customErr) 37 38 done := make(chan struct{}, 1) 39 asyncDownsampler := NewAsyncDownsampler(func() (Downsampler, error) { 40 return nil, customErr 41 }, done) 42 require.NotNil(t, asyncDownsampler) 43 44 // Wait for downsampler to be done initializing (which we mock to return an error) 45 <-done 46 47 _, err := asyncDownsampler.NewMetricsAppender() 48 assert.EqualError(t, err, expectedErrStr) 49 } 50 51 func TestAsyncDownsamplerUninitialized(t *testing.T) { 52 ctrl := gomock.NewController(t) 53 defer ctrl.Finish() 54 55 mockDownsampler := NewMockDownsampler(ctrl) 56 57 // Sleep until test has finished 58 var wg sync.WaitGroup 59 wg.Add(1) 60 defer wg.Done() 61 62 asyncDownsampler := NewAsyncDownsampler(func() (Downsampler, error) { 63 wg.Wait() 64 return mockDownsampler, nil 65 }, nil) 66 require.NotNil(t, asyncDownsampler) 67 68 _, err := asyncDownsampler.NewMetricsAppender() 69 assert.EqualError(t, err, errDownsamplerUninitialized.Error()) 70 } 71 72 func TestAsyncDownsamplerInitialized(t *testing.T) { 73 ctrl := gomock.NewController(t) 74 defer ctrl.Finish() 75 76 mockMetricsAppender := NewMockMetricsAppender(ctrl) 77 78 mockDownsampler := NewMockDownsampler(ctrl) 79 mockDownsampler.EXPECT().NewMetricsAppender().Return(mockMetricsAppender, nil) 80 81 done := make(chan struct{}, 1) 82 asyncDownsampler := NewAsyncDownsampler(func() (Downsampler, error) { 83 return mockDownsampler, nil 84 }, done) 85 require.NotNil(t, asyncDownsampler) 86 // Wait for session to be done initializing 87 <-done 88 89 metricsAppender, err := asyncDownsampler.NewMetricsAppender() 90 assert.NoError(t, err) 91 concreteMetricsAppender, ok := metricsAppender.(*MockMetricsAppender) 92 require.True(t, ok) 93 assert.True(t, concreteMetricsAppender == mockMetricsAppender) 94 }