Protractor promise not resolving on declaration - protractor

When I execute the statements without using promises, it just returns a promise.
Like the following does not work:
devices = element.all(devicesRepeater)
expect(devices.length).toEqual factories.devices.length
But promise style works:
element.all(devicesRepeater).then (devices)->
expect(devices.length).toEqual factories.devices.length
But its shown in the protractor API that it should resolve e.g.:
http://angular.github.io/protractor/#/api?view=ProtractorBy.prototype.
// Returns a promise that resolves to an array var
rows = element.all(by.repeater('cat in pets'))
Same is true for waiting and all. Like in the following question, waiting blocks and then the expect call works:
How can I wait for a condition?
Whereas in my project, I have to put all the logic inside the promise callback block.
Should I write a then block for each of them or is there a way to block until promise resolves.

length is not a property of ElementArrayFinder. Use count() instead:
devices = element.all(devicesRepeater)
expect(devices.count()).toEqual factories.devices.length

Related

Fluter : How is whenCompleted() different from then() in Future?

Can somebody please explain How is whenCompleted() different from then() in Future with example?
Based on my understanding:
WhenCompleted:
This is called before then (Immediately the future completes). You can use this function if you do not need the value returned by the future, but you have to do something after the future is completed. It can also return an error.
Then:
You can use this if you need to access the value of the future and do something after the future function is executed. It can aslo return an error.
These are the only differences I could point out.

How to write unit test when you use Future?

I've wrote a class with some functions that does HTTP calls and returns a Future[String]. I use those functions inside a method that I need to write some tests:
def score(rawEvent: Json) = {
httpService
.get("name", formatJsonAttribute(rawEvent.name))
.onComplete { op =>
op.map { json =>
//What must be tested
}
}
}
The function onComplete doesn't have a return type - it returns Unit. How can I replace that onComplete to make my function return something to be tested?
I completely agree with #Michal, that you should always prefer map to onComplete with Futures. However I'd like to point out that, as you said yourself, what you wish to test is not the HTTP call itself (which relies on an HTTP client you probably don't need to test, a response from a server on which you may have no control, ...), but what you do with its answer.
So why not write a test, not on the function score, but on the function you wrote in your onComplete (or map, if you decided to change it)?
That way you will be able to test it with precise values for json, that you may wish to define as the result you will get from the server, but that you can control completely (for instance, you could test border cases without forcing your server to give unusual responses).
Testing that the two (HTTP call and callback function) sit well together is not a unit-test question, but an integration-test question, and should be done only once you know that your function does what is expected of it.
At that time, you will effectively need to check the value of a Future, in which case, you can use Await.result as #Michal suggested, or use the relevant constructs that your test framework gives. For instance, scalatest has an AsyncTestSuite trait for this kind of issue.
Use map instead of onComplete. It will also provide you with resolved value inside mapping function. The return type of score function will be Future[T] where T will be the result type of your processing.
In the tests you can use scala.concurrent.Await.result() function.

Async await assignment

I was looking over some Scala server code and I saw thins async/await block:
async {
while (cancellationToken.nonCancelled) {
val (request, exchange) = await(listener.nextRequest)
respond(exchange, cancellationToken, handler(request))
}
}
How can this be correct syntax?
As I understand it:
For every execution of the while loop
Thread 1 will execute the code from the while loop except the one in the await clause.
Thread 2 will go in the await clause.
But then Thread 1 will have val (request, exchange) uninstantiated in case Thread 2 doesn't finish computing.
These values will be passed to the respond and handler methods uninstantiated.
So how can you have an assignment in two different threads?
So how can you have an assignment in two different threads?
async-await's main goal is to allow you to do asynchronous programming in a synchronous fashion.
What really happens is that the awaited call executes listener.nextRequest and asynchronously waits for it's completion, it doesn't execute the next line of code until then. This guarantees that if the next line of code is executed, it's values are populated. The assignment should happen where it is visible to the next LOC in the method.
This is possible due to the fact that the async macro actually transforms this code into a state-machine, where the first part is the execution up until the first await, and the next part is everything after.

How to implement asynchronous task in a function that must return some value?

I am using asynchronous operation (using parse SDK).
I created a function like this:
let result = try query.fromLocalDatastore().findObjects()
return result
the problem is that in console I get a message like:
Warning: A long-running operation is being executed on the main thread.
Break on warnBlockingOperationOnMainThread() to debug.
As I assume this is bad design I am looking for solution for this.
I changed findObjects with findObjectsWithBlock but problem with this is asynchronous nature so I never get result back from function (actually it always late :)).
My question is:
Is there a way to run this code but to avoid warning in console?
Am I right that this is bad way to write this code in a function, where another approach would be to write code from function in a place where I call function, make it asynchronous and loose the benefit of code reuse?
trying;
let result = query.fromLocalDatastore().findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if error == nil {
// results will contain users with a hometown team with a winning record
}

Is there an F# equivalent to Scala's Promise?

Is there something like Scala's Promise in F#?
While futures are defined as a type of read-only placeholder object
created for a result which doesn’t yet exist, a promise can be thought
of as a writeable, single-assignment container, which completes a
future. That is, a promise can be used to successfully complete a
future with a value (by “completing” the promise) using the success
method. Conversely, a promise can also be used to complete a future
with an exception, by failing the promise, using the failure method.
The Async stuff covers part of this, but if you've got code that works outside the Async environment, Promises are a handy tool. (You can do things like complete a Promise in a UI thread, for example - even when the UI environment knows nothing at all about Async.)
The .Net equivalent of a promise is a TaskCompletionSource, so you can use them from F#. You can create an Async<T> from a Task<T> using Async.AwaitTask e.g.
let tcs = new TaskCompletionSource<int>()
let ta: Async<int> = Async.AwaitTask tcs.Task
//complete completion source using `SetResult`\`SetException` etc.