(t *testing.T)
| 282 | } |
| 283 | |
| 284 | func TestAuthenticateSingleFlight(t *testing.T) { |
| 285 | f := newFakeGitHub(t) |
| 286 | m := newManager(t, f) |
| 287 | |
| 288 | // Hold the owner inside begin() (browser open blocks) so a concurrent caller |
| 289 | // observes the in-progress flow rather than starting its own. |
| 290 | entered := make(chan struct{}) |
| 291 | release := make(chan struct{}) |
| 292 | var once sync.Once |
| 293 | m.openURL = func(u string) error { |
| 294 | once.Do(func() { close(entered) }) |
| 295 | <-release |
| 296 | return browserGet(u) |
| 297 | } |
| 298 | |
| 299 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 300 | defer cancel() |
| 301 | |
| 302 | ownerDone := make(chan error, 1) |
| 303 | go func() { |
| 304 | _, err := m.Authenticate(ctx, nil) |
| 305 | ownerDone <- err |
| 306 | }() |
| 307 | |
| 308 | <-entered // owner is now mid-flow with status "starting" |
| 309 | |
| 310 | out, err := m.Authenticate(ctx, nil) |
| 311 | require.NoError(t, err) |
| 312 | require.NotNil(t, out.UserAction) |
| 313 | assert.Contains(t, out.UserAction.Message, "already in progress") |
| 314 | |
| 315 | close(release) |
| 316 | require.NoError(t, <-ownerDone) |
| 317 | |
| 318 | assert.Equal(t, "gho_access", waitForToken(t, m)) |
| 319 | // Exactly one authorization happened despite the concurrent callers. |
| 320 | assert.Equal(t, []string{"authorization_code"}, f.recordedGrants()) |
| 321 | } |
nothing calls this directly
no test coverage detected