Skip to content
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ void testURLSessionTaskCommon(
await request.drain<void>();
request.response.headers.set('Content-Type', 'text/plain');
request.response.write('Hello World');
// Add a small delay to allow cancellation to run before the request
// is completed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it feasible to coordinate the sequencing without an arbitrary delay? Should we wait on a Completer here and have the test body complete it after the cancellation runs? It will require edits in existing tests, but much better to be explicit about the interleaving.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I misunderstood what was going on. I removed the delay and updated the documentation.

What is happening is that that cancel, when complete, transitions to the completed state. And that sometimes happens before we can observe the state as cancelled.

If this doesn't make the test reliable, I should probably just remove it.

await Future<void>.delayed(const Duration(seconds: 2));
await request.response.close();
});
final session = URLSession.sharedSession();
Expand All @@ -235,22 +238,30 @@ void testURLSessionTaskCommon(
task.toString(); // Just verify that there is no crash.
});

test('cancel', () {
task.cancel();
if (suspendedAfterCancel) {
expect(
task.state,
NSURLSessionTaskState.NSURLSessionTaskStateSuspended,
);
} else {
expect(
task.state,
NSURLSessionTaskState.NSURLSessionTaskStateCanceling,
);
}
expect(task.response, null);
task.toString(); // Just verify that there is no crash.
});
test(
'cancel',
() {
task.cancel();
if (suspendedAfterCancel) {
expect(
task.state,
NSURLSessionTaskState.NSURLSessionTaskStateSuspended,
);
} else {
expect(
task.state,
NSURLSessionTaskState.NSURLSessionTaskStateCanceling,
);
}
expect(task.response, null);
task.toString(); // Just verify that there is no crash.
},
// If the task completes before cancelling then the task state will be
// `NSURLSessionTaskStateCompleted`. So run the test a few times to allow
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we care about testing this specific behavior if it's so hard to observe it in usage? Should we allow the completed case as a successful test?

        expect(
          task.state,
          anyOf(
            NSURLSessionTaskState.NSURLSessionTaskStateSuspended,
            NSURLSessionTaskState.NSURLSessionTaskStateCompleted,
          ),
        );

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we care about testing this specific behavior if it's so hard to observe it in usage?

At some point my .toString() would throw for certain states so I created a test to catch that. But it might not be worth it.

Should we allow the completed case as a successful test?

        expect(
          task.state,
          anyOf(
            NSURLSessionTaskState.NSURLSessionTaskStateSuspended,
            NSURLSessionTaskState.NSURLSessionTaskStateCompleted,
          ),
        );

That would work but it would miss the intent of the test i.e. to verify that a cancelled test is actually cancelled instead of running to completion (and to verify that toString works correctly ;-))

// the timing to work out (which still isn't a great approach but I can't
// think of a better way).
retry: 5,
);

test('completed', () async {
task.resume();
Expand Down
Loading