# Test ContextService ability to handle disconnections, including handling
# cleanup of dependent (via=) contexts.

- name: integration/context_service/reconnection.yml
  hosts: test-targets
  tasks:
    - mitogen_shutdown_all:

    - custom_python_detect_environment:
      register: ssh_account_env

    - become: true
      custom_python_detect_environment:
      register: old_become_env

    - name: Kill ssh process
      become: true
      shell: |
        bash -c "( sleep 3; kill -9 {{ssh_account_env.pid}}; ) & disown"

    - connection: local
      vars:
        ansible_python_interpreter: "{{ ansible_playbook_python }}"
      shell: sleep 3

    - wait_for_connection:

    - become: true
      custom_python_detect_environment:
      register: new_become_env

    # Verify the PIDs really changed (i.e. disconnection happened)
    - assert:
        that:
          - old_become_env.pid != new_become_env.pid
        fail_msg: |
          old_become_env={{ old_become_env }}
          new_become_env={{ new_become_env }}
  tags:
    - reconnection
