Enable overriding test default timeout with TIMEOUT env
authorIgor Kulaychuk <i.kulaychuk@samsung.com>
Wed, 20 Jun 2018 19:57:22 +0000 (22:57 +0300)
committerIgor Kulaychuk <i.kulaychuk@samsung.com>
Wed, 20 Jun 2018 20:11:18 +0000 (23:11 +0300)
tests/README.md
tests/runner/Runner.cs

index 5656040..4126f57 100644 (file)
@@ -7,7 +7,7 @@ By default test runner looks for debugger binary in `./bin` directory and expect
 Test are run through dotnet CLI from the project root directory:
 ```
 cd tests
-dotnet test
+dotnet build && dotnet test runner
 ```
 
 It is possible to override path to the debugger binary through `PIPE` environment variable:
@@ -31,6 +31,8 @@ By default test binaries are discovered recursively under project `tests` direct
 
 Refer to `run_tests_sdb.sh` as an example of running tests on Tizen device.
 
+`TIMEOUT` environment variable overrides a default timeout (in seconds) for test's `Expect` command.
+
 ## Writing new tests
 
 ### Add new test
@@ -96,6 +98,8 @@ namespace ExampleTest
 
 * `string TestBin` is a full path to test dll file.
 
+* `int DefaultExpectTimeout` is a timeout for `Expect` operation, default value is 20 (seconds).
+
 * `ITestOutputHelper Output` is a log interface from Xunit, it contains methods like `WriteLine` etc.
 
 #### Methods
@@ -104,7 +108,7 @@ namespace ExampleTest
 
 * `void Send(string s)` sends command to the debugger process through stdin. New line is automatically appended to `s`.
 
-* `MICore.Results Expect(string s, int timeoutSec = 10)` reads debugger output line by line and checks if some line starts with `s`. If the expected line is found then it is parsed into MI responce object `MICore.Results`. In case of timeout or invalid MI responce an exception is thrown.
+* `MICore.Results Expect(string s, int timeoutSec = DefaultExpectTimeout)` reads debugger output line by line and checks if some line starts with `s`. If the expected line is found then it is parsed into MI responce object `MICore.Results`. In case of timeout or invalid MI responce an exception is thrown.
 For more info about MI responce object see https://github.com/Microsoft/MIEngine/blob/master/src/MICore/MIResults.cs
 
 * `int GetCurrentLine()` provides current line number.
index df609a5..01f1bf8 100644 (file)
@@ -35,11 +35,15 @@ namespace Runner
         [Fact]
         public void BreakpointAddRemoveTest() => ExecuteTest();
 
+        private const int DefaultTimeoutSec = 20;
+        private int expectTimeoutSec;
+
         public class ProcessInfo
         {
-            public ProcessInfo(string command, ITestOutputHelper output)
+            public ProcessInfo(string command, int defaultTimeoutSec, ITestOutputHelper output)
             {
                 this.output = output;
+                this.defaultTimeoutSec = defaultTimeoutSec;
                 process = new Process();
                 queue = new BufferBlock<string>();
 
@@ -106,6 +110,9 @@ namespace Runner
 
             public MICore.Results Expect(string text, int timeoutSec)
             {
+                if (timeoutSec < 0)
+                    timeoutSec = defaultTimeoutSec;
+
                 TimeSpan timeSpan = TimeSpan.FromSeconds(timeoutSec);
 
                 CancellationTokenSource ts = new CancellationTokenSource();
@@ -151,6 +158,7 @@ namespace Runner
 
             public Process process;
             private readonly ITestOutputHelper output;
+            private int defaultTimeoutSec { get; }
             public BufferBlock<string> queue;
         }
 
@@ -163,20 +171,23 @@ namespace Runner
                 Dictionary<string, int> lines,
                 string testSource,
                 string testBin,
+                int defaultExpectTimeout,
                 ITestOutputHelper output)
             {
                 this.processInfo = processInfo;
                 this.Lines = lines;
                 this.TestSource = testSource;
                 this.TestBin = testBin;
+                this.DefaultExpectTimeout = defaultExpectTimeout;
                 this.Output = output;
             }
             public int GetCurrentLine([CallerLineNumber] int line = 0) { return line; }
 
             public void Send(string s) => processInfo.Send(s);
-            public MICore.Results Expect(string s, int timeoutSec = 20) => processInfo.Expect(s, timeoutSec);
+            public MICore.Results Expect(string s, int timeoutSec = -1) => processInfo.Expect(s, timeoutSec);
             public readonly string TestSource;
             public readonly string TestBin;
+            public readonly int DefaultExpectTimeout;
             public readonly ITestOutputHelper Output;
         }
 
@@ -273,6 +284,10 @@ namespace Runner
                 this.debuggerCommand = Path.Combine(d.Parent.Parent.Parent.Parent.Parent.FullName, "bin", "netcoredbg");
             }
 
+            var timeout = Environment.GetEnvironmentVariable("TIMEOUT");
+            if (timeout == null || !Int32.TryParse(timeout, out expectTimeoutSec) || expectTimeoutSec < 0)
+                expectTimeoutSec = DefaultTimeoutSec;
+
             var testDir = Environment.GetEnvironmentVariable("TESTDIR");
 
             // Find all dlls
@@ -322,7 +337,7 @@ namespace Runner
             );
             script.Compile();
 
-            ProcessInfo processInfo = new ProcessInfo(debuggerCommand, output);
+            ProcessInfo processInfo = new ProcessInfo(debuggerCommand, expectTimeoutSec, output);
 
             try
             {
@@ -332,6 +347,7 @@ namespace Runner
                     lines,
                     data.srcFilePath,
                     data.dllPath,
+                    expectTimeoutSec,
                     output
                 );