summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/luv/tests/test-leaks.lua
blob: 06c6e49a0cf3baf862ac520e69e85800a434fd70 (plain) (blame)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
return require('lib/tap')(function (test)

  local function bench(uv, p, count, fn)
    collectgarbage()
    local before
    local notify = count / 8
    for i = 1, count do
      fn()
      if i % notify == 0 then
        uv.run()
        collectgarbage()
        local now = uv.resident_set_memory()
        if not before then before = now end
        p(i, now)
      end
    end
    uv.run()
    collectgarbage()
    local after = uv.resident_set_memory()
    p{
      before = before,
      after = after,
    }
    assert(after < before * 1.5)
  end

  test("fs-write", function (print, p, expect, uv)
    bench(uv, p, 0x7000, function ()
      local path = "_test_"
      local fd = assert(uv.fs_open(path, "w", 438))
      uv.fs_write(fd, "Hello World\n", -1)
      uv.fs_write(fd, {"with\n", "more\n", "lines\n"}, -1)
      uv.fs_close(fd)
      uv.fs_unlink(path)
    end)
  end)

  test("lots-o-timers", function (print, p, expect, uv)
    bench(uv, p, 0x10000, function ()
      local timer = uv.new_timer()
      uv.close(timer)
    end)
  end)

  test("lots-o-timers with canceled callbacks", function (print, p, expect, uv)
    bench(uv, p, 0x10000, function ()
      local timer = uv.new_timer()
      uv.timer_start(timer, 100, 100, function ()
      end)
      uv.timer_stop(timer)
      uv.close(timer, function ()
      end)
      uv.run()
    end)
  end)

  test("lots-o-timers with real timeouts", function (print, p, expect, uv)
    bench(uv, p, 0x500, function ()
      local timer = uv.new_timer()
      uv.timer_start(timer, 10, 0, expect(function ()
        uv.timer_stop(timer)
        uv.close(timer, function ()
        end)
      end))
    end)
  end)

  test("reading file async", function (print, p, expect, uv)
    local mode = tonumber("644", 8)
    bench(uv, p, 0x500, function ()
      local onOpen, onStat, onRead, onClose
      local fd, stat

      onOpen = expect(function (err, result)
        assert(not err, err)
        fd = result
        uv.fs_fstat(fd, onStat)
      end)

      onStat = expect(function (err, result)
        assert(not err, err)
        stat = result
        uv.fs_read(fd, stat.size, 0, onRead)
      end)

      onRead = expect(function (err, data)
        assert(not err, err)
        assert(#data == stat.size)
        uv.fs_close(fd, onClose)
      end)

      onClose = expect(function (err)
        assert(not err, err)
      end)

      assert(uv.fs_open("README.md", "r", mode, onOpen))
    end)
  end)

  test("reading file sync", function (print, p, expect, uv)
    local mode = tonumber("644", 8)
    bench(uv, p, 0x2000, function ()
      local fd = assert(uv.fs_open("README.md", "r", mode))
      local stat = assert(uv.fs_fstat(fd))
      local data = assert(uv.fs_read(fd, stat.size, 0))
      assert(#data == stat.size)
      assert(uv.fs_close(fd))
    end)
  end)

  test("invalid file", function (print, p, expect, uv)
    local mode = tonumber("644", 8)
    bench(uv, p, 0x1500, function ()
      local req = uv.fs_open("BAD_FILE", "r", mode, expect(function (err, fd)
        assert(not fd)
        assert(err)
      end))
    end)
  end)

  test("invalid file sync", function (print, p, expect, uv)
    local mode = tonumber("644", 8)
    bench(uv, p, 0x20000, function ()
      local fd, err = uv.fs_open("BAD_FILE", "r", mode)
      assert(not fd)
      assert(err)
    end)
  end)

  test("invalid spawn args", function (print, p, expect, uv)
    -- Regression test for #73
    bench(uv, p, 0x10000, function ()
      local ret, err = pcall(function ()
        return uv.spawn("ls", {
          args = {"-l", "-h"},
          stdio = {0, 1, 2},
          env = {"EXTRA=true"},
          gid = false, -- Should be integer
        })
      end)
      assert(not ret)
      assert(err)
    end)
  end)

  test("stream writing with string and array", function (print, p, expect, uv)
    local port = 0
    local server = uv.new_tcp()
    local data
    local count = 0x800
    server:unref()
    server:bind("127.0.0.1", port)
    server:listen(128, expect(function (err)
      assert(not err, err)
      local client = uv.new_tcp()
      server:accept(client)
      client:write(data)
      client:read_start(expect(function (err, data)
        assert(not err, err)
        assert(data)
        client:close()
      end))
    end, count))
    local address = server:getsockname()
    bench(uv, p, count, function ()
      data = string.rep("Hello", 500)
      local socket = uv.new_tcp()
      socket:connect(address.ip, address.port, expect(function (err)
        assert(not err, err)
        socket:read_start(expect(function (err, chunk)
          assert(not err, err)
          assert(chunk)
          local data = {}
          for i = 0, 100 do
            data[i + 1] = string.rep(string.char(i), 100)
          end
          socket:write(data)
          socket:close()
        end))
      end))
      uv.run()
    end)
    server:close()
  end)

end)