forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFramedReaderTest.cpp
More file actions
101 lines (78 loc) · 3.26 KB
/
FramedReaderTest.cpp
File metadata and controls
101 lines (78 loc) · 3.26 KB
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
// Copyright 2004-present Facebook. All Rights Reserved.
#include <gtest/gtest.h>
#include "rsocket/framing/FramedReader.h"
#include "rsocket/test/test_utils/MockDuplexConnection.h"
using namespace rsocket;
using namespace testing;
using namespace yarpl::mocks;
TEST(FramedReader, TinyFrame) {
auto version = std::make_shared<ProtocolVersion>(ProtocolVersion::Latest);
auto reader = yarpl::make_ref<FramedReader>(version);
// Not using hex string-literal as std::string ctor hits '\x00' and stops
// reading.
auto buf = folly::IOBuf::createCombined(4);
buf->append(4);
buf->writableData()[0] = '\x00';
buf->writableData()[1] = '\x00';
buf->writableData()[2] = '\x00';
buf->writableData()[3] = '\x02';
reader->onSubscribe(yarpl::flowable::Subscription::empty());
reader->onNext(std::move(buf));
auto subscriber = yarpl::make_ref<
StrictMock<MockSubscriber<std::unique_ptr<folly::IOBuf>>>>();
EXPECT_CALL(*subscriber, onSubscribe_(_));
EXPECT_CALL(*subscriber, onError_(_));
reader->setInput(subscriber);
subscriber->awaitTerminalEvent();
reader->onComplete();
}
TEST(FramedReader, CantDetectVersion) {
auto version = std::make_shared<ProtocolVersion>(ProtocolVersion::Unknown);
auto reader = yarpl::make_ref<FramedReader>(version);
auto buf = folly::IOBuf::copyBuffer("ABCDEFGHIJKLMNOP");
reader->onSubscribe(yarpl::flowable::Subscription::empty());
reader->onNext(std::move(buf));
auto subscriber = yarpl::make_ref<
StrictMock<MockSubscriber<std::unique_ptr<folly::IOBuf>>>>();
EXPECT_CALL(*subscriber, onSubscribe_(_));
EXPECT_CALL(*subscriber, onError_(_));
reader->setInput(subscriber);
subscriber->awaitTerminalEvent();
reader->onComplete();
}
TEST(FramedReader, SubscriberCompleteAfterError) {
auto version = std::make_shared<ProtocolVersion>(ProtocolVersion::Latest);
auto reader = yarpl::make_ref<FramedReader>(version);
auto subscription = yarpl::make_ref<StrictMock<MockSubscription>>();
EXPECT_CALL(*subscription, request_(_));
EXPECT_CALL(*subscription, cancel_());
reader->onSubscribe(subscription);
auto subscriber = yarpl::make_ref<
StrictMock<MockSubscriber<std::unique_ptr<folly::IOBuf>>>>();
EXPECT_CALL(*subscriber, onSubscribe_(_));
EXPECT_CALL(*subscriber, onError_(_))
.WillOnce(Invoke([](folly::exception_wrapper ew) {
EXPECT_EQ(ew.get_exception()->what(), std::string{"Oops"});
}));
reader->setInput(subscriber);
reader->error("Oops");
reader->onComplete();
}
TEST(FramedReader, SubscriberErrorAfterError) {
auto version = std::make_shared<ProtocolVersion>(ProtocolVersion::Latest);
auto reader = yarpl::make_ref<FramedReader>(version);
auto subscription = yarpl::make_ref<StrictMock<MockSubscription>>();
EXPECT_CALL(*subscription, request_(_));
EXPECT_CALL(*subscription, cancel_());
reader->onSubscribe(subscription);
auto subscriber = yarpl::make_ref<
StrictMock<MockSubscriber<std::unique_ptr<folly::IOBuf>>>>();
EXPECT_CALL(*subscriber, onSubscribe_(_));
EXPECT_CALL(*subscriber, onError_(_))
.WillOnce(Invoke([](folly::exception_wrapper ew) {
EXPECT_EQ(ew.get_exception()->what(), std::string{"Oops"});
}));
reader->setInput(subscriber);
reader->error("Oops");
reader->onError(std::runtime_error{"Not oops"});
}