Ver código fonte

use lua socket to check server availability because travis has no netstat

Nick Hildebrant 10 anos atrás
pai
commit
1a984a3b1b
2 arquivos alterados com 23 adições e 16 exclusões
  1. 4 2
      test/ci_tests/01_basic/basic_spec.lua
  2. 19 14
      test/ci_tests/civet.lua

+ 4 - 2
test/ci_tests/01_basic/basic_spec.lua

@@ -1,5 +1,5 @@
 civet = require "test/ci_tests/civet"
-curl = require "cURL"
+local curl = require "cURL"
 
 describe("civetweb basic", function()
 
@@ -19,12 +19,14 @@ describe("civetweb basic", function()
       out = out .. str
     end
 
-    curl.easy()
+    local c = curl.easy()
       :setopt_url('http://localhost:' .. civet.port .. "/")
       :setopt_writefunction(capture)
       :perform()
     :close()
 
+    --print('rescode:' .. c.getinfo(curl.INFO_RESPONSE_CODE))
+
     assert.are.equal('Index of', string.match(out, 'Index of'))
     assert.are.equal('01_basic_test_dir', string.match(out, '01_basic_test_dir'))
     assert.are.equal('01_basic_test_file', string.match(out, '01_basic_test_file'))

+ 19 - 14
test/ci_tests/civet.lua

@@ -1,7 +1,10 @@
+socket = require "socket"
 
 local civet = {}
 
 civet.port=12345
+civet.max_retry=100
+civet.start_delay=0.1
 
 function civet.start(docroot)
   -- TODO: use a property
@@ -11,25 +14,27 @@ function civet.start(docroot)
   .. " -document_root " .. docroot
   .. " > /dev/null 2>&1 &"
   ))
-  -- wait until the server is listening (TODO: Linux only)
-  while true do
-    local f = assert(io.popen('netstat -an | grep '
-      .. civet.port .. ' | grep -i listen'))
-    local out = f:read('*all')
-    if string.match(out, civet.port) then break end
-    f:close()
+  -- wait until the server answers
+  for i=1,civet.max_retry do
+    local s = socket.connect('127.0.0.1', civet.port)
+    if s then
+      s:close()
+      break
+    end
+    socket.select(nil, nil, civet.start_delay) -- sleep
   end
 end
 
 function civet.stop()
   os.execute('killall civetweb')
-  -- wait until the server is listening (TODO: Linux only)
-  while true do
-    local f = assert(io.popen('netstat -an | grep '
-      .. civet.port .. ' | grep -i listen'))
-    local out = f:read('*all')
-    if not string.match(out, civet.port) then break end
-    f:close()
+  -- wait until the server port closes
+  for i=1,civet.max_retry do
+    local s = socket.connect('127.0.0.1', civet.port)
+    if not s then
+      break
+    end
+    s:close()
+    socket.select(nil, nil, civet.start_delay) -- sleep
   end
 end