libuv timer how to make it run asynchronously - libuv

I was playing with libuv and quickjs(JS engine). And implementing setTimeout function using its core JSRuntime.
My JS code looks like below, which run synchronously rather than in async fashion as it should:
console.log("Start");
//this setTimeout will block and , end will be run only once this finish
setTimeout(()=>{
console.log('Timeout has run');
},5000);
console.log("End");
The output of this program is :
Start
Timeout has run
*****This one runs after 5 second*******
End
My Libuv code is as such (not exactly like this, but somewhat like this):
int setup_setTimeout(uv_loop_t *loop){
uv_timer_t* timer_req=(uv_timer_t*)malloc(sizeof(uv_timer_t));//without this I get segmentation error, as in my timerCallback it seems t be lost
uv_timer_init(loop, timer_req);
uv_timer_start(timer_req,timerCallback,delay,0);
uv_run(loop, UV_RUN_DEFAULT);
std::cout<<"*****This one runs after 5 second*******"<<std::endl;
}
I am a noob (with libuv usage), and hence is in doubt if there is something I am missing or running it wrong. As the code seems to run synhronously rather than in async fashion.

Ok , so I found the solution.
As I have not posted the full code, I wil try to explain how I resolved the issue.
uv_run(loop, UV_RUN_DEFAULT); should be run after JS script evaluation.
The problem is if you call uv_run , it will start looking for events it needs to process and the only event it is waiting for is the timer thing, and hence it will start running it.
First evaluate the script using your JS Engine, which should invoke all events in the script.
And at last after evaluating call uv_run.

Related

What is SourceFunction#run is supposed to work in Flink?

I have implemented a Source by extending RichSourceFunction for our Message Queue that Flink doesn't support.
When I implements the run method whose signature is:
override def run(sc: SourceFunction.SourceContext[String]): Unit = {
val msg = read_from_mq
sc.collect(msg)
}
When the run method is called, if there is no newer message in message queue,
Should I run without calling sc.collect or
I can wait until newer data comes(in this case, run method will be blocked).
I would prefer the 2nd one,not sure if this is the correct usage.
The run method of a Flink source should loop, endlessly producing output until its cancel method is called. When there's nothing to produce, then it's best if you can find a way to do a blocking wait.
The apache nifi source connector is another reasonable example to use as a model. You will note that it sleeps for a configurable interval when there's nothing for it to do.
As you probably know both options are functionally correct and will yield correct results.
This being said the second one is preferred because you're not holding the thread. In fact, if you take a look at the RabbitMQ connector implementation you'll notice that this exactly how it is implemented: inside its run it indirectly waits for messages to be placed on a BlockingQueue.

Libev: how to schedule a callback to be called as soon as possible

I'm learning libev and I've stumbled upon this question. Assume that I want to process something as soon as possible but not now (i.e. not in the current executing function). For example I want to divide some big synchronous job into multiple pieces that will be queued so that other callbacks can fire in between. In other words I want to schedule a callback with timeout 0.
So the first idea is to use ev_timer with timeout 0. The first question is: is that efficient? Is libev capable of transforming 0 timeout timer into an efficient "call as soon as possible" job? I assume it is not.
I've been digging through libev's docs and I found other options as well:
it can artificially delay invoking the callback by using a prepare or idle watcher
So the idle watcher is probably not going to be good here because
Idle watchers trigger events when no other events of the same or higher priority are pending
Which probably is not what I want. Prepare watchers might work here. But why not check watcher? Is there any crutial difference in the context I'm talking about?
The other option these docs suggest is:
or more sneakily, by reusing an existing (stopped) watcher and pushing it into the pending queue:
ev_set_cb (watcher, callback);
ev_feed_event (EV_A_ watcher, 0);
But that would require to always have a stopped watcher. Also since I don't know a priori how many calls I want to schedule at the same time then I would have to have multiple watchers and additionally keep track of them via some kind of list and increase it when needed.
So am I on the right track? Are these all possibilities or am I missing something simple?
You may want to check out the ev_prepare watcher. That one is scheduled for execution as the last handler in the given event loop iteration. It can be used for 'Execute this task ASAP' implementations. You can create dedicated watcher for each task you want to execute, or you can implement a queue with a one prepare watcher that is started once queue contains at least one task.
Alternatively, you can implement similar mechanism using ev_idle watcher, but this time, it will be executed only if the application doesn't process any 'higher priority' watcher handlers.

Test Case execution for infinite time using testng

I want to execute some test cases in my selenium framework for infinite time means it should run 24*7. I have tried to search a lot in google but unable to find the solution for it. please help me as how can i achieve this execution using TestNG.
If you want it through testng then what #mackowski suggested should also work - though reports will get overwritten. If you want long running tests and not necessarily all the time tests, then you can put invocationCount as a high number too.
However, I think you should be taking help of Jenkins to schedule this job, say every 2 minutes every hour every day of the week -
simple configuration will handle this for you.
Your reports would be saved for each run
A failure in one will not cause the run to be aborted.
Plus you may run out of memory if you do it in one run.
Take your pick.
There are couple ways of doing that. You need to run your tests in infinite loop. The one way of doing this is to write simple Java program that will run your tests over and over again.
Here is example code
public static void main(String[]args ) {
while(true) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { Run2.class });
testng.addListener(tla);
testng.run();
}
}
Here you can find how to run TestNG programmatically http://testng.org/doc/documentation-main.html#running-testng-programmatically

Can the time out issue be the consequence of browser.sleep() and browser.waitForAngular?

In protractor scripts i'm always having problem of time out, even if i put a big time out jasmine interval, allscripttimeout...
And in some places i'm obliged to wait until element are present like in home page until the url is totally loaded.
Can the time out issue be the consequence of
browser.sleep(time_ms);
browser.waitForAngular();
If so, how can i fix this problem ?
Here the problem in details
Thanks,
Yes they could be -- browser.sleep() would only timeout if you had it sleep longer than your Jasmine timeout interval (default is 30 seconds).
browser.waitForAngular() is automatically applied to every webDriver action by Protractor so you shouldn't need to call it. If this time's out then your app is still synchronizing something.
Both of these would result in A Jasmine spec timed out. Resetting the WebDriver Control Flow. following by Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. if it takes too long.
I'm not positive how you would fix it - you've had a lot of questions about timeouts (for good reason), but at this point I think you need to use browser.ignoreSynchronization = true; and treat your app as non-Angular if you're having this many timeout issues. Something is preventing synchronization from finishing.
There are several helper methods that you can create and execute seamlessly on non-Angular apps by extending Protractor's functions to avoid explicit browser.sleep()'s . For example, the below code pauses test execution until isPresent returns true (or until a failure by exceeding the timeout I specified)
Util.prototype.waitForElementPresent = function (el, time) {
var timeout = time || 0,
return browser.wait(function() {
return el.isPresent();
}, timeout)
};

App Engine: Is it possible to enqueue tasks asynchronously?

Many of my handlers add a task to a task queue to do non-critical background processing. Since this processing isn't critical, if the call to taskqueue.add() throws an exception, my code just ignores it.
Tonight the task queue seemed to be down for around half an hour. Although my handlers correctly ignored the failure, they took about 5 seconds for the taskqueue.add() call to timeout and move on to processing the rest of the page. This therefore made my site run very slowly.
So, is it possible to enqueue a task asynchronously - meaning a way to add a task, without waiting to see if the addition succeeded?
Alternatively, is there a way to reduce that timeout from 5 seconds down to eg 1 second?
Thanks.
You can use the new taskqueue methods create_rpc and add_async. If you don't care if the add succeeds, simply call add_async and ignore the result. If you care, but only want to wait 1 second, set the deadline when calling create_rpc, and use the return value as the RPC argument to add_async. Call get_result to find out if the tasks were successfully added.
I think you can't do anything about it because the RPC call underneath the add method is a synchronous blocking API call.
You could try to add some check using the Capabilities API.
I am pretty sure GAE announced that TQ adds will be async with the next release (experimental feature).

Resources