@@ -6,6 +6,7 @@ package fixes
66import (
77 "context"
88 "errors"
9+ "sync"
910 "testing"
1011 "time"
1112
@@ -19,6 +20,7 @@ import (
1920
2021// MockORMStore represents a mock implementation for testing connection leak fixes
2122type MockORMStore struct {
23+ mu sync.RWMutex
2224 activeConnections map [string ]int
2325 maxConnections int
2426}
@@ -33,11 +35,15 @@ func NewMockORMStore(maxConnections int) *MockORMStore {
3335
3436// GetActiveConnections returns the number of active connections for a database
3537func (m * MockORMStore ) GetActiveConnections (database string ) int {
38+ m .mu .RLock ()
39+ defer m .mu .RUnlock ()
3640 return m .activeConnections [database ]
3741}
3842
3943// SimulateConnection simulates creating a connection to a database
4044func (m * MockORMStore ) SimulateConnection (database string ) error {
45+ m .mu .Lock ()
46+ defer m .mu .Unlock ()
4147 if m .activeConnections [database ] >= m .maxConnections {
4248 return ErrTooManyConnections
4349 }
@@ -47,6 +53,8 @@ func (m *MockORMStore) SimulateConnection(database string) error {
4753
4854// SimulateDisconnection simulates closing a connection
4955func (m * MockORMStore ) SimulateDisconnection (database string ) {
56+ m .mu .Lock ()
57+ defer m .mu .Unlock ()
5058 if m .activeConnections [database ] > 0 {
5159 m .activeConnections [database ]--
5260 }
@@ -163,7 +171,13 @@ func TestConcurrentDatabaseAccess(t *testing.T) {
163171
164172 // Worker 1: Access db1 repeatedly
165173 go func () {
166- defer func () { done <- true }()
174+ defer func () {
175+ // Ensure we always send to done channel to prevent test hanging
176+ select {
177+ case done <- true :
178+ default :
179+ }
180+ }()
167181 for {
168182 select {
169183 case <- ctx .Done ():
@@ -181,7 +195,13 @@ func TestConcurrentDatabaseAccess(t *testing.T) {
181195
182196 // Worker 2: Access db2 repeatedly
183197 go func () {
184- defer func () { done <- true }()
198+ defer func () {
199+ // Ensure we always send to done channel to prevent test hanging
200+ select {
201+ case done <- true :
202+ default :
203+ }
204+ }()
185205 for {
186206 select {
187207 case <- ctx .Done ():
@@ -201,9 +221,15 @@ func TestConcurrentDatabaseAccess(t *testing.T) {
201221 time .Sleep (time .Millisecond * 100 )
202222 cancel ()
203223
204- // Wait for workers to finish
205- <- done
206- <- done
224+ // Wait for workers to finish with timeout protection
225+ for i := 0 ; i < 2 ; i ++ {
226+ select {
227+ case <- done :
228+ // Worker finished
229+ case <- time .After (5 * time .Second ):
230+ t .Fatal ("Test timed out waiting for workers to finish" )
231+ }
232+ }
207233
208234 // Verify no connections are leaked
209235 db1Count := mockStore .GetActiveConnections ("db1" )
0 commit comments