OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "sync/engine/traffic_recorder.h" | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/time/time.h" | |
9 #include "base/values.h" | |
10 #include "sync/protocol/sync.pb.h" | |
11 #include "sync/util/time.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace syncer { | |
15 | |
16 const unsigned int kMaxMessages = 10; | |
17 const unsigned int kMaxMessageSize = 5 * 1024; | |
18 | |
19 // Ensure the number of records don't exceed |kMaxMessages|. | |
20 TEST(TrafficRecorderTest, MaxRecordsTest) { | |
21 TrafficRecorder recorder(kMaxMessages, kMaxMessageSize); | |
22 sync_pb::ClientToServerResponse response; | |
23 | |
24 for (unsigned int i = 0; i < 2*kMaxMessages; ++i) | |
25 recorder.RecordClientToServerResponse(response); | |
26 | |
27 EXPECT_EQ(recorder.records().size(), kMaxMessages); | |
28 } | |
29 | |
30 // Ensure records with size greater than |kMaxMessageSize| are truncated. | |
31 TEST(TrafficRecorderTest, MaxMessageSizeTest) { | |
32 sync_pb::ClientToServerResponse response; | |
33 | |
34 sync_pb::ClientToServerResponse::Error* error = response.mutable_error(); | |
35 std::string error_description(kMaxMessageSize * 2, 'a'); | |
36 error->set_error_description(error_description); | |
37 | |
38 TrafficRecorder recorder(kMaxMessages, kMaxMessageSize); | |
39 recorder.RecordClientToServerResponse(response); | |
40 | |
41 TrafficRecorder::TrafficRecord record = recorder.records().front(); | |
42 EXPECT_TRUE(record.truncated); | |
43 EXPECT_TRUE(record.message.empty()); | |
44 } | |
45 | |
46 // Test implementation of TrafficRecorder. | |
47 class TestTrafficRecorder : public TrafficRecorder { | |
48 public: | |
49 TestTrafficRecorder(unsigned int max_messages, unsigned int max_message_size) | |
50 : TrafficRecorder(max_messages, max_message_size) { | |
51 set_time(0); | |
52 } | |
53 virtual ~TestTrafficRecorder() {} | |
54 | |
55 virtual base::Time GetTime() OVERRIDE { | |
56 return time_; | |
57 } | |
58 | |
59 void set_time(int64 time) { | |
60 time_ = ProtoTimeToTime(time); | |
61 } | |
62 | |
63 void set_time(base::Time time) { | |
64 time_ = time; | |
65 } | |
66 | |
67 private: | |
68 base::Time time_; | |
69 }; | |
70 | |
71 // Ensure that timestamp is recorded correctly in traffic record. | |
72 TEST(TrafficRecorderTest, TimestampTest) { | |
73 sync_pb::ClientToServerResponse response; | |
74 | |
75 TestTrafficRecorder recorder(kMaxMessages, kMaxMessageSize); | |
76 recorder.set_time(3); | |
77 recorder.RecordClientToServerResponse(response); | |
78 | |
79 base::Time expect_time = ProtoTimeToTime(3); | |
80 TrafficRecorder::TrafficRecord record = recorder.records().front(); | |
81 EXPECT_EQ(expect_time, record.timestamp); | |
82 } | |
83 | |
84 // Ensure that timestamps are recorded correctly in traffic records. | |
85 TEST(TrafficRecorderTest, MultipleTimestampTest) { | |
86 sync_pb::ClientToServerResponse response; | |
87 base::Time sample_time_1 = ProtoTimeToTime(GG_INT64_C(1359484676659)); | |
88 base::Time sample_time_2 = ProtoTimeToTime(GG_INT64_C(135948467665932)); | |
89 | |
90 TestTrafficRecorder recorder(kMaxMessages, kMaxMessageSize); | |
91 recorder.set_time(sample_time_1); | |
92 recorder.RecordClientToServerResponse(response); | |
93 recorder.set_time(sample_time_2); | |
94 recorder.RecordClientToServerResponse(response); | |
95 | |
96 TrafficRecorder::TrafficRecord record_1 = recorder.records().front(); | |
97 TrafficRecorder::TrafficRecord record_2 = recorder.records().back(); | |
98 EXPECT_EQ(sample_time_1, record_1.timestamp); | |
99 EXPECT_EQ(sample_time_2, record_2.timestamp); | |
100 } | |
101 | |
102 // Ensure that timestamp is added to ListValue of DictionaryValues in ToValue(). | |
103 TEST(TrafficRecorderTest, ToValueTimestampTest) { | |
104 sync_pb::ClientToServerResponse response; | |
105 base::Time sample_time = ProtoTimeToTime(GG_INT64_C(135948467665932)); | |
106 std::string expect_time_str = GetTimeDebugString(sample_time); | |
107 | |
108 TestTrafficRecorder recorder(kMaxMessages, kMaxMessageSize); | |
109 recorder.set_time(sample_time); | |
110 recorder.RecordClientToServerResponse(response); | |
111 | |
112 scoped_ptr<base::ListValue> value; | |
113 value.reset(recorder.ToValue()); | |
114 | |
115 base::DictionaryValue* record_value; | |
116 std::string time_str; | |
117 | |
118 ASSERT_TRUE(value->GetDictionary(0, &record_value)); | |
119 EXPECT_TRUE(record_value->GetString("timestamp", &time_str)); | |
120 EXPECT_EQ(expect_time_str, time_str); | |
121 } | |
122 | |
123 } // namespace syncer | |
OLD | NEW |