web/src/regression/util/TestResourceManager.ts
* place and for easy resource cleanup at the end of tests. Also prints which resources are created
* and destroyed in case tests are aborted midway through and manual cleanup is required.
*/
export class TestResourceManager {

This comment has been minimized.

Copy link
@lguychard

lguychard yesterday

Member

Rather than introducing a new class/concept to the codebase, I would consider reusing Disposable (an Unsubscribable supporting async resource disposal) pattern from sourcegraph-typescript (example usage) :

// Return `Promise<AsyncDisposable>` instead of `Promise<void>`.
  export async function ensureLoggedInOrCreateTestUser(...): Promise<AsyncDisposable> {
      console.log(`Creating test user "${name}"`)
      return {
          dispose: () => {
              console.log(`Destroying user ${name}`)
              return deleteUser(...)
          }
      }
  }


  describe('Search regression test suite', () => {

      const disposables = new Set<Disposable | AsyncDisposable | Unsubscribable>()

      // Free resources created during testing. 
      afterAll(() => disposeAllAsync(disposables))

      test('something', () => {
          disposables.add(
              await ensureLoggedInOrCreateTestUser({...})
          )
      })
  })

Pros:

  • Developers working in our codebase are already familiar with the subscription / subscription bag pattern (and, if they're not, it is documented).
  • The cleanup logic is encapsulated, and lives next to the resource creation logic. It is not duplicated if ensureLoggedInOrCreateTestUser() is called from multiple test suites. Callers need not look up how a given resource should be cleaned up, they simply need to handle the returned disposable / subscription.
Select a reply ctrl .

The content you are editing has changed. Please try again.

Attach files by dragging & dropping, selecting or pasting them. Uploading your files… We don’t support that file type. with a GIF, JPEG, JPG, PNG, DOCX, GZ, LOG, PDF, PPTX, TXT, XLSX or ZIP. Attaching documents requires write permission to this repository. with a GIF, JPEG, JPG, PNG, DOCX, GZ, LOG, PDF, PPTX, TXT, XLSX or ZIP. We don’t support that file type. with a GIF, JPEG, JPG, PNG, DOCX, GZ, LOG, PDF, PPTX, TXT, XLSX or ZIP. Yowza, that’s a big file with a file smaller than 10MB. This file is empty. with a file that’s not empty. This file is hidden. with another file. Something went really wrong, and we can’t process that file.

Nothing to preview

Pick your reaction

Copy link
Reference in new issue

Reference in new issue

sourcegraph
Repositories
@lguychard

lguychard yesterday

Member

Choose a reason for hiding this comment

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

Rather than introducing a new class/concept to the codebase, I would consider reusing Disposable (an Unsubscribable supporting async resource disposal) pattern from sourcegraph-typescript (example usage) :

// Return `Promise<AsyncDisposable>` instead of `Promise<void>`.
  export async function ensureLoggedInOrCreateTestUser(...): Promise<AsyncDisposable> {
      console.log(`Creating test user "${name}"`)
      return {
          dispose: () => {
              console.log(`Destroying user ${name}`)
              return deleteUser(...)
          }
      }
  }


  describe('Search regression test suite', () => {

      const disposables = new Set<Disposable | AsyncDisposable | Unsubscribable>()

      // Free resources created during testing. 
      afterAll(() => disposeAllAsync(disposables))

      test('something', () => {
          disposables.add(
              await ensureLoggedInOrCreateTestUser({...})
          )
      })
  })

Pros:

  • Developers working in our codebase are already familiar with the subscription / subscription bag pattern (and, if they're not, it is documented).
  • The cleanup logic is encapsulated, and lives next to the resource creation logic. It is not duplicated if ensureLoggedInOrCreateTestUser() is called from multiple test suites. Callers need not look up how a given resource should be cleaned up, they simply need to handle the returned disposable / subscription.

Pick your reaction

Select a reply ctrl .

The content you are editing has changed. Please try again.

Attach files by dragging & dropping, selecting or pasting them. Uploading your files… We don’t support that file type. with a GIF, JPEG, JPG, PNG, DOCX, GZ, LOG, PDF, PPTX, TXT, XLSX or ZIP. Attaching documents requires write permission to this repository. with a GIF, JPEG, JPG, PNG, DOCX, GZ, LOG, PDF, PPTX, TXT, XLSX or ZIP. We don’t support that file type. with a GIF, JPEG, JPG, PNG, DOCX, GZ, LOG, PDF, PPTX, TXT, XLSX or ZIP. Yowza, that’s a big file with a file smaller than 10MB. This file is empty. with a file that’s not empty. This file is hidden. with another file. Something went really wrong, and we can’t process that file.

Nothing to preview

@lguychard
Select a reply ctrl .

Attach files by dragging & dropping, selecting or pasting them. Uploading your files… We don’t support that file type. with a GIF, JPEG, JPG, PNG, DOCX, GZ, LOG, PDF, PPTX, TXT, XLSX or ZIP. Attaching documents requires write permission to this repository. with a GIF, JPEG, JPG, PNG, DOCX, GZ, LOG, PDF, PPTX, TXT, XLSX or ZIP. We don’t support that file type. with a GIF, JPEG, JPG, PNG, DOCX, GZ, LOG, PDF, PPTX, TXT, XLSX or ZIP. Yowza, that’s a big file with a file smaller than 10MB. This file is empty. with a file that’s not empty. This file is hidden. with another file. Something went really wrong, and we can’t process that file.

Nothing to preview