1"""
2Test lldb-vscode disconnect request
3"""
4
5
6import unittest2
7import vscode
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11import lldbvscode_testcase
12import subprocess
13import time
14import os
15
16
17class TestVSCode_launch(lldbvscode_testcase.VSCodeTestCaseBase):
18    source = 'main.cpp'
19
20    def disconnect_and_assert_no_output_printed(self):
21        self.vscode.request_disconnect()
22        # verify we didn't get any input after disconnect
23        time.sleep(2)
24        output = self.get_stdout()
25        self.assertTrue(output is None or len(output) == 0)
26
27    @skipIfDarwin
28    @skipIfWindows
29    @skipIfRemote
30    def test_launch(self):
31        """
32            This test launches a process that would creates a file, but we disconnect
33            before the file is created, which terminates the process and thus the file is not
34            created.
35        """
36        program = self.getBuildArtifact("a.out")
37        self.build_and_launch(program, disconnectAutomatically=False)
38
39        # We set a breakpoint right before the side effect file is created
40        self.set_source_breakpoints(self.source, [line_number(self.source, '// breakpoint')])
41        self.continue_to_next_stop()
42
43        self.vscode.request_disconnect()
44        # verify we didn't produce the side effect file
45        time.sleep(1)
46        self.assertFalse(os.path.exists(program + ".side_effect"))
47
48
49    @skipIfDarwin
50    @skipIfWindows
51    @skipIfRemote
52    @expectedFailureNetBSD
53    def test_attach(self):
54        """
55            This test attaches to a process that creates a file. We attach and disconnect
56            before the file is created, and as the process is not terminated upon disconnection,
57            the file is created anyway.
58        """
59        self.build_and_create_debug_adaptor()
60        program = self.getBuildArtifact("a.out")
61
62        # Use a file as a synchronization point between test and inferior.
63        sync_file_path = lldbutil.append_to_process_working_directory(self,
64            "sync_file_%d" % (int(time.time())))
65        self.addTearDownHook(
66            lambda: self.run_platform_command(
67                "rm %s" %
68                (sync_file_path)))
69
70        self.process = subprocess.Popen([program, sync_file_path])
71        lldbutil.wait_for_file_on_target(self, sync_file_path)
72
73        self.attach(pid=self.process.pid, disconnectAutomatically=False)
74        response = self.vscode.request_evaluate("wait_for_attach = false;")
75        self.assertTrue(response['success'])
76
77        # verify we haven't produced the side effect file yet
78        self.assertFalse(os.path.exists(program + ".side_effect"))
79
80        self.vscode.request_disconnect()
81        time.sleep(2)
82        # verify we produced the side effect file, as the program continued after disconnecting
83        self.assertTrue(os.path.exists(program + ".side_effect"))
84