Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions test/plausible/session/cache_store_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,20 @@ defmodule Plausible.Session.CacheStoreTest do
end

describe "collapse order" do
defp new_site_id() do
[[site_id]] =
Plausible.ClickhouseRepo.query!("select max(site_id) + rand() from sessions_v2 FINAL").rows

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess max(site_id) + 1 works just as good. Not sure.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be part of the fixtures? Is the query expensive?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise we can use the same approach as all the other tests use, a custom large int for site_id.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be part of the fixtures? Is the query expensive?

I'll check 👍

Otherwise we can use the same approach as all the other tests use, a custom large int for site_id.

What do you mean? Which approach? Random ints we currently use are clearly prone to clashing.

@ruslandoga ruslandoga Dec 13, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be part of the fixtures? Is the query expensive?

Finished in 41.2 seconds (13.7s async, 27.4s sync)
9 doctests, 1937 tests, 0 failures, 29 excluded

vs

Finished in 116.0 seconds (69.3s async, 46.7s sync)
9 doctests, 1937 tests, 2 failures, 29 excluded

Clearly not worth it, this workaround is again, specific to that test.

@ruslandoga ruslandoga Dec 13, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's fine if it works, sorry to be nit-picky :)


Another idea I had was to use System.unique_integer([:positive]) but since it can overlap with sites.id from PostgreSQL, I'd read the current sequence value for sites and add a million (a number large enough to cover all sites inserted into PostgreSQL during the test run) and then add System.unique_integer([:positive]) to ensure uniqueness in ClickHouse-only tests. And that would be used in fixtures, since the cost is static (select nextval('sites_id_seq') from PostgreSQL or something, in test_helpers.exs).

@ruslandoga ruslandoga Dec 13, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# test_helpers.exs, there might be a better place to put it than app env
[[min_sites_id]] = Plausible.Repo.query!("select nextvar('sites_id_seq')").rows
Application.put_env(:plausible, :tests, min_sites_id: min_sites_id)
# fixtures
def event do
  # ...
  site_id: Keyword.fetch!(Application.fetch_env!(:plausible, :tests), :min_sites_id) + 1_000_000 + System.unique_integer([:positive])
  # ...
end

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

application env is another shared state we're suffering from, I'm gonna just go ahead and merge it


site_id
end

defp flush(events) do
for e <- events, do: CacheStore.on_event(e, nil)
Plausible.Session.WriteBuffer.flush()
end

test "across parts" do
e = build(:event, name: "pageview")
e = build(:event, name: "pageview", site_id: new_site_id())

flush([%{e | pathname: "/"}])
flush([%{e | pathname: "/exit"}])
Expand All @@ -138,7 +145,7 @@ defmodule Plausible.Session.CacheStoreTest do
end

test "within parts" do
e = build(:event, name: "pageview")
e = build(:event, name: "pageview", site_id: new_site_id())

flush([
%{e | pathname: "/"},
Expand All @@ -154,7 +161,7 @@ defmodule Plausible.Session.CacheStoreTest do
end

test "across and within parts" do
e = build(:event, name: "pageview")
e = build(:event, name: "pageview", site_id: new_site_id())

flush([
%{e | pathname: "/"},
Expand Down