github.com/m3db/m3@v1.5.0/src/x/instrument/process_test.go (about) 1 // Copyright (c) 2017 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 instrument 22 23 import ( 24 "fmt" 25 "io/ioutil" 26 "os" 27 "testing" 28 "time" 29 30 "github.com/fortytw2/leaktest" 31 "github.com/stretchr/testify/assert" 32 "github.com/stretchr/testify/require" 33 "github.com/uber-go/tally" 34 ) 35 36 func TestProcessReporter(t *testing.T) { 37 defer leaktest.Check(t)() 38 39 scope := tally.NewTestScope("", nil) 40 41 countOpen := 32 42 closeFds := func() {} 43 for i := 0; i < countOpen; i++ { 44 tmpFile, err := ioutil.TempFile("", "example") 45 if err != nil { 46 require.FailNow(t, fmt.Sprintf("could not open temp file: %v", err)) 47 } 48 49 // Explicitly close with closeFds call so we can defer the leaktest 50 // and it always executes last 51 currCloseFds := closeFds 52 closeFds = func() { 53 currCloseFds() 54 assert.NoError(t, tmpFile.Close()) 55 assert.NoError(t, os.Remove(tmpFile.Name())) 56 } 57 } 58 59 every := 10 * time.Millisecond 60 61 r := NewProcessReporter(scope, every) 62 require.NoError(t, r.Start()) 63 64 time.Sleep(2 * every) 65 66 _, ok := scope.Snapshot().Gauges()["process.num-fds+"] 67 if !ok { 68 require.FailNow(t, "metric for fds not found after waiting 2x interval") 69 } 70 71 require.NoError(t, r.Stop()) 72 73 closeFds() 74 } 75 76 func TestProcessReporterDoesNotOpenMoreThanOnce(t *testing.T) { 77 r := NewProcessReporter(tally.NoopScope, 10*time.Millisecond) 78 assert.NoError(t, r.Start()) 79 assert.Error(t, r.Start()) 80 assert.NoError(t, r.Stop()) 81 } 82 83 func TestProcessReporterDoesNotCloseMoreThanOnce(t *testing.T) { 84 r := NewProcessReporter(tally.NoopScope, 10*time.Millisecond) 85 assert.NoError(t, r.Start()) 86 assert.NoError(t, r.Stop()) 87 assert.Error(t, r.Stop()) 88 } 89 90 func TestProcessReporterDoesNotOpenWithInvalidReportInterval(t *testing.T) { 91 r := NewProcessReporter(tally.NoopScope, 0) 92 assert.Error(t, r.Start()) 93 }