Skip to content

Commit

Permalink
enhanced telemetry tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslopezf committed Mar 29, 2024
1 parent e49123d commit a76e4f8
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions telemetry/wrapper_test.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,54 @@
package telemetry

import (
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

func TestNow(t *testing.T) {
initTelemetry(true)
// TelemetrySuite is a struct that holds the setup for the telemetry tests.
// It includes a mutex to ensure that tests that depend on the global state
// do not run in parallel, which can cause race conditions and unpredictable results.
type TelemetrySuite struct {
suite.Suite
mu sync.Mutex
}

currentTime := time.Now()
telemetryTime := Now()
// SetupTest is called before each test to reset the global state to a known disabled state.
// This ensures each test starts with the telemetry disabled
func (suite *TelemetrySuite) SetupTest() {
initTelemetry(false)
}

assert.NotEqual(t, time.Time{}, telemetryTime, "Now() should not return zero time when telemetry is enabled")
assert.WithinDuration(t, currentTime, telemetryTime, time.Second, "Now() should be close to current time")
// TestNow tests the Now function when telemetry is enabled and disabled.
func (suite *TelemetrySuite) TestNow() {
suite.mu.Lock()
defer suite.mu.Unlock()

initTelemetry(false)
initTelemetry(true)
telemetryTime := Now()
suite.NotEqual(time.Time{}, telemetryTime, "Now() should not return zero time when telemetry is enabled")

initTelemetry(false)
telemetryTime = Now()
assert.Equal(t, time.Time{}, telemetryTime, "Now() should return zero time when telemetry is disabled")
suite.Equal(time.Time{}, telemetryTime, "Now() should return zero time when telemetry is disabled")
}

func TestIsTelemetryEnabled(t *testing.T) {
// TestIsTelemetryEnabled tests the isTelemetryEnabled function.
func (suite *TelemetrySuite) TestIsTelemetryEnabled() {
suite.mu.Lock()
defer suite.mu.Unlock()

initTelemetry(true)
if !isTelemetryEnabled() {
t.Errorf("isTelemetryEnabled() should return true when globalTelemetryEnabled is set to true")
}
suite.True(isTelemetryEnabled(), "isTelemetryEnabled() should return true when globalTelemetryEnabled is set to true")

initTelemetry(false)
if isTelemetryEnabled() {
t.Errorf("isTelemetryEnabled() should return false when globalTelemetryEnabled is set to false")
}
suite.False(isTelemetryEnabled(), "isTelemetryEnabled() should return false when globalTelemetryEnabled is set to false")
}

// TestTelemetrySuite initiates the test suite.
func TestTelemetrySuite(t *testing.T) {
suite.Run(t, new(TelemetrySuite))
}

0 comments on commit a76e4f8

Please sign in to comment.