Skip to content

Commit 00f23cf

Browse files
cstocktonChris Stockton
andauthored
chore: add 100% test coverage to apitask package (#2146)
Quick PR to button up the apitask package. Co-authored-by: Chris Stockton <[email protected]>
1 parent 2c8ea61 commit 00f23cf

File tree

3 files changed

+162
-20
lines changed

3 files changed

+162
-20
lines changed

internal/api/apitask/apitask.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Package apitask provides a background execution context for background work
2+
// that limits the execution time to the current request.
13
package apitask
24

35
import (

internal/api/apitask/apitask_test.go

Lines changed: 143 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package apitask
22

33
import (
44
"context"
5+
"errors"
6+
"fmt"
7+
"net/http"
58
"sync/atomic"
69
"testing"
710
"time"
@@ -22,31 +25,34 @@ func taskFn(typ string, fn func(context.Context) error) Task {
2225
return &taskFunc{typ: typ, fn: fn}
2326
}
2427

25-
func TestRequestWorker(t *testing.T) {
28+
func TestContext(t *testing.T) {
2629
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
2730
defer cancel()
2831

29-
t.Run("RunTasks", func(t *testing.T) {
30-
{
31-
rw, ok := from(ctx)
32-
require.False(t, ok, "request worker must not found in context")
33-
require.Nil(t, rw, "request worker must be nil")
34-
}
32+
{
33+
rw, ok := from(ctx)
34+
require.False(t, ok, "request worker must not found in context")
35+
require.Nil(t, rw, "request worker must be nil")
36+
}
3537

36-
withCtx := With(ctx)
37-
{
38-
rw, ok := from(withCtx)
39-
require.True(t, ok, "request worker not found in context")
40-
require.NotNil(t, rw, "request worker was nil")
41-
42-
withCtxDupe := With(withCtx)
43-
sameRw, ok := from(withCtxDupe)
44-
require.True(t, ok, "request worker not found in context")
45-
require.True(t, rw == sameRw, "request worker should be created only once")
46-
}
47-
})
38+
withCtx := With(ctx)
39+
{
40+
rw, ok := from(withCtx)
41+
require.True(t, ok, "request worker not found in context")
42+
require.NotNil(t, rw, "request worker was nil")
43+
44+
withCtxDupe := With(withCtx)
45+
sameRw, ok := from(withCtxDupe)
46+
require.True(t, ok, "request worker not found in context")
47+
require.True(t, rw == sameRw, "request worker should be created only once")
48+
}
49+
}
4850

49-
t.Run("RunTasks", func(t *testing.T) {
51+
func TestRun(t *testing.T) {
52+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
53+
defer cancel()
54+
55+
t.Run("Success", func(t *testing.T) {
5056
withCtx := With(ctx)
5157

5258
calls := new(atomic.Int64)
@@ -68,4 +74,121 @@ func TestRequestWorker(t *testing.T) {
6874
require.Equal(t, expCalls, gotCalls)
6975
}
7076
})
77+
t.Run("Failure", func(t *testing.T) {
78+
79+
// exp no errors when async
80+
t.Run("WhenAsync", func(t *testing.T) {
81+
withCtx := With(ctx)
82+
83+
calls := new(atomic.Int64)
84+
expCalls := 0
85+
sentinel := errors.New("sentinel")
86+
for range 16 {
87+
expCalls++
88+
task := taskFn("test.run", func(ctx context.Context) error {
89+
calls.Add(1)
90+
return sentinel
91+
})
92+
err := Run(withCtx, task)
93+
require.NoError(t, err)
94+
}
95+
96+
{
97+
Wait(withCtx)
98+
99+
gotCalls := int(calls.Load())
100+
require.Equal(t, expCalls, gotCalls)
101+
}
102+
})
103+
104+
// exp errors when sync
105+
t.Run("WhenSync", func(t *testing.T) {
106+
withCtx := ctx
107+
108+
calls := new(atomic.Int64)
109+
expCalls := 0
110+
sentinel := errors.New("sentinel")
111+
for range 16 {
112+
expCalls++
113+
task := taskFn("test.run", func(ctx context.Context) error {
114+
calls.Add(1)
115+
return sentinel
116+
})
117+
err := Run(withCtx, task)
118+
require.Error(t, err)
119+
}
120+
121+
{
122+
Wait(withCtx)
123+
124+
gotCalls := int(calls.Load())
125+
require.Equal(t, expCalls, gotCalls)
126+
}
127+
})
128+
})
129+
}
130+
131+
func TestMiddleware(t *testing.T) {
132+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
133+
defer cancel()
134+
135+
t.Run("Success", func(t *testing.T) {
136+
var errs []error
137+
hrFunc := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
138+
for i := range 10 {
139+
typ := fmt.Sprintf("test-task-%v", i)
140+
task := taskFn(typ, func(ctx context.Context) error {
141+
return nil
142+
})
143+
144+
err := Run(r.Context(), task)
145+
errs = append(errs, err)
146+
}
147+
})
148+
hr := Middleware(hrFunc)
149+
150+
req, err := http.NewRequestWithContext(ctx, "GET", "/", nil)
151+
require.NoError(t, err)
152+
153+
for i := range 10 {
154+
hr.ServeHTTP(nil, req)
155+
require.Equal(t, 10*(i+1), len(errs))
156+
}
157+
for _, e := range errs {
158+
require.NoError(t, e)
159+
}
160+
})
161+
162+
t.Run("Failure", func(t *testing.T) {
163+
var errs []error
164+
hrFunc := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
165+
for i := range 10 {
166+
typ := fmt.Sprintf("test-task-%v", i)
167+
task := taskFn(typ, func(ctx context.Context) error {
168+
return nil
169+
})
170+
err := Run(r.Context(), task)
171+
errs = append(errs, err)
172+
}
173+
})
174+
hr := Middleware(hrFunc)
175+
176+
ctx = With(ctx)
177+
req, err := http.NewRequestWithContext(ctx, "GET", "/", nil)
178+
require.NoError(t, err)
179+
180+
hr.ServeHTTP(nil, req)
181+
require.Equal(t, 10, len(errs))
182+
183+
for _, e := range errs {
184+
require.NoError(t, e)
185+
}
186+
187+
errs = nil
188+
hr.ServeHTTP(nil, req)
189+
require.Equal(t, 10, len(errs))
190+
for _, e := range errs {
191+
require.Error(t, e)
192+
}
193+
})
71194
}

internal/api/options_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
78
"github.com/supabase/auth/internal/conf"
9+
"github.com/supabase/auth/internal/e2e"
10+
"github.com/supabase/auth/internal/mailer"
811
)
912

1013
func TestNewLimiterOptions(t *testing.T) {
@@ -28,3 +31,17 @@ func TestNewLimiterOptions(t *testing.T) {
2831
assert.NotNil(t, rl.SSO)
2932
assert.NotNil(t, rl.SAMLAssertion)
3033
}
34+
35+
func TestMailerOptions(t *testing.T) {
36+
globalCfg := e2e.Must(e2e.Config())
37+
conn := e2e.Must(e2e.Conn(globalCfg))
38+
39+
sentinelMailer := mailer.NewMailClient(globalCfg)
40+
mailerOpts := &MailerOptions{MailerClientFunc: func() mailer.MailClient {
41+
return sentinelMailer
42+
}}
43+
a := NewAPIWithVersion(globalCfg, conn, apiTestVersion, mailerOpts)
44+
45+
got := a.mailerClientFunc()
46+
require.Equal(t, sentinelMailer, got)
47+
}

0 commit comments

Comments
 (0)