Skip to content

Commit 00a9a9d

Browse files
Add new TmpPath public function (#306)
* Add new TmpPath public function to return a custom tmp path to the given integration's file. * Fix possible JMX nil pointer
1 parent 93e64c8 commit 00a9a9d

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

jmx/jmx.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/*
22
Package jmx is a library to get metrics through JMX. It requires additional
33
setup. Read https://github.com/newrelic/infra-integrations-sdk#jmx-support for
4-
instructions. */
4+
instructions.
5+
*/
56
package jmx
67

78
import (
@@ -242,8 +243,10 @@ func openConnection(config *connectionConfig) (err error) {
242243
}
243244

244245
go func() {
245-
if err = cmd.Wait(); err != nil {
246-
cmdErrC <- jmxClientError(fmt.Sprintf("nrjmx error: %s [proc-state: %s]", err, cmd.ProcessState))
246+
if cmd != nil {
247+
if err = cmd.Wait(); err != nil {
248+
cmdErrC <- jmxClientError(fmt.Sprintf("nrjmx error: %s [proc-state: %s]", err, cmd.ProcessState))
249+
}
247250
}
248251

249252
cmd = nil

persist/storer.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ func DefaultPath(integrationName string) string {
8383
return file
8484
}
8585

86+
// TmpPath returns the temp folder/filename dir to a Storer for an integration using the given tmpDir or will use the default if not set.
87+
// The name of the file will be the given name of the integration with the .json extension.
88+
func TmpPath(tempDir, integrationName string) string {
89+
dir := tmpIntegrationDir(tempDir)
90+
file := filepath.Join(dir, integrationName+".json")
91+
92+
return file
93+
}
94+
8695
func tmpIntegrationDir(tempDir string) string {
8796
if tempDir == "" {
8897
tempDir = os.TempDir()

persist/storer_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,48 @@ func TestFileStore_DeleteOldEntriesUponSaving(t *testing.T) {
516516
assert.EqualError(t, err, ErrNotFound.Error())
517517
}
518518

519+
func TestFileStoreTmpPath_Save_and_Delete(t *testing.T) {
520+
// Reset global variable affected by other tests to the original
521+
// value used by the library.
522+
SetNow(time.Now)
523+
524+
tempDir := path.Join(t.TempDir(), "custom")
525+
526+
// Given a file storer
527+
// filePath includes integrationsDir conetant because the call to TmpPath sets that subFolder.
528+
filePath := path.Join(tempDir, integrationsDir, "test.json")
529+
ttl := 1 * time.Second
530+
531+
storer, err := NewFileStore(TmpPath(tempDir, "test"), log.NewStdErr(true), ttl)
532+
533+
// When a valid storer contains keys with timestamp greater than TTL
534+
storer.Set("expiredKey", "val")
535+
time.Sleep(ttl + time.Second)
536+
537+
storer.Set("recentKey", "v")
538+
539+
assert.NoError(t, storer.Save())
540+
541+
var val interface{}
542+
543+
_, err = storer.Get("recentKey", &val)
544+
assert.NoError(t, err)
545+
546+
// Expired keys are removed from the storer on saving.
547+
_, err = storer.Get("expiredKey", &val)
548+
assert.EqualError(t, err, ErrNotFound.Error())
549+
550+
storer, err = NewFileStore(filePath, log.NewStdErr(true), ttl)
551+
assert.NoError(t, err)
552+
553+
_, err = storer.Get("recentKey", &val)
554+
assert.NoError(t, err)
555+
556+
// Expired keys have been removed from the file.
557+
_, err = storer.Get("expiredKey", &val)
558+
assert.EqualError(t, err, ErrNotFound.Error())
559+
}
560+
519561
var data = []byte(`{"Timestamp":1650971736,"Value":["1","2","3","4"]}`)
520562

521563
func Benchmark_UnmashalEntireStruct(b *testing.B) {

0 commit comments

Comments
 (0)