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.
5 #include "sync/engine/traffic_recorder.h"
7 #include "base/memory/scoped_ptr.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"
16 const unsigned int kMaxMessages
= 10;
17 const unsigned int kMaxMessageSize
= 5 * 1024;
19 // Ensure the number of records don't exceed |kMaxMessages|.
20 TEST(TrafficRecorderTest
, MaxRecordsTest
) {
21 TrafficRecorder
recorder(kMaxMessages
, kMaxMessageSize
);
22 sync_pb::ClientToServerResponse response
;
24 for (unsigned int i
= 0; i
< 2*kMaxMessages
; ++i
)
25 recorder
.RecordClientToServerResponse(response
);
27 EXPECT_EQ(recorder
.records().size(), kMaxMessages
);
30 // Ensure records with size greater than |kMaxMessageSize| are truncated.
31 TEST(TrafficRecorderTest
, MaxMessageSizeTest
) {
32 sync_pb::ClientToServerResponse response
;
34 sync_pb::ClientToServerResponse::Error
* error
= response
.mutable_error();
35 std::string
error_description(kMaxMessageSize
* 2, 'a');
36 error
->set_error_description(error_description
);
38 TrafficRecorder
recorder(kMaxMessages
, kMaxMessageSize
);
39 recorder
.RecordClientToServerResponse(response
);
41 TrafficRecorder::TrafficRecord record
= recorder
.records().front();
42 EXPECT_TRUE(record
.truncated
);
43 EXPECT_TRUE(record
.message
.empty());
46 // Test implementation of TrafficRecorder.
47 class TestTrafficRecorder
: public TrafficRecorder
{
49 TestTrafficRecorder(unsigned int max_messages
, unsigned int max_message_size
)
50 : TrafficRecorder(max_messages
, max_message_size
) {
53 virtual ~TestTrafficRecorder() {}
55 virtual base::Time
GetTime() OVERRIDE
{
59 void set_time(int64 time
) {
60 time_
= ProtoTimeToTime(time
);
63 void set_time(base::Time time
) {
71 // Ensure that timestamp is recorded correctly in traffic record.
72 TEST(TrafficRecorderTest
, TimestampTest
) {
73 sync_pb::ClientToServerResponse response
;
75 TestTrafficRecorder
recorder(kMaxMessages
, kMaxMessageSize
);
77 recorder
.RecordClientToServerResponse(response
);
79 base::Time expect_time
= ProtoTimeToTime(3);
80 TrafficRecorder::TrafficRecord record
= recorder
.records().front();
81 EXPECT_EQ(expect_time
, record
.timestamp
);
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));
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
);
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
);
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
);
108 TestTrafficRecorder
recorder(kMaxMessages
, kMaxMessageSize
);
109 recorder
.set_time(sample_time
);
110 recorder
.RecordClientToServerResponse(response
);
112 scoped_ptr
<ListValue
> value
;
113 value
.reset(recorder
.ToValue());
115 DictionaryValue
* record_value
;
116 std::string time_str
;
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
);
123 } // namespace syncer