Skip to content

Commit 0eff8c1

Browse files
committed
Increase lock acquisition timeout to 120 seconds and add shared state fallback for integration tests.
1 parent 6922467 commit 0eff8c1

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

main_test.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ func getLockFilePath() string {
4747
// acquireLock attempts to acquire a file-based lock
4848
func acquireLock() (*os.File, error) {
4949
lockFile := getLockFilePath()
50-
// Try to create lock file exclusively
51-
for i := 0; i < 50; i++ {
50+
// Try to create lock file exclusively (wait up to ~2 minutes)
51+
for i := 0; i < 1200; i++ { // 1200 * 100ms = 120s
5252
f, err := os.OpenFile(lockFile, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
5353
if err == nil {
5454
// Write our PID to the lock file
@@ -58,7 +58,7 @@ func acquireLock() (*os.File, error) {
5858
// Lock file exists, wait and retry
5959
time.Sleep(100 * time.Millisecond)
6060
}
61-
return nil, fmt.Errorf("failed to acquire lock after 5 seconds")
61+
return nil, fmt.Errorf("failed to acquire lock after 120 seconds")
6262
}
6363

6464
// releaseLock releases the file-based lock
@@ -103,8 +103,27 @@ func TestMain(m *testing.M) {
103103
// Acquire lock to prevent race condition with other packages
104104
lock, err := acquireLock()
105105
if err != nil {
106-
log.Printf("Warning: Failed to acquire lock: %v. Tests may fail.", err)
107-
os.Exit(1)
106+
log.Printf("Warning: Failed to acquire lock: %v. Waiting for shared state...", err)
107+
// If we cannot get the lock, another package is likely starting containers.
108+
// Wait for the shared state file to appear and reuse those containers.
109+
deadline := time.Now().Add(2 * time.Minute)
110+
for time.Now().Before(deadline) {
111+
if state, err := loadContainerState(); err == nil && state != nil {
112+
log.Printf("Reusing existing MongoDB container at %s", state.MongoURI)
113+
log.Printf("Reusing existing CouchDB container at %s", state.CouchURL)
114+
sharedMongoURI = state.MongoURI
115+
sharedCouchURL = state.CouchURL
116+
shouldCleanupContainers = false
117+
// Run tests without owning the containers
118+
code := m.Run()
119+
os.Exit(code)
120+
}
121+
time.Sleep(250 * time.Millisecond)
122+
}
123+
log.Printf("Warning: Shared state not found after waiting. Integration tests may be skipped.")
124+
// Proceed without containers (integration tests will self-skip if unavailable)
125+
code := m.Run()
126+
os.Exit(code)
108127
}
109128

110129
// Try to load existing container state

storage/shared_test.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ func getLockFilePath() string {
4545
// acquireLock attempts to acquire a file-based lock
4646
func acquireLock() (*os.File, error) {
4747
lockFile := getLockFilePath()
48-
// Try to create lock file exclusively
49-
for i := 0; i < 50; i++ {
48+
// Try to create lock file exclusively (wait up to ~2 minutes)
49+
for i := 0; i < 1200; i++ { // 1200 * 100ms = 120s
5050
f, err := os.OpenFile(lockFile, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
5151
if err == nil {
5252
// Write our PID to the lock file
@@ -56,7 +56,7 @@ func acquireLock() (*os.File, error) {
5656
// Lock file exists, wait and retry
5757
time.Sleep(100 * time.Millisecond)
5858
}
59-
return nil, fmt.Errorf("failed to acquire lock after 5 seconds")
59+
return nil, fmt.Errorf("failed to acquire lock after 120 seconds")
6060
}
6161

6262
// releaseLock releases the file-based lock
@@ -101,8 +101,27 @@ func TestMain(m *testing.M) {
101101
// Acquire lock to prevent race condition with other packages
102102
lock, err := acquireLock()
103103
if err != nil {
104-
log.Printf("Warning: Failed to acquire lock: %v. Tests may fail.", err)
105-
os.Exit(1)
104+
log.Printf("Warning: Failed to acquire lock: %v. Waiting for shared state...", err)
105+
// If we cannot get the lock, another package is likely starting containers.
106+
// Wait for the shared state file to appear and reuse those containers.
107+
deadline := time.Now().Add(2 * time.Minute)
108+
for time.Now().Before(deadline) {
109+
if state, err := loadContainerState(); err == nil && state != nil {
110+
log.Printf("Reusing existing MongoDB container at %s", state.MongoURI)
111+
log.Printf("Reusing existing CouchDB container at %s", state.CouchURL)
112+
sharedMongoURI = state.MongoURI
113+
sharedCouchURL = state.CouchURL
114+
shouldCleanupContainers = false
115+
containersInitialized = true
116+
code := m.Run()
117+
os.Exit(code)
118+
}
119+
time.Sleep(250 * time.Millisecond)
120+
}
121+
log.Printf("Warning: Shared state not found after waiting. Integration tests may be skipped.")
122+
containersInitialized = true
123+
code := m.Run()
124+
os.Exit(code)
106125
}
107126

108127
// Try to load existing container state

0 commit comments

Comments
 (0)