go - 如何以编程方式证明此代码具有竞争条件?

标签 go race-condition httpserver

我被告知此代码在设计上存在竞争条件,尽管我尽了最大努力,但我无法证明它确实如此。

func (h *handler) loggingMiddleware(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        h.Log = log.WithFields(log.Fields{
            "method":     r.Method,
            "requestURI": r.RequestURI,
        })
        next.ServeHTTP(w, r)
    })
}

我试过 go build -race,然后运行二进制文件:PORT=3000 ./main 并加载像 hey -n 10000 -c 200 http 这样的创建者://localhost:3000.

此处的其余代码:https://raw.githubusercontent.com/kaihendry/context-youtube/master/5/main.go

或者

type handler struct{ Log *log.Entry }

func New() (h *handler) { return &handler{Log: log.WithFields(log.Fields{"test": "FAIL"})} }

func (h *handler) index(w http.ResponseWriter, r *http.Request) {
    h.Log.Info("index")
    fmt.Fprintf(w, "hello")
}

func (h *handler) about(w http.ResponseWriter, r *http.Request) {
    h.Log.Info("about")
    fmt.Fprintf(w, "about")
}

func main() {
    h := New()
    app := mux.NewRouter()
    app.HandleFunc("/", h.index)
    app.HandleFunc("/about", h.about)
    app.Use(h.loggingMiddleware)
    if err := http.ListenAndServe(":"+os.Getenv("PORT"), app); err != nil {
        log.WithError(err).Fatal("error listening")
    }
}

如果我不能证明它有竞争条件,我可以假设设置 h.Log 是安全的吗?

最佳答案

有一种编程方式,为此你必须做两件事:

  • 重现不雅条件
  • 并在启动 go 工具时使用 -race 选项

最好是为它编写单元测试,这样测试也是可重现的,并且在每次构建/部署时自动运行/检查。

好的,那么如何重现呢?

简单地编写一个启动 2 个 goroutines 的测试,一个调用 index 处理程序,一个调用 about 处理程序,故意不同步,这就是触发种族检测器。

使用 net/http/httptest包以轻松测试处理程序。 httptest.NewServer()给你一个准备好的服务器,“武装”了你传递给它的处理程序。

这是一个简单的测试示例,它将触发竞争条件。将它放在一个名为 main_test.go 的文件中,紧挨着您的 main.go 文件:

package main

import (
    "fmt"
    "net/http"
    "net/http/httptest"
    "sync"
    "testing"

    "github.com/gorilla/mux"
)

func TestRace(t *testing.T) {
    h := New()
    app := mux.NewRouter()
    app.HandleFunc("/", h.index)
    app.HandleFunc("/about", h.about)
    app.Use(h.loggingMiddleware)

    server := httptest.NewServer(app)
    defer server.Close()

    wg := &sync.WaitGroup{}
    for _, path := range []string{"/", "/about"} {
        path := path
        wg.Add(1)
        go func() {
            defer wg.Done()
            req, err := http.NewRequest(http.MethodGet, server.URL+path, nil)
            fmt.Println(server.URL + path)
            if err != nil {
                panic(err)
            }
            res, err := http.DefaultClient.Do(req)
            if err != nil {
                panic(err)
            }
            defer res.Body.Close()
        }()
    }
    wg.Wait()
}

你必须运行它

go test -race

示例输出将是:

http://127.0.0.1:33007/
http://127.0.0.1:33007/about
==================
WARNING: DATA RACE
Write at 0x00c000098030 by goroutine 17:
  play.(*handler).loggingMiddleware.func1()
      /home/icza/tmp/gows/src/play/main.go:16 +0x1ce
  net/http.HandlerFunc.ServeHTTP()
      /usr/local/go/src/net/http/server.go:1964 +0x51
  github.com/gorilla/mux.(*Router).ServeHTTP()
      /home/icza/tmp/gows/src/github.com/gorilla/mux/mux.go:212 +0x12e
  net/http.serverHandler.ServeHTTP()
      /usr/local/go/src/net/http/server.go:2741 +0xc4
  net/http.(*conn).serve()
      /usr/local/go/src/net/http/server.go:1847 +0x80a

Previous write at 0x00c000098030 by goroutine 16:
  play.(*handler).loggingMiddleware.func1()
      /home/icza/tmp/gows/src/play/main.go:16 +0x1ce
  net/http.HandlerFunc.ServeHTTP()
      /usr/local/go/src/net/http/server.go:1964 +0x51
  github.com/gorilla/mux.(*Router).ServeHTTP()
      /home/icza/tmp/gows/src/github.com/gorilla/mux/mux.go:212 +0x12e
  net/http.serverHandler.ServeHTTP()
      /usr/local/go/src/net/http/server.go:2741 +0xc4
  net/http.(*conn).serve()
      /usr/local/go/src/net/http/server.go:1847 +0x80a

Goroutine 17 (running) created at:
  net/http.(*Server).Serve()
      /usr/local/go/src/net/http/server.go:2851 +0x4c5
  net/http/httptest.(*Server).goServe.func1()
      /usr/local/go/src/net/http/httptest/server.go:280 +0xac

Goroutine 16 (running) created at:
  net/http.(*Server).Serve()
      /usr/local/go/src/net/http/server.go:2851 +0x4c5
  net/http/httptest.(*Server).goServe.func1()
      /usr/local/go/src/net/http/httptest/server.go:280 +0xac
==================
2019/01/06 14:58:50  info index                     method=GET requestURI=/
2019/01/06 14:58:50  info about                     method=GET requestURI=/about
--- FAIL: TestRace (0.00s)
    testing.go:771: race detected during execution of test
FAIL
exit status 1
FAIL    play    0.011s

测试失败,表明存在数据竞争。

注意事项:

sync.WaitGroup 的同步是为了等待2个启动的goroutines,而不是为了同步访问处理程序的记录器(这会导致数据竞争)。如果您修复数据竞争,测试将正常运行并结束(等待 2 个启动的测试 goroutines 完成)。

关于go - 如何以编程方式证明此代码具有竞争条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54058007/

相关文章:

Golang 事务退出处理

google-app-engine - 无法存储在数据存储 gcloud 中

error-handling - awk 可以跳过不存在的文件,无竞争吗?

c# - 开源 .NET 嵌入式 web/http 服务器

node.js - 无法运行全局安装的 Node 包

unit-testing - 我应该使用一个接口(interface)来允许模拟吗?

go - 如何在Golang中为空 slice 的第一个索引分配值?

algorithm - 使用 MPI 在 2D 域上并行化

javascript - 需要加载 javascript 库还是只是等待它加载?

python - 错误: No 'Access-Control-Allow-Origin' header in a Python web server