Repository navigation
fix(api): keep bulk notification archive from stalling the API #2155
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
Open
Eli Bosley (elibosley)
wants to merge
7
commits into
main
Choose a base branch
from
fix/bulk-notification-archive
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+610
−79
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7eaeb2c
fix(api): keep bulk notification archive from stalling the API
elibosley 0d1718c
fix(api): stop bulk notification restores from toasting every file
elibosley 50bbb94
fix(api): tighten bulk notification selection and error reporting
elibosley 5e29f53
test(api): make the shared-batch delete test wait for the deletion to…
elibosley c02f943
test(api): detect the deletion's own synchronization in the shared-ba…
elibosley 4764c92
fix(api): preserve unread copies during bulk restores
elibosley 715abb4
fix(web): refresh notifications after a failed bulk archive
elibosley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
296 changes: 296 additions & 0 deletions
296
api/src/unraid-api/graph/resolvers/notifications/notifications.bulk.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,296 @@ | ||
| import { ConfigService } from '@nestjs/config'; | ||
| import { copyFile, mkdir, mkdtemp, readdir, readFile, rm, writeFile } from 'fs/promises'; | ||
| import { tmpdir } from 'os'; | ||
| import { join } from 'path'; | ||
|
|
||
| import type { FSWatcher } from 'chokidar'; | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| import { pubsub, PUBSUB_CHANNEL } from '@app/core/pubsub.js'; | ||
| import { | ||
| NotificationImportance, | ||
| NotificationType, | ||
| } from '@app/unraid-api/graph/resolvers/notifications/notifications.model.js'; | ||
| import { NotificationsResolver } from '@app/unraid-api/graph/resolvers/notifications/notifications.resolver.js'; | ||
| import { NotificationsService } from '@app/unraid-api/graph/resolvers/notifications/notifications.service.js'; | ||
|
|
||
| // Native macOS watching drops events during rename bursts; polling observes every change. | ||
| vi.mock('@app/environment.js', async (importOriginal) => ({ | ||
| ...(await importOriginal<typeof import('@app/environment.js')>()), | ||
| CHOKIDAR_USEPOLLING: true, | ||
| })); | ||
|
|
||
| const { UNREAD, ARCHIVE } = NotificationType; | ||
| const COUNT = 120; | ||
|
|
||
| describe.sequential('bulk notification moves', () => { | ||
| let basePath: string; | ||
| let service: NotificationsService; | ||
| let subscription: number; | ||
| let overviewPublishes: number; | ||
| let ids: string[]; | ||
|
|
||
| const file = (type: NotificationType, id: string) => join(basePath, type.toLowerCase(), id); | ||
|
|
||
| beforeEach(async () => { | ||
| basePath = await mkdtemp(join(tmpdir(), 'notification-bulk-')); | ||
| await Promise.all( | ||
| Object.values(NotificationType).map((type) => mkdir(join(basePath, type.toLowerCase()))) | ||
| ); | ||
| ids = Array.from({ length: COUNT }, (_, index) => `docker-update-${index}.notify`); | ||
| for (const id of ids) { | ||
| await writeFile( | ||
| file(UNREAD, id), | ||
| 'timestamp=1700000000\nevent=Docker\nsubject=Update\ndescription=Update\nimportance=normal\n' | ||
| ); | ||
| await copyFile(file(UNREAD, id), file(ARCHIVE, id)); | ||
| } | ||
| subscription = await pubsub.subscribe(PUBSUB_CHANNEL.NOTIFICATION_OVERVIEW, () => { | ||
| overviewPublishes++; | ||
| }); | ||
| service = new NotificationsService( | ||
| new ConfigService({ store: { dynamix: { notify: { path: basePath } } } }) | ||
| ); | ||
| await Reflect.get(service, 'initialization'); | ||
| overviewPublishes = 0; | ||
| }); | ||
|
|
||
| afterEach(async () => { | ||
| const watcher: FSWatcher | null = Reflect.get(NotificationsService, 'watcher'); | ||
| await watcher?.close(); | ||
| Reflect.set(NotificationsService, 'watcher', null); | ||
| await Reflect.get(service, 'overviewUpdate'); | ||
| await Reflect.get(service, 'warningsUpdate'); | ||
| pubsub.unsubscribe(subscription); | ||
| await rm(basePath, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it('archives every unread notification with a bounded number of overview broadcasts', async () => { | ||
| const { overview } = await service.archiveAll(); | ||
|
|
||
| expect(await readdir(join(basePath, 'unread'))).toEqual([]); | ||
| expect(overview.unread.total).toBe(0); | ||
| expect(overview.archive.total).toBe(COUNT); | ||
| expect(overviewPublishes).toBeLessThan(COUNT / 10); | ||
| }); | ||
|
|
||
| it('moves the remaining ids when some ids in a bulk request are missing', async () => { | ||
| const result = await service.archiveIds([...ids, 'missing.notify']); | ||
|
|
||
| expect(result.successes).toBe(COUNT); | ||
| expect(result.errors).toHaveLength(1); | ||
| expect(service.getOverview().archive.total).toBe(COUNT); | ||
|
|
||
| await service.unarchiveAll(); | ||
| expect(service.getOverview().unread.total).toBe(COUNT); | ||
| expect(service.getOverview().archive.total).toBe(0); | ||
| }); | ||
|
|
||
| it('moves each requested id once even if the list contains duplicates', async () => { | ||
| const [id] = ids; | ||
| const resolver = new NotificationsResolver(service); | ||
|
|
||
| await expect(resolver.archiveNotifications([id, id])).resolves.toMatchObject({ | ||
| archive: { total: 1 }, | ||
| }); | ||
| await expect(resolver.unarchiveNotifications([id, id])).resolves.toMatchObject({ | ||
| archive: { total: 0 }, | ||
| unread: { total: COUNT }, | ||
| }); | ||
| }); | ||
|
|
||
| it('leaves unread entries that cannot be loaded in place', async () => { | ||
| const directory = 'directory.notify'; | ||
| await mkdir(file(UNREAD, directory)); | ||
|
|
||
| const result = await service.archiveIds([directory, ids[0]]); | ||
|
|
||
| expect(result.successes).toBe(1); | ||
| expect(result.errors).toHaveLength(1); | ||
| expect(await readdir(join(basePath, 'unread'))).toContain(directory); | ||
| expect(await readdir(join(basePath, 'archive'))).not.toContain(directory); | ||
| }); | ||
|
|
||
| it('reports a reconciliation failure through the bulk mutation', async () => { | ||
| const [id] = ids; | ||
| const internal = service as unknown as { | ||
| readNotificationCopy(id: string, type: NotificationType): Promise<unknown>; | ||
| }; | ||
| const read = internal.readNotificationCopy.bind(service); | ||
| const spy = vi.spyOn(internal, 'readNotificationCopy').mockImplementation((entry, type) => { | ||
| if (entry === id && type === ARCHIVE) return Promise.reject(new Error('read failed')); | ||
| return read(entry, type); | ||
| }); | ||
|
|
||
| try { | ||
| const resolver = new NotificationsResolver(service); | ||
| await expect(resolver.archiveNotifications([id])).rejects.toThrow( | ||
| 'Bulk notification move did not complete' | ||
| ); | ||
| expect(await readdir(join(basePath, 'unread'))).not.toContain(id); | ||
| } finally { | ||
| spy.mockRestore(); | ||
| } | ||
| }); | ||
|
|
||
| it('still applies an external edit made right after the service moved the file', async () => { | ||
| const [id] = ids; | ||
| await service.archiveNotification({ id }); | ||
| await writeFile( | ||
| file(ARCHIVE, id), | ||
| 'timestamp=1700000001\nevent=Docker\nsubject=Update\ndescription=Edited\nimportance=alert\n' | ||
| ); | ||
|
|
||
| await vi.waitFor(() => expect(service.getOverview().archive.alert).toBe(1), { | ||
| timeout: 10_000, | ||
| }); | ||
| expect(service.getOverview().archive.total).toBe(1); | ||
| }, 20_000); | ||
|
|
||
| it('restores in bulk without announcing each restored notification', async () => { | ||
| const added: string[] = []; | ||
| const addedSubscription = await pubsub.subscribe( | ||
| PUBSUB_CHANNEL.NOTIFICATION_ADDED, | ||
| (payload: { notificationAdded: { id: string } }) => added.push(payload.notificationAdded.id) | ||
| ); | ||
| const watcher: FSWatcher = Reflect.get(NotificationsService, 'watcher'); | ||
| let unreadAdds = 0; | ||
| let unreadUnlinks = 0; | ||
| let lastEventAt = Date.now(); | ||
| watcher.on('all', (event, path) => { | ||
| lastEventAt = Date.now(); | ||
| if (!path.startsWith(join(basePath, 'unread'))) return; | ||
| if (event === 'add') unreadAdds++; | ||
| if (event === 'unlink') unreadUnlinks++; | ||
| }); | ||
| try { | ||
| await service.archiveAll(); | ||
| await vi.waitFor(() => expect(unreadUnlinks).toBe(COUNT), { timeout: 10_000 }); | ||
| await service.unarchiveAll(); | ||
| expect(service.getOverview().unread.total).toBe(COUNT); | ||
|
|
||
| await vi.waitFor(() => expect(unreadAdds).toBe(COUNT), { timeout: 10_000 }); | ||
| await vi.waitFor(() => expect(Date.now() - lastEventAt).toBeGreaterThan(1_000), { | ||
| timeout: 15_000, | ||
| interval: 100, | ||
| }); | ||
| await Reflect.get(service, 'overviewUpdate'); | ||
| expect(added).toEqual([]); | ||
|
|
||
| const [id] = ids; | ||
| await service.archiveNotification({ id }); | ||
| await vi.waitFor(() => expect(unreadUnlinks).toBe(COUNT + 1), { timeout: 10_000 }); | ||
| await service.markAsUnread({ id }); | ||
| await vi.waitFor(() => expect(added).toEqual([id]), { timeout: 10_000 }); | ||
| } finally { | ||
| pubsub.unsubscribe(addedSubscription); | ||
| } | ||
| }, 30_000); | ||
|
|
||
| it('restores archive-only notifications without overwriting newer unread copies', async () => { | ||
| const [legacy, archived] = ids; | ||
| await service.archiveNotification({ id: archived }); | ||
| await writeFile( | ||
| file(UNREAD, legacy), | ||
| 'timestamp=1700000003\nevent=Docker\nsubject=Update\ndescription=Edited\nimportance=alert\n' | ||
| ); | ||
|
|
||
| for (const importance of [NotificationImportance.INFO, undefined]) { | ||
| const result = await service.unarchiveAll(importance); | ||
| expect(result.errorOccurred).toBe(false); | ||
| } | ||
|
|
||
| expect(await readFile(file(UNREAD, legacy), 'utf8')).toContain('importance=alert'); | ||
| expect(await readdir(join(basePath, 'archive'))).toContain(legacy); | ||
| expect(await readdir(join(basePath, 'unread'))).toContain(archived); | ||
| }); | ||
|
|
||
| it('preserves an unread copy that appeared after bulk restore selection', async () => { | ||
| const [id, restorable] = ids; | ||
| await service.archiveIds([id, restorable]); | ||
| const internal = service as unknown as { | ||
| selectIdsToMove(type: NotificationType): Promise<unknown>; | ||
| }; | ||
| const select = internal.selectIdsToMove.bind(service); | ||
| const selection = vi.spyOn(internal, 'selectIdsToMove').mockImplementationOnce(async (type) => { | ||
| const selected = await select(type); | ||
| await writeFile( | ||
| file(UNREAD, id), | ||
| 'timestamp=1700000003\nevent=Docker\nsubject=Newer\ndescription=Edited\nimportance=alert\n' | ||
| ); | ||
| return selected; | ||
| }); | ||
|
|
||
| try { | ||
| await expect(service.unarchiveAll()).resolves.toMatchObject({ | ||
| successes: 1, | ||
| errorOccurred: false, | ||
| overview: { unread: { alert: 1, total: COUNT }, archive: { total: 0 } }, | ||
| }); | ||
| await expect(service.unarchiveIds([id])).resolves.toMatchObject({ | ||
| successes: 0, | ||
| errorOccurred: false, | ||
| }); | ||
| expect(await readFile(file(UNREAD, id), 'utf8')).toContain('subject=Newer'); | ||
| expect(await readFile(file(ARCHIVE, id), 'utf8')).toContain('subject=Update'); | ||
| expect(await readdir(join(basePath, 'archive'))).not.toContain(restorable); | ||
| } finally { | ||
| selection.mockRestore(); | ||
| } | ||
| }); | ||
|
|
||
| it('reports files that cannot be read while filtering a bulk move by importance', async () => { | ||
| await mkdir(file(UNREAD, 'unreadable.notify')); | ||
|
|
||
| const result = await service.archiveAll(NotificationImportance.INFO); | ||
|
|
||
| expect(result.successes).toBe(COUNT); | ||
| expect(result.errors).toHaveLength(1); | ||
| expect(result.errorOccurred).toBe(true); | ||
| await expect( | ||
| new NotificationsResolver(service).archiveAll(NotificationImportance.INFO) | ||
| ).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it('does not fail a delete because an unrelated id in the same batch failed', async () => { | ||
| const internal = service as unknown as { | ||
| enqueueOverviewUpdate<T>(action: () => Promise<T>): Promise<T>; | ||
| synchronizeNotifications(ids: Iterable<string>): Promise<unknown>; | ||
| readNotificationCopy(id: string, type: NotificationType): Promise<unknown>; | ||
| }; | ||
| const read = internal.readNotificationCopy.bind(service); | ||
| const spy = vi | ||
| .spyOn(internal, 'readNotificationCopy') | ||
| .mockImplementation((id, type) => | ||
| id === 'unrelated.notify' ? Promise.reject(new Error('read failed')) : read(id, type) | ||
| ); | ||
| const synchronize = internal.synchronizeNotifications.bind(service); | ||
| let deletionJoined = false; | ||
| const joinSpy = vi | ||
| .spyOn(internal, 'synchronizeNotifications') | ||
| .mockImplementation((requested: Iterable<string>) => { | ||
| const list = [...requested]; | ||
| // Watcher events request one id at a time; only the deletion requests the whole folder. | ||
| if (list.length === COUNT) deletionJoined = true; | ||
| return synchronize(list); | ||
| }); | ||
| let release: (() => void) | undefined; | ||
| const blocker = internal.enqueueOverviewUpdate( | ||
| () => new Promise<void>((resolve) => (release = resolve)) | ||
| ); | ||
| try { | ||
| await vi.waitFor(() => expect(release).toBeDefined()); | ||
| const unrelated = synchronize(['unrelated.notify']); | ||
| const deletion = service.deleteNotifications(ARCHIVE); | ||
| await vi.waitFor(() => expect(deletionJoined).toBe(true)); | ||
| release?.(); | ||
| await blocker; | ||
| await expect(deletion).resolves.toBeDefined(); | ||
| await expect(unrelated).resolves.toBeDefined(); | ||
| } finally { | ||
| release?.(); | ||
| joinSpy.mockRestore(); | ||
| spy.mockRestore(); | ||
| } | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.