PollingAgent"
From Documentation
(1st draft) |
m (correct highlight (via JWB)) |
||
Line 3: | Line 3: | ||
Since 1.2.1 | Since 1.2.1 | ||
− | ZATS Mimic introduces the < | + | ZATS Mimic introduces the <code>PollingAgent</code> to simulate client polling operation to support testing applications that uses <code>Server Push</code> mechanism. |
The following are the usage steps: | The following are the usage steps: | ||
− | # Obtain a < | + | # Obtain a <code>PollingAgent</code> object from desktop. |
− | #:Note that you should use the same < | + | #:Note that you should use the same <code>PollingAgent</code> object on calling all methods. |
− | # Start a polling by invoking < | + | # Start a polling by invoking <code>start(int timeout)</code> method. |
− | #:The < | + | #:The <code>timeout</code> indicates for how many milliseconds we should timeout the polling; a 0(zero) <code>timeout</code> means never timeout. |
− | # Invoke < | + | # Invoke <code>polling(int delay)</code> method which will send a dummy client polling request to server and wait the specified delay time(in milliseconds) before returning. The <code>polling</code> method returns a boolean to indicate if the polling is stopped(false) or not(true) so you can control when to leave a polling loop. |
− | #:A polling is stopped if the < | + | #:A polling is stopped if the <code>stop()</code> method is called explicitly or if the polling has timed out since <code>start</code> the polling. |
− | # You can invoke < | + | # You can invoke <code>stop()</code> method to stop a polling explicitly. |
= Send dummy client polling request to server and wait = | = Send dummy client polling request to server and wait = | ||
− | Assume that we have a ZK application that use < | + | Assume that we have a ZK application that use <code>Server Push</code> mechanism to update the component attributes after an user triggering a long server operation. We will need <code>PollingAgent</code> to simulate sending the dummy client polling request and wait server to complete its long operation. |
− | <source lang="java" start="24" | + | <source lang="java" start="24" highlight="31, 32, 33, 38, 42"> |
@Test | @Test | ||
public void test() throws Exception { | public void test() throws Exception { | ||
Line 39: | Line 39: | ||
} | } | ||
</source> | </source> | ||
− | * '''Line 31''': Cast desktop to < | + | * '''Line 31''': Cast desktop to <code>PollingAgent</code> and keep its reference. |
− | * '''Line 32''': Invoke < | + | * '''Line 32''': Invoke <code>start(5000)</code> method to start a polling which will timeout in 5 seconds(5000 milliseconds). |
− | * '''Line 33''': Invoke < | + | * '''Line 33''': Invoke <code>polling(500)</code> method to send the dummy client polling request to server and wait 500 milliseconds before returning; check the return value to decide if stop the polling loop. |
− | * '''Line 38''': Invoke < | + | * '''Line 38''': Invoke <code>stop()</code> method to stop the polling since we have get the value pushed from server. |
* '''Line 42''': Use a timeout boolean variable to distinguish whether the polling loop is timeout or explicitly stopped. | * '''Line 42''': Use a timeout boolean variable to distinguish whether the polling loop is timeout or explicitly stopped. | ||
Latest revision as of 02:55, 18 January 2022
Since 1.2.1
ZATS Mimic introduces the PollingAgent
to simulate client polling operation to support testing applications that uses Server Push
mechanism.
The following are the usage steps:
- Obtain a
PollingAgent
object from desktop.- Note that you should use the same
PollingAgent
object on calling all methods.
- Note that you should use the same
- Start a polling by invoking
start(int timeout)
method.- The
timeout
indicates for how many milliseconds we should timeout the polling; a 0(zero)timeout
means never timeout.
- The
- Invoke
polling(int delay)
method which will send a dummy client polling request to server and wait the specified delay time(in milliseconds) before returning. Thepolling
method returns a boolean to indicate if the polling is stopped(false) or not(true) so you can control when to leave a polling loop.- A polling is stopped if the
stop()
method is called explicitly or if the polling has timed out sincestart
the polling.
- A polling is stopped if the
- You can invoke
stop()
method to stop a polling explicitly.
Send dummy client polling request to server and wait
Assume that we have a ZK application that use Server Push
mechanism to update the component attributes after an user triggering a long server operation. We will need PollingAgent
to simulate sending the dummy client polling request and wait server to complete its long operation.
@Test
public void test() throws Exception {
DesktopAgent desktop = Zats.newClient().connect("/long.zul");
ComponentAgent button = desktop.query("button");
button.click(); //trigger long operation (will cost 3000 milliseconds)
boolean timeout = true;
PollingAgent agent = desktop.as(PollingAgent.class);
agent.start(5000); // start the client polling and timeout in 5000 milliseconds(5 seconds)
while (agent.polling(500)) { //check every 500 milliseconds before polling is stopped
ComponentAgent label = desktop.query("label");
//assert expected status then break
if ( label != null){
Assert.assertEquals("success", label.as(Label.class).getValue());
agent.stop(); // stop the polling as we have done the test
timeout = false;
}
}
Assert.assertFalse("Timeout!", timeout); // client polling timeout
}
- Line 31: Cast desktop to
PollingAgent
and keep its reference. - Line 32: Invoke
start(5000)
method to start a polling which will timeout in 5 seconds(5000 milliseconds). - Line 33: Invoke
polling(500)
method to send the dummy client polling request to server and wait 500 milliseconds before returning; check the return value to decide if stop the polling loop. - Line 38: Invoke
stop()
method to stop the polling since we have get the value pushed from server. - Line 42: Use a timeout boolean variable to distinguish whether the polling loop is timeout or explicitly stopped.
Supported Components
DesktopAgent | 5, 6 |