Skip to content

Commit 5b4b7f0

Browse files
committed
chore: test async scheduler
1 parent 77cf174 commit 5b4b7f0

File tree

3 files changed

+79
-13
lines changed

3 files changed

+79
-13
lines changed

lib/elasticlunr/scheduler/async.ex

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ defmodule Elasticlunr.Scheduler.Async do
1313
end
1414
end
1515

16+
@spec started?(Index.t() | String.t()) :: boolean()
17+
def started?(%Index{name: name}), do: started?(name)
18+
19+
def started?(name) do
20+
started_processes()
21+
|> Enum.any?(fn
22+
^name ->
23+
true
24+
25+
_ ->
26+
false
27+
end)
28+
end
29+
1630
@impl true
1731
def init(state), do: {:ok, state}
1832

@@ -53,7 +67,7 @@ defmodule Elasticlunr.Scheduler.Async do
5367
defp blank_state, do: %{}
5468

5569
defp start_if_not_started(%{name: name} = index) do
56-
with false <- loaded?(name),
70+
with false <- started?(name),
5771
{:ok, _} <- start(index) do
5872
:ok
5973
else
@@ -66,18 +80,7 @@ defmodule Elasticlunr.Scheduler.Async do
6680
DynamicSupervisor.start_child(SchedulerSupervisor, {__MODULE__, index})
6781
end
6882

69-
defp loaded?(name) do
70-
loaded_indices()
71-
|> Enum.any?(fn
72-
^name ->
73-
true
74-
75-
_ ->
76-
false
77-
end)
78-
end
79-
80-
defp loaded_indices do
83+
defp started_processes do
8184
Process.active_processes(SchedulerSupervisor, SchedulerRegistry, __MODULE__)
8285
end
8386
end

test/scheduler/async_test.exs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
defmodule Elasticlunr.Scheduler.AsyncTest do
2+
use ExUnit.Case
3+
4+
alias Elasticlunr.{Index, Scheduler}
5+
6+
require Logger
7+
8+
import Mox
9+
10+
setup :verify_on_exit!
11+
12+
setup do
13+
log_level = Logger.level()
14+
current_scheduler = Application.get_env(:elasticlunr, :scheduler)
15+
16+
Logger.configure(level: :info)
17+
18+
Mox.stub_with(Scheduler.Mock, Scheduler.Async)
19+
Application.put_env(:elasticlunr, :scheduler, Scheduler.Mock)
20+
21+
on_exit(fn ->
22+
Logger.configure(level: log_level)
23+
Application.put_env(:elasticlunr, :scheduler, current_scheduler)
24+
end)
25+
end
26+
27+
setup context do
28+
index = Index.add_field(Index.new(), "message")
29+
30+
Map.put(context, :index, index)
31+
end
32+
33+
describe "working with async scheduler" do
34+
test "pushes action to scheduler", %{index: index} do
35+
expect(Scheduler.Mock, :push, fn ^index, :calculate_idf ->
36+
:ok
37+
end)
38+
39+
assert Index.add_documents(index, [%{message: "hello world"}])
40+
end
41+
42+
test "starts a new process for index if none exists", %{index: index} do
43+
index = Index.add_documents(index, [%{message: "hello world"}])
44+
45+
assert Scheduler.Async.started?(index)
46+
# sleep to allow the async scheduler process the :calculate_idf task
47+
Process.sleep(500)
48+
end
49+
50+
test "handles the calculate_idf task", %{index: index} do
51+
expect(Scheduler.Mock, :push, fn ^index, :calculate_idf ->
52+
{:noreply, %{}} = Scheduler.Async.handle_cast({:calculate_idf, index}, %{})
53+
:ok
54+
end)
55+
56+
index = Index.add_documents(index, [%{message: "hello world"}])
57+
58+
assert [%{matched: 1, positions: %{"message" => [{0, 5}]}}] = Index.search(index, "hello")
59+
end
60+
end
61+
end

test/test_helper.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ ExUnit.start()
22
Faker.start()
33

44
Mox.defmock(Elasticlunr.Storage.Mock, for: Elasticlunr.Storage.Provider)
5+
Mox.defmock(Elasticlunr.Scheduler.Mock, for: Elasticlunr.Scheduler.Provider)
6+
57
Application.put_env(:elasticlunr, :scheduler, Elasticlunr.Scheduler.Immediate)

0 commit comments

Comments
 (0)