Skip to content

Commit

Permalink
Add new TmpPath public function (#306)
Browse files Browse the repository at this point in the history
* Add new TmpPath public function to return a custom tmp path to the given integration's file.
* Fix possible JMX nil pointer
  • Loading branch information
alvarocabanas committed Feb 13, 2024
1 parent 93e64c8 commit 00a9a9d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
9 changes: 6 additions & 3 deletions jmx/jmx.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
Package jmx is a library to get metrics through JMX. It requires additional
setup. Read https://github.com/newrelic/infra-integrations-sdk#jmx-support for
instructions. */
instructions.
*/
package jmx

import (
Expand Down Expand Up @@ -242,8 +243,10 @@ func openConnection(config *connectionConfig) (err error) {
}

go func() {
if err = cmd.Wait(); err != nil {
cmdErrC <- jmxClientError(fmt.Sprintf("nrjmx error: %s [proc-state: %s]", err, cmd.ProcessState))
if cmd != nil {
if err = cmd.Wait(); err != nil {
cmdErrC <- jmxClientError(fmt.Sprintf("nrjmx error: %s [proc-state: %s]", err, cmd.ProcessState))
}
}

cmd = nil
Expand Down
9 changes: 9 additions & 0 deletions persist/storer.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ func DefaultPath(integrationName string) string {
return file
}

// 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.
// The name of the file will be the given name of the integration with the .json extension.
func TmpPath(tempDir, integrationName string) string {
dir := tmpIntegrationDir(tempDir)
file := filepath.Join(dir, integrationName+".json")

return file
}

func tmpIntegrationDir(tempDir string) string {
if tempDir == "" {
tempDir = os.TempDir()
Expand Down
42 changes: 42 additions & 0 deletions persist/storer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,48 @@ func TestFileStore_DeleteOldEntriesUponSaving(t *testing.T) {
assert.EqualError(t, err, ErrNotFound.Error())
}

func TestFileStoreTmpPath_Save_and_Delete(t *testing.T) {
// Reset global variable affected by other tests to the original
// value used by the library.
SetNow(time.Now)

tempDir := path.Join(t.TempDir(), "custom")

// Given a file storer
// filePath includes integrationsDir conetant because the call to TmpPath sets that subFolder.
filePath := path.Join(tempDir, integrationsDir, "test.json")
ttl := 1 * time.Second

storer, err := NewFileStore(TmpPath(tempDir, "test"), log.NewStdErr(true), ttl)

// When a valid storer contains keys with timestamp greater than TTL
storer.Set("expiredKey", "val")
time.Sleep(ttl + time.Second)

storer.Set("recentKey", "v")

assert.NoError(t, storer.Save())

var val interface{}

_, err = storer.Get("recentKey", &val)
assert.NoError(t, err)

// Expired keys are removed from the storer on saving.
_, err = storer.Get("expiredKey", &val)
assert.EqualError(t, err, ErrNotFound.Error())

storer, err = NewFileStore(filePath, log.NewStdErr(true), ttl)
assert.NoError(t, err)

_, err = storer.Get("recentKey", &val)
assert.NoError(t, err)

// Expired keys have been removed from the file.
_, err = storer.Get("expiredKey", &val)
assert.EqualError(t, err, ErrNotFound.Error())
}

var data = []byte(`{"Timestamp":1650971736,"Value":["1","2","3","4"]}`)

func Benchmark_UnmashalEntireStruct(b *testing.B) {
Expand Down

0 comments on commit 00a9a9d

Please sign in to comment.