Skip to content

feat(tests): integration with Oban and Broadway#1056

Draft
solnic wants to merge 3 commits into
masterfrom
feat/tests-integration-with-common-libs
Draft

feat(tests): integration with Oban and Broadway#1056
solnic wants to merge 3 commits into
masterfrom
feat/tests-integration-with-common-libs

Conversation

@solnic
Copy link
Copy Markdown
Collaborator

@solnic solnic commented May 13, 2026

Streamlines test setup for popular Oban and Broadway libs, so that we can have tests like this:

defmodule SyncService.SyncWorkerTest do
  use SyncService.DataCase, async: false

  import Sentry.Test.Assertions
  import SyncService.Test.Fixtures

  alias SyncService.SyncWorker
  alias SyncService.Test.Oban, as: TestOban

  setup do
    Sentry.Test.setup_sentry(allowance: [Oban])

    start_supervised!(TestOban.child_spec())

    :ok
  end

  defp run_worker!(args), do: TestOban.run!(SyncWorker, args)

  describe "upsert via real Oban worker process" do
    test "stamps sync_rev and emits metrics" do
      org_id = setup_org()

      assert :success =
               run_worker!(%{
                 org_id: org_id,
                 operation: "upsert",
                 user_id: "test-user-1",
                 entity_type: "organizations",
                 entities: [
                   %{"id" => "org-pipe-1", "name" => "Pipe Org", "slug" => "pipe-org"}
                 ]
               })

      org = get_organization(org_id, "org-pipe-1")
      assert org
      assert org.sync_rev >= 1

      assert_sentry_metric(:counter,
        name: "sync.job.started",
        attributes: %{operation: "upsert"}
      )

      assert_sentry_metric(:counter,
        name: "sync.job.completed",
        attributes: %{operation: "upsert"}
      )

      assert_sentry_metric(:distribution,
        name: "sync.job.duration",
        unit: "millisecond",
        attributes: %{operation: "upsert"}
      )
    end
  end
end

@solnic solnic changed the title Feat/tests integration with common libs feat(tests): integration with Oban and Broadway May 13, 2026
@solnic solnic force-pushed the feat/tests-support-async-tests-with-tp branch 7 times, most recently from 9f7c9b8 to d18a6d5 Compare May 18, 2026 10:39
@solnic solnic force-pushed the feat/tests-integration-with-common-libs branch from 260edad to ee71a75 Compare May 19, 2026 08:15
Base automatically changed from feat/tests-support-async-tests-with-tp to master May 20, 2026 11:50
solnic and others added 3 commits May 21, 2026 09:06
Introduces the mechanism that future commits will use to install
per-test telemetry handlers for popular libraries (Oban, Broadway).
This commit ships only the infrastructure: option parsing, a unique
per-test handler ID, automatic detach on test exit, and a generic
__handle_allowance_event__/4 handler that calls allow_sentry_reports/2
in response to the configured event.

The internal allowance_handlers/1 dispatch is empty — any atom passed
under :allowance currently raises a clear ArgumentError naming the
unsupported entry. Oban and Broadway clauses land in follow-up
commits.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Installs telemetry handlers that capture the test pid at Oban job
insert time and route the worker's captured events back to that test
on job start. The pairing makes auto-allowance safe under async: true
even when multiple tests share an Oban supervisor, because every job
is uniquely tied to the test that scheduled it (rather than to
whichever telemetry handler happened to fire first).

The tag store lives in Sentry.Test.Registry as a public ETS table
(:sentry_test_oban_job_tags) alongside the existing scope-allow
table. Tags are dropped on :stop/:exception and defensively on test
exit so jobs that crash before completion don't leave stale entries
behind.

Handlers guard on `is_integer(job.id)` so synthetic jobs from inline
mode or ad-hoc telemetry simulations (no persisted id) are skipped
silently. The :inline / :manual Oban testing modes run jobs in the
test pid anyway, so this auto-allowance is a no-op for them — it
only adds capability for the production-like worker case from
issue #1052.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Adds Broadway to the :allowance dispatch. The handler subscribes to
[:broadway, :processor, :start] and [:broadway, :batch_processor, :start],
reads the first :sentry_test_owner found in metadata.messages[*].metadata,
and calls allow_sentry_reports/2 for that test pid.

This follows the same shape Broadway documents for Ecto sandbox
testing (metadata: %{ecto_sandbox: self()}) — no wrapper around
Broadway.test_message/3, no hidden state, and async-safe by design
because each message carries its origin test pid. Messages without
:sentry_test_owner are silently skipped.

Integration coverage adds a minimal Broadway pipeline
(PhoenixApp.TestBroadway) and a 3-test broadway_test.exs in
phoenix_app proving: (1) tagged + allowance captures, (2) untagged +
allowance does not, (3) tagged + no allowance does not. async: true
on the describe block to validate cross-test isolation through a
shared pipeline.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@solnic solnic force-pushed the feat/tests-integration-with-common-libs branch from ee71a75 to ea7e14f Compare May 21, 2026 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant