Skip to content

Commit

Permalink
add test sleep feature to assignment tests
Browse files Browse the repository at this point in the history
  • Loading branch information
padmaJS committed Dec 2, 2024
1 parent 8f802bd commit cca0ca0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
10 changes: 5 additions & 5 deletions lib/handin/assignment_submission_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ defmodule Handin.AssignmentSubmissionServer do
end

defp run_single_test(state, assignment_test) do
if assignment_test.enable_test_sleep do
Process.sleep(assignment_test.test_sleep_duration * 60 * 1000)
end

case @machine_api.exec(state.machine_id, "./#{assignment_test.id}.sh") do
{:ok, %{"exit_code" => 0} = response} ->
handle_successful_test(state, assignment_test, response)

if assignment_test.name == "save the output to a file" do
Process.sleep(120_000)
end

{:ok, response} ->
handle_failed_test(state, assignment_test, response)

Expand Down Expand Up @@ -249,7 +249,7 @@ defmodule Handin.AssignmentSubmissionServer do
%{
command: assignment_test.command,
assignment_test_id: assignment_test.id,
output: Base.decode64!(response["output"]),
output: "Good",
expected_output: response["expected_output"]
},
state
Expand Down
17 changes: 16 additions & 1 deletion lib/handin/assignments/assignment_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ defmodule Handin.Assignments.AssignmentTest do
field :ttl, :integer, default: 60
field :enable_custom_test, :boolean, default: false
field :custom_test, :string
field :enable_test_sleep, :boolean, default: false
field :test_sleep_duration, :integer

belongs_to :assignment, Assignment

Expand All @@ -42,7 +44,9 @@ defmodule Handin.Assignments.AssignmentTest do
:expected_output_file_content,
:ttl,
:enable_custom_test,
:custom_test
:custom_test,
:enable_test_sleep,
:test_sleep_duration
]

@doc false
Expand All @@ -54,6 +58,7 @@ defmodule Handin.Assignments.AssignmentTest do
|> maybe_validate_expected_output_type()
|> maybe_validate_points_on_pass()
|> maybe_validate_points_on_fail()
|> maybe_validate_test_sleep_duration()
end

def new_changeset(assignment_test, attrs) do
Expand Down Expand Up @@ -151,4 +156,14 @@ defmodule Handin.Assignments.AssignmentTest do
validate_required(changeset, [:command, :expected_output_type])
end
end

defp maybe_validate_test_sleep_duration(changeset) do
if get_field(changeset, :enable_test_sleep) do
changeset
|> validate_required(:test_sleep_duration)
|> validate_number(:test_sleep_duration, greater_than: 0, message: "must be greater than 0")
else
changeset
end
end
end
8 changes: 8 additions & 0 deletions lib/handin_web/live/assignment_live/tests.ex
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ defmodule HandinWeb.AssignmentLive.Tests do
label="Expected Text"
/>
<% end %>
<.input field={@form[:enable_test_sleep]} type="checkbox" label="Enable Test Sleep" />
<.input
:if={Phoenix.HTML.Form.normalize_value("checkbox", @form[:enable_test_sleep].value)}
field={@form[:test_sleep_duration]}
type="number"
label="Test Sleep Duration (minutes)"
step="1"
/>
<.button
class="text-white inline-flex items-center bg-primary-700 hover:bg-primary-800 focus:ring-4 focus:outline-none focus:ring-primary-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule Handin.Repo.Migrations.AddTestSleepDurationToAssignmentTest do
use Ecto.Migration

def change do
alter table(:assignment_tests) do
add :enable_test_sleep, :boolean, default: false
add :test_sleep_duration, :integer
end
end
end

0 comments on commit cca0ca0

Please sign in to comment.