Пример #1
0
func BenchmarkLimitListener(b *testing.B) {
	var wg sync.WaitGroup
	wg.Add(b.N)

	l := httpd.LimitListener(&fakeListener{}, b.N)
	for i := 0; i < b.N; i++ {
		go func() {
			c, err := l.Accept()
			if err != nil {
				b.Fatal(err)
			}
			c.Close()
			wg.Done()
		}()
	}
	wg.Wait()
}
Пример #2
0
func TestLimitListener(t *testing.T) {
	conns := make(chan net.Conn, 2)
	l := httpd.LimitListener(&fakeListener{
		AcceptFn: func() (net.Conn, error) {
			select {
			case c := <-conns:
				if c != nil {
					return c, nil
				}
			default:
			}
			return nil, io.EOF
		},
	}, 1)
	c1, c2 := &fakeConn{}, &fakeConn{}
	conns <- c1
	conns <- c2

	var c net.Conn
	var err error
	if c, err = l.Accept(); err != nil {
		t.Fatalf("expected accept to succeed: %s", err)
	}

	if _, err = l.Accept(); err != io.EOF {
		t.Fatalf("expected eof, got %s", err)
	} else if !c2.closed {
		t.Fatalf("expected connection to be automatically closed")
	}
	c.Close()

	conns <- &fakeConn{}
	if _, err = l.Accept(); err != nil {
		t.Fatalf("expeced accept to succeed: %s", err)
	}
}