This source file includes following definitions.
- ModelTypeSetEquals
- BuildUnknownVersionInvalidation
- GetHintBufferSize
- InvalidationsOutOfSync
- ProtoLocallyModifiedCount
- ProtoRefreshRequestedCount
- SetInvalidationsInSync
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- IsInvalidationUnacknowledged
- IsInvalidationAcknowledged
- IsInvalidationDropped
- AllInvalidationsAccountedFor
- SendInvalidation
- SendUnknownVersionInvalidation
- RecordSuccessfulSyncCycle
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "sync/internal_api/public/base/model_type_test_util.h"
#include "sync/notifier/invalidation_util.h"
#include "sync/notifier/mock_ack_handler.h"
#include "sync/notifier/object_id_invalidation_map.h"
#include "sync/sessions/nudge_tracker.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace syncer {
namespace {
testing::AssertionResult ModelTypeSetEquals(ModelTypeSet a, ModelTypeSet b) {
if (a.Equals(b)) {
return testing::AssertionSuccess();
} else {
return testing::AssertionFailure()
<< "Left side " << ModelTypeSetToString(a)
<< ", does not match rigth side: " << ModelTypeSetToString(b);
}
}
syncer::Invalidation BuildUnknownVersionInvalidation(ModelType type) {
invalidation::ObjectId id;
bool result = RealModelTypeToObjectId(type, &id);
DCHECK(result);
return Invalidation::InitUnknownVersion(id);
}
}
namespace sessions {
class NudgeTrackerTest : public ::testing::Test {
public:
NudgeTrackerTest() {
SetInvalidationsInSync();
}
static size_t GetHintBufferSize() {
return NudgeTracker::kDefaultMaxPayloadsPerType;
}
bool InvalidationsOutOfSync() const {
sync_pb::GetUpdateTriggers gu_trigger;
nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger);
return gu_trigger.invalidations_out_of_sync();
}
int ProtoLocallyModifiedCount(ModelType type) const {
sync_pb::GetUpdateTriggers gu_trigger;
nudge_tracker_.FillProtoMessage(type, &gu_trigger);
return gu_trigger.local_modification_nudges();
}
int ProtoRefreshRequestedCount(ModelType type) const {
sync_pb::GetUpdateTriggers gu_trigger;
nudge_tracker_.FillProtoMessage(type, &gu_trigger);
return gu_trigger.datatype_refresh_nudges();
}
void SetInvalidationsInSync() {
nudge_tracker_.OnInvalidationsEnabled();
nudge_tracker_.RecordSuccessfulSyncCycle();
}
protected:
NudgeTracker nudge_tracker_;
};
TEST_F(NudgeTrackerTest, EmptyNudgeTracker) {
EXPECT_FALSE(nudge_tracker_.IsSyncRequired());
EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::UNKNOWN,
nudge_tracker_.GetLegacySource());
sync_pb::GetUpdateTriggers gu_trigger;
nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger);
EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::UNKNOWN,
nudge_tracker_.GetLegacySource());
}
TEST_F(NudgeTrackerTest, SourcePriorities) {
const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234);
const base::TimeTicks t1 = t0 + base::TimeDelta::FromSeconds(10);
nudge_tracker_.SetNextRetryTime(t0);
nudge_tracker_.SetSyncCycleStartTime(t1);
EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RETRY,
nudge_tracker_.GetLegacySource());
nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS));
EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::LOCAL,
nudge_tracker_.GetLegacySource());
nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(TYPED_URLS));
EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::DATATYPE_REFRESH,
nudge_tracker_.GetLegacySource());
nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS));
EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::DATATYPE_REFRESH,
nudge_tracker_.GetLegacySource());
ObjectIdInvalidationMap invalidation_map =
BuildInvalidationMap(PREFERENCES, 1, "hint");
nudge_tracker_.RecordRemoteInvalidation(invalidation_map);
EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::NOTIFICATION,
nudge_tracker_.GetLegacySource());
nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS));
EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::NOTIFICATION,
nudge_tracker_.GetLegacySource());
nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(TYPED_URLS));
EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::NOTIFICATION,
nudge_tracker_.GetLegacySource());
}
TEST_F(NudgeTrackerTest, HintCoalescing) {
{
ObjectIdInvalidationMap invalidation_map =
BuildInvalidationMap(BOOKMARKS, 1, "bm_hint_1");
nudge_tracker_.RecordRemoteInvalidation(invalidation_map);
sync_pb::GetUpdateTriggers gu_trigger;
nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger);
ASSERT_EQ(1, gu_trigger.notification_hint_size());
EXPECT_EQ("bm_hint_1", gu_trigger.notification_hint(0));
EXPECT_FALSE(gu_trigger.client_dropped_hints());
}
{
ObjectIdInvalidationMap invalidation_map =
BuildInvalidationMap(BOOKMARKS, 2, "bm_hint_2");
nudge_tracker_.RecordRemoteInvalidation(invalidation_map);
sync_pb::GetUpdateTriggers gu_trigger;
nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger);
ASSERT_EQ(2, gu_trigger.notification_hint_size());
EXPECT_EQ("bm_hint_1", gu_trigger.notification_hint(0));
EXPECT_EQ("bm_hint_2", gu_trigger.notification_hint(1));
EXPECT_FALSE(gu_trigger.client_dropped_hints());
}
{
ObjectIdInvalidationMap invalidation_map =
BuildInvalidationMap(PASSWORDS, 1, "pw_hint_1");
nudge_tracker_.RecordRemoteInvalidation(invalidation_map);
sync_pb::GetUpdateTriggers bm_gu_trigger;
nudge_tracker_.FillProtoMessage(BOOKMARKS, &bm_gu_trigger);
ASSERT_EQ(2, bm_gu_trigger.notification_hint_size());
EXPECT_EQ("bm_hint_1", bm_gu_trigger.notification_hint(0));
EXPECT_EQ("bm_hint_2",
bm_gu_trigger.notification_hint(1));
EXPECT_FALSE(bm_gu_trigger.client_dropped_hints());
sync_pb::GetUpdateTriggers pw_gu_trigger;
nudge_tracker_.FillProtoMessage(PASSWORDS, &pw_gu_trigger);
ASSERT_EQ(1, pw_gu_trigger.notification_hint_size());
EXPECT_EQ("pw_hint_1", pw_gu_trigger.notification_hint(0));
EXPECT_FALSE(pw_gu_trigger.client_dropped_hints());
}
}
TEST_F(NudgeTrackerTest, DropHintsLocally_OneAtATime) {
for (size_t i = 0; i < GetHintBufferSize(); ++i) {
ObjectIdInvalidationMap invalidation_map =
BuildInvalidationMap(BOOKMARKS, i, "hint");
nudge_tracker_.RecordRemoteInvalidation(invalidation_map);
}
{
sync_pb::GetUpdateTriggers gu_trigger;
nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger);
EXPECT_EQ(GetHintBufferSize(),
static_cast<size_t>(gu_trigger.notification_hint_size()));
EXPECT_FALSE(gu_trigger.client_dropped_hints());
}
ObjectIdInvalidationMap invalidation_map2 =
BuildInvalidationMap(BOOKMARKS, 1000, "new_hint");
nudge_tracker_.RecordRemoteInvalidation(invalidation_map2);
{
sync_pb::GetUpdateTriggers gu_trigger;
nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger);
EXPECT_TRUE(gu_trigger.client_dropped_hints());
ASSERT_EQ(GetHintBufferSize(),
static_cast<size_t>(gu_trigger.notification_hint_size()));
EXPECT_EQ("new_hint", gu_trigger.notification_hint(GetHintBufferSize()-1));
EXPECT_EQ("hint", gu_trigger.notification_hint(0));
}
}
TEST_F(NudgeTrackerTest, DropHintsLocally_ManyHints) {
ObjectIdInvalidationMap invalidation_map;
for (size_t i = 0; i < GetHintBufferSize(); ++i) {
invalidation_map.Insert(BuildInvalidation(BOOKMARKS, i, "hint"));
}
nudge_tracker_.RecordRemoteInvalidation(invalidation_map);
{
sync_pb::GetUpdateTriggers gu_trigger;
nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger);
EXPECT_EQ(GetHintBufferSize(),
static_cast<size_t>(gu_trigger.notification_hint_size()));
EXPECT_FALSE(gu_trigger.client_dropped_hints());
}
ObjectIdInvalidationMap invalidation_map2;
invalidation_map2.Insert(BuildInvalidation(BOOKMARKS, 1000, "new_hint"));
invalidation_map2.Insert(BuildInvalidation(BOOKMARKS, 1001, "newer_hint"));
nudge_tracker_.RecordRemoteInvalidation(invalidation_map2);
{
sync_pb::GetUpdateTriggers gu_trigger;
nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger);
EXPECT_TRUE(gu_trigger.client_dropped_hints());
ASSERT_EQ(GetHintBufferSize(),
static_cast<size_t>(gu_trigger.notification_hint_size()));
EXPECT_EQ("newer_hint",
gu_trigger.notification_hint(GetHintBufferSize()-1));
EXPECT_EQ("new_hint", gu_trigger.notification_hint(GetHintBufferSize()-2));
EXPECT_EQ("hint", gu_trigger.notification_hint(0));
}
}
TEST_F(NudgeTrackerTest, DropHintsAtServer_Alone) {
ObjectIdInvalidationMap invalidation_map;
invalidation_map.Insert(BuildUnknownVersionInvalidation(BOOKMARKS));
nudge_tracker_.RecordRemoteInvalidation(invalidation_map);
{
sync_pb::GetUpdateTriggers gu_trigger;
nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger);
EXPECT_TRUE(gu_trigger.server_dropped_hints());
EXPECT_FALSE(gu_trigger.client_dropped_hints());
ASSERT_EQ(0, gu_trigger.notification_hint_size());
}
nudge_tracker_.RecordSuccessfulSyncCycle();
{
sync_pb::GetUpdateTriggers gu_trigger;
nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger);
EXPECT_FALSE(gu_trigger.client_dropped_hints());
EXPECT_FALSE(gu_trigger.server_dropped_hints());
ASSERT_EQ(0, gu_trigger.notification_hint_size());
}
}
TEST_F(NudgeTrackerTest, DropHintsAtServer_WithOtherInvalidations) {
ObjectIdInvalidationMap invalidation_map;
invalidation_map.Insert(BuildUnknownVersionInvalidation(BOOKMARKS));
invalidation_map.Insert(BuildInvalidation(BOOKMARKS, 10, "hint"));
nudge_tracker_.RecordRemoteInvalidation(invalidation_map);
{
sync_pb::GetUpdateTriggers gu_trigger;
nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger);
EXPECT_TRUE(gu_trigger.server_dropped_hints());
EXPECT_FALSE(gu_trigger.client_dropped_hints());
ASSERT_EQ(1, gu_trigger.notification_hint_size());
EXPECT_EQ("hint", gu_trigger.notification_hint(0));
}
nudge_tracker_.RecordSuccessfulSyncCycle();
{
sync_pb::GetUpdateTriggers gu_trigger;
nudge_tracker_.FillProtoMessage(BOOKMARKS, &gu_trigger);
EXPECT_FALSE(gu_trigger.client_dropped_hints());
EXPECT_FALSE(gu_trigger.server_dropped_hints());
ASSERT_EQ(0, gu_trigger.notification_hint_size());
}
}
TEST_F(NudgeTrackerTest, EnableDisableInvalidations) {
nudge_tracker_.OnInvalidationsDisabled();
EXPECT_TRUE(InvalidationsOutOfSync());
EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
nudge_tracker_.OnInvalidationsEnabled();
EXPECT_TRUE(InvalidationsOutOfSync());
EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
EXPECT_FALSE(InvalidationsOutOfSync());
EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
nudge_tracker_.OnInvalidationsDisabled();
EXPECT_TRUE(InvalidationsOutOfSync());
EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
EXPECT_TRUE(InvalidationsOutOfSync());
EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
EXPECT_TRUE(InvalidationsOutOfSync());
EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
}
TEST_F(NudgeTrackerTest, WriteLocallyModifiedTypesToProto) {
EXPECT_EQ(0, ProtoLocallyModifiedCount(PREFERENCES));
nudge_tracker_.RecordLocalChange(ModelTypeSet(PREFERENCES));
EXPECT_EQ(1, ProtoLocallyModifiedCount(PREFERENCES));
nudge_tracker_.RecordSuccessfulSyncCycle();
EXPECT_EQ(0, ProtoLocallyModifiedCount(PREFERENCES));
}
TEST_F(NudgeTrackerTest, WriteRefreshRequestedTypesToProto) {
EXPECT_EQ(0, ProtoRefreshRequestedCount(SESSIONS));
nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(SESSIONS));
EXPECT_EQ(1, ProtoRefreshRequestedCount(SESSIONS));
nudge_tracker_.RecordSuccessfulSyncCycle();
EXPECT_EQ(0, ProtoRefreshRequestedCount(SESSIONS));
}
TEST_F(NudgeTrackerTest, IsSyncRequired) {
EXPECT_FALSE(nudge_tracker_.IsSyncRequired());
nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS));
EXPECT_TRUE(nudge_tracker_.IsSyncRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
EXPECT_FALSE(nudge_tracker_.IsSyncRequired());
nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(SESSIONS));
EXPECT_TRUE(nudge_tracker_.IsSyncRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
EXPECT_FALSE(nudge_tracker_.IsSyncRequired());
ObjectIdInvalidationMap invalidation_map =
BuildInvalidationMap(PREFERENCES, 1, "hint");
nudge_tracker_.RecordRemoteInvalidation(invalidation_map);
EXPECT_TRUE(nudge_tracker_.IsSyncRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
EXPECT_FALSE(nudge_tracker_.IsSyncRequired());
}
TEST_F(NudgeTrackerTest, IsGetUpdatesRequired) {
EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS));
EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(SESSIONS));
EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
ObjectIdInvalidationMap invalidation_map =
BuildInvalidationMap(PREFERENCES, 1, "hint");
nudge_tracker_.RecordRemoteInvalidation(invalidation_map);
EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
}
TEST_F(NudgeTrackerTest, IsSyncRequired_Throttling) {
const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234);
const base::TimeDelta throttle_length = base::TimeDelta::FromMinutes(10);
const base::TimeTicks t1 = t0 + throttle_length;
EXPECT_FALSE(nudge_tracker_.IsSyncRequired());
nudge_tracker_.RecordLocalChange(ModelTypeSet(SESSIONS));
EXPECT_TRUE(nudge_tracker_.IsSyncRequired());
nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS),
throttle_length,
t0);
EXPECT_FALSE(nudge_tracker_.IsSyncRequired());
nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(BOOKMARKS));
EXPECT_TRUE(nudge_tracker_.IsSyncRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
EXPECT_FALSE(nudge_tracker_.IsSyncRequired());
nudge_tracker_.UpdateTypeThrottlingState(t1);
EXPECT_FALSE(nudge_tracker_.IsTypeThrottled(SESSIONS));
EXPECT_TRUE(nudge_tracker_.IsSyncRequired());
}
TEST_F(NudgeTrackerTest, IsGetUpdatesRequired_Throttling) {
const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234);
const base::TimeDelta throttle_length = base::TimeDelta::FromMinutes(10);
const base::TimeTicks t1 = t0 + throttle_length;
EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(SESSIONS));
EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS),
throttle_length,
t0);
EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
nudge_tracker_.RecordLocalRefreshRequest(ModelTypeSet(BOOKMARKS));
EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
nudge_tracker_.UpdateTypeThrottlingState(t1);
EXPECT_FALSE(nudge_tracker_.IsTypeThrottled(SESSIONS));
EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
}
TEST_F(NudgeTrackerTest, NoTypesThrottled) {
EXPECT_FALSE(nudge_tracker_.IsAnyTypeThrottled());
EXPECT_FALSE(nudge_tracker_.IsTypeThrottled(SESSIONS));
EXPECT_TRUE(nudge_tracker_.GetThrottledTypes().Empty());
}
TEST_F(NudgeTrackerTest, ThrottleAndUnthrottle) {
const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234);
const base::TimeDelta throttle_length = base::TimeDelta::FromMinutes(10);
const base::TimeTicks t1 = t0 + throttle_length;
nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS, PREFERENCES),
throttle_length,
t0);
EXPECT_TRUE(nudge_tracker_.IsAnyTypeThrottled());
EXPECT_TRUE(nudge_tracker_.IsTypeThrottled(SESSIONS));
EXPECT_TRUE(nudge_tracker_.IsTypeThrottled(PREFERENCES));
EXPECT_FALSE(nudge_tracker_.GetThrottledTypes().Empty());
EXPECT_EQ(throttle_length, nudge_tracker_.GetTimeUntilNextUnthrottle(t0));
nudge_tracker_.UpdateTypeThrottlingState(t1);
EXPECT_FALSE(nudge_tracker_.IsAnyTypeThrottled());
EXPECT_FALSE(nudge_tracker_.IsTypeThrottled(SESSIONS));
EXPECT_TRUE(nudge_tracker_.GetThrottledTypes().Empty());
}
TEST_F(NudgeTrackerTest, OverlappingThrottleIntervals) {
const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(1234);
const base::TimeDelta throttle1_length = base::TimeDelta::FromMinutes(10);
const base::TimeDelta throttle2_length = base::TimeDelta::FromMinutes(20);
const base::TimeTicks t1 = t0 + throttle1_length;
const base::TimeTicks t2 = t0 + throttle2_length;
nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS, PREFERENCES),
throttle2_length,
t0);
EXPECT_TRUE(ModelTypeSetEquals(
ModelTypeSet(SESSIONS, PREFERENCES),
nudge_tracker_.GetThrottledTypes()));
EXPECT_EQ(throttle2_length,
nudge_tracker_.GetTimeUntilNextUnthrottle(t0));
nudge_tracker_.SetTypesThrottledUntil(ModelTypeSet(SESSIONS, BOOKMARKS),
throttle1_length,
t0);
EXPECT_TRUE(ModelTypeSetEquals(
ModelTypeSet(SESSIONS, PREFERENCES, BOOKMARKS),
nudge_tracker_.GetThrottledTypes()));
EXPECT_EQ(throttle1_length,
nudge_tracker_.GetTimeUntilNextUnthrottle(t0));
nudge_tracker_.UpdateTypeThrottlingState(t1);
EXPECT_TRUE(ModelTypeSetEquals(
ModelTypeSet(SESSIONS, PREFERENCES),
nudge_tracker_.GetThrottledTypes()));
EXPECT_EQ(throttle2_length - throttle1_length,
nudge_tracker_.GetTimeUntilNextUnthrottle(t1));
nudge_tracker_.UpdateTypeThrottlingState(t2);
EXPECT_TRUE(nudge_tracker_.GetThrottledTypes().Empty());
}
TEST_F(NudgeTrackerTest, Retry) {
const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(12345);
const base::TimeTicks t3 = t0 + base::TimeDelta::FromSeconds(3);
const base::TimeTicks t4 = t0 + base::TimeDelta::FromSeconds(4);
nudge_tracker_.SetNextRetryTime(t3);
nudge_tracker_.SetSyncCycleStartTime(t0);
EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
EXPECT_FALSE(nudge_tracker_.IsGetUpdatesRequired());
nudge_tracker_.SetSyncCycleStartTime(t4);
EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
EXPECT_TRUE(nudge_tracker_.IsGetUpdatesRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
nudge_tracker_.SetSyncCycleStartTime(t4);
EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
}
TEST_F(NudgeTrackerTest, IsRetryRequired_MidCycleUpdate1) {
const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(12345);
const base::TimeTicks t1 = t0 + base::TimeDelta::FromSeconds(1);
const base::TimeTicks t2 = t0 + base::TimeDelta::FromSeconds(2);
const base::TimeTicks t5 = t0 + base::TimeDelta::FromSeconds(5);
const base::TimeTicks t6 = t0 + base::TimeDelta::FromSeconds(6);
nudge_tracker_.SetNextRetryTime(t0);
nudge_tracker_.SetSyncCycleStartTime(t1);
EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
nudge_tracker_.SetNextRetryTime(t5);
EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
nudge_tracker_.SetSyncCycleStartTime(t2);
EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
nudge_tracker_.SetSyncCycleStartTime(t6);
EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
}
TEST_F(NudgeTrackerTest, IsRetryRequired_MidCycleUpdate2) {
const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(12345);
const base::TimeTicks t1 = t0 + base::TimeDelta::FromSeconds(1);
const base::TimeTicks t3 = t0 + base::TimeDelta::FromSeconds(3);
const base::TimeTicks t5 = t0 + base::TimeDelta::FromSeconds(5);
const base::TimeTicks t6 = t0 + base::TimeDelta::FromSeconds(6);
nudge_tracker_.RecordLocalChange(ModelTypeSet(BOOKMARKS));
nudge_tracker_.SetNextRetryTime(t1);
nudge_tracker_.SetSyncCycleStartTime(t0);
EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
nudge_tracker_.SetNextRetryTime(t5);
EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
nudge_tracker_.SetSyncCycleStartTime(t3);
EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
nudge_tracker_.SetSyncCycleStartTime(t6);
EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
}
TEST_F(NudgeTrackerTest, IsRetryRequired_FailedCycle) {
const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(12345);
const base::TimeTicks t1 = t0 + base::TimeDelta::FromSeconds(1);
const base::TimeTicks t2 = t0 + base::TimeDelta::FromSeconds(2);
nudge_tracker_.SetNextRetryTime(t0);
nudge_tracker_.SetSyncCycleStartTime(t1);
EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
nudge_tracker_.SetSyncCycleStartTime(t2);
EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
}
TEST_F(NudgeTrackerTest, IsRetryRequired_FailedCycleIncludesUpdate) {
const base::TimeTicks t0 = base::TimeTicks::FromInternalValue(12345);
const base::TimeTicks t1 = t0 + base::TimeDelta::FromSeconds(1);
const base::TimeTicks t3 = t0 + base::TimeDelta::FromSeconds(3);
const base::TimeTicks t4 = t0 + base::TimeDelta::FromSeconds(4);
const base::TimeTicks t5 = t0 + base::TimeDelta::FromSeconds(5);
const base::TimeTicks t6 = t0 + base::TimeDelta::FromSeconds(6);
nudge_tracker_.SetNextRetryTime(t0);
nudge_tracker_.SetSyncCycleStartTime(t1);
EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
nudge_tracker_.SetNextRetryTime(t5);
EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
nudge_tracker_.SetSyncCycleStartTime(t3);
EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
nudge_tracker_.SetSyncCycleStartTime(t4);
EXPECT_FALSE(nudge_tracker_.IsRetryRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
nudge_tracker_.SetSyncCycleStartTime(t6);
EXPECT_TRUE(nudge_tracker_.IsRetryRequired());
nudge_tracker_.RecordSuccessfulSyncCycle();
}
class NudgeTrackerAckTrackingTest : public NudgeTrackerTest {
public:
NudgeTrackerAckTrackingTest() {}
bool IsInvalidationUnacknowledged(const syncer::Invalidation& invalidation) {
base::RunLoop().RunUntilIdle();
return mock_ack_handler_.IsUnacked(invalidation);
}
bool IsInvalidationAcknowledged(const syncer::Invalidation& invalidation) {
base::RunLoop().RunUntilIdle();
return mock_ack_handler_.IsAcknowledged(invalidation);
}
bool IsInvalidationDropped(const syncer::Invalidation& invalidation) {
base::RunLoop().RunUntilIdle();
return mock_ack_handler_.IsDropped(invalidation);
}
bool AllInvalidationsAccountedFor() {
return mock_ack_handler_.AllInvalidationsAccountedFor();
}
Invalidation SendInvalidation(
ModelType type,
int64 version,
const std::string& hint) {
syncer::Invalidation invalidation = BuildInvalidation(type, version, hint);
mock_ack_handler_.RegisterInvalidation(&invalidation);
ObjectIdInvalidationMap invalidation_map;
invalidation_map.Insert(invalidation);
nudge_tracker_.RecordRemoteInvalidation(invalidation_map);
return invalidation;
}
Invalidation SendUnknownVersionInvalidation(ModelType type) {
syncer::Invalidation invalidation = BuildUnknownVersionInvalidation(type);
mock_ack_handler_.RegisterInvalidation(&invalidation);
ObjectIdInvalidationMap invalidation_map;
invalidation_map.Insert(invalidation);
nudge_tracker_.RecordRemoteInvalidation(invalidation_map);
return invalidation;
}
void RecordSuccessfulSyncCycle() {
nudge_tracker_.RecordSuccessfulSyncCycle();
}
private:
syncer::MockAckHandler mock_ack_handler_;
base::MessageLoop loop_;
};
TEST_F(NudgeTrackerAckTrackingTest, SimpleAcknowledgement) {
Invalidation inv = SendInvalidation(BOOKMARKS, 10, "hint");
EXPECT_TRUE(IsInvalidationUnacknowledged(inv));
RecordSuccessfulSyncCycle();
EXPECT_TRUE(IsInvalidationAcknowledged(inv));
EXPECT_TRUE(AllInvalidationsAccountedFor());
}
TEST_F(NudgeTrackerAckTrackingTest, ManyAcknowledgements) {
Invalidation inv1 = SendInvalidation(BOOKMARKS, 10, "hint");
Invalidation inv2 = SendInvalidation(BOOKMARKS, 14, "hint2");
Invalidation inv3 = SendInvalidation(PREFERENCES, 8, "hint3");
EXPECT_TRUE(IsInvalidationUnacknowledged(inv1));
EXPECT_TRUE(IsInvalidationUnacknowledged(inv2));
EXPECT_TRUE(IsInvalidationUnacknowledged(inv3));
RecordSuccessfulSyncCycle();
EXPECT_TRUE(IsInvalidationAcknowledged(inv1));
EXPECT_TRUE(IsInvalidationAcknowledged(inv2));
EXPECT_TRUE(IsInvalidationAcknowledged(inv3));
EXPECT_TRUE(AllInvalidationsAccountedFor());
}
TEST_F(NudgeTrackerAckTrackingTest, OverflowAndRecover) {
std::vector<Invalidation> invalidations;
Invalidation inv10 = SendInvalidation(BOOKMARKS, 10, "hint");
for (size_t i = 1; i < GetHintBufferSize(); ++i) {
invalidations.push_back(SendInvalidation(BOOKMARKS, i+10, "hint"));
}
for (std::vector<Invalidation>::iterator it = invalidations.begin();
it != invalidations.end(); ++it) {
EXPECT_TRUE(IsInvalidationUnacknowledged(*it));
}
Invalidation inv5 = SendInvalidation(BOOKMARKS, 5, "old_hint");
EXPECT_TRUE(IsInvalidationDropped(inv5));
Invalidation inv100 = SendInvalidation(BOOKMARKS, 100, "new_hint");
EXPECT_TRUE(IsInvalidationDropped(inv10));
RecordSuccessfulSyncCycle();
for (std::vector<Invalidation>::iterator it = invalidations.begin();
it != invalidations.end(); ++it) {
EXPECT_TRUE(IsInvalidationAcknowledged(*it));
}
EXPECT_TRUE(IsInvalidationAcknowledged(inv100));
EXPECT_TRUE(AllInvalidationsAccountedFor());
}
TEST_F(NudgeTrackerAckTrackingTest, UnknownVersionFromServer_Simple) {
Invalidation inv = SendUnknownVersionInvalidation(BOOKMARKS);
EXPECT_TRUE(IsInvalidationUnacknowledged(inv));
RecordSuccessfulSyncCycle();
EXPECT_TRUE(IsInvalidationAcknowledged(inv));
EXPECT_TRUE(AllInvalidationsAccountedFor());
}
TEST_F(NudgeTrackerAckTrackingTest, UnknownVersionFromServer_Complex) {
Invalidation inv1 = SendUnknownVersionInvalidation(BOOKMARKS);
Invalidation inv2 = SendInvalidation(BOOKMARKS, 10, "hint");
Invalidation inv3 = SendUnknownVersionInvalidation(BOOKMARKS);
Invalidation inv4 = SendUnknownVersionInvalidation(BOOKMARKS);
Invalidation inv5 = SendInvalidation(BOOKMARKS, 20, "hint2");
EXPECT_TRUE(IsInvalidationAcknowledged(inv1));
EXPECT_TRUE(IsInvalidationAcknowledged(inv3));
EXPECT_TRUE(IsInvalidationUnacknowledged(inv2));
EXPECT_TRUE(IsInvalidationUnacknowledged(inv4));
EXPECT_TRUE(IsInvalidationUnacknowledged(inv5));
RecordSuccessfulSyncCycle();
EXPECT_TRUE(IsInvalidationAcknowledged(inv1));
EXPECT_TRUE(IsInvalidationAcknowledged(inv2));
EXPECT_TRUE(IsInvalidationAcknowledged(inv3));
EXPECT_TRUE(IsInvalidationAcknowledged(inv4));
EXPECT_TRUE(IsInvalidationAcknowledged(inv5));
EXPECT_TRUE(AllInvalidationsAccountedFor());
}
}
}