~bigbes/tarantool

tarantool-protobuf

ref: 53840866a6ef3349d42bf589a6646aaad6869386 tarantool-protobuf/test/grpc_streaming_test.lua -rw-r--r-- 10.8 KiB
53840866 — Eugene Blikh codegen: emit <Msg>_decode_unsafe for trusted-source decoding (6bb) 2 months ago
                                                                                
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
-- gRPC streaming over fiber channels: server-streaming, client-streaming,
-- and bidirectional flavors, run end-to-end through the loopback transport.
--
-- Parameterized over both codegen modes since the generated client/server
-- bodies differ between full and runtime mode (different per-message
-- encode/decode call sites).

local t     = require('luatest')
local fiber = require('fiber')
local pb    = require('pb')

local MODES = {'full', 'runtime'}

for _, mode in ipairs(MODES) do
    local hello = require(mode .. '.hello.hello_pb')
    local g = t.group('grpc_stream.' .. mode)

    -- ---------------------------------------------------------------------
    -- Server-streaming: one request in, many replies out.
    -- ---------------------------------------------------------------------

    g.test_server_stream_yields_replies_in_order = function()
        local impl = {
            StreamHellos = function(req, stream, _)
                for i = 1, 3 do
                    stream:send({greeting = req.name .. '#' .. tostring(i)})
                end
            end,
        }
        local client = hello.Greeter_client(pb.grpc.loopback(
            hello.Greeter_server(impl)))

        local stream = client.StreamHellos({name = 'X'}, {})
        local replies = {}
        while true do
            local r, err = stream:recv()
            if r == nil then
                t.assert_equals(err, nil, 'unexpected stream error: ' .. tostring(err))
                break
            end
            replies[#replies + 1] = r.greeting
        end
        t.assert_equals(replies, {'X#1', 'X#2', 'X#3'})
    end

    g.test_server_stream_handler_can_be_empty = function()
        -- A handler that returns without sending anything is a valid
        -- (empty) stream — recv() must return nil on the first call.
        local impl = {StreamHellos = function(_, _, _) end}
        local client = hello.Greeter_client(pb.grpc.loopback(
            hello.Greeter_server(impl)))
        local stream = client.StreamHellos({name = 'x'}, {})
        local r, err = stream:recv()
        t.assert_equals(r, nil)
        t.assert_equals(err, nil)
    end

    g.test_server_stream_handler_error_surfaces = function()
        local impl = {
            StreamHellos = function(_, stream, _)
                stream:send({greeting = 'first'})
                error('boom from handler')
            end,
        }
        local client = hello.Greeter_client(pb.grpc.loopback(
            hello.Greeter_server(impl)))
        local stream = client.StreamHellos({name = 'x'}, {})

        -- First message comes through cleanly.
        local r1 = stream:recv()
        t.assert_equals(r1.greeting, 'first')

        -- Then end-of-stream with the handler's error string.
        local r2, err = stream:recv()
        t.assert_equals(r2, nil)
        t.assert_str_contains(tostring(err), 'boom from handler')
    end

    g.test_missing_streaming_handler_errors = function()
        local client = hello.Greeter_client(pb.grpc.loopback(
            hello.Greeter_server({})))
        local stream = client.StreamHellos({name = 'x'}, {})
        local r, err = stream:recv()
        t.assert_equals(r, nil)
        t.assert_str_contains(tostring(err), 'StreamHellos: handler missing')
    end

    -- ---------------------------------------------------------------------
    -- Client-streaming: many requests in, one reply out.
    -- ---------------------------------------------------------------------

    g.test_client_stream_collects_and_replies = function()
        local impl = {
            CollectHellos = function(stream, _)
                local names = {}
                while true do
                    local req = stream:recv()
                    if req == nil then break end
                    names[#names + 1] = req.name
                end
                return {greeting = 'collected: ' .. table.concat(names, ',')}
            end,
        }
        local client = hello.Greeter_client(pb.grpc.loopback(
            hello.Greeter_server(impl)))

        local call = client.CollectHellos({})
        call:send({name = 'a'})
        call:send({name = 'b'})
        call:send({name = 'c'})
        call:close_send()

        local reply, err = call:recv()
        t.assert_equals(err, nil)
        t.assert_equals(reply.greeting, 'collected: a,b,c')

        -- recv after the response should return nil (stream ended).
        local tail, err2 = call:recv()
        t.assert_equals(tail, nil)
        t.assert_equals(err2, nil)
    end

    g.test_client_stream_empty_is_valid = function()
        local impl = {
            CollectHellos = function(stream, _)
                t.assert_equals(stream:recv(), nil)
                return {greeting = 'empty'}
            end,
        }
        local client = hello.Greeter_client(pb.grpc.loopback(
            hello.Greeter_server(impl)))
        local call = client.CollectHellos({})
        call:close_send()
        local reply = call:recv()
        t.assert_equals(reply.greeting, 'empty')
    end

    -- ---------------------------------------------------------------------
    -- Bidirectional streaming: both sides send + recv independently.
    -- ---------------------------------------------------------------------

    g.test_bidi_echo_loop = function()
        -- Server echoes each request back with a "you said: " prefix.
        local impl = {
            Chat = function(stream, _)
                while true do
                    local req = stream:recv()
                    if req == nil then break end
                    stream:send({greeting = 'you said: ' .. req.name})
                end
            end,
        }
        local client = hello.Greeter_client(pb.grpc.loopback(
            hello.Greeter_server(impl)))

        local call = client.Chat({})

        -- Drive the client side from a separate fiber so we can interleave
        -- sends and receives without deadlocking.
        local reader_done = fiber.channel(1)
        local received = {}
        fiber.create(function()
            while true do
                local r, err = call:recv()
                if r == nil then
                    reader_done:put(err or false)
                    break
                end
                received[#received + 1] = r.greeting
            end
        end)

        call:send({name = 'foo'})
        call:send({name = 'bar'})
        call:send({name = 'baz'})
        call:close_send()

        local terminal = reader_done:get(5)
        t.assert(terminal == false, 'reader saw error: ' .. tostring(terminal))
        t.assert_equals(received, {
            'you said: foo', 'you said: bar', 'you said: baz',
        })
    end

    g.test_bidi_handler_error_surfaces = function()
        local impl = {
            Chat = function(_, _)
                error('chat exploded')
            end,
        }
        local client = hello.Greeter_client(pb.grpc.loopback(
            hello.Greeter_server(impl)))
        local call = client.Chat({})
        call:close_send()
        local r, err = call:recv()
        t.assert_equals(r, nil)
        t.assert_str_contains(tostring(err), 'chat exploded')
    end

    g.test_bidi_cancel_stops_further_recv = function()
        -- Server sends forever; client cancels after one message.
        local server_finished = fiber.channel(1)
        local impl = {
            Chat = function(stream, _)
                local i = 0
                while stream:send({greeting = 'tick:' .. tostring(i)}) do
                    i = i + 1
                    if i > 100 then break end -- safety
                    fiber.yield()
                end
                server_finished:put(i)
            end,
        }
        local client = hello.Greeter_client(pb.grpc.loopback(
            hello.Greeter_server(impl)))
        local call = client.Chat({})
        local first = call:recv()
        t.assert_str_contains(first.greeting, 'tick:')
        call:cancel()

        -- Give the server fiber a chance to observe the cancel.
        local final_i = server_finished:get(5)
        t.assert(final_i ~= nil, 'server did not stop after cancel')
        t.assert(final_i <= 100, 'server kept running past safety bound')
    end
end

-- ---------------------------------------------------------------------------
-- pb.grpc.new_stream_pair: unit-level coverage of the primitive itself
-- (independent of generated code).
-- ---------------------------------------------------------------------------

local u = t.group('grpc_stream.primitive')

u.test_send_recv_byte_passthrough = function()
    local client, server = pb.grpc.new_stream_pair()
    fiber.create(function()
        local req = server:recv()
        server:send('reply:' .. req)
        server:_finish(nil)
    end)
    client:send('ping')
    client:close_send()
    local out = client:recv()
    t.assert_equals(out, 'reply:ping')
    t.assert_equals(client:recv(), nil)
end

u.test_finish_with_error_propagates = function()
    local client, server = pb.grpc.new_stream_pair()
    fiber.create(function()
        server:_finish('handler error')
    end)
    local b, err = client:recv()
    t.assert_equals(b, nil)
    t.assert_equals(err, 'handler error')
end

u.test_send_after_close_send_errors = function()
    local client, _ = pb.grpc.new_stream_pair()
    client:close_send()
    t.assert_error(function() client:send('nope') end)
end

-- ---------------------------------------------------------------------------
-- multiplex: streaming methods route correctly across multiple servers.
-- ---------------------------------------------------------------------------

local m = t.group('grpc_stream.multiplex')

m.test_multiplex_routes_streams_to_right_server = function()
    local hello = require('full.hello.hello_pb')
    -- Two independent Greeter servers; multiplex would normally complain
    -- about the duplicate paths, so for this test we keep just one with a
    -- streaming impl and verify routing works via the multiplex transport.
    local impl = {
        StreamHellos = function(req, stream, _)
            stream:send({greeting = 'mp:' .. req.name})
        end,
    }
    local mux = pb.grpc.multiplex({hello.Greeter_server(impl)})
    local client = hello.Greeter_client(mux)
    local s = client.StreamHellos({name = 'X'}, {})
    local r = s:recv()
    t.assert_equals(r.greeting, 'mp:X')
    t.assert_equals(s:recv(), nil)
end

m.test_multiplex_duplicate_streaming_path_errors = function()
    -- Construct two minimal server tables that share a streaming path so
    -- we hit the duplicate-stream branch without also tripping the
    -- duplicate-unary branch.
    local stream_entry = {
        kind = 'server_stream',
        handler = function() end,
    }
    local s1 = {methods = {}, streams = {['/dup/Path'] = stream_entry}}
    local s2 = {methods = {}, streams = {['/dup/Path'] = stream_entry}}
    t.assert_error_msg_contains('duplicate streaming route', function()
        pb.grpc.multiplex({s1, s2})
    end)
end