Welcome to mirror list, hosted at ThFree Co, Russian Federation.

process_handle.cpp « tests « test - github.com/windirstat/llfio.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 238eda5f3b1050ca71d94474ad1f9796dea3e336 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* Integration testing for process handle
(C) 2016-2020 Niall Douglas <http://www.nedproductions.biz/> (13 commits)
File Created: Aug 2016


Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License in the accompanying file
Licence.txt or at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


Distributed under the Boost Software License, Version 1.0.
    (See accompanying file Licence.txt or copy at
          http://www.boost.org/LICENSE_1_0.txt)
*/

#define QUICKCPPLIB_BOOST_UNIT_TEST_CUSTOM_MAIN_DEFINED

#define _CRT_SECURE_NO_WARNINGS 1

#include "../test_kernel_decl.hpp"

static inline void TestProcessHandle(bool with_redirection) {
  namespace llfio = LLFIO_V2_NAMESPACE;
  std::vector<llfio::process_handle> children;
  auto &self = llfio::process_handle::current();
  auto myexepath = self.current_path().value();
  std::cout << "My process executable's path is " << myexepath << std::endl;
  auto myenv = self.environment();
  fprintf(stderr, "appveyor debug environment(): just after environment, it returned %p\n", myenv.get());
  std::cout << "My process environment contains:";
  if(myenv)
  {
    for(auto &i : *myenv)
    {
      std::cout << "\n  " << i;
    }
  }
  std::cout << "\n" << std::endl;
  llfio::process_handle::flag flags = llfio::process_handle::flag::wait_on_close;
  if(!with_redirection)
  {
    flags |= llfio::process_handle::flag::no_redirect;
  }
  for(size_t n=0; n<4; n++)
  {
    char buffer[64];
    sprintf(buffer, "--testchild,%u", (unsigned) n);
    llfio::path_view_component arg(buffer);
    children.push_back(llfio::process_handle::launch_process(myexepath, {&arg, 1}, flags).value());
  }
  if(with_redirection)
  {
    for(size_t n = 0; n < 4; n++)
    {
      char _buffer[256];
      llfio::pipe_handle::buffer_type buffer((llfio::byte *) _buffer, sizeof(_buffer));
      children[n].in_pipe().read({{&buffer, 1}, 0}).value();
      _buffer[buffer.size()] = 0;
      std::cout << "Child process " << n << " wrote:\n   " << _buffer;
    }
  }
  for(size_t n = 0; n < 4; n++)
  {
    auto exitcode = children[n].wait().value();
    BOOST_CHECK(exitcode == (intptr_t) n + 1);
    std::cout << "The exit code of child process " << n << " was " << exitcode << std::endl;
  }
}

KERNELTEST_TEST_KERNEL(integration, llfio, process_handle, no_redirect, "Tests that llfio::process_handle without redirection works as expected", TestProcessHandle(false))
KERNELTEST_TEST_KERNEL(integration, llfio, process_handle, redirect, "Tests that llfio::process_handle with redirection works as expected", TestProcessHandle(true))

int main(int argc, char *argv[])
{
  using namespace KERNELTEST_V1_NAMESPACE;
  for(int n = 1; n < argc; n++)
  {
    if(0 == strncmp(argv[n], "--testchild,", 12))  // NOLINT
    {
      // Format is --testchild,no
      auto no = atoi(argv[n] + 12);
      std::cout << "I am child process " << no << std::endl;
      std::this_thread::sleep_for(std::chrono::seconds(3));
      return 1 + no;
    }
  }
  int result = QUICKCPPLIB_BOOST_UNIT_TEST_RUN_TESTS(argc, argv);
  return result;
}