Skip to content

Commit

Permalink
add integration test for rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin67 committed Nov 27, 2023
1 parent d785a66 commit efda5dd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/inngest/function.ex
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ defmodule Inngest.Function do
}
|> maybe_debounce()
|> maybe_batch_events()
|> maybe_rate_limit()
] ++ handler
end

Expand Down
5 changes: 2 additions & 3 deletions test/inngest/function/cases/batch_events_test.exs
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions test/inngest/function/cases/rate_limit_test.exs
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 25 in test/inngest/function/cases/rate_limit_test.exs

View workflow job for this annotation

GitHub Actions / Test (Elixir 1.14 / OTP 24.3)

variable "run_id" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 25 in test/inngest/function/cases/rate_limit_test.exs

View workflow job for this annotation

GitHub Actions / Test (Elixir 1.14 / OTP 25.3)

variable "run_id" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 25 in test/inngest/function/cases/rate_limit_test.exs

View workflow job for this annotation

GitHub Actions / Test (Elixir 1.15 / OTP 24.3)

variable "run_id" is unused (if the variable is not meant to be used, prefix it with an underscore)

Check warning on line 25 in test/inngest/function/cases/rate_limit_test.exs

View workflow job for this annotation

GitHub Actions / Test (Elixir 1.15 / OTP 25.3)

variable "run_id" is unused (if the variable is not meant to be used, prefix it with an underscore)
}
] = data
else
nil
end
end)
|> Enum.filter(&(!is_nil(&1)))

assert Enum.count(fn_runs) <= 2
end
end
21 changes: 21 additions & 0 deletions test/support/cases/rate_limit_fn.ex
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit efda5dd

Please sign in to comment.