summaryrefslogtreecommitdiff
path: root/src/mongo/transport/grpc/grpc_session_test.cpp
blob: de4f7e2897e932d0a88c36ae66d581234341b054 (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
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
/**
 *    Copyright (C) 2023-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include <memory>

#include "mongo/platform/atomic_word.h"
#include "mongo/rpc/message.h"
#include "mongo/rpc/op_msg.h"
#include "mongo/transport/grpc/grpc_session.h"
#include "mongo/transport/grpc/test_fixtures.h"
#include "mongo/unittest/death_test.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/net/hostandport.h"

namespace mongo::transport::grpc {
namespace {

class SessionTest : public ServiceContextWithClockSourceMockTest {
public:
    auto makeMessage() {
        OpMsg msg;
        msg.body = BSON("id" << _nextMessage.fetchAndAdd(1));
        return msg.serialize();
    }

private:
    AtomicWord<int> _nextMessage{0};
};

class IngressSessionTest : public SessionTest {
public:
    static constexpr auto kStreamTimeout = Seconds(1);
    static constexpr auto kClientId = "c08663ac-2f6c-408d-8829-97e67eef9f23";
    static constexpr auto kRemote = "FakeHost:1234";

    void setUp() override {
        SessionTest::setUp();
        // The MockStreamTestFixtures created here doesn't contain any references to the channel or
        // server, so it's okay to let stubFixture go out of scope.
        MockStubTestFixtures stubFixture;
        _fixture = stubFixture.makeStreamTestFixtures(
            getServiceContext()->getFastClockSource()->now() + kStreamTimeout, _clientMetadata);
    }

    void tearDown() override {
        _fixture.reset();
    }

    auto fixture() {
        return _fixture.get();
    }

    auto makeSession() {
        auto swClientId = UUID::parse(kClientId);
        ASSERT_OK(swClientId.getStatus());
        return std::make_unique<IngressSession>(nullptr,
                                                _fixture->serverCtx.get(),
                                                _fixture->serverStream.get(),
                                                swClientId.getValue());
    }

private:
    const MetadataView _clientMetadata = {{"foo", "bar"}};
    std::unique_ptr<MockStreamTestFixtures> _fixture;
};

TEST_F(IngressSessionTest, NoClientId) {
    auto session = std::make_unique<IngressSession>(
        nullptr, fixture()->serverCtx.get(), fixture()->serverStream.get(), boost::none);
    ASSERT_FALSE(session->clientId());
    session->end();
}

TEST_F(IngressSessionTest, AlwaysCancelsStream) {
    auto session = makeSession();
    ASSERT_FALSE(fixture()->serverCtx->isCancelled());
    session.reset();
    ASSERT_TRUE(fixture()->serverCtx->isCancelled());
}

TEST_F(IngressSessionTest, GetClientId) {
    auto session = makeSession();
    ASSERT_TRUE(session->clientId());
    ASSERT_EQ(session->clientId()->toString(), kClientId);
}

TEST_F(IngressSessionTest, GetRemote) {
    auto session = makeSession();
    ASSERT_EQ(session->remote().toString(), MockStubTestFixtures::kClientAddress);
}

TEST_F(IngressSessionTest, IsConnected) {
    auto session = makeSession();
    ASSERT_TRUE(session->isConnected());
    fixture()->serverCtx->tryCancel();
    ASSERT_FALSE(session->isConnected());
}

TEST_F(IngressSessionTest, End) {
    auto session = makeSession();
    session->end();
    ASSERT_FALSE(session->isConnected());
    ASSERT_TRUE(session->terminationStatus());
    ASSERT_OK(*session->terminationStatus());
}

TEST_F(IngressSessionTest, TerminateWithError) {
    const Status error(ErrorCodes::InternalError, "Some Error");
    auto session = makeSession();
    session->terminate(error);
    ASSERT_FALSE(session->isConnected());
    ASSERT_TRUE(session->terminationStatus());
    ASSERT_EQ(*session->terminationStatus(), error);
    ASSERT_TRUE(fixture()->serverCtx->isCancelled());
}

TEST_F(IngressSessionTest, TerminateRetainsStatus) {
    const Status error(ErrorCodes::InternalError, "Some Error");
    auto session = makeSession();
    session->terminate(error);
    session->end();
    ASSERT_EQ(*session->terminationStatus(), error);
}

TEST_F(IngressSessionTest, ReadAndWrite) {
    auto session = makeSession();
    {
        auto msg = makeMessage();
        fixture()->clientStream->write(msg.sharedBuffer());
        auto swReceived = session->sourceMessage();
        ASSERT_OK(swReceived.getStatus());
        ASSERT_EQ_MSG(swReceived.getValue(), msg);
    }

    {
        auto msg = makeMessage();
        ASSERT_OK(session->sinkMessage(msg));
        auto sent = fixture()->clientStream->read();
        ASSERT_TRUE(sent);
        ASSERT_EQ_MSG(Message{*sent}, msg);
    }
}

TEST_F(IngressSessionTest, ReadFromClosedStream) {
    auto session = makeSession();
    fixture()->serverCtx->tryCancel();
    auto swReceived = session->sourceMessage();
    ASSERT_EQ(swReceived.getStatus(), ErrorCodes::StreamTerminated);
}

TEST_F(IngressSessionTest, ReadTimesOut) {
    auto session = makeSession();
    clockSource().advance(2 * kStreamTimeout);
    auto swReceived = session->sourceMessage();
    ASSERT_EQ(swReceived.getStatus(), ErrorCodes::StreamTerminated);
}

TEST_F(IngressSessionTest, WriteToClosedStream) {
    auto session = makeSession();
    fixture()->serverCtx->tryCancel();
    ASSERT_EQ(session->sinkMessage(Message{}), ErrorCodes::StreamTerminated);
}

TEST_F(IngressSessionTest, WriteTimesOut) {
    auto session = makeSession();
    clockSource().advance(2 * kStreamTimeout);
    ASSERT_EQ(session->sinkMessage(Message{}), ErrorCodes::StreamTerminated);
}

}  // namespace
}  // namespace mongo::transport::grpc