Reputation: 1
I'm working on a Flutter app that communicates with a server using event queues. I've implemented exception handling for scenarios like a "BAD_EVENT_QUEUE_ID" error, but I'm struggling to test it. I want to simulate a dead event queue on my device to verify that my exception handling works correctly.
void poll() async {
final backoffMachine = BackoffMachine();
while (true) {
if (_debugLoopSignal != null) {
await _debugLoopSignal!.future;
assert(() {
_debugLoopSignal = Completer();
return true;
}());
}
final GetEventsResult result;
try {
result = await getEvents(store. Connection,
queueId: queueId, lastEventId: lastEventId);
//store.isStale=false;
// store.setIsStale(false);
// print('isstale: ${store.isStale}');
} catch (e) {
switch (e) {
case ZulipApiException(code: 'BAD_EVENT_QUEUE_ID'):
// store.isStale=true;
store.exceptionMessage='Lost event queue ';
store.setIsStale(true,store.exceptionMessage);
assert(debugLog('Lost event queue for $store. Replacing…'));
await store._globalStore._reloadPerAccount(store.accountId);
dispose();
debugLog('… Event queue replaced.');
return;
case Server5xxException() || NetworkException():
assert(debugLog('Transient error polling event queue for $store: $e\n'
'Backing off, then will retry…'));
// drives the connecting snackbar
store.exceptionMessage='error polling';
store.setIsStale(true,store.exceptionMessage);
// TODO tell user if transient polling errors persist
// TODO reset to short backoff eventually
await backoffMachine.wait();
assert(debugLog('… Backoff wait complete, retrying poll.'));
continue;
default:
assert(debugLog('Error polling event queue for $store: $e\n'
'Backing off and retrying even though may be hopeless…'));
store.exceptionMessage='non-transient';
store.setIsStale(true,store.exceptionMessage);
// TODO tell user on non-transient error in polling
await backoffMachine.wait();
assert(debugLog('… Backoff wait complete, retrying poll.'));
continue;
}
}
final events = result.events;
for (final event in events) {
store.handleEvent(event);
}
if (events.isNotEmpty) {
lastEventId = events.last.id;
}
}
}
I've tried to trigger the exception by disconnecting from the internet, but i want to test it since my device still believes it has an internet connection, I don't get the feedback I need.
What methods or tools can I use to intentionally simulate a dead event queue on my device and verify that my exception handling code is working as expected? Additionally, how can I ensure that I'm correctly catching and handling the "BAD_EVENT_QUEUE_ID" exception?
Any insights or suggestions would be greatly appreciated. Thank you!
Upvotes: 0
Views: 22