Skip to content

Commit 2523e2a

Browse files
test timing
1 parent 122aba2 commit 2523e2a

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

internal/integration/integration_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,6 @@ func TestSubscriptionClose(t *testing.T) {
252252
_ = `# @genqlient
253253
subscription countClose { countClose }`
254254

255-
ctx := context.Background()
256-
server := server.RunServer()
257-
defer server.Close()
258-
259255
cases := []struct {
260256
name string
261257
unsub bool
@@ -272,6 +268,12 @@ func TestSubscriptionClose(t *testing.T) {
272268

273269
for _, tc := range cases {
274270
t.Run(tc.name, func(t *testing.T) {
271+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
272+
defer cancel()
273+
274+
server := server.RunServer()
275+
defer server.Close()
276+
275277
wsClient := newRoundtripWebSocketClient(t, server.URL)
276278

277279
_, err := wsClient.Start(ctx)

internal/integration/server/server.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,32 +157,38 @@ func (m mutationResolver) CreateUser(ctx context.Context, input NewUser) (*User,
157157
return &newUser, nil
158158
}
159159

160-
func countTo(stopCount int) (<-chan int, error) {
160+
func countTo(ctx context.Context, stopCount int) (<-chan int, error) {
161161
respChan := make(chan int, 1)
162162
go func(respChan chan int) {
163163
defer close(respChan)
164164
for counter := range stopCount {
165-
respChan <- counter
166-
time.Sleep(100 * time.Millisecond)
165+
select {
166+
case <-ctx.Done():
167+
fmt.Println("ctx done:", ctx.Err())
168+
return
169+
default:
170+
respChan <- counter
171+
time.Sleep(100 * time.Millisecond)
172+
}
167173
}
168174
}(respChan)
169175
return respChan, nil
170176
}
171177

172178
func (s *subscriptionResolver) Count(ctx context.Context) (<-chan int, error) {
173-
return countTo(10)
179+
return countTo(ctx, 10)
174180
}
175181

176182
func (s *subscriptionResolver) CountAuthorized(ctx context.Context) (<-chan int, error) {
177183
if getAuthToken(ctx) != "authorized-user-token" {
178184
return nil, fmt.Errorf("unauthorized")
179185
}
180186

181-
return countTo(10)
187+
return countTo(ctx, 10)
182188
}
183189

184190
func (s *subscriptionResolver) CountClose(ctx context.Context) (<-chan int, error) {
185-
return countTo(1000)
191+
return countTo(ctx, 1000)
186192
}
187193

188194
const AuthKey = "authToken"

0 commit comments

Comments
 (0)