selenium icon indicating copy to clipboard operation
selenium copied to clipboard

Control flow ordering looks wrong

Open hankduan opened this issue 11 years ago • 5 comments

I have the following code:

var webdriver = require('selenium-webdriver');

var d = webdriver.promise.defer();
var thePromise = d.promise;

var t = thePromise.then(function(res) {
  console.log('thePromise->frame1');
  return res;
});

webdriver.promise.controlFlow().execute(function() {
  return t.then(function(a) {
    console.log("thePromise->frame1->frame1");
  });
});

thePromise.then(function(x) {return x}).then(function(text) {
  console.log('thePromise->frame2');
});

d.fulfill();

From my understanding, webdriver should be handling the promises one frame at a time (i.e. resolve in a depth-first traversal order), and I'm expecting the output:

thePromise->frame1 thePromise->frame1->frame1 thePromise->frame2

Instead, this executes as thePromise->frame1 thePromise->frame2 thePromise->frame1->frame1

Is my understanding of the schedule wrong or is there something wrong with the order of execution?

hankduan avatar Jun 17 '15 21:06 hankduan

I'm not an expert in this stuff, but the top-level then functions that register their callbacks are all run immediately in this example. But the then inside the controlFlow().execute does not run until the control flow comes back to run the function that was registered with it.

If you add a console.log call inside the execute callback, you'll see that happens after d.fulfill(), too (so the then it registers must happen after that point).

Did this test case get oversimplified?

tullmann avatar Jun 18 '15 00:06 tullmann

@tullmann Sorry I don't quite get that. To clarify, I was thinking 'thePromise->frame1->frame1' should print before 'thePromise->frame2' because webdriver should resolve everything within frame1 before attempting to resolve frame2. (i.e. the frames should be resolved in a depth-first traversal order). Or am I totally mistaken about the order of execution for the promises/frames?

hankduan avatar Jun 18 '15 00:06 hankduan

In your example, I believe this code:

thePromise.then(function(x) {return x}).then(function(text) {
  console.log('thePromise->frame2');
});

is executing in the first/default/top control flow frame (I think this is what you are calling "Frame1"?). This executes along with all the other code at the "top-level". (Sprinkle the code with console.log('CF', webdriver.promise.controlFlow()); calls to see what I mean.)

Once control reaches the end of this test, is when you really enter the controlFlow-directed code (and that is when the function registered with execute actually runs).

Again, I am not an expert on this stuff, so hopefully I'm not misdirecting you.

Another way of looking at this, is that this statement:

var t = thePromise.then(function(res) {
  console.log('thePromise->frame1');
  return res;
});

does not change/register anything with the control flow.

Here is an example that explicitly registers (noop) callbacks with the control flow, hangs a "then" off those execution steps, and produces the output you're aiming for:

var webdriver = require('selenium-webdriver');

var noop = function() {};

var flow = webdriver.promise.controlFlow()
var d = webdriver.promise.defer();
var thePromise = d.promise;

flow.execute(noop).then(function(res) {
   console.log('flow->frame1');
   return res;
});

flow.execute(function() {
   return thePromise.then(function(a) {
      console.log("thePromise->frame1->frame1");
   });
});

flow.execute(noop).then(function(text) {
   console.log('flow->frame2');
});

d.fulfill();

I think you're expecting your example to be somewhat equivalent to this example? I don't think "bare" promises are registered with the control flow, though, so I believe that is why it the examples are different?

tullmann avatar Jun 18 '15 01:06 tullmann

@tullmann I think you're right, but the behavior is still confusing because sometimes promises do get added to the control flow, and then they seem to print in the wrong order. For example, logging at the end of the original example gives:

ControlFlow::1
| (active) Frame::7
| | Task::6<my task>
| | Frame::4
| | Frame::9

Logging the control flow again inside the first thePromise.then gives:

ControlFlow::1
| (active) Frame::7
| | Task::6<my task>
| | (running) Frame::4
| | Frame::9

Why are frames 4 and 9 even printed if they're not in the control flow? Maybe we just need some clarification here.

juliemr avatar Jun 18 '15 17:06 juliemr

Quick note, longer write up to follow: the original code snippet is executing tasks in the expected order. The promise callbacks don't schedule any tasks, so they are allowed to operate "independently" of the flow.

jleyba avatar Jun 19 '15 07:06 jleyba