summaryrefslogtreecommitdiffstats
path: root/Tests/CMakeLib/testUVHandlePtr.cxx
blob: c97d755ead5433024ec6925e53b1ce1044ca2aa8 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <functional>
#include <iostream>
#include <memory>

#include <cm3p/uv.h>

#include "cmGetPipes.h"
#include "cmUVHandlePtr.h"

static bool testIdle()
{
  bool idled = false;

  cm::uv_loop_ptr loop;
  loop.init();

  auto cb = [](uv_idle_t* handle) {
    auto idledPtr = static_cast<bool*>(handle->data);
    *idledPtr = true;
    uv_idle_stop(handle);
  };

  cm::uv_idle_ptr idle;
  idle.init(*loop, &idled);
  idle.start(cb);
  uv_run(loop, UV_RUN_DEFAULT);

  if (!idled) {
    std::cerr << "uv_idle_ptr did not trigger callback" << std::endl;
    return false;
  }

  idled = false;

  idle.start(cb);
  idle.stop();
  uv_run(loop, UV_RUN_DEFAULT);

  if (idled) {
    std::cerr << "uv_idle_ptr::stop did not stop callback" << std::endl;
    return false;
  }

  return true;
}

static bool testTimer()
{
  bool timed = false;

  cm::uv_loop_ptr loop;
  loop.init();

  auto cb = [](uv_timer_t* handle) {
    auto timedPtr = static_cast<bool*>(handle->data);
    *timedPtr = true;
    uv_timer_stop(handle);
  };

  cm::uv_timer_ptr timer;
  timer.init(*loop, &timed);
  timer.start(cb, 10, 0);
  uv_run(loop, UV_RUN_DEFAULT);

  if (!timed) {
    std::cerr << "uv_timer_ptr did not trigger callback" << std::endl;
    return false;
  }

  timed = false;
  timer.start(cb, 10, 0);
  timer.stop();
  uv_run(loop, UV_RUN_DEFAULT);

  if (timed) {
    std::cerr << "uv_timer_ptr::stop did not stop callback" << std::endl;
    return false;
  }

  return true;
}

static bool testWriteCallback()
{
  int pipe[] = { -1, -1 };
  if (cmGetPipes(pipe) < 0) {
    std::cout << "cmGetPipes() returned an error" << std::endl;
    return false;
  }

  cm::uv_loop_ptr loop;
  loop.init();

  cm::uv_pipe_ptr pipeRead;
  pipeRead.init(*loop, 0);
  uv_pipe_open(pipeRead, pipe[0]);

  cm::uv_pipe_ptr pipeWrite;
  pipeWrite.init(*loop, 0);
  uv_pipe_open(pipeWrite, pipe[1]);

  char c = '.';
  uv_buf_t buf = uv_buf_init(&c, sizeof(c));
  int status = -1;
  auto cb = std::make_shared<std::function<void(int)>>(
    [&status](int s) { status = s; });

  // Test getting a callback after the write is done.
  cm::uv_write(pipeWrite, &buf, 1, cb);
  uv_run(loop, UV_RUN_DEFAULT);
  if (status != 0) {
    std::cout << "cm::uv_write non-zero status: " << status << std::endl;
    return false;
  }

  // Test deleting the callback before it is made.
  status = -1;
  cm::uv_write(pipeWrite, &buf, 1, cb);
  cb.reset();
  uv_run(loop, UV_RUN_DEFAULT);
  if (status != -1) {
    std::cout << "cm::uv_write callback incorrectly called with status: "
              << status << std::endl;
    return false;
  }

  return true;
}

int testUVHandlePtr(int, char** const)
{
  bool passed = true;
  passed = testIdle() && passed;
  passed = testTimer() && passed;
  passed = testWriteCallback() && passed;
  return passed ? 0 : -1;
}