Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modelのtestでdata race #773

Closed
H1rono opened this issue Jun 28, 2024 · 2 comments · Fixed by #783
Closed

modelのtestでdata race #773

H1rono opened this issue Jun 28, 2024 · 2 comments · Fixed by #783

Comments

@H1rono
Copy link
Member

H1rono commented Jun 28, 2024

https:/traPtitech/Jomon/actions/runs/9711575171/job/26804587744#step:8:1

テスト失敗時のログ一部
github.com/traPtitech/Jomon		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/ent/comment		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/ent		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/ent/groupbudget		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/ent/enttest		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/ent/file		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/ent/group		coverage: 0.0% of statements
?   	github.com/traPtitech/Jomon/ent/predicate	[no test files]
	github.com/traPtitech/Jomon/ent/hook		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/ent/migrate		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/ent/request		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/ent/requeststatus		coverage: 0.0% of statements
?   	github.com/traPtitech/Jomon/ent/runtime	[no test files]
	github.com/traPtitech/Jomon/ent/requesttarget		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/ent/schema		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/ent/tag		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/ent/transaction		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/ent/transactiondetail		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/ent/user		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/logging		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/model/mock_model		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/storage		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/service		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/storage/mock_storage		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/testutil		coverage: 0.0% of statements
	github.com/traPtitech/Jomon/testutil/random		coverage: 0.0% of statements
==================
WARNING: DATA RACE
Write at 0x000001bb2040 by goroutine 1945:
  entgo.io/ent/dialect/sql/schema.(*Atlas).setupTables()
      /home/runner/go/pkg/mod/entgo.io/ent@v0.[13](https:/traPtitech/Jomon/actions/runs/9711575171/job/26804587744#step:8:14).1/dialect/sql/schema/atlas.go:1060 +0x5be
  entgo.io/ent/dialect/sql/schema.(*Atlas).Create()
      /home/runner/go/pkg/mod/entgo.io/[email protected]/dialect/sql/schema/atlas.go:107 +0x7b
  github.com/traPtitech/Jomon/ent/migrate.Create()
      /home/runner/work/Jomon/Jomon/ent/migrate/migrate.go:54 +0x1e8
  github.com/traPtitech/Jomon/ent/migrate.(*Schema).Create()
      /home/runner/work/Jomon/Jomon/ent/migrate/migrate.go:45 +0xe5
  github.com/traPtitech/Jomon/model.SetupTestEntClient()
      /home/runner/work/Jomon/Jomon/model/model_test.go:41 +0xa71
  github.com/traPtitech/Jomon/model.TestEntRepository_GetTransactions()
      /home/runner/work/Jomon/Jomon/model/transaction_impl_test.go:17 +0x65
  testing.tRunner()
      /opt/hostedtoolcache/go/1.22.3/x64/src/testing/testing.go:1689 +0x21e
  testing.(*T).Run.gowrap1()
      /opt/hostedtoolcache/go/1.22.3/x64/src/testing/testing.go:1742 +0x44

Previous write at 0x000001bb21c0 by goroutine 1945:
  entgo.io/ent/dialect/sql/schema.(*Atlas).setupTables()
      /home/runner/go/pkg/mod/entgo.io/[email protected]/dialect/sql/schema/atlas.go:1060 +0x5be
  entgo.io/ent/dialect/sql/schema.(*Atlas).Create()
      /home/runner/go/pkg/mod/entgo.io/[email protected]/dialect/sql/schema/atlas.go:107 +0x7b
  github.com/traPtitech/Jomon/ent/migrate.Create()
      /home/runner/work/Jomon/Jomon/ent/migrate/migrate.go:54 +0x1e8
  github.com/traPtitech/Jomon/ent/migrate.(*Schema).Create()
      /home/runner/work/Jomon/Jomon/ent/migrate/migrate.go:45 +0xe5
  github.com/traPtitech/Jomon/model.SetupTestEntClient()
      /home/runner/work/Jomon/Jomon/model/model_test.go:41 +0xa71
  github.com/traPtitech/Jomon/model.setup()
      /home/runner/work/Jomon/Jomon/model/repository_test.go:18 +0x79
  github.com/traPtitech/Jomon/model.TestEntRepository_UpdateTransaction()
      /home/runner/work/Jomon/Jomon/model/transaction_impl_test.go:635 +0x52
  testing.tRunner()
      /opt/hostedtoolcache/go/1.22.3/x64/src/testing/testing.go:1689 +0x21e
  testing.(*T).Run.gowrap1()
      /opt/hostedtoolcache/go/1.22.3/x64/src/testing/testing.go:1742 +0x44

Goroutine 1942 (running) created at:
  testing.(*T).Run()
      /opt/hostedtoolcache/go/1.22.3/x64/src/testing/testing.go:1742 +0x825
  testing.runTests.func1()
      /opt/hostedtoolcache/go/1.22.3/x64/src/testing/testing.go:2161 +0x85
  testing.tRunner()
      /opt/hostedtoolcache/go/1.22.3/x64/src/testing/testing.go:1689 +0x21e
  testing.runTests()
      /opt/hostedtoolcache/go/1.22.3/x64/src/testing/testing.go:2[15](https:/traPtitech/Jomon/actions/runs/9711575171/job/26804587744#step:8:16)9 +0x8be
  testing.(*M).Run()
      /opt/hostedtoolcache/go/1.22.3/x64/src/testing/testing.go:2027 +0xf17
  main.main()
      _testmain.go:[16](https:/traPtitech/Jomon/actions/runs/9711575171/job/26804587744#step:8:17)7 +0x2e4

Goroutine 1945 (running) created at:
  testing.(*T).Run()
      /opt/hostedtoolcache/go/1.22.3/x64/src/testing/testing.go:[17](https:/traPtitech/Jomon/actions/runs/9711575171/job/26804587744#step:8:18)42 +0x825
  testing.runTests.func1()
      /opt/hostedtoolcache/go/1.22.3/x64/src/testing/testing.go:2161 +0x85
  testing.tRunner()
      /opt/hostedtoolcache/go/1.22.3/x64/src/testing/testing.go:1689 +0x21e
  testing.runTests()
      /opt/hostedtoolcache/go/1.22.3/x64/src/testing/testing.go:2159 +0x8be
  testing.(*M).Run()
      /opt/hostedtoolcache/go/1.22.3/x64/src/testing/testing.go:[20](https:/traPtitech/Jomon/actions/runs/9711575171/job/26804587744#step:8:21)27 +0xf17
  main.main()
      _testmain.go:167 +0x2e4
==================
--- FAIL: TestEntRepository_GetTransaction (0.31s)
    ...
--- FAIL: TestEntRepository_CreateTransaction (0.26s)
    ...
--- FAIL: TestEntRepository_UpdateTransaction (0.30s)
    ...
--- FAIL: TestEntRepository_GetTransactions (2.28s)
    ...
FAIL
coverage: 25.9% of statements
FAIL	github.com/traPtitech/Jomon/model	14.889s
ok  	github.com/traPtitech/Jomon/router	1.313s	coverage: 6.6% of statements
FAIL
@H1rono
Copy link
Member Author

H1rono commented Jun 30, 2024

client, storage, err := setup(t, ctx, "get_transactions")

func setup(t *testing.T, ctx context.Context, dbName string) (*ent.Client, storage.Storage, error) {
t.Helper()
client, err := SetupTestEntClient(t, dbPrefix+dbName)

func SetupTestEntClient(t *testing.T, dbName string) (*ent.Client, error) {
entOptions := []enttest.Option{
enttest.WithOptions(ent.Log(t.Log)),
}
dbUser := testutil.GetEnvOrDefault("MARIADB_USERNAME", "root")
dbPass := testutil.GetEnvOrDefault("MARIADB_PASSWORD", "password")
dbHost := testutil.GetEnvOrDefault("MARIADB_HOSTNAME", "db")
dbPort := testutil.GetEnvOrDefault("MARIADB_PORT", "3306")
dbDsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/?charset=utf8mb4&parseTime=True&loc=Local", dbUser, dbPass, dbHost, dbPort)
conn, err := sql.Open("mysql", dbDsn)
if err != nil {
return nil, err
}
defer conn.Close()
_, err = conn.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", dbName))
if err != nil {
return nil, err
}
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", dbUser, dbPass, dbHost, dbPort, dbName)
client := enttest.Open(t, "mysql", dsn, entOptions...).Debug()
ctx := context.Background()
if err := client.Schema.Create(ctx); err != nil {
return nil, err
}

func (s *Schema) Create(ctx context.Context, opts ...schema.MigrateOption) error {
return Create(ctx, s, Tables, opts...)
}

func Create(ctx context.Context, s *Schema, tables []*schema.Table, opts ...schema.MigrateOption) error {
migrate, err := schema.NewMigrate(s.drv, opts...)
if err != nil {
return fmt.Errorf("ent/migrate: %w", err)
}
return migrate.Create(ctx, tables...)
}

最後のmigrate.Createmigrate.Tablesへの書き込みが発生している

@H1rono
Copy link
Member Author

H1rono commented Jun 30, 2024

端的に言うと、setupが排他的に呼ばれる必要がある

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant