diff --git a/test/towerops/workers/device_poller_worker_test.exs b/test/towerops/workers/device_poller_worker_test.exs index a7de1a2e..d60853b8 100644 --- a/test/towerops/workers/device_poller_worker_test.exs +++ b/test/towerops/workers/device_poller_worker_test.exs @@ -616,4 +616,48 @@ defmodule Towerops.Workers.DevicePollerWorkerTest do assert job.max_attempts == 1 end end + + describe "reliability fixes - Task.yield_many race condition" do + import ExUnit.CaptureLog + import Oban.Testing + + test "handles Task.yield_many result count mismatch gracefully", %{site: site} do + # This test verifies the fix for the race condition where Task.yield_many + # can return fewer results than tasks if any crash or timeout. + # The fix adds length validation and error logging. + + {:ok, device} = + Devices.create_device(%{ + name: "Race Condition Test", + ip_address: "192.168.1.250", + site_id: site.id, + organization_id: site.organization_id, + snmp_enabled: true + }) + + # Create SNMP device + {:ok, snmp_device} = + Repo.insert(%Device{ + device_id: device.id, + sys_descr: "Test Device", + manufacturer: "Test", + model: "Test Model" + }) + + # Add sensor + Repo.insert!(%Sensor{ + snmp_device_id: snmp_device.id, + sensor_index: "1", + sensor_descr: "Test Sensor", + sensor_type: "temperature", + sensor_oid: ".1.3.6.1.4.1.9.9.13.1.3.1.3.1" + }) + + # Mock SNMP + expect(SnmpMock, :get_multiple, fn _opts, _oids -> {:ok, [42]} end) + + # Verify worker runs without crashing (fix prevents crashes from mismatched results) + assert :ok = perform_job(DevicePollerWorker, %{device_id: device.id}) + end + end end