forked from trpc-group/trpc-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrpc_compressor_test.cc
More file actions
112 lines (92 loc) · 4.06 KB
/
Copy pathtrpc_compressor_test.cc
File metadata and controls
112 lines (92 loc) · 4.06 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
102
103
104
105
106
107
108
109
110
111
112
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
#include "trpc/compressor/trpc_compressor.h"
#include "gtest/gtest.h"
#include "trpc/compressor/compressor_factory.h"
#include "trpc/compressor/testing/compressor_testing.h"
#include "trpc/util/buffer/buffer.h"
namespace trpc::compressor::testing {
TEST(TrpcCompressor, Init) {
ASSERT_TRUE(Init());
Destroy();
}
TEST(TrpcCompressor, CompressSuccess) {
CompressType type{132};
CompressorFactory::GetInstance()->Clear();
auto p = MakeRefCounted<MockCompressor>();
EXPECT_CALL(*p, Type()).WillOnce(::testing::Return(type));
EXPECT_CALL(*p, DoCompress(::testing::_, ::testing::_, ::testing::_)).WillRepeatedly(::testing::Return(true));
EXPECT_CALL(*p, DoDecompress(::testing::_, ::testing::_)).WillRepeatedly(::testing::Return(true));
ASSERT_TRUE(CompressorFactory::GetInstance()->Register(p));
NoncontiguousBuffer compress_in = CreateBufferSlow("hello world");
ASSERT_TRUE(CompressIfNeeded(type, compress_in));
NoncontiguousBuffer decompress_in = CreateBufferSlow("hello world");
ASSERT_TRUE(DecompressIfNeeded(type, decompress_in));
NoncontiguousBuffer in;
NoncontiguousBuffer out;
ASSERT_TRUE(Compress(type, in, out));
ASSERT_TRUE(Compress(type, in, out));
}
TEST(TrpcCompressor, CompressFail) {
CompressType type{133};
auto p = MakeRefCounted<MockCompressor>();
EXPECT_CALL(*p, Type()).WillOnce(::testing::Return(type));
EXPECT_CALL(*p, DoCompress(::testing::_, ::testing::_, ::testing::_))
.WillRepeatedly(::testing::Return(false));
EXPECT_CALL(*p, DoDecompress(::testing::_, ::testing::_)).WillRepeatedly(::testing::Return(false));
CompressorFactory::GetInstance()->Clear();
ASSERT_TRUE(CompressorFactory::GetInstance()->Register(p));
NoncontiguousBuffer compress_in = CreateBufferSlow("hello world");
ASSERT_FALSE(CompressIfNeeded(type, compress_in));
NoncontiguousBuffer decompress_in = CreateBufferSlow("hello world");
ASSERT_FALSE(DecompressIfNeeded(type, decompress_in));
NoncontiguousBuffer in = CreateBufferSlow("hello world");
NoncontiguousBuffer out;
ASSERT_FALSE(Compress(type, in, out));
ASSERT_FALSE(Compress(type, in, out));
}
TEST(TrpcCompressor, CompressorNotSupport) {
CompressType type{134};
CompressorFactory::GetInstance()->Clear();
NoncontiguousBuffer compress_in = CreateBufferSlow("hello world");
ASSERT_FALSE(CompressIfNeeded(type, compress_in));
NoncontiguousBuffer decompress_in = CreateBufferSlow("hello world");
ASSERT_FALSE(DecompressIfNeeded(type, decompress_in));
NoncontiguousBuffer in = CreateBufferSlow("hello world");
NoncontiguousBuffer out;
ASSERT_FALSE(Compress(type, in, out));
ASSERT_FALSE(Compress(type, in, out));
}
TEST(TrpcCompressor, CompressorTypeNone) {
NoncontiguousBuffer compress_in = CreateBufferSlow("hello world");
ASSERT_TRUE(CompressIfNeeded(kNone, compress_in));
NoncontiguousBuffer decompress_in = CreateBufferSlow("hello world");
ASSERT_TRUE(DecompressIfNeeded(kNone, decompress_in));
NoncontiguousBuffer in = CreateBufferSlow("hello world");
NoncontiguousBuffer out;
ASSERT_FALSE(Compress(kNone, in, out));
ASSERT_FALSE(Compress(kMaxType, in, out));
ASSERT_FALSE(Decompress(kNone, in, out));
ASSERT_FALSE(Decompress(kMaxType, in, out));
}
TEST(TrpcCompressor, CompressorTypeMaxType) {
NoncontiguousBuffer compress_in = CreateBufferSlow("hello world");
ASSERT_FALSE(CompressIfNeeded(kMaxType, compress_in));
NoncontiguousBuffer decompress_in = CreateBufferSlow("hello world");
ASSERT_FALSE(DecompressIfNeeded(kMaxType, decompress_in));
NoncontiguousBuffer in = CreateBufferSlow("hello world");
NoncontiguousBuffer out;
ASSERT_FALSE(Compress(kMaxType, in, out));
ASSERT_FALSE(Decompress(kMaxType, in, out));
}
} // namespace trpc::compressor::testing