From 6c524b4a1b9fec2fe4abf03fa83b8d6624c23e26 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Wed, 16 Feb 2022 22:15:28 -0300 Subject: tests: wait longer for mock server to start --- spec/fetch_spec.lua | 5 ++++- spec/util/test_env.lua | 38 +++++++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 14 deletions(-) (limited to 'spec') diff --git a/spec/fetch_spec.lua b/spec/fetch_spec.lua index 008a91c8..0087159b 100644 --- a/spec/fetch_spec.lua +++ b/spec/fetch_spec.lua @@ -39,7 +39,8 @@ describe("luarocks fetch #unit #mock", function() describe("fetch.fetch_url", function() it("fetches the url argument and returns the absolute path of the fetched file", function() - local fetchedfile = fetch.fetch_url("http://localhost:8080/file/a_rock.lua") + local fetchedfile, err = fetch.fetch_url("http://localhost:8080/file/a_rock.lua") + assert(fetchedfile, err) assert.truthy(are_same_files(fetchedfile, lfs.currentdir() .. "/a_rock.lua")) local fd = assert(io.open(fetchedfile, "r")) local fetchedcontent = assert(fd:read("*a")) @@ -109,6 +110,7 @@ describe("luarocks fetch #unit #mock", function() it("returns true and fetches the url into a temporary dir", function() local fetchedfile, tmpdir = fetch.fetch_url_at_temp_dir("http://localhost:8080/file/a_rock.lua", "test") + assert(fetchedfile, tmpdir) assert.truthy(are_same_files(fetchedfile, tmpdir .. "/a_rock.lua")) local fd = assert(io.open(fetchedfile, "r")) local fetchedcontent = assert(fd:read("*a")) @@ -121,6 +123,7 @@ describe("luarocks fetch #unit #mock", function() it("returns true and fetches the url into a temporary dir with custom filename", function() local fetchedfile, tmpdir = fetch.fetch_url_at_temp_dir("http://localhost:8080/file/a_rock.lua", "test", "my_a_rock.lua") + assert(fetchedfile, tmpdir) assert.truthy(are_same_files(fetchedfile, tmpdir .. "/my_a_rock.lua")) assert.truthy(string.find(tmpdir, "test")) local fd = assert(io.open(fetchedfile, "r")) diff --git a/spec/util/test_env.lua b/spec/util/test_env.lua index 35c27dc8..c40921cc 100644 --- a/spec/util/test_env.lua +++ b/spec/util/test_env.lua @@ -836,28 +836,40 @@ local function setup_luarocks() print("LuaRocks set up correctly!") end +local function mock_api_call(path) + if test_env.TEST_TARGET_OS == "windows" then + return test_env.execute(Q(test_env.testing_paths.win_tools .. "/wget") .. " --quiet --timeout=5 --tries=1 localhost:8080" .. path) + else + return test_env.execute("curl localhost:8080" .. path) + end +end + function test_env.mock_server_init() local testing_paths = test_env.testing_paths assert(test_env.need_rock("restserver-xavante")) - local final_command - local sleep_command + if test_env.TEST_TARGET_OS == "windows" then - final_command = test_env.execute_helper("start /b \"\" " .. Q(testing_paths.lua) .. " " .. Q(testing_paths.util_dir .. "/mock-server.lua") .. " " .. Q(testing_paths.fixtures_dir), true, test_env.env_variables) - sleep_command = "timeout 1 > NUL" + os.execute(test_env.execute_helper("start /b \"\" " .. Q(testing_paths.lua) .. " " .. Q(testing_paths.util_dir .. "/mock-server.lua") .. " " .. Q(testing_paths.fixtures_dir), true, test_env.env_variables)) else - final_command = test_env.execute_helper(testing_paths.lua .. " " .. testing_paths.util_dir .. "/mock-server.lua " .. testing_paths.fixtures_dir .. " &", true, test_env.env_variables) - sleep_command = "sleep 1" + os.execute(test_env.execute_helper(testing_paths.lua .. " " .. testing_paths.util_dir .. "/mock-server.lua " .. testing_paths.fixtures_dir .. " &", true, test_env.env_variables)) + end + + for _ = 1, 10 do + if mock_api_call("/api/tool_version") then + break + end + + if test_env.TEST_TARGET_OS == "windows" then + os.execute("timeout 1 > NUL") + else + os.execute("sleep 1") + end end - os.execute(final_command) - os.execute(sleep_command) + end function test_env.mock_server_done() - if test_env.TEST_TARGET_OS == "windows" then - os.execute(Q(test_env.testing_paths.win_tools .. "/wget") .. " --quiet --timeout=5 --tries=1 localhost:8080/shutdown") - else - os.execute("curl localhost:8080/shutdown") - end + mock_api_call("/shutdown") end local function find_binary_rock(src_rock, dir) -- cgit v1.2.3-55-g6feb