MCPcopy Create free account
hub / github.com/devnullvoid/pvetui / TestAuthManager_ConcurrentAccess

Function TestAuthManager_ConcurrentAccess

pkg/api/auth_test.go:391–460  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

389}
390
391func TestAuthManager_ConcurrentAccess(t *testing.T) {
392 // Create a test server that simulates slow authentication
393 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
394 if r.URL.Path == testEndpoint {
395 // Add a small delay to test concurrent access
396 time.Sleep(10 * time.Millisecond)
397
398 response := map[string]interface{}{
399 "data": map[string]interface{}{
400 "ticket": "concurrent-ticket",
401 "CSRFPreventionToken": "concurrent-csrf",
402 "username": "testuser",
403 },
404 }
405
406 w.Header().Set("Content-Type", "application/json")
407 _ = json.NewEncoder(w).Encode(response)
408
409 return
410 }
411
412 http.NotFound(w, r)
413 }))
414 defer server.Close()
415
416 httpClient := &HTTPClient{
417 baseURL: server.URL,
418 client: server.Client(),
419 }
420 logger := testutils.NewTestLogger()
421
422 authManager := NewAuthManagerWithPassword(httpClient, "testuser", "testpass", logger)
423
424 // Start multiple goroutines trying to authenticate concurrently
425 const numGoroutines = 5
426 results := make(chan *AuthToken, numGoroutines)
427 errors := make(chan error, numGoroutines)
428
429 for i := 0; i < numGoroutines; i++ {
430 go func() {
431 token, err := authManager.GetValidToken(context.Background())
432 if err != nil {
433 errors <- err
434 } else {
435 results <- token
436 }
437 }()
438 }
439
440 // Collect results
441 var tokens []*AuthToken
442
443 for i := 0; i < numGoroutines; i++ {
444 select {
445 case token := <-results:
446 tokens = append(tokens, token)
447 case err := <-errors:
448 t.Errorf("Unexpected error: %v", err)

Callers

nothing calls this directly

Calls 7

NewTestLoggerFunction · 0.92
HeaderMethod · 0.80
ClientMethod · 0.80
GetValidTokenMethod · 0.80
SetMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected