-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e49123d
commit a76e4f8
Showing
1 changed file
with
35 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |