-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from utilitywarehouse/as-race-fix
fix recursive locking and add test to detect deadlocks
- Loading branch information
Showing
7 changed files
with
180 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
module github.com/utilitywarehouse/git-mirror | ||
|
||
go 1.22.0 | ||
go 1.23.0 | ||
|
||
require ( | ||
github.com/google/go-cmp v0.6.0 | ||
github.com/prometheus/client_golang v1.19.1 | ||
github.com/prometheus/client_golang v1.20.1 | ||
github.com/sasha-s/go-deadlock v0.3.5 | ||
gopkg.in/yaml.v3 v3.0.1 | ||
) | ||
|
||
require ( | ||
github.com/beorn7/perks v1.0.1 // indirect | ||
github.com/cespare/xxhash/v2 v2.2.0 // indirect | ||
github.com/cespare/xxhash/v2 v2.3.0 // indirect | ||
github.com/kr/text v0.2.0 // indirect | ||
github.com/prometheus/client_model v0.5.0 // indirect | ||
github.com/prometheus/common v0.48.0 // indirect | ||
github.com/prometheus/procfs v0.12.0 // indirect | ||
golang.org/x/sys v0.17.0 // indirect | ||
google.golang.org/protobuf v1.33.0 // indirect | ||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect | ||
github.com/petermattis/goid v0.0.0-20240813172612-4fcff4a6cae7 // indirect | ||
github.com/prometheus/client_model v0.6.1 // indirect | ||
github.com/prometheus/common v0.55.0 // indirect | ||
github.com/prometheus/procfs v0.15.1 // indirect | ||
golang.org/x/sys v0.24.0 // indirect | ||
google.golang.org/protobuf v1.34.2 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//go:build deadlock_test | ||
|
||
package lock | ||
|
||
import "github.com/sasha-s/go-deadlock" | ||
|
||
// this type is used only in test for deadlock detection | ||
type RWMutex struct { | ||
deadlock.RWMutex | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
//go:build !deadlock_test | ||
|
||
package lock | ||
|
||
import "sync" | ||
|
||
type RWMutex struct { | ||
sync.RWMutex | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//go:build deadlock_test | ||
|
||
package mirror | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func Test_mirror_detect_race(t *testing.T) { | ||
testTmpDir := mustTmpDir(t) | ||
defer os.RemoveAll(testTmpDir) | ||
|
||
ctx, cancel := context.WithCancel(context.TODO()) | ||
defer cancel() | ||
|
||
upstream := filepath.Join(testTmpDir, testUpstreamRepo) | ||
root := filepath.Join(testTmpDir, testRoot) | ||
link1 := "link1" // on testBranchMain branch | ||
link2 := "link2" // on remote HEAD | ||
ref1 := testMainBranch | ||
ref2 := "HEAD" | ||
testName := t.Name() | ||
|
||
t.Log("TEST-1: init upstream") | ||
fileSHA1 := mustInitRepo(t, upstream, "file", testName+"-1") | ||
|
||
repo := mustCreateRepoAndMirror(t, upstream, root, link1, ref1) | ||
// add worktree for HEAD | ||
if err := repo.AddWorktreeLink(link2, ref2, ""); err != nil { | ||
t.Fatalf("unable to add worktree error: %v", err) | ||
} | ||
// mirror again for 2nd worktree | ||
if err := repo.Mirror(ctx); err != nil { | ||
t.Fatalf("unable to mirror error: %v", err) | ||
} | ||
|
||
// verify checkout files | ||
assertCommitLog(t, repo, "HEAD", "", fileSHA1, testName+"-1", []string{"file"}) | ||
assertLinkedFile(t, root, link1, "file", testName+"-1") | ||
assertLinkedFile(t, root, link2, "file", testName+"-1") | ||
|
||
// start mirror loop | ||
go repo.StartLoop(ctx) | ||
close(repo.stop) | ||
|
||
t.Log("TEST-2: forward HEAD") | ||
fileSHA2 := mustCommit(t, upstream, "file", testName+"-2") | ||
|
||
t.Run("test-1", func(t *testing.T) { | ||
wg := &sync.WaitGroup{} | ||
// all following assertions will always be true | ||
// this test is about testing deadlocks and detecting race conditions | ||
for i := 0; i < 100; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
if err := repo.Mirror(ctx); err != nil { | ||
log.Fatalf("unable to mirror error: %v", err) | ||
} | ||
|
||
assertLinkedFile(t, root, link1, "file", testName+"-2") | ||
assertLinkedFile(t, root, link2, "file", testName+"-2") | ||
}() | ||
|
||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
|
||
assertCommitLog(t, repo, "HEAD", "", fileSHA2, testName+"-2", []string{"file"}) | ||
}() | ||
} | ||
|
||
// clone tests | ||
for i := 0; i < 10; i++ { | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
if err := repo.Mirror(ctx); err != nil { | ||
log.Fatalf("unable to mirror error: %v", err) | ||
} | ||
}() | ||
|
||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
tempClone := mustTmpDir(t) | ||
defer os.RemoveAll(tempClone) | ||
|
||
if cloneSHA, err := repo.Clone(ctx, tempClone, testMainBranch, "", i%2 == 0); err != nil { | ||
t.Fatalf("unexpected error %s", err) | ||
} else { | ||
if cloneSHA != fileSHA2 { | ||
t.Errorf("clone sha mismatch got:%s want:%s", cloneSHA, fileSHA2) | ||
} | ||
assertFile(t, filepath.Join(tempClone, "file"), testName+"-2") | ||
} | ||
}() | ||
} | ||
wg.Wait() | ||
}) | ||
|
||
} |