@@ -47,8 +47,8 @@ func getLockFilePath() string {
4747// acquireLock attempts to acquire a file-based lock
4848func 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
0 commit comments