Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
thrift-util-test.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <iostream>
18 
19 #include <gtest/gtest.h>
20 #include "util/network-util.h"
21 #include "rpc/thrift-util.h"
22 
23 #include "gen-cpp/RuntimeProfile_types.h"
24 
25 #include "common/names.h"
26 
27 namespace impala {
28 
29 TEST(ThriftUtil, SimpleSerializeDeserialize) {
30  Status status;
31  // Loop over compact and binary protocols
32  for (int i = 0; i < 2; ++i) {
33  bool compact = (i == 0);
34  ThriftSerializer serializer(compact);
35 
36  TCounter counter;
37  counter.__set_name("Test");
38  counter.__set_unit(TUnit::UNIT);
39  counter.__set_value(123);
40 
41  vector<uint8_t> msg;
42  status = serializer.Serialize(&counter, &msg);
43  EXPECT_TRUE(status.ok());
44 
45  uint8_t* buffer1 = NULL;
46  uint8_t* buffer2 = NULL;
47  uint32_t len1 = 0;
48  uint32_t len2 = 0;
49 
50  status = serializer.Serialize(&counter, &len1, &buffer1);
51  EXPECT_TRUE(status.ok());
52 
53  EXPECT_EQ(len1, msg.size());
54  EXPECT_TRUE(memcmp(buffer1, &msg[0], len1) == 0);
55 
56  // Serialize again and ensure the memory buffer is the same and being reused.
57  status = serializer.Serialize(&counter, &len2, &buffer2);
58  EXPECT_TRUE(status.ok());
59 
60  EXPECT_EQ(len1, len2);
61  EXPECT_TRUE(buffer1 == buffer2);
62 
63  TCounter deserialized_counter;
64  status = DeserializeThriftMsg(buffer1, &len1, compact, &deserialized_counter);
65  EXPECT_TRUE(status.ok());
66  EXPECT_EQ(len1, len2);
67  EXPECT_TRUE(counter == deserialized_counter);
68  }
69 }
70 
72  EXPECT_TRUE(TNetworkAddressComparator(MakeNetworkAddress("aaaa", 500),
73  MakeNetworkAddress("zzzz", 500)));
74  EXPECT_FALSE(TNetworkAddressComparator(MakeNetworkAddress("zzzz", 500),
75  MakeNetworkAddress("aaaa", 500)));
76  EXPECT_TRUE(TNetworkAddressComparator(MakeNetworkAddress("aaaa", 500),
77  MakeNetworkAddress("aaaa", 501)));
78  EXPECT_FALSE(TNetworkAddressComparator(MakeNetworkAddress("aaaa", 501),
79  MakeNetworkAddress("aaaa", 500)));
80 
81  EXPECT_FALSE(TNetworkAddressComparator(MakeNetworkAddress("aaaa", 500),
82  MakeNetworkAddress("aaaa", 500)));
83 }
84 
85 }
86 
87 int main(int argc, char **argv) {
88  ::testing::InitGoogleTest(&argc, argv);
89  return RUN_ALL_TESTS();
90 }
TEST(AtomicTest, Basic)
Definition: atomic-test.cc:28
TNetworkAddress MakeNetworkAddress(const string &hostname, int port)
Definition: network-util.cc:96
bool TNetworkAddressComparator(const TNetworkAddress &a, const TNetworkAddress &b)
Definition: thrift-util.cc:168
Status Serialize(T *obj, std::vector< uint8_t > *result)
Serializes obj into result. Result will contain a copy of the memory.
Definition: thrift-util.h:48
int main(int argc, char **argv)
Status DeserializeThriftMsg(JNIEnv *env, jbyteArray serialized_msg, T *deserialized_msg)
bool ok() const
Definition: status.h:172