MCPcopy
hub / github.com/valyala/fasthttp / TestServerMaxConnsPerIPLimit

Function TestServerMaxConnsPerIPLimit

server_test.go:849–925  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

847}
848
849func TestServerMaxConnsPerIPLimit(t *testing.T) {
850 t.Parallel()
851
852 s := &Server{
853 Handler: func(ctx *RequestCtx) {
854 ctx.WriteString("OK") //nolint:errcheck
855 },
856 MaxConnsPerIP: 1,
857 Logger: &testLogger{},
858 }
859
860 ln := fasthttputil.NewInmemoryListener()
861
862 serverCh := make(chan struct{})
863 go func() {
864 fakeLN := &fakeIPListener{
865 Listener: ln,
866 }
867 if err := s.Serve(fakeLN); err != nil {
868 t.Errorf("unexpected error: %v", err)
869 }
870 close(serverCh)
871 }()
872
873 clientCh := make(chan struct{})
874 go func() {
875 c1, err := ln.Dial()
876 if err != nil {
877 t.Errorf("unexpected error: %v", err)
878 }
879 c2, err := ln.Dial()
880 if err != nil {
881 t.Errorf("unexpected error: %v", err)
882 }
883 br := bufio.NewReader(c2)
884 var resp Response
885 if err = resp.Read(br); err != nil {
886 t.Errorf("unexpected error: %v", err)
887 }
888 if resp.StatusCode() != StatusTooManyRequests {
889 t.Errorf("unexpected status code for the second connection: %d. Expecting %d",
890 resp.StatusCode(), StatusTooManyRequests)
891 }
892
893 if _, err = c1.Write([]byte("GET / HTTP/1.1\r\nHost: aa\r\n\r\n")); err != nil {
894 t.Errorf("unexpected error when writing to the first connection: %v", err)
895 }
896 br = bufio.NewReader(c1)
897 if err = resp.Read(br); err != nil {
898 t.Errorf("unexpected error: %v", err)
899 }
900 if resp.StatusCode() != StatusOK {
901 t.Errorf("unexpected status code for the first connection: %d. Expecting %d",
902 resp.StatusCode(), StatusOK)
903 }
904 if string(resp.Body()) != "OK" {
905 t.Errorf("unexpected body for the first connection: %q. Expecting %q", resp.Body(), "OK")
906 }

Callers

nothing calls this directly

Calls 10

ServeMethod · 0.95
DialMethod · 0.95
ReadMethod · 0.95
StatusCodeMethod · 0.95
BodyMethod · 0.95
CloseMethod · 0.95
NewInmemoryListenerFunction · 0.92
NewReaderMethod · 0.80
WriteMethod · 0.65
WriteStringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…