Kong / go-pdk

Write Kong plugins in Go! 🦍
https://pkg.go.dev/github.com/Kong/go-pdk
Apache License 2.0
153 stars 49 forks source link

Test package Race detected Warning when DoHttps #154

Closed barockok closed 1 year ago

barockok commented 1 year ago

I got race detected when trying sample code to try testing package

type PluginConfig struct {
}

func (cfg PluginConfig) Access(pdk *pdk.PDK) {
    h, _ := pdk.Request.GetHeader("X-Hi")
    pdk.Response.SetHeader("x-hello-from-go", h)
}
func TestPlugin(t *testing.T) {
    env, err := test.New(t, test.Request{
        Method: "GET",
        Url:    "http://example.com?q=search&x=9",
        Headers: map[string][]string{
            "X-Hi": {"Go says Hi!"},
        },
    })
    assert.NoError(t, err)

    env.DoHttps(&PluginConfig{})
    assert.Equal(t, 200, env.ClientRes.Status)
    assert.Equal(t, "Go says Hi!", env.ClientRes.Headers.Get("x-hello-from-go"))
}

the race error shown

Running tool: /opt/homebrew/opt/go/libexec/bin/go test -timeout 5s -run ^TestPlugin$ github.com/barockok/kong-warden -race -v

=== RUN   TestPlugin
    /Users/zidni.mubarok/Documents/Barock-Nitip/Code/simple-abac/docker/kong/warden/test.go:579: Access
==================
WARNING: DATA RACE
Write at 0x00c0001c0088 by goroutine 8:
  github.com/Kong/go-pdk/test.(*TestEnv).DoHttp()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/test/test.go:635 +0x104
  github.com/Kong/go-pdk/test.(*TestEnv).DoHttps()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/test/test.go:645 +0x48
  github.com/barockok/kong-warden.TestPlugin()
      /Users/zidni.mubarok/Documents/Barock-Nitip/Code/simple-abac/docker/kong/warden/warden_test.go:72 +0x194
  testing.tRunner()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1446 +0x188
  testing.(*T).Run.func1()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1493 +0x40

Previous read at 0x00c0001c0088 by goroutine 9:
  github.com/Kong/go-pdk/test.(*TestEnv).IsRunning()
      <autogenerated>:1 +0x40
  github.com/Kong/go-pdk/bridge/bridgetest.MockFunc.func1()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/bridge/bridgetest/bridgetest.go:141 +0xc4

Goroutine 8 (running) created at:
  testing.(*T).Run()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1493 +0x55c
  testing.runTests.func1()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1846 +0x90
  testing.tRunner()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1446 +0x188
  testing.runTests()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1844 +0x6c0
  testing.(*M).Run()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1726 +0x870
  main.main()
      _testmain.go:51 +0x2fc

Goroutine 9 (running) created at:
  github.com/Kong/go-pdk/bridge/bridgetest.MockFunc()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/bridge/bridgetest/bridgetest.go:118 +0xe8
  github.com/Kong/go-pdk/test.New()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/test/test.go:206 +0x2b4
  github.com/barockok/kong-warden.TestPlugin()
      /Users/zidni.mubarok/Documents/Barock-Nitip/Code/simple-abac/docker/kong/warden/warden_test.go:63 +0x154
  testing.tRunner()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1446 +0x188
  testing.(*T).Run.func1()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1493 +0x40
==================
==================
WARNING: DATA RACE
Write at 0x00c0001c00b8 by goroutine 8:
  github.com/Kong/go-pdk/test.(*Response).merge()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/test/test.go:156 +0x60
  github.com/Kong/go-pdk/test.(*TestEnv).DoResponse()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/test/test.go:593 +0xcc
  github.com/Kong/go-pdk/test.(*TestEnv).DoHttp()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/test/test.go:636 +0x158
  github.com/Kong/go-pdk/test.(*TestEnv).DoHttps()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/test/test.go:645 +0x48
  github.com/barockok/kong-warden.TestPlugin()
      /Users/zidni.mubarok/Documents/Barock-Nitip/Code/simple-abac/docker/kong/warden/warden_test.go:72 +0x194
  testing.tRunner()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1446 +0x188
  testing.(*T).Run.func1()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1493 +0x40

Previous read at 0x00c0001c00b8 by goroutine 9:
  github.com/Kong/go-pdk/test.(*TestEnv).IsRunning()
      <autogenerated>:1 +0x40
  github.com/Kong/go-pdk/bridge/bridgetest.MockFunc.func1()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/bridge/bridgetest/bridgetest.go:141 +0xc4

Goroutine 8 (running) created at:
  testing.(*T).Run()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1493 +0x55c
  testing.runTests.func1()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1846 +0x90
  testing.tRunner()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1446 +0x188
  testing.runTests()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1844 +0x6c0
  testing.(*M).Run()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1726 +0x870
  main.main()
      _testmain.go:51 +0x2fc

Goroutine 9 (running) created at:
  github.com/Kong/go-pdk/bridge/bridgetest.MockFunc()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/bridge/bridgetest/bridgetest.go:118 +0xe8
  github.com/Kong/go-pdk/test.New()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/test/test.go:206 +0x2b4
  github.com/barockok/kong-warden.TestPlugin()
      /Users/zidni.mubarok/Documents/Barock-Nitip/Code/simple-abac/docker/kong/warden/warden_test.go:63 +0x154
  testing.tRunner()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1446 +0x188
  testing.(*T).Run.func1()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1493 +0x40
==================
==================
WARNING: DATA RACE
Write at 0x00c0001c00c0 by goroutine 8:
  github.com/Kong/go-pdk/test.(*Response).merge()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/test/test.go:159 +0x90
  github.com/Kong/go-pdk/test.(*TestEnv).DoResponse()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/test/test.go:593 +0xcc
  github.com/Kong/go-pdk/test.(*TestEnv).DoHttp()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/test/test.go:636 +0x158
  github.com/Kong/go-pdk/test.(*TestEnv).DoHttps()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/test/test.go:645 +0x48
  github.com/barockok/kong-warden.TestPlugin()
      /Users/zidni.mubarok/Documents/Barock-Nitip/Code/simple-abac/docker/kong/warden/warden_test.go:72 +0x194
  testing.tRunner()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1446 +0x188
  testing.(*T).Run.func1()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1493 +0x40

Previous read at 0x00c0001c00c0 by goroutine 9:
  github.com/Kong/go-pdk/test.(*TestEnv).IsRunning()
      <autogenerated>:1 +0x40
  github.com/Kong/go-pdk/bridge/bridgetest.MockFunc.func1()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/bridge/bridgetest/bridgetest.go:141 +0xc4

Goroutine 8 (running) created at:
  testing.(*T).Run()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1493 +0x55c
  testing.runTests.func1()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1846 +0x90
  testing.tRunner()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1446 +0x188
  testing.runTests()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1844 +0x6c0
  testing.(*M).Run()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1726 +0x870
  main.main()
      _testmain.go:51 +0x2fc

Goroutine 9 (running) created at:
  github.com/Kong/go-pdk/bridge/bridgetest.MockFunc()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/bridge/bridgetest/bridgetest.go:118 +0xe8
  github.com/Kong/go-pdk/test.New()
      /Users/zidni.mubarok/.go/pkg/mod/github.com/!kong/go-pdk@v0.8.0/test/test.go:206 +0x2b4
  github.com/barockok/kong-warden.TestPlugin()
      /Users/zidni.mubarok/Documents/Barock-Nitip/Code/simple-abac/docker/kong/warden/warden_test.go:63 +0x154
  testing.tRunner()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1446 +0x188
  testing.(*T).Run.func1()
      /opt/homebrew/opt/go/libexec/src/testing/testing.go:1493 +0x40
==================
    /Users/zidni.mubarok/Documents/Barock-Nitip/Code/simple-abac/docker/kong/warden/testing.go:1319: race detected during execution of test
--- FAIL: TestPlugin (0.00s)
=== CONT
    /Users/zidni.mubarok/Documents/Barock-Nitip/Code/simple-abac/docker/kong/warden/testing.go:1319: race detected during execution of test
FAIL
FAIL    github.com/barockok/kong-warden 0.223s

system info :

go-pdk : version v0.8.0 go : version go1.20.7

StarlightIbuki commented 1 year ago

I've skimmed the code. There is a race condition in the mocking test package. Here the go routine tries to read the status that is written by the testing (main) routine. I'm writing a fix to switch to use channel.