func StartServerProxy()

in internal/mock/cloudsql.go [288:331]


func StartServerProxy(t *testing.T, i FakeCSQLInstance) func() {

	ln, err := tls.Listen("tcp", ":3307", &tls.Config{
		Certificates: i.certs.serverChain(i.useStandardTLSValidation),
		ClientCAs:    i.certs.clientCAPool(),
		ClientAuth:   tls.RequireAndVerifyClientCert,
	})
	if err != nil {
		t.Fatalf("failed to start listener: %v", err)
	}
	ctx, cancel := context.WithCancel(context.Background())
	go func() {
		for {
			select {
			case <-ctx.Done():
				return
			default:
				conn, aErr := ln.Accept()
				if opErr, ok := aErr.(net.Error); ok {
					if opErr.Timeout() {
						continue
					}
					return
				}
				if aErr == io.EOF {
					return
				}
				if aErr != nil {
					t.Logf("Fake server accept error: %v", aErr)
					return
				}
				_, wErr := conn.Write([]byte(i.name))
				if wErr != nil {
					t.Logf("Fake server write error: %v", wErr)
				}
				_ = conn.Close()
			}
		}
	}()
	return func() {
		cancel()
		_ = ln.Close()
	}
}