server_fortest.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package ipnserver
  4. import (
  5. "context"
  6. "net/http"
  7. "tailscale.com/ipn/ipnauth"
  8. )
  9. // BlockWhileInUseByOtherForTest blocks while the actor can't connect to the server because
  10. // the server is in use by a different actor. It is used in tests only.
  11. func (s *Server) BlockWhileInUseByOtherForTest(ctx context.Context, actor ipnauth.Actor) error {
  12. return s.blockWhileIdentityInUse(ctx, actor)
  13. }
  14. // BlockWhileInUseForTest blocks until the server becomes idle (no active requests),
  15. // or the specified context is done. It returns the context's error if it is done.
  16. // It is used in tests only.
  17. func (s *Server) BlockWhileInUseForTest(ctx context.Context) error {
  18. ready, cleanup := s.zeroReqWaiter.add(&s.mu, ctx)
  19. s.mu.Lock()
  20. busy := len(s.activeReqs) != 0
  21. s.mu.Unlock()
  22. if busy {
  23. <-ready
  24. }
  25. cleanup()
  26. return ctx.Err()
  27. }
  28. // ServeHTTPForTest responds to a single LocalAPI HTTP request.
  29. // The request's context carries the actor that made the request
  30. // and can be created with [NewContextWithActorForTest].
  31. // It is used in tests only.
  32. func (s *Server) ServeHTTPForTest(w http.ResponseWriter, r *http.Request) {
  33. s.serveHTTP(w, r)
  34. }