Skip to content

Commit

Permalink
Fixed bug and added extra checks for starting backends
Browse files Browse the repository at this point in the history
Co-authored-by: Koen Bollen <213502+koenbollen@users.noreply.github.com>
  • Loading branch information
erikdubbelboer and koenbollen committed May 17, 2024
1 parent 77395ac commit 3db119c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/testproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
}

if err := db.Ping(ctx); err != nil {
logger.Fatal("failed to ping db", zap.Error(err))
logger.Fatal("failed to ping db", zap.Error(err), zap.String("url", os.Getenv("DATABASE_URL")))
}

connections := make(map[string]net.Conn)
Expand Down
15 changes: 13 additions & 2 deletions features/support/steps/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { After, Given } from '@cucumber/cucumber'
import { World } from '../world'

Given('the {string} backend is running', async function (this: World, backend: string) {
return await new Promise(resolve => {
return await new Promise((resolve, reject) => {
const port = 10000 + Math.ceil(Math.random() * 1000)
const env: NodeJS.ProcessEnv = {
...process.env,
ADDR: `127.0.0.1:${port}`,
ENV: 'test'
}

if (this.databaseURL === undefined) {
if (this.databaseURL !== undefined) {
env.DATABASE_URL = this.databaseURL
}

Expand All @@ -20,14 +20,21 @@ Given('the {string} backend is running', async function (this: World, backend: s
env
})
prc.stderr.setEncoding('utf8')

let resolved = false
prc.stderr.on('data', (data: string) => {
const lines = data.split('\n')
lines.forEach(line => {
try {
const entry = JSON.parse(line)
const severity = entry.severity.toLowerCase()
if (!resolved && (severity === 'error' || severity === 'emergency')) {
return reject(new Error('error before backend was started: ' + entry.message))
}
if (entry.message === 'using database') {
this.databaseURL = entry.url
} else if (entry.message === 'listening') {
resolved = true
resolve(undefined)
}
} catch (_) {
Expand All @@ -37,6 +44,10 @@ Given('the {string} backend is running', async function (this: World, backend: s
})
prc.addListener('exit', () => {
this.print(`${backend} exited`)

if (!resolved) {
reject(new Error(`${backend} exited before it was ready`))
}
})

// Create a promise that resolves when the backend is closed so
Expand Down
2 changes: 0 additions & 2 deletions features/support/steps/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ Given('these lobbies exist:', async function (this: World, lobbies: DataTable) {
values.push(`(${v.join(', ')})`)
})

console.log('INSERT INTO lobbies (' + columns.join(', ') + ') VALUES ' + values.join(', '))

await fetch(`${this.testproxyURL}/sql`, {
method: 'POST',
body: 'INSERT INTO lobbies (' + columns.join(', ') + ') VALUES ' + values.join(', ')
Expand Down

0 comments on commit 3db119c

Please sign in to comment.