Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 5 | #include "src/states/state_change_queue.h" |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 6 | |
Alex Deymo | f6cbe32 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 7 | #include <gtest/gtest.h> |
Alex Vakulenko | 8a05beb | 2015-11-24 17:13:20 -0800 | [diff] [blame] | 8 | #include <weave/test/unittest_utils.h> |
Alex Deymo | f6cbe32 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 9 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 10 | #include "src/bind_lambda.h" |
Anton Muhin | 0182945 | 2014-11-21 02:16:04 +0400 | [diff] [blame] | 11 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 12 | namespace weave { |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 13 | |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 14 | using test::CreateDictionaryValue; |
| 15 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 16 | class StateChangeQueueTest : public ::testing::Test { |
| 17 | public: |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 18 | void SetUp() override { queue_.reset(new StateChangeQueue(100)); } |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 19 | |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 20 | void TearDown() override { queue_.reset(); } |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 21 | |
| 22 | std::unique_ptr<StateChangeQueue> queue_; |
| 23 | }; |
| 24 | |
| 25 | TEST_F(StateChangeQueueTest, Empty) { |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 26 | EXPECT_TRUE(queue_->GetAndClearRecordedStateChanges().empty()); |
| 27 | } |
| 28 | |
| 29 | TEST_F(StateChangeQueueTest, UpdateOne) { |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 30 | auto timestamp = base::Time::Now(); |
| 31 | ASSERT_TRUE(queue_->NotifyPropertiesUpdated( |
Alex Vakulenko | 6869ed7 | 2015-12-04 13:59:23 -0800 | [diff] [blame] | 32 | timestamp, *CreateDictionaryValue("{'prop': {'name': 23}}"))); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 33 | auto changes = queue_->GetAndClearRecordedStateChanges(); |
Vitaly Buka | 52d006a | 2015-11-21 17:14:51 -0800 | [diff] [blame] | 34 | ASSERT_EQ(1u, changes.size()); |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 35 | EXPECT_EQ(timestamp, changes.front().timestamp); |
| 36 | EXPECT_JSON_EQ("{'prop':{'name': 23}}", *changes.front().changed_properties); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 37 | EXPECT_TRUE(queue_->GetAndClearRecordedStateChanges().empty()); |
| 38 | } |
| 39 | |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 40 | TEST_F(StateChangeQueueTest, UpdateMany) { |
| 41 | auto timestamp1 = base::Time::Now(); |
| 42 | const std::string state1 = "{'prop': {'name1': 23}}"; |
| 43 | auto timestamp2 = timestamp1 + base::TimeDelta::FromSeconds(1); |
| 44 | const std::string state2 = |
| 45 | "{'prop': {'name1': 17, 'name2': 1.0, 'name3': false}}"; |
Vitaly Buka | 34668e7 | 2015-12-15 14:46:47 -0800 | [diff] [blame] | 46 | ASSERT_TRUE(queue_->NotifyPropertiesUpdated(timestamp1, |
| 47 | *CreateDictionaryValue(state1))); |
| 48 | ASSERT_TRUE(queue_->NotifyPropertiesUpdated(timestamp2, |
| 49 | *CreateDictionaryValue(state2))); |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 50 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 51 | auto changes = queue_->GetAndClearRecordedStateChanges(); |
Vitaly Buka | 52d006a | 2015-11-21 17:14:51 -0800 | [diff] [blame] | 52 | ASSERT_EQ(2u, changes.size()); |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 53 | EXPECT_EQ(timestamp1, changes[0].timestamp); |
| 54 | EXPECT_JSON_EQ(state1, *changes[0].changed_properties); |
| 55 | EXPECT_EQ(timestamp2, changes[1].timestamp); |
| 56 | EXPECT_JSON_EQ(state2, *changes[1].changed_properties); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 57 | EXPECT_TRUE(queue_->GetAndClearRecordedStateChanges().empty()); |
| 58 | } |
| 59 | |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 60 | TEST_F(StateChangeQueueTest, GroupByTimestamp) { |
| 61 | base::Time timestamp = base::Time::Now(); |
| 62 | base::TimeDelta time_delta = base::TimeDelta::FromMinutes(1); |
| 63 | |
| 64 | ASSERT_TRUE(queue_->NotifyPropertiesUpdated( |
Alex Vakulenko | 6869ed7 | 2015-12-04 13:59:23 -0800 | [diff] [blame] | 65 | timestamp, *CreateDictionaryValue("{'prop': {'name1': 1}}"))); |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 66 | |
| 67 | ASSERT_TRUE(queue_->NotifyPropertiesUpdated( |
Alex Vakulenko | 6869ed7 | 2015-12-04 13:59:23 -0800 | [diff] [blame] | 68 | timestamp, *CreateDictionaryValue("{'prop': {'name2': 2}}"))); |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 69 | |
| 70 | ASSERT_TRUE(queue_->NotifyPropertiesUpdated( |
Alex Vakulenko | 6869ed7 | 2015-12-04 13:59:23 -0800 | [diff] [blame] | 71 | timestamp, *CreateDictionaryValue("{'prop': {'name1': 3}}"))); |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 72 | |
| 73 | ASSERT_TRUE(queue_->NotifyPropertiesUpdated( |
Alex Vakulenko | 6869ed7 | 2015-12-04 13:59:23 -0800 | [diff] [blame] | 74 | timestamp + time_delta, |
| 75 | *CreateDictionaryValue("{'prop': {'name1': 4}}"))); |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 76 | |
| 77 | auto changes = queue_->GetAndClearRecordedStateChanges(); |
Vitaly Buka | 52d006a | 2015-11-21 17:14:51 -0800 | [diff] [blame] | 78 | ASSERT_EQ(2u, changes.size()); |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 79 | |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 80 | const std::string expected1 = "{'prop': {'name1': 3, 'name2': 2}}"; |
| 81 | const std::string expected2 = "{'prop': {'name1': 4}}"; |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 82 | EXPECT_EQ(timestamp, changes[0].timestamp); |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 83 | EXPECT_JSON_EQ(expected1, *changes[0].changed_properties); |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 84 | EXPECT_EQ(timestamp + time_delta, changes[1].timestamp); |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 85 | EXPECT_JSON_EQ(expected2, *changes[1].changed_properties); |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 88 | TEST_F(StateChangeQueueTest, MaxQueueSize) { |
| 89 | queue_.reset(new StateChangeQueue(2)); |
| 90 | base::Time start_time = base::Time::Now(); |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 91 | base::TimeDelta time_delta1 = base::TimeDelta::FromMinutes(1); |
| 92 | base::TimeDelta time_delta2 = base::TimeDelta::FromMinutes(3); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 93 | |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 94 | ASSERT_TRUE(queue_->NotifyPropertiesUpdated( |
Alex Vakulenko | 6869ed7 | 2015-12-04 13:59:23 -0800 | [diff] [blame] | 95 | start_time, |
| 96 | *CreateDictionaryValue("{'prop': {'name1': 1, 'name2': 2}}"))); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 97 | |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 98 | ASSERT_TRUE(queue_->NotifyPropertiesUpdated( |
| 99 | start_time + time_delta1, |
Alex Vakulenko | 6869ed7 | 2015-12-04 13:59:23 -0800 | [diff] [blame] | 100 | *CreateDictionaryValue("{'prop': {'name1': 3, 'name3': 4}}"))); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 101 | |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 102 | ASSERT_TRUE(queue_->NotifyPropertiesUpdated( |
| 103 | start_time + time_delta2, |
Alex Vakulenko | 6869ed7 | 2015-12-04 13:59:23 -0800 | [diff] [blame] | 104 | *CreateDictionaryValue("{'prop': {'name10': 10, 'name11': 11}}"))); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 105 | |
| 106 | auto changes = queue_->GetAndClearRecordedStateChanges(); |
Vitaly Buka | 52d006a | 2015-11-21 17:14:51 -0800 | [diff] [blame] | 107 | ASSERT_EQ(2u, changes.size()); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 108 | |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 109 | const std::string expected1 = |
| 110 | "{'prop': {'name1': 3, 'name2': 2, 'name3': 4}}"; |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 111 | EXPECT_EQ(start_time + time_delta1, changes[0].timestamp); |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 112 | EXPECT_JSON_EQ(expected1, *changes[0].changed_properties); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 113 | |
Vitaly Buka | 34668e7 | 2015-12-15 14:46:47 -0800 | [diff] [blame] | 114 | const std::string expected2 = "{'prop': {'name10': 10, 'name11': 11}}"; |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 115 | EXPECT_EQ(start_time + time_delta2, changes[1].timestamp); |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 116 | EXPECT_JSON_EQ(expected2, *changes[1].changed_properties); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 119 | } // namespace weave |