diff --git a/packages/omamobile-mmd/lib/bridge.py b/packages/omamobile-mmd/lib/bridge.py index 7ea950002..b02e80f3a 100644 --- a/packages/omamobile-mmd/lib/bridge.py +++ b/packages/omamobile-mmd/lib/bridge.py @@ -157,6 +157,8 @@ def on_status(self, status): def on_lost(self): if self.modem is None: return + for path in list(self.calls): + self.finish_pending_delete(path, MMError('Failed', 'omamobile-sitd went away before call release')) for path in list(self.calls) + list(self.messages) + [self.sim, self.bearer]: if path: self.x.remove(path) @@ -315,7 +317,10 @@ def sync_calls(self): self.call_state(path, CALL_STATES.get(c['state'], mm.CALL_UNKNOWN), c.get('reason', '')) for sid, path in list(self.sitd_calls.items()): if sid not in seen and path in self.calls and self.calls[path]['seen']: - # sitd forgot it (another client deleted it): it has ended. + # sitd only drops a call after it has ended (delete-call needs + # terminated; a restart restores live calls), so a call it no + # longer lists is over, whether or not our hangup was what + # ended it: terminated, and a pending Delete completes. self.call_state(path, mm.CALL_TERMINATED, '') self.forgotten &= seen @@ -324,7 +329,7 @@ def new_call(self, direction, number, state, sid=None): reason = (mm.REASON_INCOMING_NEW if direction == mm.DIRECTION_INCOMING and state != mm.CALL_UNKNOWN else mm.REASON_OUTGOING_STARTED if state != mm.CALL_UNKNOWN else mm.REASON_UNKNOWN) self.calls[path] = dict(sid=sid, direction=direction, number=number, state=state, busy=False, - seen=sid is not None) + seen=sid is not None, releasing=False, pending_delete=None) if sid: self.sitd_calls[sid] = path self.x.add(path, {mm.CALL: dict(State=state, StateReason=reason, Direction=direction, @@ -358,8 +363,55 @@ def call_state(self, path, state, sitd_reason): self.x.emit(path, mm.CALL, 'StateChanged', '(iiu)', (old, state, reason)) print(f'Call {path}: {CALL_STATE_NAMES.get(old, old)} -> {CALL_STATE_NAMES.get(state, state)}', flush=True) + if state == mm.CALL_TERMINATED: + self.finish_pending_delete(path) + + def start_release(self, call): + """One hang-up outstanding at a time, for 20 s at most: sitd answers + the request as soon as it has sent the release, so a release that is + accepted but never completes must not block the user's next press + for good. With today's sitd the window never runs out that way: when + the call is still there 12 s after the hang-up, sitd turns the radio + off and stops for an attended reboot (unconfirmed_release in + omamobile-modem-sit), the bridge withdraws the modem with its calls, + and the next press finds no call, an error rather than a second + hang-up at the modem. The window is for a sitd that stays up. The + bridge never retries on its own.""" + call['releasing'] = True + if call.get('release_timer'): + GLib.source_remove(call['release_timer']) + + def expire(): + call['release_timer'] = 0 + call['releasing'] = False + return False + call['release_timer'] = GLib.timeout_add_seconds(20, expire) + + def end_release(self, call): + call['releasing'] = False + if call.get('release_timer'): + GLib.source_remove(call['release_timer']) + call['release_timer'] = 0 + + def finish_pending_delete(self, path, error=None): + call = self.calls.get(path) + pending = call and call.get('pending_delete') + if pending is None: + return + call['pending_delete'] = None + if pending['timer']: + GLib.source_remove(pending['timer']) + if error is not None: + pending['reply'].error(error) + else: + if call['sid']: + self.link.request('delete-call', lambda result, error: None, id=call['sid']) + self.delete_call(path) + pending['reply'].ok() def delete_call(self, path): + if path in self.calls: + self.end_release(self.calls[path]) call = self.calls.pop(path) if call['sid']: self.sitd_calls.pop(call['sid'], None) @@ -536,31 +588,71 @@ def handle_voice(self, method, args, reply): if path not in self.calls: raise MMError('NotFound', 'No such call') call = self.calls[path] + if call['busy'] or call['pending_delete'] is not None: + raise MMError('InProgress', 'The call is being changed') + if call['sid'] and call['state'] not in (*LIVE, mm.CALL_TERMINATED): + raise MMError('WrongState', 'The call state is not known') if call['sid'] and call['state'] in LIVE: - # MM hangs up a call that is deleted while it lasts. + # Keep the object until sitd reports that release completed. + # A hang-up that sitd refused, or that was never confirmed, + # leaves no block: the next DeleteCall or Hangup from the user + # tries once more; the bridge never retries on its own. + pending = dict(reply=reply, timer=0) + call['pending_delete'] = pending + + def release_timeout(): + if path in self.calls and self.calls[path].get('pending_delete') is pending: + pending['timer'] = 0 + self.finish_pending_delete(path, MMError('Failed', 'Call release was not confirmed')) + return False + + pending['timer'] = GLib.timeout_add_seconds(20, release_timeout) + if call['releasing']: + return + self.start_release(call) + def hung(result, error): - if path in self.calls: - self.delete_call(path) - reply.ok() + if error is not None and self.calls.get(path) is call: + self.end_release(call) + self.finish_pending_delete(path, sitd_error(error)) return self.link.request('hangup', hung, id=call['sid']) if call['sid']: self.link.request('delete-call', lambda result, error: None, id=call['sid']) self.delete_call(path) return reply.ok() if method == 'HangupAll': - live = [c['sid'] for c in self.calls.values() if c['sid'] and c['state'] in LIVE] + if any(c['busy'] for c in self.calls.values()): + raise MMError('InProgress', 'A call is being placed') + live = [(path, c) for path, c in self.calls.items() + if c['sid'] and c['state'] in LIVE and not c['releasing']] + skipped = [path for path, c in self.calls.items() + if c['sid'] and c['state'] in LIVE and c['releasing']] for path, c in self.calls.items(): if not c['sid'] and c['state'] == mm.CALL_UNKNOWN: self.call_state(path, mm.CALL_TERMINATED, 'terminated') failures = [] - - def next_hangup(result=None, error=None): - if error is not None: - failures.append(error) - if live: - return self.link.request('hangup', next_hangup, id=live.pop(0)) + if skipped: + failures.append(MMError('InProgress', 'Call release is already in progress')) + + def next_hangup(): + while live: + path, call = live.pop(0) + if (self.calls.get(path) is not call or call['state'] not in LIVE + or call['releasing']): + continue + self.start_release(call) + + def hung(result, error, path=path, call=call): + if error is not None: + failure = sitd_error(error) + failures.append(failure) + if self.calls.get(path) is call: + self.end_release(call) + self.finish_pending_delete(path, failure) + next_hangup() + return self.link.request('hangup', hung, id=call['sid']) if failures: - return reply.error(sitd_error(failures[0])) + return reply.error(failures[0]) reply.ok() return next_hangup() raise unsupported(method) @@ -604,7 +696,16 @@ def dialled(result, error): raise MMError('InProgress', 'The call is being placed') self.call_state(path, mm.CALL_TERMINATED, 'terminated') return reply.ok() - return self.link.request('hangup', self.answer(reply), id=call['sid']) + if call['releasing']: + raise MMError('InProgress', 'Call release is already in progress') + self.start_release(call) + + def hung(result, error): + if error is not None and self.calls.get(path) is call: + self.end_release(call) + self.finish_pending_delete(path, sitd_error(error)) + self.answer(reply)(result, error) + return self.link.request('hangup', hung, id=call['sid']) if method == 'SendDtmf': keys = args[0] if not re.fullmatch(r'[0-9*#]{1,32}', keys or ''): diff --git a/packages/omamobile-mmd/test/all b/packages/omamobile-mmd/test/all index f53ffa2c2..1163ff58b 100755 --- a/packages/omamobile-mmd/test/all +++ b/packages/omamobile-mmd/test/all @@ -19,7 +19,7 @@ done pass "scripts parse" command -v dbus-daemon >/dev/null || fail "dbus-daemon (package dbus) is needed for the tests" -out=$(cd test && python3 -W ignore -m unittest -v test_api test_bridge test_libmm test_chain 2>&1) || { +out=$(cd test && python3 -W ignore -m unittest -v test_api test_env test_bridge test_libmm test_chain 2>&1) || { printf '%s\n' "$out" | grep -v -e Deprecat -e EventLoopPolicy | tail -40; fail "unit and bus tests"; } skips=$(printf '%s\n' "$out" | grep -c "skipped '" || true) pass "API, bridge, libmm-glib and chain tests$( ((skips)) && echo " (# SKIP $skips: $(printf '%s\n' "$out" | grep -o "skipped '[^']*'" | sort -u | tr '\n' ' '))")" diff --git a/packages/omamobile-mmd/test/conformance.py b/packages/omamobile-mmd/test/conformance.py index ada493a35..5e9cf7025 100644 --- a/packages/omamobile-mmd/test/conformance.py +++ b/packages/omamobile-mmd/test/conformance.py @@ -168,8 +168,16 @@ def calls_objects(): 'answered from Calls')), 'Calls answers through Call.Accept') calls_api(path, 'org.gnome.Calls.Call', 'SendDtmf', GLib.Variant('(s)', ('7',))) calls_api(path, 'org.gnome.Calls.Call', 'Hangup') - check(bool(wait_until(lambda: chain.client.prop(mm_call, mm.CALL, 'State') == mm.CALL_TERMINATED, 10, - 'hung up from Calls')), 'Calls hangs up through Call.Hangup') + # Calls may delete the object as soon as it ends. Check the recorded + # transition and the modem request, rather than polling a vanished object. + def terminated(call_path): + return any(s[0] == call_path and s[1] == mm.CALL and s[3][1] == mm.CALL_TERMINATED + for s in chain.client.named('StateChanged')) + + check(bool(wait_until(lambda: terminated(mm_call), 10, 'hung up from Calls')), + 'Calls reports the incoming call ended') + check(len([e for e in chain.entries() if e.get('type') == 'req' and e.get('id') == 0x0d03]) == 1, + 'Calls sends one incoming hangup to the modem') check([e.get('key') for e in chain.entries() if e.get('event') == 'dtmf'] == ['7'], 'Calls sends DTMF') subprocess.run(['gnome-calls', f'--dial={OTHER}'], env=dict(os.environ, DBUS_SESSION_BUS_ADDRESS=session_address), @@ -183,12 +191,12 @@ def calls_objects(): if not v['org.gnome.Calls.Call']['Inbound']], 10, 'outgoing in Calls')[0] calls_api(out_path, 'org.gnome.Calls.Call', 'Hangup') - def ended_or_deleted(): - # Calls deletes a call (Voice.DeleteCall) once it has ended. - if dialled[0] not in chain.client.prop(ROOT + '/Modem/0', mm.VOICE, 'Calls'): - return 'deleted' - return chain.client.prop(dialled[0], mm.CALL, 'State') == mm.CALL_TERMINATED - check(wait_until(ended_or_deleted, 10, 'ended') == 'deleted', 'Calls hangs up and deletes its call') + check(bool(wait_until(lambda: terminated(dialled[0]), 10, 'outgoing call ended')), + 'Calls reports the outgoing call ended') + check(bool(wait_until(lambda: dialled[0] not in chain.client.prop(ROOT + '/Modem/0', mm.VOICE, 'Calls'), + 10, 'outgoing call deleted')), 'Calls deletes its ended call') + check(len([e for e in chain.entries() if e.get('type') == 'req' and e.get('id') == 0x0d03]) == 2, + 'Calls sends one outgoing hangup to the modem') with open(log, errors='replace') as f: text = f.read() bad = [line for line in text.splitlines() if re.search(r'CRITICAL|Segmentation|dumped core', line)] diff --git a/packages/omamobile-mmd/test/env.py b/packages/omamobile-mmd/test/env.py index 831e70de4..20506a623 100644 --- a/packages/omamobile-mmd/test/env.py +++ b/packages/omamobile-mmd/test/env.py @@ -56,13 +56,33 @@ def __init__(self, directory): stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) self.address = 'unix:path=' + self.path deadline = time.monotonic() + 10 - while not os.path.exists(self.path): - if time.monotonic() > deadline or self.proc.poll() is not None: - raise RuntimeError('private dbus-daemon did not start') - time.sleep(.02) - self.connection = Gio.DBusConnection.new_for_address_sync( - self.address, Gio.DBusConnectionFlags.AUTHENTICATION_CLIENT | - Gio.DBusConnectionFlags.MESSAGE_BUS_CONNECTION, None, None) + try: + while not os.path.exists(self.path): + if time.monotonic() > deadline or self.proc.poll() is not None: + raise RuntimeError('private dbus-daemon did not start') + time.sleep(.02) + while True: + try: + self.connection = Gio.DBusConnection.new_for_address_sync( + self.address, Gio.DBusConnectionFlags.AUTHENTICATION_CLIENT | + Gio.DBusConnectionFlags.MESSAGE_BUS_CONNECTION, None, None) + break + except GLib.Error as error: + # bind() creates the socket before listen() accepts clients. + if not error.matches(Gio.io_error_quark(), Gio.IOErrorEnum.CONNECTION_REFUSED): + raise + if time.monotonic() > deadline or self.proc.poll() is not None: + raise RuntimeError('private dbus-daemon did not start') from error + time.sleep(.02) + except BaseException: + if self.proc.poll() is None: + self.proc.terminate() + try: + self.proc.wait(5) + except subprocess.TimeoutExpired: + self.proc.kill() + self.proc.wait() + raise def close(self): self.connection.close_sync(None) diff --git a/packages/omamobile-mmd/test/test_bridge.py b/packages/omamobile-mmd/test/test_bridge.py index 0f9b18cd9..70349e383 100644 --- a/packages/omamobile-mmd/test/test_bridge.py +++ b/packages/omamobile-mmd/test/test_bridge.py @@ -7,6 +7,7 @@ registered_status, spin, tempdir, wait_until import os import re +import threading import unittest import bridge @@ -287,6 +288,275 @@ def dial(request): spin(.2) self.assertEqual(self.modem('Calls', mm.VOICE), [], 'a deleted call does not come back') + def test_delete_live_call_preserves_it_when_hangup_is_refused(self): + self.sitd.status['calls'] = [call('200', 'active')] + self.push() + path = wait_until(lambda: self.modem('Calls', mm.VOICE), 5, 'live call')[0] + + def refuse(request): + raise FakeRefusal('Hangup could not be confirmed') + + self.sitd.handlers['hangup'] = refuse + self.assertEqual(self.error_of(MODEM0, mm.VOICE, 'DeleteCall', 'o', (path,)), + mm.ERROR_PREFIX + 'Failed') + self.assertEqual(self.sitd.requests[-1], dict(action='hangup', id='200')) + self.assertEqual(self.modem('Calls', mm.VOICE), [path]) + self.assertEqual(self.client.prop(path, mm.CALL, 'State'), mm.CALL_ACTIVE) + self.push() + spin(.2) + self.assertEqual(self.modem('Calls', mm.VOICE), [path]) + # the refused attempt is over, so a new Hangup is a new attempt + self.assertEqual(self.error_of(path, mm.CALL, 'Hangup'), mm.ERROR_PREFIX + 'Failed') + self.assertEqual(self.sitd.actions().count('hangup'), 2) + + def test_delete_live_call_waits_for_confirmed_release(self): + self.sitd.status['calls'] = [call('200', 'active')] + self.sitd.handlers['hangup'] = lambda request: {} + self.push() + path = wait_until(lambda: self.modem('Calls', mm.VOICE), 5, 'live call')[0] + result = self.client.call_async(MODEM0, mm.VOICE, 'DeleteCall', 'o', (path,)) + wait_until(lambda: self.sitd.actions().count('hangup') == 1, 5, 'hangup request') + spin(.2) + self.assertEqual(result, {}, 'the modem has not confirmed release') + self.assertEqual(self.modem('Calls', mm.VOICE), [path]) + self.assertEqual(self.error_of(MODEM0, mm.VOICE, 'DeleteCall', 'o', (path,)), + mm.ERROR_PREFIX + 'InProgress') + self.sitd.status['calls'] = [call('200', 'terminated', reason='terminated')] + self.push() + wait_until(lambda: result, 5, 'confirmed deletion') + self.assertEqual(result.get('result'), ()) + self.assertEqual(self.modem('Calls', mm.VOICE), []) + self.assertEqual(self.sitd.actions().count('hangup'), 1) + wait_until(lambda: self.sitd.actions().count('delete-call') == 1, 5, 'sitd call cleanup') + self.assertEqual(self.sitd.requests[-1], dict(action='delete-call', id='200')) + + def test_delete_after_hangup_does_not_send_a_second_hangup(self): + self.sitd.status['calls'] = [call('200', 'active')] + self.sitd.handlers['hangup'] = lambda request: {} + self.push() + path = wait_until(lambda: self.modem('Calls', mm.VOICE), 5, 'live call')[0] + self.client.call(path, mm.CALL, 'Hangup') + result = self.client.call_async(MODEM0, mm.VOICE, 'DeleteCall', 'o', (path,)) + spin(.2) + self.assertEqual(result, {}) + self.assertEqual(self.sitd.actions().count('hangup'), 1) + self.sitd.status['calls'] = [call('200', 'terminated', reason='terminated')] + self.push() + wait_until(lambda: result, 5, 'confirmed deletion') + self.assertEqual(result.get('result'), ()) + self.assertEqual(self.modem('Calls', mm.VOICE), []) + + def test_disappearing_live_call_is_terminated_and_deleted(self): + self.sitd.status['calls'] = [call('200', 'active')] + self.sitd.handlers['hangup'] = lambda request: {} + self.push() + path = wait_until(lambda: self.modem('Calls', mm.VOICE), 5, 'live call')[0] + result = self.client.call_async(MODEM0, mm.VOICE, 'DeleteCall', 'o', (path,)) + wait_until(lambda: self.sitd.actions().count('hangup') == 1, 5, 'hangup request') + # sitd drops a call only once it has ended, so a call it no longer + # lists is over: terminated, and the pending Delete completes. + self.sitd.status['calls'] = [] + self.push() + wait_until(lambda: result, 5, 'the delete completes') + self.assertNotIn('error', result) + self.assertEqual(self.modem('Calls', mm.VOICE), []) + + def test_release_confirmation_can_arrive_before_hangup_reply(self): + self.sitd.status['calls'] = [call('200', 'active')] + entered, proceed = threading.Event(), threading.Event() + self.addCleanup(proceed.set) + + def delayed(request): + entered.set() + if not proceed.wait(5): + raise FakeRefusal('test timed out') + return {} + + self.sitd.handlers['hangup'] = delayed + self.push() + path = wait_until(lambda: self.modem('Calls', mm.VOICE), 5, 'live call')[0] + result = self.client.call_async(MODEM0, mm.VOICE, 'DeleteCall', 'o', (path,)) + wait_until(entered.is_set, 5, 'hangup request') + self.sitd.status['calls'] = [call('200', 'terminated', reason='terminated')] + self.push() + wait_until(lambda: result, 5, 'confirmed deletion') + self.assertEqual(result.get('result'), ()) + proceed.set() + spin(.2) + self.assertEqual(self.modem('Calls', mm.VOICE), []) + + def test_service_loss_fails_pending_delete(self): + self.sitd.status['calls'] = [call('200', 'active')] + self.sitd.handlers['hangup'] = lambda request: {} + self.push() + path = wait_until(lambda: self.modem('Calls', mm.VOICE), 5, 'live call')[0] + result = self.client.call_async(MODEM0, mm.VOICE, 'DeleteCall', 'o', (path,)) + wait_until(lambda: self.sitd.actions().count('hangup') == 1, 5, 'hangup request') + self.sitd.stop() + wait_until(lambda: result, 5, 'service-loss refusal') + self.assertIn(mm.ERROR_PREFIX + 'Failed', result['error'].message) + + def test_delete_during_dial_is_refused(self): + entered, proceed = threading.Event(), threading.Event() + self.addCleanup(proceed.set) + + def delayed(request): + entered.set() + if not proceed.wait(5): + raise FakeRefusal('test timed out') + return dict(call='100', number=request['number']) + + self.sitd.handlers['dial'] = delayed + path = self.client.call(MODEM0, mm.VOICE, 'CreateCall', 'a{sv}', + ({'number': GLib.Variant('s', OTHER)},))[0] + started = self.client.call_async(path, mm.CALL, 'Start') + wait_until(entered.is_set, 5, 'dial request') + self.assertEqual(self.error_of(MODEM0, mm.VOICE, 'DeleteCall', 'o', (path,)), + mm.ERROR_PREFIX + 'InProgress') + self.assertEqual(self.error_of(MODEM0, mm.VOICE, 'HangupAll'), mm.ERROR_PREFIX + 'InProgress') + proceed.set() + wait_until(lambda: started, 5, 'dial reply') + self.assertEqual(started.get('result'), ()) + self.assertIn(path, self.modem('Calls', mm.VOICE)) + + def test_hangup_all_does_not_repeat_a_pending_release(self): + self.sitd.status['calls'] = [call('200', 'active')] + self.sitd.handlers['hangup'] = lambda request: {} + self.push() + path = wait_until(lambda: self.modem('Calls', mm.VOICE), 5, 'live call')[0] + self.client.call(path, mm.CALL, 'Hangup') + # a release is outstanding: HangupAll sends nothing and says so + self.assertEqual(self.error_of(MODEM0, mm.VOICE, 'HangupAll'), mm.ERROR_PREFIX + 'InProgress') + self.assertEqual(self.sitd.actions().count('hangup'), 1) + + def test_refused_hangup_reports_error_and_a_new_press_tries_again(self): + self.sitd.status['calls'] = [call('200', 'active')] + def refuse(request): + raise FakeRefusal('Hangup could not be confirmed') + self.sitd.handlers['hangup'] = refuse + self.push() + path = wait_until(lambda: self.modem('Calls', mm.VOICE), 5, 'live call')[0] + self.assertEqual(self.error_of(path, mm.CALL, 'Hangup'), mm.ERROR_PREFIX + 'Failed') + self.assertEqual(self.sitd.actions().count('hangup'), 1) + # The bridge never retries on its own, but each new request from the + # user is a new attempt once the last one has failed + self.assertEqual(self.error_of(MODEM0, mm.VOICE, 'DeleteCall', 'o', (path,)), + mm.ERROR_PREFIX + 'Failed') + self.assertEqual(self.sitd.actions().count('hangup'), 2) + self.assertEqual(self.error_of(MODEM0, mm.VOICE, 'HangupAll'), mm.ERROR_PREFIX + 'Failed') + self.assertEqual(self.sitd.actions().count('hangup'), 3) + self.assertEqual(self.modem('Calls', mm.VOICE), [path]) + # and once sitd takes it, the call ends: sitd reports the release and the object goes + self.sitd.handlers['hangup'] = lambda request: {} + self.client.call(path, mm.CALL, 'Hangup') + self.assertEqual(self.sitd.actions().count('hangup'), 4) + self.sitd.status['calls'] = [call('200', 'terminated', reason='terminated')] + self.push() + wait_until(lambda: self.client.prop(path, mm.CALL, 'State') == mm.CALL_TERMINATED, 5, 'terminated') + self.client.call(MODEM0, mm.VOICE, 'DeleteCall', 'o', (path,)) + self.sitd.status['calls'] = [] + self.push() + wait_until(lambda: self.modem('Calls', mm.VOICE) == [], 5, 'call gone') + + def test_unconfirmed_hangup_blocks_for_20_s_then_a_new_press_tries_again(self): + # sitd answers Hangup as soon as it has sent the release; if the call + # never ends and sitd stays up, a second press is InProgress for 20 s, + # then a new attempt. The stand-in sitd here stays up; today's real + # one stops at its own 12 s deadline (the next test). + self.sitd.status['calls'] = [call('200', 'active')] + self.sitd.handlers['hangup'] = lambda request: {} + self.push() + path = wait_until(lambda: self.modem('Calls', mm.VOICE), 5, 'live call')[0] + self.client.call(path, mm.CALL, 'Hangup') + self.assertEqual(self.error_of(path, mm.CALL, 'Hangup'), mm.ERROR_PREFIX + 'InProgress') + self.assertEqual(self.error_of(MODEM0, mm.VOICE, 'HangupAll'), mm.ERROR_PREFIX + 'InProgress') + self.assertEqual(self.sitd.actions().count('hangup'), 1) + spin(21) + self.client.call(path, mm.CALL, 'Hangup') + self.assertEqual(self.sitd.actions().count('hangup'), 2) + self.assertEqual(self.modem('Calls', mm.VOICE), [path]) + + def test_unconfirmed_hangup_then_sitd_stops_takes_the_modem_and_the_call_away(self): + # What today's sitd does when the call never ends: at its own 12 s + # deadline it turns the radio off and stops for an attended reboot + # (unconfirmed_release in omamobile-modem-sit), before the bridge's + # 20 s window is over. The bridge then withdraws the modem with its + # calls, so the next press finds no call object: an error, not + # InProgress for good, and no second hang-up reaches the modem. + self.sitd.status['calls'] = [call('200', 'active')] + self.sitd.handlers['hangup'] = lambda request: {} + self.push() + path = wait_until(lambda: self.modem('Calls', mm.VOICE), 5, 'live call')[0] + self.client.call(path, mm.CALL, 'Hangup') + self.assertEqual(self.error_of(path, mm.CALL, 'Hangup'), mm.ERROR_PREFIX + 'InProgress') + self.sitd.stop() + wait_until(lambda: not self.client.objects(), 5, 'the modem withdrawn') + self.assertEqual(self.error_of(path, mm.CALL, 'Hangup'), 'org.freedesktop.DBus.Error.UnknownMethod') + self.assertEqual(self.sitd.actions().count('hangup'), 1) + + def test_pending_delete_fails_when_prior_hangup_is_refused(self): + for method in ('Hangup', 'HangupAll'): + with self.subTest(method=method): + self.sitd.status['calls'] = [call('200', 'active')] + entered, proceed = threading.Event(), threading.Event() + self.addCleanup(proceed.set) + + def delayed_refusal(request): + entered.set() + if not proceed.wait(5): + raise FakeRefusal('test timed out') + raise FakeRefusal('Hangup could not be confirmed') + + self.sitd.handlers['hangup'] = delayed_refusal + self.push() + path = wait_until(lambda: self.modem('Calls', mm.VOICE), 5, 'live call')[0] + if method == 'Hangup': + hung = self.client.call_async(path, mm.CALL, method) + else: + hung = self.client.call_async(MODEM0, mm.VOICE, method) + wait_until(entered.is_set, 5, 'hangup request') + deleted = self.client.call_async(MODEM0, mm.VOICE, 'DeleteCall', 'o', (path,)) + spin(.2) + self.assertEqual(deleted, {}) + proceed.set() + wait_until(lambda: hung and deleted, 5, 'both errors') + self.assertIn(mm.ERROR_PREFIX + 'Failed', hung['error'].message) + self.assertIn(mm.ERROR_PREFIX + 'Failed', deleted['error'].message) + self.assertEqual(self.modem('Calls', mm.VOICE), [path]) + self.sitd.status['calls'] = [call('200', 'terminated', reason='terminated')] + self.push() + wait_until(lambda: self.client.prop(path, mm.CALL, 'State') == mm.CALL_TERMINATED, + 5, 'terminated') + self.client.call(MODEM0, mm.VOICE, 'DeleteCall', 'o', (path,)) + self.sitd.status['calls'] = [] + self.push() + spin(.2) + + def test_unconfirmed_release_times_out_without_deleting_or_retrying(self): + self.sitd.status['calls'] = [call('200', 'active')] + self.sitd.handlers['hangup'] = lambda request: {} + self.push() + path = wait_until(lambda: self.modem('Calls', mm.VOICE), 5, 'live call')[0] + result = self.client.call_async(MODEM0, mm.VOICE, 'DeleteCall', 'o', (path,)) + wait_until(lambda: result, 25, 'release confirmation timeout') + self.assertIn(mm.ERROR_PREFIX + 'Failed', result['error'].message) + self.assertEqual(self.modem('Calls', mm.VOICE), [path]) + self.assertEqual(self.client.prop(path, mm.CALL, 'State'), mm.CALL_ACTIVE) + self.assertEqual(self.sitd.actions().count('hangup'), 1) + # after the timeout a new press from the user is a new attempt (with a + # sitd that is still up; today's has stopped by then) + self.client.call(path, mm.CALL, 'Hangup') + self.assertEqual(self.sitd.actions().count('hangup'), 2) + + def test_delete_call_with_unknown_modem_state_is_refused(self): + self.sitd.status['calls'] = [call('200', 'unrecognized')] + self.push() + path = wait_until(lambda: self.modem('Calls', mm.VOICE), 5, 'unknown call')[0] + self.assertEqual(self.error_of(MODEM0, mm.VOICE, 'DeleteCall', 'o', (path,)), + mm.ERROR_PREFIX + 'WrongState') + self.assertEqual(self.modem('Calls', mm.VOICE), [path]) + self.assertEqual(self.sitd.actions(), []) + def test_incoming_call(self): self.sitd.handlers.update(accept=lambda r: {}) self.sitd.status['calls'] = [call('200', 'ringing-in')] diff --git a/packages/omamobile-mmd/test/test_env.py b/packages/omamobile-mmd/test/test_env.py new file mode 100644 index 000000000..c25e8a53e --- /dev/null +++ b/packages/omamobile-mmd/test/test_env.py @@ -0,0 +1,70 @@ +# SPDX-License-Identifier: MIT +"""Private D-Bus startup must tolerate a socket that is not listening yet.""" +import os +from types import SimpleNamespace +import unittest +from unittest import mock + +import env +from env import Gio, GLib + + +class PrivateBusStartup(unittest.TestCase): + def setUp(self): + self.directory, cleanup = env.tempdir() + self.addCleanup(cleanup) + # bind() has created the path, but listen() has not necessarily run. + open(os.path.join(self.directory, 'bus'), 'w').close() + self.process = mock.Mock() + self.process.poll.return_value = None + self.connection = mock.Mock() + self.connect = mock.Mock() + self.gio = SimpleNamespace(DBusConnectionFlags=Gio.DBusConnectionFlags, + DBusConnection=SimpleNamespace(new_for_address_sync=self.connect), + io_error_quark=Gio.io_error_quark, IOErrorEnum=Gio.IOErrorEnum) + for patcher in (mock.patch.object(env, 'Gio', self.gio), + mock.patch.object(env.subprocess, 'Popen', return_value=self.process), + mock.patch.object(env.time, 'sleep')): + patcher.start() + self.addCleanup(patcher.stop) + + @staticmethod + def error(code): + return GLib.Error.new_literal(Gio.io_error_quark(), 'test error', code) + + def test_refused_connection_retries_until_bus_is_listening(self): + self.connect.side_effect = [self.error(Gio.IOErrorEnum.CONNECTION_REFUSED), self.connection] + bus = env.PrivateBus(self.directory) + self.assertIs(bus.connection, self.connection) + self.assertEqual(self.connect.call_count, 2) + bus.close() + self.process.terminate.assert_called_once() + self.process.wait.assert_called_once_with(5) + + def test_other_connection_error_stops_and_cleans_up(self): + self.connect.side_effect = self.error(Gio.IOErrorEnum.PERMISSION_DENIED) + with self.assertRaises(GLib.Error): + env.PrivateBus(self.directory) + self.connect.assert_called_once() + self.process.terminate.assert_called_once() + self.process.wait.assert_called_once_with(5) + + def test_refusal_timeout_stops_and_cleans_up(self): + self.connect.side_effect = self.error(Gio.IOErrorEnum.CONNECTION_REFUSED) + with mock.patch.object(env.time, 'monotonic', side_effect=[0, 11]): + with self.assertRaisesRegex(RuntimeError, 'did not start'): + env.PrivateBus(self.directory) + self.process.terminate.assert_called_once() + self.process.wait.assert_called_once_with(5) + + def test_daemon_exit_stops_retrying(self): + self.connect.side_effect = self.error(Gio.IOErrorEnum.CONNECTION_REFUSED) + self.process.poll.return_value = 1 + with self.assertRaisesRegex(RuntimeError, 'did not start'): + env.PrivateBus(self.directory) + self.connect.assert_called_once() + self.process.terminate.assert_not_called() + + +if __name__ == '__main__': + unittest.main() diff --git a/packages/omamobile-mmd/version b/packages/omamobile-mmd/version index b1e80bb24..845639eef 100644 --- a/packages/omamobile-mmd/version +++ b/packages/omamobile-mmd/version @@ -1 +1 @@ -0.1.3 +0.1.4