Skip to content

Commit

Permalink
Fix: get long_poll_timeout in seconds not milliesconds (#1834)
Browse files Browse the repository at this point in the history
Co-authored-by: Stefanos Mousafeiris <[email protected]>
  • Loading branch information
KyleAMathews and msfstef authored Oct 10, 2024
1 parent 41845cb commit a1127e5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule Electric.Plug.ServeShapePlug do
defmodule TimeUtils do
@oct9th2024 DateTime.from_naive!(~N[2024-10-09 00:00:00], "Etc/UTC")
def seconds_since_oct9th_2024_next_interval(conn) do
long_poll_timeout = conn.assigns.config[:long_poll_timeout]
long_poll_timeout = floor(conn.assigns.config[:long_poll_timeout] / 1000)
now = DateTime.utc_now()

diff_in_seconds = DateTime.diff(now, @oct9th2024, :second)
Expand Down
32 changes: 32 additions & 0 deletions packages/sync-service/test/electric/plug/serve_shape_plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,38 @@ defmodule Electric.Plug.ServeShapePlugTest do
end

describe "ServeShapePlug" do
test "seconds_since_oct9th_2024_next_interval" do
# Mock the conn struct with assigns
# 20 seconds
conn = %Plug.Conn{assigns: %{config: %{long_poll_timeout: 20000}}}

# Calculate the expected next interval
now = DateTime.utc_now()
oct9th2024 = DateTime.from_naive!(~N[2024-10-09 00:00:00], "Etc/UTC")
diff_in_seconds = DateTime.diff(now, oct9th2024, :second)
expected_interval = ceil(diff_in_seconds / 20) * 20

# Assert that the function returns the expected value
assert Electric.Plug.ServeShapePlug.TimeUtils.seconds_since_oct9th_2024_next_interval(conn) ==
expected_interval
end

test "seconds_since_oct9th_2024_next_interval with different timeout" do
# Mock the conn struct with a different timeout
# 30 seconds
conn = %Plug.Conn{assigns: %{config: %{long_poll_timeout: 30000}}}

# Calculate the expected next interval
now = DateTime.utc_now()
oct9th2024 = DateTime.from_naive!(~N[2024-10-09 00:00:00], "Etc/UTC")
diff_in_seconds = DateTime.diff(now, oct9th2024, :second)
expected_interval = ceil(diff_in_seconds / 30) * 30

# Assert that the function returns the expected value
assert Electric.Plug.ServeShapePlug.TimeUtils.seconds_since_oct9th_2024_next_interval(conn) ==
expected_interval
end

test "returns 400 for invalid params" do
conn =
conn(:get, %{"root_table" => ".invalid_shape"}, "?offset=invalid")
Expand Down

0 comments on commit a1127e5

Please sign in to comment.