Files
zeekayandHanzo Dev 1ab6e799c1 cd: the module is github.com/hanzoai/cd, where it is actually hosted
go.mod still declared `module github.com/hanzoai/deploy` after the repo was
renamed. A Go module path is not cosmetic — it is the resolvable location. So
`go get github.com/hanzoai/cd` could not work, and anything importing this
module had to spell a name that no longer exists at that address. The build log
of v3.6.0 is where it surfaced: the compiler was still emitting
github.com/hanzoai/deploy/cmd/cd/commands.

3541 import paths across 690 .go, 19 .proto, 4 .yaml, 3 .sh, 2 .mod, Procfile
and Makefile. Plus the 62 ghcr.io/hanzoai/deploy image references, moved to
ghcr.io/hanzoai/cd to match hanzo.yml's build target — a distinct string from
the module path, replaced separately so neither could clobber the other.

go mod tidy clean, CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ./... clean.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-28 21:16:24 -07:00

202 lines
4.0 KiB
Go

package repository
import (
"errors"
"io"
"testing"
"time"
"github.com/stretchr/testify/assert"
utilio "github.com/hanzoai/cd/util/io"
)
// execute given action and return false if action have not completed within 1 second
func lockQuickly(action func() (io.Closer, error)) (io.Closer, bool) {
done := make(chan io.Closer)
go func() {
closer, _ := action()
done <- closer
}()
select {
case <-time.After(1 * time.Second):
return nil, false
case closer := <-done:
return closer, true
}
}
func numberOfInits(initializedTimes *int) func(_ bool) (io.Closer, error) {
return func(_ bool) (io.Closer, error) {
*initializedTimes++
return utilio.NopCloser, nil
}
}
func TestLock_SameRevision(t *testing.T) {
t.Parallel()
lock := NewRepositoryLock()
initializedTimes := 0
init := numberOfInits(&initializedTimes)
closer1, done := lockQuickly(func() (io.Closer, error) {
return lock.Lock("myRepo", "1", true, init)
})
if !assert.True(t, done) {
return
}
closer2, done := lockQuickly(func() (io.Closer, error) {
return lock.Lock("myRepo", "1", true, init)
})
if !assert.True(t, done) {
return
}
assert.Equal(t, 1, initializedTimes)
utilio.Close(closer1)
utilio.Close(closer2)
}
func TestLock_DifferentRevisions(t *testing.T) {
t.Parallel()
lock := NewRepositoryLock()
initializedTimes := 0
init := numberOfInits(&initializedTimes)
closer1, done := lockQuickly(func() (io.Closer, error) {
return lock.Lock("myRepo", "1", true, init)
})
if !assert.True(t, done) {
return
}
_, done = lockQuickly(func() (io.Closer, error) {
return lock.Lock("myRepo", "2", true, init)
})
if !assert.False(t, done) {
return
}
utilio.Close(closer1)
_, done = lockQuickly(func() (io.Closer, error) {
return lock.Lock("myRepo", "2", true, init)
})
if !assert.True(t, done) {
return
}
}
func TestLock_NoConcurrentWithSameRevision(t *testing.T) {
t.Parallel()
lock := NewRepositoryLock()
initializedTimes := 0
init := numberOfInits(&initializedTimes)
closer1, done := lockQuickly(func() (io.Closer, error) {
return lock.Lock("myRepo", "1", false, init)
})
if !assert.True(t, done) {
return
}
_, done = lockQuickly(func() (io.Closer, error) {
return lock.Lock("myRepo", "1", false, init)
})
if !assert.False(t, done) {
return
}
utilio.Close(closer1)
}
func TestLock_FailedInitialization(t *testing.T) {
t.Parallel()
lock := NewRepositoryLock()
closer1, done := lockQuickly(func() (io.Closer, error) {
return lock.Lock("myRepo", "1", true, func(_ bool) (io.Closer, error) {
return utilio.NopCloser, errors.New("failed")
})
})
if !assert.True(t, done) {
return
}
assert.Nil(t, closer1)
closer2, done := lockQuickly(func() (io.Closer, error) {
return lock.Lock("myRepo", "1", true, func(_ bool) (io.Closer, error) {
return utilio.NopCloser, nil
})
})
if !assert.True(t, done) {
return
}
utilio.Close(closer2)
}
func TestLock_SameRevisionFirstNotConcurrent(t *testing.T) {
t.Parallel()
lock := NewRepositoryLock()
initializedTimes := 0
init := numberOfInits(&initializedTimes)
closer1, done := lockQuickly(func() (io.Closer, error) {
return lock.Lock("myRepo", "1", false, init)
})
if !assert.True(t, done) {
return
}
_, done = lockQuickly(func() (io.Closer, error) {
return lock.Lock("myRepo", "1", true, init)
})
if !assert.False(t, done) {
return
}
assert.Equal(t, 1, initializedTimes)
utilio.Close(closer1)
}
func TestLock_CleanForNonConcurrent(t *testing.T) {
t.Parallel()
lock := NewRepositoryLock()
initClean := false
init := func(clean bool) (io.Closer, error) {
initClean = clean
return utilio.NopCloser, nil
}
closer, done := lockQuickly(func() (io.Closer, error) {
return lock.Lock("myRepo", "1", true, init)
})
assert.True(t, done)
// first time always clean because we cannot be sure about the state of repository
assert.True(t, initClean)
utilio.Close(closer)
closer, done = lockQuickly(func() (io.Closer, error) {
return lock.Lock("myRepo", "1", true, init)
})
assert.True(t, done)
assert.False(t, initClean)
utilio.Close(closer)
}