add cleanup sub-command that remove local bugs and identities by kalinstaykov · Pull Request #933 · git-bug/git-bug

Yes, there is a race condition that is not detectable in the current test for some reason but when I make a new test that adds 2 identities and 2 bugs and tries to concurrently remove them the go tooling is able to detect the race condition. Here's the test:

func TestCacheConcurrent(t *testing.T) {
	repo := repository.CreateGoGitTestRepo(t, false)
	cache, err := NewRepoCacheNoEvents(repo)
	require.NoError(t, err)
	// Create, set and get user identity
	iden1, err := cache.Identities().New("Kaz", "kaz@ketterdam.grisha")
	require.NoError(t, err)
	err = cache.SetUserIdentity(iden1)
	require.NoError(t, err)
	iden2, err := cache.Identities().New("Inej", "inej@ketterdam.grisha")
	require.NoError(t, err)

	bug1, _, err := cache.Bugs().New("title", "message")
	require.NoError(t, err)
	bug2, _, err := cache.Bugs().New("title", "message")
	require.NoError(t, err)

	go func() {
		cache.Identities().Remove(iden1.Id().String()[:10])
		cache.Identities().Remove(iden2.Id().String()[:10])
		cache.Bugs().Remove(bug1.Id().String()[:10])
		cache.Bugs().Remove(bug2.Id().String()[:10])
	}()

	require.NoError(t, err)
}

This test will reliably fail and the race condition is now detectable:

kstaykov:git-bug/ (feat-cleanup-subcommand✗) $ go test -race -v -run TestCacheConcurrent -count=1 ./cache                                      [10:49:18]
=== RUN   TestCacheConcurrent
==================
WARNING: DATA RACE
Read at 0x00c000423d10 by goroutine 29:
  runtime.mapaccess1_faststr()
      /opt/homebrew/Cellar/go/1.19.3/libexec/src/runtime/map_faststr.go:13 +0x41c
  github.com/MichaelMure/git-bug/repository.(*GoGitRepo).GetIndex()
      /Users/kstaykov/proj/git-bug/repository/gogit.go:330 +0xac
  github.com/MichaelMure/git-bug/repository.(*replaceKeyring).GetIndex()
      <autogenerated>:1 +0x5c
  github.com/MichaelMure/git-bug/cache.(*SubCache[...]).Remove()
      /Users/kstaykov/proj/git-bug/cache/subcache.go:395 +0x2f8
  github.com/MichaelMure/git-bug/cache.TestCacheConcurrent.func1()
      /Users/kstaykov/proj/git-bug/cache/repo_cache_test.go:186 +0xe8

Previous write at 0x00c000423d10 by goroutine 11:
  runtime.mapassign_faststr()
      /opt/homebrew/Cellar/go/1.19.3/libexec/src/runtime/map_faststr.go:203 +0x42c
  github.com/MichaelMure/git-bug/repository.(*GoGitRepo).Close()
      /Users/kstaykov/proj/git-bug/repository/gogit.go:225 +0xa4
  github.com/MichaelMure/git-bug/repository.CreateGoGitTestRepo.func1()
      /Users/kstaykov/proj/git-bug/repository/gogit_testing.go:35 +0x2c
  testing.(*common).Cleanup.func1()
      /opt/homebrew/Cellar/go/1.19.3/libexec/src/testing/testing.go:1041 +0x13c
  testing.(*common).runCleanup()
      /opt/homebrew/Cellar/go/1.19.3/libexec/src/testing/testing.go:1210 +0xe4
  testing.tRunner.func2()
      /opt/homebrew/Cellar/go/1.19.3/libexec/src/testing/testing.go:1440 +0x48
  runtime.deferreturn()
      /opt/homebrew/Cellar/go/1.19.3/libexec/src/runtime/panic.go:476 +0x30
  testing.(*T).Run.func1()
      /opt/homebrew/Cellar/go/1.19.3/libexec/src/testing/testing.go:1493 +0x40

Goroutine 29 (running) created at:
  github.com/MichaelMure/git-bug/cache.TestCacheConcurrent()
      /Users/kstaykov/proj/git-bug/cache/repo_cache_test.go:185 +0x3d8
  testing.tRunner()
      /opt/homebrew/Cellar/go/1.19.3/libexec/src/testing/testing.go:1446 +0x188
  testing.(*T).Run.func1()
      /opt/homebrew/Cellar/go/1.19.3/libexec/src/testing/testing.go:1493 +0x40

Goroutine 11 (running) created at:
  testing.(*T).Run()
      /opt/homebrew/Cellar/go/1.19.3/libexec/src/testing/testing.go:1493 +0x55c
  testing.runTests.func1()
      /opt/homebrew/Cellar/go/1.19.3/libexec/src/testing/testing.go:1846 +0x90
  testing.tRunner()
      /opt/homebrew/Cellar/go/1.19.3/libexec/src/testing/testing.go:1446 +0x188
  testing.runTests()
      /opt/homebrew/Cellar/go/1.19.3/libexec/src/testing/testing.go:1844 +0x6c0
  testing.(*M).Run()
      /opt/homebrew/Cellar/go/1.19.3/libexec/src/testing/testing.go:1726 +0x870
  main.main()
      _testmain.go:59 +0x2fc
==================
    testing.go:1319: race detected during execution of test
--- FAIL: TestCacheConcurrent (0.16s)
=== CONT  
    testing.go:1319: race detected during execution of test
FAIL
FAIL    github.com/MichaelMure/git-bug/cache    0.762s
FAIL
kstaykov:git-bug/ (feat-cleanup-subcommand✗) $