MCPcopy
hub / github.com/usememos/memos / waitForDB

Function waitForDB

store/test/containers.go:160–189  ·  view source on GitHub ↗

waitForDB polls the database until it's ready or timeout is reached.

(driver, dsn string, timeout time.Duration)

Source from the content-addressed store, hash-verified

158
159// waitForDB polls the database until it's ready or timeout is reached.
160func waitForDB(driver, dsn string, timeout time.Duration) error {
161 ctx, cancel := context.WithTimeout(context.Background(), timeout)
162 defer cancel()
163
164 ticker := time.NewTicker(500 * time.Millisecond)
165 defer ticker.Stop()
166
167 var lastErr error
168 for {
169 select {
170 case <-ctx.Done():
171 if lastErr != nil {
172 return errors.Errorf("timeout waiting for %s database: %v", driver, lastErr)
173 }
174 return errors.Errorf("timeout waiting for %s database to be ready", driver)
175 case <-ticker.C:
176 db, err := sql.Open(driver, dsn)
177 if err != nil {
178 lastErr = err
179 continue
180 }
181 err = db.PingContext(ctx)
182 db.Close()
183 if err == nil {
184 return nil
185 }
186 lastErr = err
187 }
188 }
189}
190
191// GetPostgresDSN starts a PostgreSQL container (if not already running) and creates a fresh database for this test.
192func GetPostgresDSN(t *testing.T) string {

Callers 2

GetMySQLDSNFunction · 0.85
GetPostgresDSNFunction · 0.85

Calls 3

DoneMethod · 0.80
CloseMethod · 0.65
StopMethod · 0.45

Tested by

no test coverage detected