diff --git a/packages/client-common/__tests__/utils/client.ts b/packages/client-common/__tests__/utils/client.ts index 59f8795..5b239cf 100644 --- a/packages/client-common/__tests__/utils/client.ts +++ b/packages/client-common/__tests__/utils/client.ts @@ -24,9 +24,9 @@ beforeAll(async () => { ) if (isCloudTestEnv() && databaseName === undefined) { const cloudInitClient = createTestClient({ - request_timeout: 10_000, + request_timeout: 30_000, }) - await wakeUpPing(cloudInitClient) + await wakeUpQuery(cloudInitClient) databaseName = await createRandomDatabase(cloudInitClient) await cloudInitClient.close() } @@ -101,14 +101,26 @@ export async function createRandomDatabase( maybeOnCluster = ` ON CLUSTER '{cluster}'` } const ddl = `CREATE DATABASE IF NOT EXISTS ${databaseName}${maybeOnCluster}` - await client.command({ - query: ddl, - clickhouse_settings: { - wait_end_of_query: 1, - }, - }) - console.log(`\nCreated database ${databaseName}`) - return databaseName + const startTime = Date.now() + try { + console.log(`\nCreating database ${databaseName} with DDL:\n${ddl}`) + await client.command({ + query: ddl, + clickhouse_settings: { + wait_end_of_query: 1, + }, + }) + console.log( + `\nCreated database ${databaseName} after ${Date.now() - startTime} ms`, + ) + return databaseName + } catch (e) { + console.error( + `Failed to create database ${databaseName} after ${Date.now() - startTime} ms`, + e, + ) + process.exit(1) + } } export async function createTable( @@ -136,24 +148,34 @@ export function getTestDatabaseName(): string { } const MaxPingRetries = 30 -export async function wakeUpPing(client: ClickHouseClient): Promise { +export async function wakeUpQuery(client: ClickHouseClient): Promise { let attempts = 1 let lastError: Error | unknown let isAwake = false while (attempts <= MaxPingRetries) { - const result = await client.ping() - isAwake = result.success - if (result.success) { + try { + const rs = await client.query({ + query: 'SELECT number FROM system.numbers LIMIT 1', + format: 'JSONEachRow', + clickhouse_settings: { + wait_end_of_query: 1, + }, + }) + console.log('Got a successful wake-up response:', await rs.json()) + isAwake = true break + } catch (e) { + console.error( + `Service is still waking up, query attempts so far: ${attempts}. Cause:`, + e, + ) + lastError = e + attempts++ } - console.warn( - `Service is still waking up, ping attempts so far: ${attempts}. Cause:`, - result.error, - ) - lastError = result.error - attempts++ } - if (!isAwake) { + if (isAwake) { + console.log(`Service is awake after ${attempts} attempts`) + } else { console.error( `Failed to wake up the service after ${MaxPingRetries} attempts, exiting. Last error:`, lastError,