aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/grpc/server/ut/grpc_response_ut.cpp
blob: 97952f416677c7e1bcf9ae2ff53e021952102373 (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
#include <library/cpp/grpc/server/grpc_response.h>
#include <library/cpp/testing/unittest/registar.h>

#include <google/protobuf/duration.pb.h>
#include <grpc++/impl/codegen/proto_utils.h>
#include <grpc++/impl/grpc_library.h>

using namespace NGrpc;

using google::protobuf::Duration;

Y_UNIT_TEST_SUITE(ResponseTest) {

    template <typename T>
    grpc::ByteBuffer Serialize(T resp) {
        grpc::ByteBuffer buf;
        bool ownBuf = false;
        grpc::Status status = grpc::SerializationTraits<T>::Serialize(resp, &buf, &ownBuf);
        UNIT_ASSERT(status.ok());
        return buf;
    }

    template <typename T>
    T Deserialize(grpc::ByteBuffer* buf) {
        T message;
        auto status = grpc::SerializationTraits<T>::Deserialize(buf, &message);
        UNIT_ASSERT(status.ok());
        return message;
    }

    Y_UNIT_TEST(UniversalResponseMsg) {
        Duration d1;
        d1.set_seconds(12345);
        d1.set_nanos(67890);

        auto buf = Serialize(TUniversalResponse<Duration>(&d1));
        Duration d2 = Deserialize<Duration>(&buf);

        UNIT_ASSERT_VALUES_EQUAL(d2.seconds(), 12345);
        UNIT_ASSERT_VALUES_EQUAL(d2.nanos(), 67890);
    }

    Y_UNIT_TEST(UniversalResponseBuf) {
        Duration d1;
        d1.set_seconds(123);
        d1.set_nanos(456);

        TString data = d1.SerializeAsString();
        grpc::Slice dataSlice{data.data(), data.size()};
        grpc::ByteBuffer dataBuf{&dataSlice, 1};

        auto buf = Serialize(TUniversalResponse<Duration>(&dataBuf));
        Duration d2 = Deserialize<Duration>(&buf);

        UNIT_ASSERT_VALUES_EQUAL(d2.seconds(), 123);
        UNIT_ASSERT_VALUES_EQUAL(d2.nanos(), 456);
    }

    Y_UNIT_TEST(UniversalResponseRefMsg) {
        Duration d1;
        d1.set_seconds(12345);
        d1.set_nanos(67890);

        auto buf = Serialize(TUniversalResponseRef<Duration>(&d1));
        Duration d2 = Deserialize<Duration>(&buf);

        UNIT_ASSERT_VALUES_EQUAL(d2.seconds(), 12345);
        UNIT_ASSERT_VALUES_EQUAL(d2.nanos(), 67890);
    }

    Y_UNIT_TEST(UniversalResponseRefBuf) {
        Duration d1;
        d1.set_seconds(123);
        d1.set_nanos(456);

        TString data = d1.SerializeAsString();
        grpc::Slice dataSlice{data.data(), data.size()};
        grpc::ByteBuffer dataBuf{&dataSlice, 1};

        auto buf = Serialize(TUniversalResponseRef<Duration>(&dataBuf));
        Duration d2 = Deserialize<Duration>(&buf);

        UNIT_ASSERT_VALUES_EQUAL(d2.seconds(), 123);
        UNIT_ASSERT_VALUES_EQUAL(d2.nanos(), 456);
    }
}