Need to test an ajax timeout condition - asp.net

As the title mentions, I have a timeout callback handler on an ajax call, and I want to be able to test that condition but nothing is coming to mind immediately on ways I can force my application to hit that state, any suggestions?

You could always run a server-side script that keeps running for a period of time.
For example:
<?php
sleep(10); //sleep for 10 seconds.
print "This script has finished.";
>

First off, I think you need to be clearer in your question - what technology are you using and where is this process that is timing out - server-side or client-side?
If you want to have the server-side code take a long time and you are using .NET, place this line in the method you call server-side:
System.Threading.Thread.Sleep(timeoutMilliseconds);
As long as you use a number sufficient so that your client-side code assumes the server has timed out, you should be good.

YUI Connection Manager allows you to introduce slowdown in your Javascript to test AJAX against latency.

Related

Do I always have to "wait" for page loads when using selenium on non-ajax pages?

I'm writing some BDD tests using Cucumber, Selenium and Xunit for a legacy ASP.Net application. The way the pages are designed, every "click" leads to a new page being fetched from the server. If I have to automate the tests for a particular page, should I have a line similar to the following after every "click"?
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
wait.Until(...); //Wait until something about the page is true
I'm not sure if Selenium would wait implicitly for page loads without my explicitly having to state this all the time. What is the recommended pattern to handle this scenario?
It's cumbersome to always have an idea of "some element" so that I can put it in the Until method and that leads to brittle tests. The ASP.Net pages are littered with lots of dynamic controls and a whole slew of page refreshes which makes the test code quite unreadable.
My Proposed Solution: Write an extension method that does the waiting implicitly and that takes a parameter of an element-id to wait on. But I'm just refactoring the above problem into a more manageable place. I still have a wait be explicitly performed. Is there no way to eliminate it? Does selenium have some obvious default that would handle this case without the need for such an extension method or is this really a natural way of doing it?
If you want your tests be reliable and wait only the exactly needed time interval - then yes, Explicit Waits with WebDriverWait is a perfect solution. And, it's actually a very "natural" solution - think about how you, as a user, define that the page loaded - it's usually when you see the desired content, correct? When you look at the loading page, you are constantly reevaluating the state of the page, checking whether the desired content appeared or not. Explicit Waits follow the same logic - by default, every 500 ms it checks if the expected condition is true or not, but no more than X seconds you've configured when instantiating the
WebDriverWait.
If you need to use wait.until() calls often and want to follow the DRY principle, think about applying the "Extracting Method" or other refactoring methods.
You can set the implicit wait which would be applied on every element search, or introduce hardcoded "artificial" delays, but that's not going to be reliable and would be time-wasteful - you'll end up waiting more than needed and having occasional test failures.

Workflow Pick activity not sending reply until Action complete

I have a workflow that contains a Pick activity. Each PickBranch is triggered by a WCF request. The triggered branch then sends a response to the request and performs an Action activity. But the behaviour I'm seeing indicates the response is not being sent until the Action activity is complete which is causing the original request to timeout, depending on how long the Action activity takes to complete.
In the PickBranch above, I'm adding work orders to a mobile database. Each work order takes up to 16 seconds to be added to the database. As the number of work orders increases, the greater the likelihood that the original request will timeout. What am I doing wrong?
Ok, I think I have a resolution for this. As per Maurice's answer here, I added a Delay activity following the SendReplyToReceive and the workflow then started behaving as expected.
Just tested this and it works fine. If I have a Pick with a send and receive inside a trigger and a delay inside the action, the reply is received immediately.
Are you sure the Request on your SendReply activity appears to be set correctly?
Patrick is still right, you should implement your database activity as an AsyncCodeActivity but this would not be the reason for your reply being delayed.
I my experience checking PersistBeforeSend on SendReplyToReceive to True fixes this problem. Putting Persist block after SendReplyToReceive also helps.
This is working as intended. If the operations take such a long time, would you be better served by calling them asynchronously? Check out AsyncCodeActivity here:
http://msdn.microsoft.com/en-us/library/system.activities.asynccodeactivity.aspx

Complete Classic asp page not rendering sometimes for some users (response.buffer?)

I've got a fairly complex classic asp page that for some users doesn't render completely at some times. I wonder if the response.buffer property might have something to do with that. Would setting it to false make it more likely to render completely? I suspect it's crapping out at some point but can't reproduce it on my end.
If the client is receiving a partial page and buffering is enabled, a likely cause is the buffer limit being exceeded on the server side. In that case, disabling buffering would fix the problem. It all depends how you want the response sent - disabling buffering will send the content as and when it's written rather than waiting for the script to complete. An alternative would be to put in periodic calls to Response.Flush at sensible intervals to clear the buffer and send the content in chunks as you desire. It's difficult to be certain about a fix if you can't recreate the error; give it a try and let us know how it pans out :-).
Looks like it had something to do with running too many database connections on the page on an already congested server, near as I can figure.

Practical value for concurrent-request-timeout parameter or options for avoiding concurrent access to conversation exception

In the Seam Reference Guide, one can find this paragraph:
We can set a sensible default for the concurrent request timeout (in ms) in components.xml:
<core:manager concurrent-request-timeout="500" />
However, we found that 500 ms is not nearly enough time for most of the cases we had to deal with, especially with the severe restriction seam places on conversation access.
In our application we have a combination of page scoped ajax requests (triggered by various user actions), some global scoped polling notification logic (part of the header, so included in every page) and regular links that invoke actions and/or navigate to other pages.
Therefore, we get the dreaded concurrent access to conversation exception way too often, even without any significant load on the site.
After researching the options for quite a bit, we ended up bumping this value to several seconds (we're debating whether to bump it up to 10s), as none of the recommended solutions seemed able to solve our issue completely (even forcing a global queue for all the ajax requests would still leave us exposed to a user deciding to click a link right when one of our polling calls was in progress). And we'd much rather have the users wait for a second or two instead of getting an error page just because they clicked a link at the wrong moment.
And now to the question: is there something obvious we're missing (like a way to allow concurrent access to conversations and taking care of the needed locking ourselves, for instance :)? How do people solve this problem (ajax requests mixed with user driven interaction) in seam? Disabling all the links on the page while ajax requests are in progress (as suggested by one blog page) is really not a viable option.
Any other suggestions?
TIA,
Andrei
We use 60000 or 120000 (1-2 minutes). Concurrent-request-timeout is designed to avoid deadlocks. Historically we have far more problems with timeouts than deadlocks. A better approach is to use a client-side queue (<a4j:ajaxQueue> if using RichFaces) to serialize and remove duplicate requests as much as possible, then set the timeout high enough to avoid any remaining problems.
There are many serious issues resulting from Seam's concurrent request timeouts:
The issue is the last request gets the ConcurrentRequestTimeoutException. If the user double-clicks or reloads the page, only the last request matters -- why should he get an error?
Usually the ConcurrentRequestTimeoutException is suppressed, and only secondary NullPointerExceptions and #In injection failures are shown, making debugging difficult.
Seam 2.2.1 has a severe problem where transactions, ThreadLocals, and locks may leak after a timeout occurs, especially when used with <spring:spring-transaction/>. Look at SeamPhaseListener.afterRestoreView: there's no finally block to clean up after restoreConversation fails!
In my opinion there are many poor aspects to this design, so it's best to use a much higher timeout and try to avoid the issues.
This is what we have and it works fine for us:
<core:manager concurrent-request-timeout="5000"
conversation-timeout="120000" conversation-id-parameter="cid"
parent-conversation-id-parameter="pid" />
We also use a much higher value for the concurrent-request-timeout.
At least for duplicate events you can use settings in the a4j components to filter and delay them with eventsQueue, requestDelay and ignoreDupResponses=”true”.
(Last point http://docs.jboss.org/seam/2.0.1.GA/reference/en/html/conversations.html )
Can you analyse which types of request are taking a long time? Is there a particular type which you could reduce the request time by doing the "work" asynchronously and getting the update back in your poll?
In my opinion, ajax requests should always complete fairly quickly, then you can calculate a max concurrent request time by (request time * max number of requests likely to be initiated)

Thread Sleep in Classic ASP?

I'm doing some revision on an old app that is written in classic ASP/VbScript.
It has a feature to send out an e-mail to the members of the application, but because the member list is quite large, the server rejects new e-mails after the first hundred or so are sent.
I've written some code to make it send out e-mails in burst of 20, but this still doesn't work. I think that perhaps making it sleep for a second between burst might work properly.
However, I can't seem to find a Thread.Sleep type method in VbScript.
Is there one?
This routine waits any amount of time, and doesn't use CPU:
Function asp_Wait(nMilliseconds)
Dim oShell
'' VBS: Set oShell= Wscript.CreateObject("WScript.Shell")
'' ASP:
Set oShell= Server.CreateObject("WScript.Shell")
Call oShell.run("ping 1.1.1.1 -n 1 -w " & nMilliseconds,1,TRUE)
'' Option TRUE: Wait until ping is complete
'' 1000 milli-second wait is 1 second
End Function
there is also a good hta hack that should work. Look for the A Synthetic Sleep Function here:
http://www.mvps.org/scripting/rube/index.htm
You can use :
<html>
<head>
<title>Sleep</title>
</head>
<body>
<%
function Sleep(seconds)
set oShell = CreateObject("Wscript.Shell")
cmd = "%COMSPEC% /c timeout " & seconds & " /nobreak"
oShell.Run cmd,0,1
End function
Sleep(5)
response.write("End")
%>
</body>
</html>
Are you using CDO? Since this is tagged as classic ASP I'm guessing so.
If so if you can use
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")=1 '(which is send using pickup)
instead of
myMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing")=2 '(which is send using port)
It's more reliable because it writes the email to file (usually C:\inetpub\mailroot\pickup) and IIS's SMTP server checks the folder for new mail and will retry if it fails the first time. The catch is you have to set up SMTP within the IIS snap in.
Sorry that this answer is not strictly related to the question, but in trying to answer a question, it just got way to big for comments.
#shahka, the difference is, when you are trying to connect to a DB, the code enters a "wait state", (like a callback), so no CPU time is used. It matters not about what the SESSION or THREAD is doing, it matter what ELSE the CPU/Core is doing.
For an example, find an older non-multi-core CPU. Setup your sample to sleep for 10 seconds, then run for 2-5 seconds or so, then sleep again for 10. Do that about 20,000 times. While it's running, try and use box, see just how responsive it is. Move windows around, watch the CPU usage, etc.
THAT gives you an exmaple of what's happening to this man's Web server. It becomes unresponsive, because the thread scheduler will tend to 'favor' the CPU/Core that is NOT "spun up" (as we call it). So, ALL web requests, OS operations, etc will happen on the OTHER core, thus over-loading it, giving it a feeling of being "spun up" as well.
Now, you have times when you can tight-loop a CPU and it will not matter. But, in all my years of programming, I've never found it necessary to write a tight loop like that (on purpose). Some of it comes back around to doing things the right way, and the wrong way. Doing something the wrong way will often WORK, but that does not mean it wworks CORRECTLY.
If you want a good example of this, go and pick just about any virus on the planet, and analyze it. You'll find that it order to do damage, they (virus writers) often have to do things the "wrong" way. Sure, it gets the job done, but it also breaks the virus on say, a different language version of Windows, or it crashes the whole machine negating it's purpose, etc.
Greg Hewgill was one of my early teachers about this type of stuff, and since I worked with him for many years, and supported, and later QA'ed his software, I learned a lot from him, much in the same waay I'm trying to tell you why your code sample is not good. strictly speaking, the code is fine. It works. It's well written. But, it does not FUNCTION correctly, and has adverse side effects that other, maaybe amature programmers who might be reading this looking for knowledge do not fully understand. THAT'S why I did not recommend your sample.
Not to my knowledge. You'll have to use some external code written in class VB or whatever to do it.
Or busy-wait (gak).
You know, this is one of those times that I think setting up a private MSMQ queue could be a Good Thing. Put the emails you want to send on the queue, and have a newly developed .NET service do the sending. That will free up your ASP.NET application, and allow you to manage your sendin' centrally!
This looks like a good hack:
http://www.ehow.com/how_2001270_sleep-asp-using-ado.html
The trick is to create an ADO connection object and then try to connect to a non-existant server. This will block for the duration of the connection object's timeout setting.
Be aware that IIS has a default ASP Script execution time-out default of 90 seconds, so running large scripts that send volumes of email this way will time-out unless you change the asp timeout.
var shell = Server.CreateObject("WScript.Shell");
shell.run("CHOICE /C:AB /D:A /T:1 > NUL", 1, true);
The answer is don't use the server to wait, use the client.
You can write a javascript that keeps calling Send() every second using setInterval until the queue is empty.Use an Ajax call to send one email using a server side script. Then on return of the ajax call you can also indicate in the html page that an email has been sent.
If you are using SQL Server with ASP classic you can use WAITFOR "query" to stall the script for seconds or even milliseconds:
WAITFOR DELAY '00:00:01.234' -- 1,234 ms

Resources