Skip to content

Commit 71e1d07

Browse files
fixup! Add TimeoutWithinContext to mongoutil
1 parent 5590b96 commit 71e1d07

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

internal/mongoutil/mongoutil.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,13 @@ func HostsFromURI(uri string) ([]string, error) {
8989
// TimeoutWithinContext will return true if the provided timeout is nil or if
9090
// it is less than the context deadline. If the context does not have a
9191
// deadline, it will return true.
92-
func TimeoutWithinContext(ctx context.Context, timeout *time.Duration) bool {
93-
if timeout == nil {
94-
return true
95-
}
96-
92+
func TimeoutWithinContext(ctx context.Context, timeout time.Duration) bool {
9793
deadline, ok := ctx.Deadline()
9894
if !ok {
9995
return true
10096
}
10197

10298
ctxTimeout := time.Until(deadline)
10399

104-
return ctxTimeout <= 0 || *timeout < ctxTimeout
100+
return ctxTimeout <= 0 || timeout < ctxTimeout
105101
}

internal/mongoutil/mongoutil_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestValidChangeStreamTimeouts(t *testing.T) {
4141
tests := []struct {
4242
name string
4343
ctxTimeout *time.Duration
44-
timeout *time.Duration
44+
timeout time.Duration
4545
wantTimeout time.Duration
4646
want bool
4747
}{
@@ -54,43 +54,43 @@ func TestValidChangeStreamTimeouts(t *testing.T) {
5454
{
5555
name: "Timeout shorter than context deadline",
5656
ctxTimeout: ptrutil.Ptr(10 * time.Second),
57-
timeout: ptrutil.Ptr(1 * time.Second),
57+
timeout: 1 * time.Second,
5858
want: true,
5959
},
6060
{
6161
name: "Timeout equal to context deadline",
6262
ctxTimeout: ptrutil.Ptr(1 * time.Second),
63-
timeout: ptrutil.Ptr(1 * time.Second),
63+
timeout: 1 * time.Second,
6464
want: false,
6565
},
6666
{
6767
name: "Timeout greater than context deadline",
6868
ctxTimeout: ptrutil.Ptr(1 * time.Second),
69-
timeout: ptrutil.Ptr(10 * time.Second),
69+
timeout: 10 * time.Second,
7070
want: false,
7171
},
7272
{
7373
name: "Context deadline already expired",
7474
ctxTimeout: ptrutil.Ptr(-1 * time.Second),
75-
timeout: ptrutil.Ptr(1 * time.Second),
75+
timeout: 1 * time.Second,
7676
want: true, // *timeout <= 0 branch in code
7777
},
7878
{
7979
name: "Timeout is zero, context deadline in future",
8080
ctxTimeout: ptrutil.Ptr(10 * time.Second),
81-
timeout: ptrutil.Ptr(0 * time.Second),
81+
timeout: 0 * time.Second,
8282
want: true,
8383
},
8484
{
8585
name: "Timeout is negative, context deadline in future",
8686
ctxTimeout: ptrutil.Ptr(10 * time.Second),
87-
timeout: ptrutil.Ptr(-1 * time.Second),
87+
timeout: -1 * time.Second,
8888
want: true,
8989
},
9090
{
9191
name: "Timeout provided, context has no deadline",
9292
ctxTimeout: nil,
93-
timeout: ptrutil.Ptr(1 * time.Second),
93+
timeout: 1 * time.Second,
9494
want: true,
9595
},
9696
}

0 commit comments

Comments
 (0)