Design a site like this with WordPress.com
Get started

expect timeout – Expect.NET introduction

In post where Expect.NET library was introduced was included short example how to use the library. With this post I’d like to start some post series describing the library in more detailed way. I will start with description of expect timeout handling.

Expect timeout scenario

Let’s consider scenario in which one expects some string. What will happen when the expected string never occurs? By default there will be thrown TimeoutException. Default expect timeout value is 2500 ms. Let’s look at code:

Spawn spawn = new SpawnCommand("cmd.exe");
spawn.expect(">", s => Console.WriteLine("got prompt"));
// expect timeouts examples
spawn.send("ping 8.8.8.8\n");
try
{
    spawn.expect("Ping statistics", s => Console.WriteLine(s));
}
catch (Expect.TimeoutException)
{
    Console.WriteLine("Timeout ping 8.8.8.8!");
}

Line 1 – spawn cmd.exe command.

Line 2 – we are waiting for command prompt.

In line 4 is executed command ping 8.8.8.8 which sends 4 packets to Internet address 8.8.8.8 and waits for reply. Delay between packets is 1 second, so whole command should take at least 3 seconds.

Line 7 – Expect function is called to wait for “Ping statistics” string which is part of summary at the end of ping command execution.

Lines 9-12 – handling of TimeoutException. When TimeoutException is thrown it will be printed “Timeout ping 8.8.8.8!”

After execution of the above snippet, we will get “Timeout ping 8.8.8.8!” printout as a result. So default timeout value for Expect function is too low for this command.

 Configure expect timeout value

Above described scenario fails due to too low timeout value. The Expect.NET library provides function setTimeout for Spawn class objects. Let’s improve the code and increase expect timeout value to 5 seconds.

spawn.setTimeout(5000);
spawn.expect(@">", () => spawn.send("ping 8.8.4.4\n"));
try
{
    spawn.expect("Ping statistics", s => Console.WriteLine(s));
}
catch (Expect.TimeoutException)
{
    Console.WriteLine("Timeout ping 8.8.4.4!");
}

Line 1 – configure timeout to 5 seconds (5000 ms)

In effect of the above code execution there is printed captured output.

Example application

Example application which includes above listed code examples is stored in github repository.

2 responses to “expect timeout – Expect.NET introduction”

  1. It was hard to find your blog in google search results.
    I found it on 15 spot, you have to build a lot of quality backlinks , it
    will help you to increase traffic. I know how to help
    you, just search in google – k2 seo tricks

    Like

  2. I read a lot of interesting articles here. Probably you spend a lot of time writing, i know
    how to save you a lot of work, there is an online tool
    that creates readable, google friendly articles in seconds, just type in google – laranitas free content source

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: