From 83d5b9d40d5c0e6b4e8ca74c27fa25edb9859ab3 Mon Sep 17 00:00:00 2001 From: Frankie Robertson Date: Tue, 5 Nov 2024 13:40:01 +0200 Subject: [PATCH] Add tests for revise argument to serve(...) --- test/revise.jl | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ test/runtests.jl | 1 + 2 files changed, 70 insertions(+) create mode 100644 test/revise.jl diff --git a/test/revise.jl b/test/revise.jl new file mode 100644 index 00000000..bff974f7 --- /dev/null +++ b/test/revise.jl @@ -0,0 +1,69 @@ +module ReviseTest + +using Test +using Oxygen; @oxidise +using ..Constants +using HTTP + +@get "/" function() + return text("Ok") +end + +# Test error message when Revise not used + +error_task = @async begin + for revise in (:lazy, :eager) + @test_throws "You must load Revise.jl before Oxygen.jl" serve(port=PORT, host=HOST, show_errors=false, show_banner=false, access_log=nothing, revise=revise) + end +end + +if timedwait(()->istaskdone(error_task), 60) == :timed_out + error("Timed out waiting for Revise usage error") +end + +# Test usage with user provided middleware + +module ReviseMock + #= + Need to support the following: + + Revise = Main.Revise + isempty(Revise.revision_queue) + wait(Revise.revision_event) + Revise.revise() + =# + + revision_queue = [nothing] # non-empty + revision_event = Base.Event() + notify(revision_event) # wait(...) always returns immediately + revise_called_count = 0 + + function revise() + global revise_called_count + revise_called_count += 1 + end +end + +invocation = [] + +function handler1(handler) + return function(req::HTTP.Request) + push!(invocation, 1) + handler(req) + end +end + +Oxygen.WAS_LOADED_AFTER_REVISE[] = true +@eval Main Revise = ReviseMock +serve(port=PORT, host=HOST, show_errors=false, show_banner=false, access_log=nothing, revise=:lazy, middleware=[handler1], async=true) +@test String(HTTP.get("$localhost/").body) == "Ok" +@test invocation == [1] +@test ReviseMock.revise_called_count == 1 +Main.Revise = nothing +Oxygen.WAS_LOADED_AFTER_REVISE[] = false + +terminate() +println() + +end + diff --git a/test/runtests.jl b/test/runtests.jl index 9d8ad460..5a6e69db 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -34,6 +34,7 @@ include("taskmanagement.jl") include("cronmanagement.jl") include("middlewaretests.jl") include("originaltests.jl") +include("revise.jl") #### Scenario Tests #### include("./scenarios/thunderingherd.jl")