Skip to content

Commit 3cfa478

Browse files
committed
Replaces direct time calls with mockable clock
Enables consistent test results by substituting system time with a mockable function Improves reliability and maintainability of tests
1 parent 5e1e8a0 commit 3cfa478

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

pkg/helpers/aws.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ func CloudWatchLogsSubscribe(ctx context.Context, cw cloudwatchlogsiface.CloudWa
348348
return r, nil
349349
}
350350

351+
var TimeNow = time.Now // defaults to the real clock
352+
351353
func CloudWatchLogsStream(ctx context.Context, cw cloudwatchlogsiface.CloudWatchLogsAPI, w io.WriteCloser, group, stream string, opts structs.LogsOptions) error {
352354
defer w.Close()
353355

@@ -364,10 +366,10 @@ func CloudWatchLogsStream(ctx context.Context, cw cloudwatchlogsiface.CloudWatch
364366
var start int64
365367

366368
if opts.Since != nil {
367-
start = time.Now().UTC().Add((*opts.Since) * -1).UnixMilli()
369+
start = TimeNow().UTC().Add((*opts.Since) * -1).UnixMilli()
368370
req.StartTime = aws.Int64(start)
369371
} else {
370-
req.StartTime = aws.Int64(time.Now().UTC().Add(-1 * time.Hour).UnixMilli())
372+
req.StartTime = aws.Int64(TimeNow().UTC().Add(-1 * time.Hour).UnixMilli())
371373
}
372374

373375
if stream != "" {

provider/aws/apps_test.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88
"time"
99

10+
"github.com/convox/rack/pkg/helpers"
1011
"github.com/convox/rack/pkg/options"
1112
"github.com/convox/rack/pkg/structs"
1213
"github.com/convox/rack/pkg/test/awsutil"
@@ -108,22 +109,32 @@ func TestAppLogs(t *testing.T) {
108109
}
109110

110111
func TestAppLogsSince(t *testing.T) {
112+
oldNow := helpers.TimeNow
113+
helpers.TimeNow = func() time.Time {
114+
return time.Date(2025, 4, 9, 22, 0, 0, 0, time.UTC)
115+
}
116+
defer func() { helpers.TimeNow = oldNow }()
117+
118+
mockedNow := helpers.TimeNow()
119+
wantThen := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
120+
121+
since := mockedNow.Sub(wantThen)
122+
111123
provider := StubAwsProvider(
112124
cycleListAppStackResources,
113125
cycleLogFilterLogEventsLimit1,
114126
cycleLogFilterLogEventsLimit2,
115127
)
116128
defer provider.Close()
117129

118-
buf := &bytes.Buffer{}
119-
120130
r, err := provider.AppLogs("httpd", structs.LogsOptions{
121131
Follow: options.Bool(false),
122132
Filter: options.String("test"),
123133
Prefix: options.Bool(true),
124-
Since: options.Duration(time.Since(time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC))),
134+
Since: options.Duration(since),
125135
})
126136

137+
buf := &bytes.Buffer{}
127138
io.Copy(buf, r)
128139

129140
assert.NoError(t, err)
@@ -339,9 +350,9 @@ var cycleLogFilterLogEventsLimit1 = awsutil.Cycle{
339350
],
340351
"searchedLogStreams": [
341352
{
342-
"searchedCompletely": false,
353+
"searchedCompletely": false,
343354
"logStreamName": "stream1"
344-
},
355+
},
345356
{
346357
"searchedCompletely": false,
347358
"logStreamName": "stream2"
@@ -385,9 +396,9 @@ var cycleLogFilterLogEventsLimit2 = awsutil.Cycle{
385396
],
386397
"searchedLogStreams": [
387398
{
388-
"searchedCompletely": true,
399+
"searchedCompletely": true,
389400
"logStreamName": "stream1"
390-
},
401+
},
391402
{
392403
"searchedCompletely": false,
393404
"logStreamName": "stream2"
@@ -436,9 +447,9 @@ var cycleLogFilterLogEvents1 = awsutil.Cycle{
436447
],
437448
"searchedLogStreams": [
438449
{
439-
"searchedCompletely": false,
450+
"searchedCompletely": false,
440451
"logStreamName": "stream1"
441-
},
452+
},
442453
{
443454
"searchedCompletely": false,
444455
"logStreamName": "stream2"
@@ -486,9 +497,9 @@ var cycleLogFilterLogEvents2 = awsutil.Cycle{
486497
],
487498
"searchedLogStreams": [
488499
{
489-
"searchedCompletely": true,
500+
"searchedCompletely": true,
490501
"logStreamName": "stream1"
491-
},
502+
},
492503
{
493504
"searchedCompletely": false,
494505
"logStreamName": "stream2"

0 commit comments

Comments
 (0)