Browse Source

Add use of process unit for windows OS, allows to add a timeout for that host

git-svn-id: trunk@32951 -
pierre 9 years ago
parent
commit
c638559dc1
1 changed files with 56 additions and 0 deletions
  1. 56 0
      tests/utils/redir.pp

+ 56 - 0
tests/utils/redir.pp

@@ -34,6 +34,7 @@ Interface
 {$endif}
 {$endif}
 {$ifdef windows}
 {$ifdef windows}
 {$define implemented}
 {$define implemented}
+{$define USES_UNIT_PROCESS}
 {$endif}
 {$endif}
 {$ifdef linux}
 {$ifdef linux}
 {$define implemented}
 {$define implemented}
@@ -118,6 +119,9 @@ Uses
 
 
 {$endif}
 {$endif}
 
 
+{$ifdef USES_UNIT_PROCESS}
+  process,
+{$endif USES_UNIT_PROCESS}
 
 
 {$ifdef usedos}
 {$ifdef usedos}
   dos;
   dos;
@@ -264,10 +268,12 @@ begin
     end;
     end;
 end;
 end;
 {$else}
 {$else}
+{$ifndef USES_UNIT_PROCESS}
 function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString;Flags:TExecuteFlags=[]):integer;
 function ExecuteProcess(Const Path: AnsiString; Const ComLine: AnsiString;Flags:TExecuteFlags=[]):integer;
 begin
 begin
     result:=ExecuteProcess(path,comline);
     result:=ExecuteProcess(path,comline);
 end;
 end;
+{$endif ndef USES_UNIT_PROCESS}
 {$endif}
 {$endif}
 {$ifend}
 {$ifend}
 {$endif}
 {$endif}
@@ -1060,6 +1066,56 @@ begin
 end;
 end;
 {$endif def UNIX}
 {$endif def UNIX}
 
 
+
+
+{****************************************************************************
+                                Helpers
+****************************************************************************}
+
+{$ifdef USES_UNIT_PROCESS}
+const
+  max_count = 60000; { should be 60 seconds }
+
+function ExecuteProcess(const Path: string; const ComLine: string; Flags:TExecuteFlags=[]): integer;
+var
+  P: TProcess;
+  counter : longint;
+  TerminateSentCount : longint;
+
+begin
+  result := -1;
+  counter:=0;
+  TerminateSentCount:=0;
+
+  P := TProcess.Create(nil);
+  try
+    P.CommandLine := Path + ' ' + ComLine;
+
+    P.InheritHandles:=(execinheritshandles in flags);
+
+    P.Execute;
+    while P.Running do
+      begin
+        if counter>max_count then
+          begin
+            P.Terminate(255);
+            if TerminateSentCount=0 then
+              Writeln(stderr,'Terminate requested for ',Path);
+            Inc(TerminateSentCount);
+          end;
+
+        Sleep(1);
+        inc(counter);
+      end;
+
+    result := P.ExitStatus;
+  finally
+    P.Free;
+  end;
+end;
+{$endif HAS_UNIT_PROCESS}
+
+
   procedure DosExecute(ProgName, ComLine : String);
   procedure DosExecute(ProgName, ComLine : String);