Skip to content

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

Draft
solnic wants to merge 5 commits into
feat/tests-support-async-tests-with-tpfrom
feat/tests-integration-with-common-libs
Draft

feat(tests): integration with Oban and Broadway#1056
solnic wants to merge 5 commits into
feat/tests-support-async-tests-with-tpfrom
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 and others added 5 commits May 13, 2026 13:47
Closes the gap that left buffered events (logs and metrics) emitted
from processes registered via Sentry.Test.allow_sentry_reports/2
silently dropped instead of routed to the test's collector under
async tests.

Changes
-------

* build_collecting_callback/0 becomes build_collecting_callback/1,
  capturing owner_pid at install time. The closure looks up
  owner_pid's metadata directly via NimbleOwnership.get_owned/3
  rather than walking [self() | $callers] to find the owner. This
  pins ownership lookups to the right test instead of relying on
  whichever scope happens to be reachable from the calling pid.
* find_collector/0 is removed (no other callers).
* A small caller_allowed_for?/1 helper preserves the prior safety
  check by confirming the calling pid (or its $callers) is in
  owner_pid's allow chain.

* Adds a worker→processor routing index in Sentry.Test.Registry
  (@processor_routing_table) with tag/lookup/drop helpers. The
  lookup short-circuits to nil when the table is not started so
  the production call path stays a single :ets.whereis/1.
* Sentry.Test.allow_sentry_reports/2 writes to the routing table
  using the owner pid's :sentry_telemetry_processor — no metadata
  format change required on the NimbleOwnership side.
* Sentry.TelemetryProcessor.processor_name/0 falls back to the
  routing table when the calling pid has no
  :sentry_telemetry_processor in its dictionary, so allowed
  workers' buffered events land in the owning test's per-test
  processor whose scheduler pid is in the test's allow chain.
* setup_collector/1's on_exit/1 cleans up routing rows pointing
  at this test's processor.

Async-safe under all known scenarios
------------------------------------

* async: false + buffered events from allowed pid — works.
* async: true + per-test Oban/Broadway + allowed pids — each test
  has its own processor; routing is keyed by worker pid so two
  tests cannot disagree about where a given worker's events land.
* async: true + shared global Oban + allowed pids — different
  worker pids → different routing entries → independent per-test
  processors.
* Process spawned without allow_sentry_reports/2 — unchanged; uses
  the global processor.

Adds two regression tests covering Sentry.Metrics.count/3 and
Logger.warning/1 emitted from an allowed pid.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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 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 4 times, most recently from 8d4ffae to 6fa9f76 Compare May 15, 2026 12:03
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