Skip to content

Commit

Permalink
fix: random port allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed Jan 7, 2025
1 parent 8d949b8 commit f7262ac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion yarn-project/blob-sink/src/client/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { HttpBlobSinkClient } from './http.js';
describe('HttpBlobSinkClient', () => {
runBlobSinkClientTests(async () => {
const server = new BlobSinkServer({
port: 33333,
port: 0,
});
await server.start();

Expand Down
14 changes: 12 additions & 2 deletions yarn-project/blob-sink/src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';

import express, { type Express, type Request, type Response, json } from 'express';
import { type Server } from 'http';
import { AddressInfo } from 'net';
import { z } from 'zod';

import { type BlobStore, DiskBlobStore } from '../blobstore/index.js';
Expand All @@ -23,7 +24,7 @@ import { BlobSinkMetrics } from './metrics.js';
* await service.stop();
*/
export class BlobSinkServer {
public readonly port: number;
public port: number;

private app: Express;
private server: Server | null = null;
Expand Down Expand Up @@ -135,8 +136,17 @@ export class BlobSinkServer {
}

public start(): Promise<void> {
return new Promise(resolve => {
return new Promise((resolve, reject) => {
this.server = this.app.listen(this.port, () => {
// Extract port from server address, allows setting address when
// server is started with port 0
const address = this.server?.address() as AddressInfo | null;
if (!address) {
this.log.error('Server address not found');
this.stop().then(() => reject(new Error('Server address not found')));
}

this.port = address!.port;
this.log.info(`Server is running on http://localhost:${this.port}`);
resolve();
});
Expand Down

0 comments on commit f7262ac

Please sign in to comment.