Skip to content

Commit

Permalink
Add some unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
damemi committed Jun 14, 2023
1 parent ce23f6a commit 41f1009
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
5 changes: 4 additions & 1 deletion exporter/collector/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ func NewGoogleCloudMetricsExporter(
}

if cfg.MetricConfig.WALConfig != nil {
mExp.wal = &exporterWAL{}
_, _, err = mExp.setupWAL()
if err != nil {
return nil, err
Expand All @@ -240,6 +239,10 @@ func (me *MetricsExporter) setupWAL() (uint64, uint64, error) {
return 0, 0, err
}

Check warning on line 240 in exporter/collector/metrics.go

View check run for this annotation

Codecov / codecov/patch

exporter/collector/metrics.go#L239-L240

Added lines #L239 - L240 were not covered by tests

if me.wal == nil {
me.wal = &exporterWAL{}
}

walPath := filepath.Join(me.cfg.MetricConfig.WALConfig.Directory, "gcp_metrics_wal")
me.wal.path = walPath
metricWal, err := wal.Open(walPath, &wal.Options{LogFormat: 1})
Expand Down
51 changes: 51 additions & 0 deletions exporter/collector/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package collector
import (
"fmt"
"math"
"os"
"testing"
"time"

Expand Down Expand Up @@ -2033,3 +2034,53 @@ func TestInstrumentationScopeToLabelsUTF8(t *testing.T) {
})
}
}

func TestSetupWAL(t *testing.T) {
tmpDir, _ := os.MkdirTemp("", "wal-test-")
mExp := &MetricsExporter{
cfg: Config{
MetricConfig: MetricConfig{
WALConfig: &WALConfig{
Directory: tmpDir,
MaxBackoff: time.Duration(2112 * time.Second),
},
},
},
}
firstIndex, lastIndex, err := mExp.setupWAL()
require.NoError(t, err)
require.Zero(t, firstIndex)
require.Zero(t, lastIndex)
require.Equal(t, time.Duration(2112*time.Second), mExp.wal.maxBackoff)

mExp.wal.Write(uint64(1), []byte("foo"))
mExp.wal.Write(uint64(2), []byte("bar"))
firstIndex, lastIndex, err = mExp.setupWAL()
require.NoError(t, err)
require.Equal(t, uint64(1), firstIndex)
require.Equal(t, uint64(2), lastIndex)
require.Equal(t, time.Duration(2112*time.Second), mExp.wal.maxBackoff)
}

func TestCloseWAL(t *testing.T) {
tmpDir, _ := os.MkdirTemp("", "wal-test-")
mExp := &MetricsExporter{
cfg: Config{
MetricConfig: MetricConfig{
WALConfig: &WALConfig{
Directory: tmpDir,
MaxBackoff: time.Duration(2112 * time.Second),
},
},
},
}
_, _, err := mExp.setupWAL()
require.NoError(t, err)

err = mExp.closeWAL()
require.NoError(t, err)

// check multiple closes are safe
err = mExp.closeWAL()
require.NoError(t, err)
}

0 comments on commit 41f1009

Please sign in to comment.