Skip to content

Commit 6c2c430

Browse files
Improve logging tests (#3080)
1 parent ef4ac9d commit 6c2c430

File tree

1 file changed

+33
-42
lines changed

1 file changed

+33
-42
lines changed

test/livebook/session_test.exs

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,14 +2095,16 @@ defmodule Livebook.SessionTest do
20952095

20962096
describe "code evaluation logging" do
20972097
test "logs code evaluation for regular sessions" do
2098-
session = start_session()
2099-
Session.subscribe(session.id)
2100-
2101-
{_section_id, cell_id} = insert_section_and_cell(session.pid)
2102-
21032098
unique_id = Utils.random_short_id()
21042099
# Use random ID to uniquely identify the source code of this cell
2105-
Session.set_cell_attributes(session.pid, cell_id, %{source: "#{unique_id}"})
2100+
code_cell = %{Notebook.Cell.new(:code) | source: ~s/"#{unique_id}"/}
2101+
section = %{Notebook.Section.new() | cells: [code_cell]}
2102+
notebook = %{Notebook.new() | sections: [section]}
2103+
2104+
session = start_session(notebook: notebook)
2105+
Session.subscribe(session.id)
2106+
2107+
cell_id = code_cell.id
21062108

21072109
log =
21082110
capture_log([level: :info, metadata: [:session_mode, :code, :event]], fn ->
@@ -2111,48 +2113,41 @@ defmodule Livebook.SessionTest do
21112113
end)
21122114

21132115
# Logs from other test might be captured, so we're using an unique_id
2114-
assert log =~ "code=#{unique_id}"
2116+
assert log =~ ~s/code="#{unique_id}"/
21152117
assert log =~ "Evaluating code"
21162118
assert log =~ "session_mode=default"
21172119
assert log =~ "event=code.evaluate"
21182120
end
21192121

21202122
test "logs code evaluation for preview apps" do
2121-
session = start_session()
2122-
Session.subscribe(session.id)
2123-
21242123
slug = Utils.random_short_id()
21252124
app_settings = %{Notebook.AppSettings.new() | slug: slug}
2126-
Session.set_app_settings(session.pid, app_settings)
2127-
2128-
{section_id, _cell_id} = insert_section_and_cell(session.pid)
2129-
2130-
Apps.subscribe()
2131-
Session.deploy_app(session.pid)
2132-
assert_receive {:app_created, %{slug: ^slug, pid: app_pid}}
2133-
2134-
on_exit(fn ->
2135-
App.close(app_pid)
2136-
end)
2137-
2138-
session_id = App.get_session_id(app_pid)
2139-
{:ok, app_session} = Livebook.Sessions.fetch_session(session_id)
2140-
2141-
Session.subscribe(app_session.id)
21422125

21432126
unique_id = Utils.random_short_id()
21442127
# Use random ID to uniquely identify the source code of this cell
2145-
Session.insert_cell(app_session.pid, section_id, 0, :code, %{source: "#{unique_id}"})
2146-
assert_receive {:operation, {:insert_cell, _, ^section_id, 0, :code, cell_id, _}}
2128+
code_cell = %{Notebook.Cell.new(:code) | source: ~s/"#{unique_id}"/}
2129+
section = %{Notebook.Section.new() | cells: [code_cell]}
2130+
notebook = %{Notebook.new() | sections: [section], app_settings: app_settings}
2131+
2132+
session = start_session(notebook: notebook)
2133+
Session.subscribe(session.id)
21472134

21482135
log =
21492136
capture_log([level: :info, metadata: [:session_mode, :code, :event]], fn ->
2150-
Session.queue_cell_evaluation(app_session.pid, cell_id)
2151-
assert_receive {:operation, {:add_cell_evaluation_response, _, ^cell_id, _, _}}
2137+
Apps.subscribe()
2138+
Session.deploy_app(session.pid)
2139+
assert_receive {:app_created, %{slug: ^slug, pid: app_pid}}
2140+
2141+
on_exit(fn ->
2142+
App.close(app_pid)
2143+
end)
2144+
2145+
assert_receive {:app_updated,
2146+
%{pid: ^app_pid, sessions: [%{app_status: %{execution: :executed}}]}}
21522147
end)
21532148

21542149
# Logs from other test might be captured, so we're using an unique_id
2155-
assert log =~ "code=#{unique_id}"
2150+
assert log =~ ~s/code="#{unique_id}"/
21562151
assert log =~ "Evaluating code"
21572152
assert log =~ "session_mode=app"
21582153
assert log =~ "event=code.evaluate"
@@ -2164,22 +2159,18 @@ defmodule Livebook.SessionTest do
21642159

21652160
unique_id = Utils.random_short_id()
21662161
# Use random ID to uniquely identify the source code of this cell
2167-
code_cell = %{Notebook.Cell.new(:code) | source: "#{unique_id}"}
2162+
code_cell = %{Notebook.Cell.new(:code) | source: ~s/"#{unique_id}"/}
21682163
section = %{Notebook.Section.new() | cells: [code_cell]}
21692164
notebook = %{Notebook.new() | sections: [section], app_settings: app_settings}
21702165

2171-
app_pid = deploy_notebook_sync(notebook, permanent: true)
2172-
session_id = App.get_session_id(app_pid)
2173-
2174-
{:ok, app_session} = Livebook.Sessions.fetch_session(session_id)
2175-
2176-
Session.subscribe(app_session.id)
2177-
cell_id = code_cell.id
2178-
21792166
log =
21802167
capture_log([level: :info, metadata: [:session_mode, :code, :event]], fn ->
2181-
Session.queue_cell_evaluation(app_session.pid, cell_id)
2182-
assert_receive {:operation, {:add_cell_evaluation_response, _, ^cell_id, _, _}}
2168+
Apps.subscribe()
2169+
2170+
app_pid = deploy_notebook_sync(notebook, permanent: true)
2171+
2172+
assert_receive {:app_updated,
2173+
%{pid: ^app_pid, sessions: [%{app_status: %{execution: :executed}}]}}
21832174
end)
21842175

21852176
# Logs from other test might be captured, so we're using an unique_id

0 commit comments

Comments
 (0)