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