MCPcopy Index your code
hub / github.com/tailscale/tailscale / TestRegisterRateLimited

Function TestRegisterRateLimited

control/controlclient/controlclient_test.go:412–519  ·  view source on GitHub ↗

TestRegisterRateLimited verifies that the client correctly handles 429 responses to registration requests by parsing the Retry-After header and returning a rateLimitError.

(t *testing.T)

Source from the content-addressed store, hash-verified

410// responses to registration requests by parsing the Retry-After header
411// and returning a rateLimitError.
412func TestRegisterRateLimited(t *testing.T) {
413 bakedroots.ResetForTest(t, tlstest.TestRootCA())
414
415 bus := eventbustest.NewBus(t)
416
417 controlLn, err := tls.Listen("tcp", "127.0.0.1:0", tlstest.ControlPlane.ServerTLSConfig())
418 if err != nil {
419 t.Fatal(err)
420 }
421 defer controlLn.Close()
422
423 var registerAttempts atomic.Int64
424 tc := &testcontrol.Server{
425 Logf: tstest.WhileTestRunningLogger(t),
426 MaybeRateLimitRegister: func() (bool, string, string) {
427 if registerAttempts.Add(1) == 1 {
428 return true, "30", "try again later"
429 }
430 return false, "", ""
431 },
432 }
433 controlSrv := &http.Server{
434 Handler: tc,
435 ErrorLog: logger.StdLogger(t.Logf),
436 }
437 go controlSrv.Serve(controlLn)
438
439 const fakeControlIP = "1.2.3.4"
440
441 dialer := &tsdial.Dialer{}
442 dialer.SetNetMon(netmon.NewStatic())
443 dialer.SetBus(bus)
444 dialer.SetSystemDialerForTest(func(ctx context.Context, network, addr string) (net.Conn, error) {
445 host, _, err := net.SplitHostPort(addr)
446 if err != nil {
447 return nil, fmt.Errorf("SplitHostPort(%q): %v", addr, err)
448 }
449 var d net.Dialer
450 if host == fakeControlIP {
451 return d.DialContext(ctx, network, controlLn.Addr().String())
452 }
453 return nil, fmt.Errorf("unexpected dial to %q", addr)
454 })
455
456 opts := Options{
457 Persist: persist.Persist{},
458 GetMachinePrivateKey: func() (key.MachinePrivate, error) {
459 return key.NewMachine(), nil
460 },
461 ServerURL: "https://controlplane.tstest",
462 Clock: tstime.StdClock{},
463 Hostinfo: &tailcfg.Hostinfo{
464 BackendLogID: "test-backend-log-id",
465 },
466 DiscoPublicKey: key.NewDisco().Public(),
467 Logf: t.Logf,
468 HealthTracker: health.NewTracker(bus),
469 PopBrowserURL: func(url string) {

Callers

nothing calls this directly

Calls 15

ServeMethod · 0.95
SetNetMonMethod · 0.95
SetBusMethod · 0.95
TryLoginMethod · 0.95
ResetForTestFunction · 0.92
TestRootCAFunction · 0.92
NewBusFunction · 0.92
WhileTestRunningLoggerFunction · 0.92
StdLoggerFunction · 0.92
NewStaticFunction · 0.92
NewMachineFunction · 0.92

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…