在测试期间同步测试服务器

时间:2019-07-03 01:06:32

标签: go

摘要:在测试过程中,我遇到了竞争问题,即我的服务器在向客户端提出请求之前无法可靠地准备好服务请求。

如何只在侦听器准备就绪之前阻塞,并且仍然保持可组合的公共API而不要求用户使用BYO net.Listener

我们看到以下错误,因为在client.Do(req)测试函数中调用TestRun之前,在后台旋转(阻塞)服务器的goroutine没有监听。

--- FAIL: TestRun/Server_accepts_HTTP_requests (0.00s)
        /home/matt/repos/admission-control/server_test.go:64: failed to make a request: Get https://127.0.0.1:37877: dial tcp 127.0.0.1:37877: connect: connection refused
  • 在尝试测试我自己的服务器组件的阻止和取消特性时,我没有直接使用httptest.Server
  • 我创建一个httptest.NewUnstartedServer,将其*tls.Config克隆到一个新的http.Server中,然后将其StartTLS()启动,然后关闭它,然后再调用*AdmissionServer.Run()。这还具有给我一个*http.Client并配置了匹配的RootCA的好处。
  • 在这里测试TLS非常重要,因为它公开了仅在TLS环境中的守护进程。
func newTestServer(ctx context.Context, t *testing.T) *httptest.Server {
    testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(w, "OK")
    })

    testSrv := httptest.NewUnstartedServer(testHandler)
    admissionServer, err := NewServer(nil, &noopLogger{})
    if err != nil {
        t.Fatalf("admission server creation failed: %s", err)
        return nil
    }

    // We start the test server, copy its config out, and close it down so we can
    // start our own server. This is because httptest.Server only generates a
    // self-signed TLS config after starting it.
    testSrv.StartTLS()
    admissionServer.srv = &http.Server{
        Addr:      testSrv.Listener.Addr().String(),
        Handler:   testHandler,
        TLSConfig: testSrv.TLS.Clone(),
    }
    testSrv.Close()

    // We need a better synchronization primitive here that doesn't block
    // but allows the underlying listener to be ready before 
    // serving client requests.
    go func() {
        if err := admissionServer.Run(ctx); err != nil {
            t.Fatalf("server returned unexpectedly: %s", err)
        }
    }()

    return testSrv
}
// Test that we can start a minimal AdmissionServer and handle a request.
func TestRun(t *testing.T) {
    testSrv := newTestServer(context.TODO(), t)

    t.Run("Server accepts HTTP requests", func(t *testing.T) {
        client := testSrv.Client()
        req, err := http.NewRequest(http.MethodGet, testSrv.URL, nil)
        if err != nil {
            t.Fatalf("request creation failed: %s", err)
        }

        resp, err := client.Do(req)
        if err != nil {
            t.Fatalf("failed to make a request: %s", err)
        }

    // Later sub-tests will test cancellation propagation, signal handling, etc.

对于后代,这是我们可组合的Run函数,该函数侦听goroutine,然后在for-select中阻止我们的取消和错误通道:

type AdmissionServer struct {
    srv         *http.Server
    logger      log.Logger
    GracePeriod time.Duration
}

func (as *AdmissionServer) Run(ctx context.Context) error {
    sigChan := make(chan os.Signal, 1)
    defer close(sigChan)
    signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)

    // run in goroutine
    errs := make(chan error)
    defer close(errs)
    go func() {
        as.logger.Log(
            "msg", fmt.Sprintf("admission control listening on '%s'", as.srv.Addr),
        )
        if err := as.srv.ListenAndServeTLS("", ""); err != nil && err != http.ErrServerClosed {
            errs <- err
            as.logger.Log(
                "err", err.Error(),
                "msg", "the server exited",
            )
            return
        }
        return
    }()

    // Block indefinitely until we receive an interrupt, cancellation or error
    // signal.
    for {
        select {
        case sig := <-sigChan:
            as.logger.Log(
                "msg", fmt.Sprintf("signal received: %s", sig),
            )
            return as.shutdown(ctx, as.GracePeriod)
        case err := <-errs:
            as.logger.Log(
                "msg", fmt.Sprintf("listener error: %s", err),
            )
            // We don't need to explictly call shutdown here, as
            // *http.Server.ListenAndServe closes the listener when returning an error.
            return err
        case <-ctx.Done():
            as.logger.Log(
                "msg", fmt.Sprintf("cancellation received: %s", ctx.Err()),
            )
            return as.shutdown(ctx, as.GracePeriod)
        }
    }
}

注意:

  • *AdmissionServer有一个(简单的)构造函数:为简洁起见,我省略了它。 AdmissionServer是可组合的,并且接受*http.Server,以便可以轻松地将其插入现有应用程序中。
  • 我们从中创建包装的http.Server类型的监听器本身并没有提供任何方法来判断其是否在监听;充其量,我们可以尝试再次侦听并捕获错误(例如,端口已绑定到另一个侦听器),由于net程序包没有为此暴露有用的类型错误,因此它似乎并不健壮。

1 个答案:

答案 0 :(得分:2)

作为初始化过程的一部分,您可以在启动测试套件之前尝试连接到服务器

例如,我通常在测试中具有以下功能:

// waitForServer attempts to establish a TCP connection to localhost:<port>
// in a given amount of time. It returns upon a successful connection; 
// ptherwise exits with an error.
func waitForServer(port string) {
    backoff := 50 * time.Millisecond

    for i := 0; i < 10; i++ {
        conn, err := net.DialTimeout("tcp", ":"+port, 1*time.Second)
        if err != nil {
            time.Sleep(backoff)
            continue
        }
        err = conn.Close()
        if err != nil {
            log.Fatal(err)
        }
        return
    }
    log.Fatalf("Server on port %s not up after 10 attempts", port)
}

然后在我的TestMain()中,我这样做:

func TestMain(m *testing.M) {
    go startServer()
    waitForServer(serverPort)

    // run the suite
    os.Exit(m.Run())
}