Skip to content

Commit

Permalink
Updated RandomFutureTime
Browse files Browse the repository at this point in the history
  • Loading branch information
harshilsharma63 committed Dec 23, 2024
1 parent f249da9 commit fe0df4b
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 5 deletions.
21 changes: 16 additions & 5 deletions loadtest/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,22 @@ func nextPowerOf2(val int) int {
}

// RandomFutureTime returns a random Unix timestamp, in milliseconds, in the interval
// [now+deltaStart, now+maxUntil]
// [now+deltaStart, now+deltaStart+maxUntil]
func RandomFutureTime(deltaStart, maxUntil time.Duration) int64 {
now := time.Now()
diff := maxUntil - deltaStart
offset := deltaStart.Milliseconds() + rand.Int63n(diff.Milliseconds())
randomFutureTime := now.Add(time.Duration(offset) * time.Millisecond)
return randomFutureTime.UnixMilli()
start := now.Add(deltaStart)
start.Add(maxUntil)

// Generate a random duration between 0 and maxUntil
var randomDuration time.Duration
if maxUntil > 0 {
randomDuration = time.Duration(rand.Int63n(int64(maxUntil)))
} else {
randomDuration = time.Duration(0)
}

// Add the random duration to the start time
randomTime := start.Add(randomDuration)

return randomTime.Unix()
}
97 changes: 97 additions & 0 deletions loadtest/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package loadtest

import (
"testing"
"time"
)

func TestRandomFutureTime(t *testing.T) {
deltaStart := 10 * time.Second
maxUntil := 5 * time.Minute

now := time.Now()
start := now.Add(deltaStart)
end := start.Add(maxUntil)

randomTime := RandomFutureTime(deltaStart, maxUntil)

if randomTime < start.Unix() || randomTime > end.Unix() {
t.Errorf("RandomFutureTime() = %v, want between %v and %v", randomTime, start.Unix(), end.Unix())
}
}

func TestRandomFutureTimeZeroDuration(t *testing.T) {
deltaStart := 0 * time.Second
maxUntil := 0 * time.Second

now := time.Now()
expectedTime := now.Unix()

randomTime := RandomFutureTime(deltaStart, maxUntil)

if randomTime != expectedTime {
t.Errorf("RandomFutureTime() = %v, want %v", randomTime, expectedTime)
}
}

func TestRandomFutureTimeNegativeDuration(t *testing.T) {
deltaStart := -10 * time.Second
maxUntil := -5 * time.Minute

now := time.Now()
start := now.Add(deltaStart)
end := start.Add(maxUntil)

randomTime := RandomFutureTime(deltaStart, maxUntil)

if randomTime < end.Unix() || randomTime > start.Unix() {
t.Errorf("RandomFutureTime() = %v, want between %v and %v", randomTime, end.Unix(), start.Unix())
}
}

func TestRandomFutureTimeMaxUntilZero(t *testing.T) {
deltaStart := 10 * time.Second
maxUntil := 0 * time.Second

now := time.Now()
expectedTime := now.Add(deltaStart).Unix()

randomTime := RandomFutureTime(deltaStart, maxUntil)

if randomTime != expectedTime {
t.Errorf("RandomFutureTime() = %v, want %v", randomTime, expectedTime)
}
}

func TestRandomFutureTimeDeltaStartZero(t *testing.T) {
deltaStart := 0 * time.Second
maxUntil := 5 * time.Minute

now := time.Now()
start := now
end := start.Add(maxUntil)

randomTime := RandomFutureTime(deltaStart, maxUntil)

if randomTime < start.Unix() || randomTime > end.Unix() {
t.Errorf("RandomFutureTime() = %v, want between %v and %v", randomTime, start.Unix(), end.Unix())
}
}

func TestRandomFutureTimeLargeDurations(t *testing.T) {
deltaStart := 100 * time.Hour
maxUntil := 1000 * time.Hour

now := time.Now()
start := now.Add(deltaStart)
end := start.Add(maxUntil)

randomTime := RandomFutureTime(deltaStart, maxUntil)

if randomTime < start.Unix() || randomTime > end.Unix() {
t.Errorf("RandomFutureTime() = %v, want between %v and %v", randomTime, start.Unix(), end.Unix())
}
}

0 comments on commit fe0df4b

Please sign in to comment.