diff --git a/lib/inngest/function.ex b/lib/inngest/function.ex index 761ed6c..5b40e24 100644 --- a/lib/inngest/function.ex +++ b/lib/inngest/function.ex @@ -167,6 +167,7 @@ defmodule Inngest.Function do } |> maybe_debounce() |> maybe_batch_events() + |> maybe_rate_limit() ] ++ handler end diff --git a/test/inngest/function/cases/batch_events_test.exs b/test/inngest/function/cases/batch_events_test.exs index 99d3020..ad3954e 100644 --- a/test/inngest/function/cases/batch_events_test.exs +++ b/test/inngest/function/cases/batch_events_test.exs @@ -1,8 +1,7 @@ defmodule Inngest.Function.Cases.BatchEventsTest do use ExUnit.Case, async: true - alias Inngest.Test.DevServer - import Inngest.Test.Helper - # TODO: Add test after moving batching logic to OSS + # alias Inngest.Test.DevServer + # import Inngest.Test.Helper end diff --git a/test/inngest/function/cases/rate_limit_test.exs b/test/inngest/function/cases/rate_limit_test.exs new file mode 100644 index 0000000..f409223 --- /dev/null +++ b/test/inngest/function/cases/rate_limit_test.exs @@ -0,0 +1,36 @@ +defmodule Inngest.Function.Cases.RateLimitTest do + use ExUnit.Case, async: true + + alias Inngest.Test.DevServer + import Inngest.Test.Helper + + @default_sleep 10_000 + + @tag :integration + test "should only run 2 out of 10" do + event_ids = Enum.map(1..10, fn _ -> send_test_event("test/plug.ratelimit") end) + + Process.sleep(@default_sleep) + + fn_runs = + event_ids + |> Enum.map(fn id -> + {:ok, %{"data" => data}} = DevServer.run_ids(id) + + if Enum.count(data) == 1 do + assert [ + %{ + "output" => "Rate Limited", + "status" => "Completed", + "run_id" => run_id + } + ] = data + else + nil + end + end) + |> Enum.filter(&(!is_nil(&1))) + + assert Enum.count(fn_runs) <= 2 + end +end diff --git a/test/support/cases/rate_limit_fn.ex b/test/support/cases/rate_limit_fn.ex new file mode 100644 index 0000000..77b7165 --- /dev/null +++ b/test/support/cases/rate_limit_fn.ex @@ -0,0 +1,21 @@ +defmodule Inngest.Test.Case.RateLimitFn do + @moduledoc false + + use Inngest.Function + alias Inngest.{FnOpts, Trigger} + + @func %FnOpts{ + id: "ratelimit-fn", + name: "RateLimit Function", + rate_limit: %{ + limit: 2, + period: "5s" + } + } + @trigger %Trigger{event: "test/plug.ratelimit"} + + @impl true + def exec(_ctx, _args) do + {:ok, "Rate Limited"} + end +end