|
1 | 1 | defmodule Livebook.SessionTest do
|
2 | 2 | use ExUnit.Case, async: true
|
3 | 3 |
|
| 4 | + import ExUnit.CaptureLog |
4 | 5 | import Livebook.HubHelpers
|
5 | 6 | import Livebook.AppHelpers
|
6 | 7 | import Livebook.SessionHelpers
|
@@ -2092,6 +2093,100 @@ defmodule Livebook.SessionTest do
|
2092 | 2093 | end
|
2093 | 2094 | end
|
2094 | 2095 |
|
| 2096 | + describe "code evaluation logging" do |
| 2097 | + 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 | + |
| 2103 | + unique_id = Utils.random_short_id() |
| 2104 | + # Use random ID to uniquely identify the source code of this cell |
| 2105 | + Session.set_cell_attributes(session.pid, cell_id, %{source: "#{unique_id}"}) |
| 2106 | + |
| 2107 | + log = |
| 2108 | + capture_log([level: :info, metadata: [:session_mode, :code, :event]], fn -> |
| 2109 | + Session.queue_cell_evaluation(session.pid, cell_id) |
| 2110 | + assert_receive {:operation, {:add_cell_evaluation_response, _, ^cell_id, _, _}} |
| 2111 | + end) |
| 2112 | + |
| 2113 | + # Logs from other test might be captured, so we're using an unique_id |
| 2114 | + assert log =~ "code=#{unique_id}" |
| 2115 | + assert log =~ "Evaluating code" |
| 2116 | + assert log =~ "session_mode=default" |
| 2117 | + assert log =~ "event=code.evaluate" |
| 2118 | + end |
| 2119 | + |
| 2120 | + test "logs code evaluation for preview apps" do |
| 2121 | + session = start_session() |
| 2122 | + Session.subscribe(session.id) |
| 2123 | + |
| 2124 | + slug = Utils.random_short_id() |
| 2125 | + 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) |
| 2142 | + |
| 2143 | + unique_id = Utils.random_short_id() |
| 2144 | + # 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, _}} |
| 2147 | + |
| 2148 | + log = |
| 2149 | + 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, _, _}} |
| 2152 | + end) |
| 2153 | + |
| 2154 | + # Logs from other test might be captured, so we're using an unique_id |
| 2155 | + assert log =~ "code=#{unique_id}" |
| 2156 | + assert log =~ "Evaluating code" |
| 2157 | + assert log =~ "session_mode=app" |
| 2158 | + assert log =~ "event=code.evaluate" |
| 2159 | + end |
| 2160 | + |
| 2161 | + test "does not log code evaluation for permanent apps" do |
| 2162 | + slug = Utils.random_short_id() |
| 2163 | + app_settings = %{Notebook.AppSettings.new() | slug: slug} |
| 2164 | + |
| 2165 | + unique_id = Utils.random_short_id() |
| 2166 | + # Use random ID to uniquely identify the source code of this cell |
| 2167 | + code_cell = %{Notebook.Cell.new(:code) | source: "#{unique_id}"} |
| 2168 | + section = %{Notebook.Section.new() | cells: [code_cell]} |
| 2169 | + notebook = %{Notebook.new() | sections: [section], app_settings: app_settings} |
| 2170 | + |
| 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 | + |
| 2179 | + log = |
| 2180 | + 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, _, _}} |
| 2183 | + end) |
| 2184 | + |
| 2185 | + # Logs from other test might be captured, so we're using an unique_id |
| 2186 | + refute log =~ unique_id |
| 2187 | + end |
| 2188 | + end |
| 2189 | + |
2095 | 2190 | defp start_session(opts \\ []) do
|
2096 | 2191 | {:ok, session} = Livebook.Sessions.create_session(opts)
|
2097 | 2192 |
|
|
0 commit comments