Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename MapeoManager.start() and MapeoManager.stop() methods #399

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/mapeo-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,11 +641,11 @@ export class MapeoManager extends TypedEmitter {
/**
* @param {import('./media-server.js').StartOpts} [opts]
*/
async start(opts) {
async startMediaServer(opts) {
await this.#mediaServer.start(opts)
}

async stop() {
async stopMediaServer() {
await this.#mediaServer.stop()
}

Expand Down
20 changes: 1 addition & 19 deletions test-e2e/manager-basic.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
import { test } from 'brittle'
import { randomBytes, createHash } from 'crypto'
import { KeyManager } from '@mapeo/crypto'
Expand Down Expand Up @@ -289,25 +290,6 @@ test('Consistent storage folders', async (t) => {
t.snapshot(storageNames.sort())
})

test('manager.start() and manager.stop()', async (t) => {
const manager = new MapeoManager({
rootKey: KeyManager.generateRootKey(),
projectMigrationsFolder,
clientMigrationsFolder,
dbFolder: ':memory:',
coreStorage: () => new RAM(),
})

await manager.start()
await manager.start()
await manager.stop()

await manager.start()
await manager.stop()

t.pass('start() and stop() life cycle runs without issues')
})

/**
* Generate a deterministic random bytes
*
Expand Down
73 changes: 67 additions & 6 deletions test-e2e/media-server.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
import { test } from 'brittle'
import { randomBytes } from 'crypto'
import { join } from 'path'
Expand All @@ -19,6 +20,66 @@ const projectMigrationsFolder = new URL('../drizzle/project', import.meta.url)
const clientMigrationsFolder = new URL('../drizzle/client', import.meta.url)
.pathname

test('start/stop lifecycle', async (t) => {
const manager = new MapeoManager({
rootKey: KeyManager.generateRootKey(),
projectMigrationsFolder,
clientMigrationsFolder,
dbFolder: ':memory:',
coreStorage: () => new RAM(),
})

const project = await manager.getProject(await manager.createProject())

await manager.startMediaServer()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check it's started: get a url, check it works.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need to put data there, just returning 404 is enough to know it's started.


const blobUrl1 = await project.$blobs.getUrl({
driveId: randomBytes(32).toString('hex'),
name: randomBytes(8).toString('hex'),
type: 'photo',
variant: 'original',
})
const response1 = await fetch(blobUrl1)
t.is(response1.status, 404, 'server started and listening')

await manager.startMediaServer()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get a url, check the port hasn't changed


const blobUrl2 = await project.$blobs.getUrl({
driveId: randomBytes(32).toString('hex'),
name: randomBytes(8).toString('hex'),
type: 'video',
variant: 'original',
})
t.is(
new URL(blobUrl1).port,
new URL(blobUrl2).port,
'server port is the same'
)

await manager.stopMediaServer()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check the url previously returned no longer works (should return a connection error)


await t.exception.all(async () => {
await fetch(blobUrl2)
}, 'failed to fetch due to connection error')

await manager.startMediaServer()

const blobUrl3 = await project.$blobs.getUrl({
driveId: randomBytes(32).toString('hex'),
name: randomBytes(8).toString('hex'),
type: 'audio',
variant: 'original',
})
const response3 = await fetch(blobUrl3)
t.is(response3.status, 404, 'server started and listening')

await manager.stopMediaServer()

await t.exception.all(async () => {
await fetch(blobUrl3)
}, 'failed to fetch due to connection error')
})

test('retrieving blobs using url', async (t) => {
const clock = FakeTimers.install({ shouldAdvanceTime: true })
t.teardown(() => clock.uninstall())
Expand All @@ -40,12 +101,12 @@ test('retrieving blobs using url', async (t) => {
type: 'photo',
variant: 'original',
})
}, 'getting blob url fails if manager.start() has not been called yet')
}, 'getting blob url fails if manager.startMediaServer() has not been called yet')

clock.tick(100_000)
await exceptionPromise1

await manager.start()
await manager.startMediaServer()

await t.test('blob does not exist', async (st) => {
const blobUrl = await project.$blobs.getUrl({
Expand Down Expand Up @@ -96,7 +157,7 @@ test('retrieving blobs using url', async (t) => {
st.alike(body, expected, 'matching reponse body')
})

await manager.stop()
await manager.stopMediaServer()

const exceptionPromise2 = t.exception(async () => {
await project.$blobs.getUrl({
Expand Down Expand Up @@ -130,12 +191,12 @@ test('retrieving icons using url', async (t) => {
pixelDensity: 1,
size: 'small',
})
}, 'getting icon url fails if manager.start() has not been called yet')
}, 'getting icon url fails if manager.startMediaServer() has not been called yet')

clock.tick(100_000)
await exceptionPromise1

await manager.start()
await manager.startMediaServer()

await t.test('icon does not exist', async (st) => {
const nonExistentIconId = randomBytes(32).toString('hex')
Expand Down Expand Up @@ -194,7 +255,7 @@ test('retrieving icons using url', async (t) => {
st.alike(body, iconBuffer, 'matching response body')
})

await manager.stop()
await manager.stopMediaServer()

const exceptionPromise2 = t.exception(async () => {
await project.$icons.getIconUrl(randomBytes(32).toString('hex'), {
Expand Down
Loading