When defining a Future as follows:
Future<HttpRequest> httpRequest = HttpRequest.request(url,
method: method, requestHeaders: requestHeaders);
I want to handle a timeout after 5 secondes. I'm writing my code like this :
httpRequest.timeout(const Duration (seconds:5),onTimeout : _onTimeout());
Where my timeout function is :
_onTimeout() => print("Time Out occurs");
According to the Future timeout() method documentation , If onTimeout is omitted, a timeout will cause the returned future to complete with a TimeoutException. But With my code , my method _onTimeout() is properly called (but immediately, not after 5 seconds) and I always get a
TimeException after 5 seconds... (TimeoutException after 0:00:05.000000: Future not completed )
Am I missing something ?
Change this line
httpRequest.timeout(const Duration (seconds:5),onTimeout : _onTimeout());
to
httpRequest.timeout(const Duration (seconds:5),onTimeout : () => _onTimeout());
or just pass a reference to the function (without the ())
httpRequest.timeout(const Duration (seconds:5),onTimeout : _onTimeout);
This way the closure that calls _onTimeout() will be passed to timeout().
In the former code the result of the _onTimeout() call will be passed to timeout()
Future.await[_doSome].then((data){
print(data);
}).timeout(Duration(seconds: 10));
Using async/await style. You can add .timeout to any Future you are awaiting.
final result = await InternetAddress
.lookup('example.com')
.timeout(
Duration(seconds: 10),
onTimeout: () => throw TimeoutException('Can\'t connect in 10 seconds.'),
);
In order to stop any Future by timeout one can use timeout(). There are two examples:
Throw exception after timeout
final someHardTaskFuture = Future.delayed(const Duration(hours: 1), () => 42);
final newFutureWithTimeoutAndException = someHardTaskFuture.timeout(const Duration(seconds: 3));
Returns default value (11) on timeout
final someHardTaskFuture = Future.delayed(const Duration(hours: 1), () => 42);
final newFutureWithTimeoutAndDefaultValue = someHardTaskFuture
.timeout(const Duration(seconds: 3), onTimeout: () => 11);
print(await newFutureWithTimeoutAndDefaultValue);
Related
Is there any convenient way to assert all the items emitted by a Stream in order until it is canceled?
If I use:
expectLater(
stream,
emitsInOrder(<String>[
'item1',
'item2',
]),
);
and the Stream emits ['item1', 'item2', 'item3'] , the test won't fail.
The only way I've found so far is the following:
var count = 0;
final expected = ['item1', 'item2', 'item3'];
stream.listen(
expectAsync1(
(final result) {
expect(result, expected[count++]);
},
count: expected.length,
),
);
But it is a bit verbose and not very easy to read. Is there a simpler/more elegant way?
You can collect the items into a list, using toList, then compare it to your own expectation list:
await expectLater(stream.toList(), completion(expected));
This does not handle the case where the stream doesn't close at all (but then, nothing does, you just have to wait for a timeout).
It doesn't catch errors until all events have been emitted, the emitsInOrder approach is better for that. Not shorter, though.
emitsDone can be used if the Stream is closed at some point.
E.g:
test('Test', () async {
final controller = StreamController<String>();
final stream = controller.stream;
final matcher = expectLater(
stream,
emitsInOrder(<dynamic>[
'Item1',
'Item2',
emitsDone,
]),
);
controller
..add('Item1')
..add('Item2')
..add('Item3')
..close();
await matcher;
await controller.close();
});
The test fails with error:
Expected: should do the following in order:
• emit an event that 'Item1'
• emit an event that 'Item2'
• be done
Actual: <Instance of '_ControllerStream<String>'>
Which: emitted • Item1
• Item2
• Item3
x Stream closed.
which didn't be done
As #Irn suggest, a more compact alternative for Streams that complete at some point is using toList:
test('Test', () async {
final controller = StreamController<String>();
final stream = controller.stream;
final matcher = expectLater(stream.toList(), completion(<String>['Item1', 'Item2']));
controller
..add('Item1')
..add('Item2')
..add('Item3')
..close();
await matcher;
await controller.close();
});
If the Stream is never closed, you can add a timeout and check the items that have been emitted in that period:
test('Test3', () async {
final controller = StreamController<String>();
final stream = controller.stream.timeout(const Duration(milliseconds: 200));
final matcher = expectLater(
stream,
emitsInOrder(<dynamic>[
'Item1',
'Item2',
]),
);
controller
..add('Item1')
..add('Item2');
await matcher;
await controller.close();
});
Playing with Dart, is it possible to create a delay constructing a Future?:
Future<String>.value("Hello").then((newsDigest) {
print(newsDigest);
}) // .delayed(Duration(seconds: 5))
Yes, this is possible:
factory Future.delayed(Duration duration, [FutureOr<T> computation()]) {
_Future<T> result = new _Future<T>();
new Timer(duration, () {
try {
result._complete(computation?.call());
} catch (e, s) {
_completeWithErrorCallback(result, e, s);
}
});
return result;
}
As you have already discovered Future.delayed constructor creates a future that runs after a delay:
From the docs:
Future<T>.delayed(
Duration duration,
[ FutureOr<T> computation()
])
The computation will be executed after the given duration has passed, and the future is completed with the result of the computation.
If computation returns a future, the future returned by this constructor will complete with the value or error of that future.
For the sake of simplicity, taking a future that complete immediately with a value, this snippet creates a delayed future that complete after 3 seconds:
import 'dart:async';
main() {
var future = Future<String>.value("Hello");
var delayedFuture = Future.delayed(Duration(seconds: 3), () => future);
delayedFuture.then((value) {
print("Done: $value");
});
}
I am trying to create a web server stream. Here is the code:
import 'dart:io';
main() async {
HttpServer requestServer = await HttpServer.bind(InternetAddress.LOOPBACK_IP_V4, 8000);
requestServer.listen((request) { //comment out this or the await for to work
request.response
..write("This is a listen stream")
..close();
});
await for (HttpRequest request in requestServer) {
request.response
..write("This is an await for stream")
..close();
}
}
What is the difference between listen and await for? They both do not work at the same time. You need to comment out one or the other to work, but there doesn't seem to be a difference in function here. Are there circumstances where there is a difference, and when should you use one over the other?
Given:
Stream<String> stream = new Stream<String>.fromIterable(['mene', 'mene', 'tekel', 'parsin']);
then:
print('BEFORE');
stream.listen((s) { print(s); });
print('AFTER');
yields:
BEFORE
AFTER
mene
mene
tekel
parsin
whereas:
print('BEFORE');
await for(String s in stream) { print(s); }
print('AFTER');
yields:
BEFORE
mene
mene
tekel
parsin
AFTER
stream.listen() sets up code that will be put on the event queue when an event arrives, then following code is executed.
await for suspends between events and keeps doing so until the stream is done, so code following it will not be executed until that happens.
I use `await for when I have a stream that I know will have finite events, and I need to process them before doing anything else (essentially as if I'm dealing with a list of futures).
Check https://www.dartlang.org/articles/language/beyond-async for a description of await for.
The main difference is when there's code afterwards. listen only register the handler and the execution continue. await for will retain execution until the stream is closed.
Thus if you add a print('hello'); at the end of your main you shouldn't see hello in the output with await for (because the request stream is never closed). Try the following code on dartpad to see the differences :
import 'dart:async';
main() async {
tenInts.listen((i) => print('int $i'));
//await for (final i in tenInts) {
// print('int $i');
//}
print('hello');
}
Stream<int> get tenInts async* {
for (int i = 1; i <= 10; i++) yield i;
}
A more imporant difference is that await for serializes the consumption of the stream items while listen will process them concurrently.
For example the code below:
import 'dart:async';
Future<void> process(int i) async {
print("start $i");
await new Future.delayed(const Duration(seconds: 1));
print("end $i");
}
main() async {
await for (final i in tenInts) {
await process(i);
}
tenInts.listen((i) async => await process(i));
print('hello');
}
Stream<int> get tenInts async* {
for (int i = 1; i <= 10; i++) yield i;
}
yields
start 1
end 1
start 2
end 2
start 3
end 3
start 4
end 4
start 5
end 5
start 6
end 6
start 7
end 7
start 8
end 8
start 9
end 9
start 10
end 10
hello
start 1
start 2
start 3
start 4
start 5
start 6
start 7
start 8
start 9
start 10
end 1
end 2
end 3
end 4
end 5
end 6
end 7
end 8
end 9
end 10
Another difference can be the listen() returns you a StreamSubscription object, which can be used to cancel/pause the subscription at any later point of time. You can set callbacks to be called for each data event or error event, and when the stream is closed.
The below demonstrates that after listening to stream for 5 seconds, we will cancel it.
Stream<int> gen() async* {
for (int i = 1; i <= 10; i++) {
await Future.delayed(Duration(seconds: 1));
yield i;
}
print("done");
}
main() async {
Stream<int> stream = gen();
var subscription = stream.listen((item){
print(item);
});
await Future.delayed(Duration(seconds: 5));
subscription.cancel();
print("Exit");
}
Output:
1
2
3
4
Exit
As Robson said:
await for serializes the consumption of the stream items while listen
will process them concurrently.
I would also like to add a note that while use listen method possible to process stream events one by one if use pause and resume methods.
Pause method should be called before first await keyword.
StreamSubscription<int> subscription;
subscription = tenInts.listen((i) async {
subscription.pause();
await process(i);
subscription.resume();
});
Future<void> process(int i) async {
print("start $i");
await new Future.delayed(const Duration(seconds: 1));
print("end $i");
}
I have code similar to:
timer = new Timer(new Duration(milliseconds: 1000), () => (throw new TimeoutException('Callback not invoked!')));
while (timer.isActive){
await new Future.delayed(const Duration(milliseconds: 1), () => "1");
}
print('this should not be reached if the exception is raised');
elsewhere I have an async callback which calls:
timer.cancel();
In the case where the callback is invoked it works fine because the callback cancels the timer.
However, I'm not really sure how to actually catch the TimeoutException in this case if it is not canceled, because it seems the exception is raised in a different scope than my main function. This means program execution continues even though
Is there a way to do some sort of try/catch or somehow handle the above timeout exception? Or a better way to do what I am trying to do?
Using dart 1.19.1.
You get different behavior depending on whether the timeout is 500ms or 1500ms:
final timer = new Future.delayed(const Duration(milliseconds: 1000),
() => (throw new Exception('Callback not invoked!')))
.timeout(const Duration(milliseconds: 500));
try {
await timer;
} on TimeoutException catch(e) {
print('this should not be reached if the exception is raised');
} on Exception catch(e) {
print('exception: $e');
}
DartPad
I realised that in current Dart SDK version 0.4.1.0_r19425 methods like setTimeout, setInterval, clearTimeout, clearInterval aren't part of Window class any more and they all moved to WorkerContext.
Is there any documentation on how to use them now? Do I need to create a new instance of WorkerContext every time I want to use them?
In addition to Timer mentioned by Chris, there is a Future-based API:
var future = new Future.delayed(const Duration(milliseconds: 10), doStuffCallback);
There is not yet direct support for cancelling a Future callback, but this works pretty well:
var future = new Future.delayed(const Duration(milliseconds: 10));
var subscription = future.asStream().listen(doStuffCallback);
// ...
subscription.cancel();
Hopefully, there will soon be a Stream version of Timer.repeating as well.
You can use:
1) SetInterval
_timer = new Timer.periodic(const Duration(seconds: 2), functionBack);
Where: `functionBack(Timer timer) {
print('again');
}
2) SetTimeOut
_timer = Timer(Duration(seconds: 5), () => print('done'));
Where _time is type Time
From this post on the group (Feb 14th 2013).
// Old Version
window.setTimeout(() { doStuff(); }, 0);
// New Version
import 'dart:async';
Timer.run(doStuffCallback);
And another example (copied from the same post)
// Old version:
var id = window.setTimeout(doStuffCallback, 10);
.... some time later....
window.clearTimeout(id);
id = window.setInterval(doStuffCallback, 1000);
window.clearInterval(id);
// New version:
var timer = new Timer(const Duration(milliseconds: 10), doStuffCallback);
... some time later ---
timer.cancel();
timer = new Timer.repeating(const Duration(seconds: 1), doStuffCallback);
timer.cancel();
Specifically, they are now part of the Timer class in the dart:async library (rather than WorkerContext, which seems to be IndexedDb specific). API docs here