Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -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/commands/cloud_command_proxy.h" |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 6 | |
| 7 | #include <memory> |
| 8 | #include <queue> |
| 9 | |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 10 | #include <gmock/gmock.h> |
| 11 | #include <gtest/gtest.h> |
Vitaly Buka | 727f3e6 | 2015-09-25 17:33:43 -0700 | [diff] [blame] | 12 | #include <weave/provider/test/fake_task_runner.h> |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 13 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 14 | #include "src/commands/command_dictionary.h" |
| 15 | #include "src/commands/command_instance.h" |
| 16 | #include "src/commands/unittest_utils.h" |
| 17 | #include "src/states/mock_state_change_queue_interface.h" |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 18 | |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 19 | using testing::_; |
| 20 | using testing::DoAll; |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 21 | using testing::Invoke; |
| 22 | using testing::Return; |
| 23 | using testing::ReturnPointee; |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 24 | using testing::SaveArg; |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 25 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 26 | namespace weave { |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 27 | |
Vitaly Buka | 0f6b2ec | 2015-08-20 15:35:19 -0700 | [diff] [blame] | 28 | using test::CreateDictionaryValue; |
| 29 | using test::CreateValue; |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 30 | |
| 31 | namespace { |
| 32 | |
| 33 | const char kCmdID[] = "abcd"; |
| 34 | |
| 35 | MATCHER_P(MatchJson, str, "") { |
| 36 | return arg.Equals(CreateValue(str).get()); |
| 37 | } |
| 38 | |
| 39 | class MockCloudCommandUpdateInterface : public CloudCommandUpdateInterface { |
| 40 | public: |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 41 | MOCK_METHOD3(UpdateCommand, |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 42 | void(const std::string&, |
| 43 | const base::DictionaryValue&, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 44 | const DoneCallback&)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 47 | // Test back-off entry that uses the test clock. |
Vitaly Buka | 0f80f7c | 2015-08-13 00:57:25 -0700 | [diff] [blame] | 48 | class TestBackoffEntry : public BackoffEntry { |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 49 | public: |
| 50 | TestBackoffEntry(const Policy* const policy, base::Clock* clock) |
Vitaly Buka | 0f80f7c | 2015-08-13 00:57:25 -0700 | [diff] [blame] | 51 | : BackoffEntry{policy}, clock_{clock} { |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 52 | creation_time_ = clock->Now(); |
| 53 | } |
| 54 | |
| 55 | private: |
Vitaly Buka | 0f80f7c | 2015-08-13 00:57:25 -0700 | [diff] [blame] | 56 | // Override from BackoffEntry to use the custom test clock for |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 57 | // the backoff calculations. |
| 58 | base::TimeTicks ImplGetTimeNow() const override { |
| 59 | return base::TimeTicks::FromInternalValue(clock_->Now().ToInternalValue()); |
| 60 | } |
| 61 | |
| 62 | base::Clock* clock_; |
| 63 | base::Time creation_time_; |
| 64 | }; |
| 65 | |
| 66 | class CloudCommandProxyTest : public ::testing::Test { |
| 67 | protected: |
| 68 | void SetUp() override { |
| 69 | // Set up the test StateChangeQueue. |
| 70 | auto callback = [this]( |
| 71 | const base::Callback<void(StateChangeQueueInterface::UpdateID)>& call) { |
| 72 | return callbacks_.Add(call).release(); |
| 73 | }; |
| 74 | EXPECT_CALL(state_change_queue_, MockAddOnStateUpdatedCallback(_)) |
| 75 | .WillRepeatedly(Invoke(callback)); |
| 76 | EXPECT_CALL(state_change_queue_, GetLastStateChangeId()) |
| 77 | .WillRepeatedly(testing::ReturnPointee(¤t_state_update_id_)); |
| 78 | |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 79 | // Set up the command schema. |
| 80 | auto json = CreateDictionaryValue(R"({ |
| 81 | 'calc': { |
| 82 | 'add': { |
| 83 | 'parameters': { |
| 84 | 'value1': 'integer', |
| 85 | 'value2': 'integer' |
| 86 | }, |
| 87 | 'progress': { |
| 88 | 'status' : 'string' |
| 89 | }, |
| 90 | 'results': { |
| 91 | 'sum' : 'integer' |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | })"); |
| 96 | CHECK(json.get()); |
Vitaly Buka | 453c4dd | 2015-10-04 18:01:50 -0700 | [diff] [blame] | 97 | CHECK(command_dictionary_.LoadCommands(*json, nullptr, nullptr)) |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 98 | << "Failed to parse test command dictionary"; |
| 99 | |
| 100 | CreateCommandInstance(); |
| 101 | } |
| 102 | |
| 103 | void CreateCommandInstance() { |
| 104 | auto command_json = CreateDictionaryValue(R"({ |
| 105 | 'name': 'calc.add', |
| 106 | 'id': 'abcd', |
| 107 | 'parameters': { |
| 108 | 'value1': 10, |
| 109 | 'value2': 20 |
| 110 | } |
| 111 | })"); |
| 112 | CHECK(command_json.get()); |
| 113 | |
Vitaly Buka | 15f5909 | 2015-07-24 16:54:32 -0700 | [diff] [blame] | 114 | command_instance_ = |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 115 | CommandInstance::FromJson(command_json.get(), Command::Origin::kCloud, |
Vitaly Buka | 15f5909 | 2015-07-24 16:54:32 -0700 | [diff] [blame] | 116 | command_dictionary_, nullptr, nullptr); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 117 | CHECK(command_instance_.get()); |
| 118 | |
| 119 | // Backoff - start at 1s and double with each backoff attempt and no jitter. |
Vitaly Buka | 0f80f7c | 2015-08-13 00:57:25 -0700 | [diff] [blame] | 120 | static const BackoffEntry::Policy policy{0, 1000, 2.0, 0.0, |
| 121 | 20000, -1, false}; |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 122 | std::unique_ptr<TestBackoffEntry> backoff{ |
Vitaly Buka | 823fdda | 2015-08-13 00:33:00 -0700 | [diff] [blame] | 123 | new TestBackoffEntry{&policy, task_runner_.GetClock()}}; |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 124 | |
| 125 | // Finally construct the CloudCommandProxy we are going to test here. |
Vitaly Buka | f9630fb | 2015-08-12 21:15:40 -0700 | [diff] [blame] | 126 | std::unique_ptr<CloudCommandProxy> proxy{new CloudCommandProxy{ |
| 127 | command_instance_.get(), &cloud_updater_, &state_change_queue_, |
| 128 | std::move(backoff), &task_runner_}}; |
Vitaly Buka | 157b16a | 2015-07-31 16:20:48 -0700 | [diff] [blame] | 129 | // CloudCommandProxy::CloudCommandProxy() subscribe itself to weave::Command |
| 130 | // notifications. When weave::Command is being destroyed it sends |
| 131 | // ::OnCommandDestroyed() and CloudCommandProxy deletes itself. |
| 132 | proxy.release(); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | StateChangeQueueInterface::UpdateID current_state_update_id_{0}; |
| 136 | base::CallbackList<void(StateChangeQueueInterface::UpdateID)> callbacks_; |
| 137 | testing::StrictMock<MockCloudCommandUpdateInterface> cloud_updater_; |
| 138 | testing::StrictMock<MockStateChangeQueueInterface> state_change_queue_; |
Vitaly Buka | 727f3e6 | 2015-09-25 17:33:43 -0700 | [diff] [blame] | 139 | testing::StrictMock<provider::test::FakeTaskRunner> task_runner_; |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 140 | std::queue<base::Closure> task_queue_; |
| 141 | CommandDictionary command_dictionary_; |
| 142 | std::unique_ptr<CommandInstance> command_instance_; |
| 143 | }; |
| 144 | |
| 145 | } // anonymous namespace |
| 146 | |
| 147 | TEST_F(CloudCommandProxyTest, ImmediateUpdate) { |
| 148 | const char expected[] = "{'state':'done'}"; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 149 | EXPECT_CALL(cloud_updater_, UpdateCommand(kCmdID, MatchJson(expected), _)); |
Vitaly Buka | 2f54897 | 2015-10-08 19:34:49 -0700 | [diff] [blame] | 150 | command_instance_->Complete({}, nullptr); |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 151 | task_runner_.RunOnce(); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | TEST_F(CloudCommandProxyTest, DelayedUpdate) { |
| 155 | // Simulate that the current device state has changed. |
| 156 | current_state_update_id_ = 20; |
| 157 | // No command update is expected here. |
Vitaly Buka | 2f54897 | 2015-10-08 19:34:49 -0700 | [diff] [blame] | 158 | command_instance_->Complete({}, nullptr); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 159 | // Still no command update here... |
| 160 | callbacks_.Notify(19); |
| 161 | // Now we should get the update... |
| 162 | const char expected[] = "{'state':'done'}"; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 163 | EXPECT_CALL(cloud_updater_, UpdateCommand(kCmdID, MatchJson(expected), _)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 164 | callbacks_.Notify(20); |
| 165 | } |
| 166 | |
| 167 | TEST_F(CloudCommandProxyTest, InFlightRequest) { |
| 168 | // SetProgress causes two consecutive updates: |
| 169 | // state=inProgress |
| 170 | // progress={...} |
| 171 | // The first state update is sent immediately, the second should be delayed. |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 172 | DoneCallback callback; |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 173 | EXPECT_CALL( |
| 174 | cloud_updater_, |
| 175 | UpdateCommand( |
| 176 | kCmdID, |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 177 | MatchJson("{'state':'inProgress', 'progress':{'status':'ready'}}"), |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 178 | _)) |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 179 | .WillOnce(SaveArg<2>(&callback)); |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 180 | EXPECT_TRUE(command_instance_->SetProgress( |
| 181 | *CreateDictionaryValue("{'status': 'ready'}"), nullptr)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 182 | |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 183 | task_runner_.RunOnce(); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | TEST_F(CloudCommandProxyTest, CombineMultiple) { |
| 187 | // Simulate that the current device state has changed. |
| 188 | current_state_update_id_ = 20; |
| 189 | // SetProgress causes two consecutive updates: |
| 190 | // state=inProgress |
| 191 | // progress={...} |
| 192 | // Both updates will be held until device state is updated. |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 193 | EXPECT_TRUE(command_instance_->SetProgress( |
| 194 | *CreateDictionaryValue("{'status': 'ready'}"), nullptr)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 195 | |
| 196 | // Now simulate the device state updated. Both updates should come in one |
| 197 | // request. |
| 198 | const char expected[] = R"({ |
| 199 | 'progress': {'status':'ready'}, |
| 200 | 'state':'inProgress' |
| 201 | })"; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 202 | EXPECT_CALL(cloud_updater_, UpdateCommand(kCmdID, MatchJson(expected), _)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 203 | callbacks_.Notify(20); |
| 204 | } |
| 205 | |
| 206 | TEST_F(CloudCommandProxyTest, RetryFailed) { |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 207 | DoneCallback callback; |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 208 | |
| 209 | const char expect[] = |
| 210 | "{'state':'inProgress', 'progress': {'status': 'ready'}}"; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 211 | EXPECT_CALL(cloud_updater_, UpdateCommand(kCmdID, MatchJson(expect), _)) |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 212 | .Times(3) |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 213 | .WillRepeatedly(SaveArg<2>(&callback)); |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 214 | auto started = task_runner_.GetClock()->Now(); |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 215 | EXPECT_TRUE(command_instance_->SetProgress( |
| 216 | *CreateDictionaryValue("{'status': 'ready'}"), nullptr)); |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 217 | task_runner_.Run(); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 218 | ErrorPtr error; |
| 219 | Error::AddTo(&error, FROM_HERE, "TEST", "TEST", "TEST"); |
| 220 | callback.Run(error->Clone()); |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 221 | task_runner_.Run(); |
| 222 | EXPECT_GE(task_runner_.GetClock()->Now() - started, |
| 223 | base::TimeDelta::FromSecondsD(0.9)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 224 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 225 | callback.Run(error->Clone()); |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 226 | task_runner_.Run(); |
| 227 | EXPECT_GE(task_runner_.GetClock()->Now() - started, |
| 228 | base::TimeDelta::FromSecondsD(2.9)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 229 | |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 230 | callback.Run(nullptr); |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 231 | task_runner_.Run(); |
| 232 | EXPECT_GE(task_runner_.GetClock()->Now() - started, |
| 233 | base::TimeDelta::FromSecondsD(2.9)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | TEST_F(CloudCommandProxyTest, GateOnStateUpdates) { |
| 237 | current_state_update_id_ = 20; |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 238 | EXPECT_TRUE(command_instance_->SetProgress( |
| 239 | *CreateDictionaryValue("{'status': 'ready'}"), nullptr)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 240 | current_state_update_id_ = 21; |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 241 | EXPECT_TRUE(command_instance_->SetProgress( |
| 242 | *CreateDictionaryValue("{'status': 'busy'}"), nullptr)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 243 | current_state_update_id_ = 22; |
Vitaly Buka | 2f54897 | 2015-10-08 19:34:49 -0700 | [diff] [blame] | 244 | command_instance_->Complete({}, nullptr); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 245 | |
| 246 | // Device state #20 updated. |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 247 | DoneCallback callback; |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 248 | const char expect1[] = R"({ |
| 249 | 'progress': {'status':'ready'}, |
| 250 | 'state':'inProgress' |
| 251 | })"; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 252 | EXPECT_CALL(cloud_updater_, UpdateCommand(kCmdID, MatchJson(expect1), _)) |
| 253 | .WillOnce(SaveArg<2>(&callback)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 254 | callbacks_.Notify(20); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 255 | callback.Run(nullptr); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 256 | |
| 257 | // Device state #21 updated. |
| 258 | const char expect2[] = "{'progress': {'status':'busy'}}"; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 259 | EXPECT_CALL(cloud_updater_, UpdateCommand(kCmdID, MatchJson(expect2), _)) |
| 260 | .WillOnce(SaveArg<2>(&callback)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 261 | callbacks_.Notify(21); |
| 262 | |
| 263 | // Device state #22 updated. Nothing happens here since the previous command |
| 264 | // update request hasn't completed yet. |
| 265 | callbacks_.Notify(22); |
| 266 | |
| 267 | // Now the command update is complete, send out the patch that happened after |
| 268 | // the state #22 was updated. |
| 269 | const char expect3[] = "{'state': 'done'}"; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 270 | EXPECT_CALL(cloud_updater_, UpdateCommand(kCmdID, MatchJson(expect3), _)) |
| 271 | .WillOnce(SaveArg<2>(&callback)); |
| 272 | callback.Run(nullptr); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | TEST_F(CloudCommandProxyTest, CombineSomeStates) { |
| 276 | current_state_update_id_ = 20; |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 277 | EXPECT_TRUE(command_instance_->SetProgress( |
| 278 | *CreateDictionaryValue("{'status': 'ready'}"), nullptr)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 279 | current_state_update_id_ = 21; |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 280 | EXPECT_TRUE(command_instance_->SetProgress( |
| 281 | *CreateDictionaryValue("{'status': 'busy'}"), nullptr)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 282 | current_state_update_id_ = 22; |
Vitaly Buka | 2f54897 | 2015-10-08 19:34:49 -0700 | [diff] [blame] | 283 | command_instance_->Complete({}, nullptr); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 284 | |
| 285 | // Device state 20-21 updated. |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 286 | DoneCallback callback; |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 287 | const char expect1[] = R"({ |
| 288 | 'progress': {'status':'busy'}, |
| 289 | 'state':'inProgress' |
| 290 | })"; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 291 | EXPECT_CALL(cloud_updater_, UpdateCommand(kCmdID, MatchJson(expect1), _)) |
| 292 | .WillOnce(SaveArg<2>(&callback)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 293 | callbacks_.Notify(21); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 294 | callback.Run(nullptr); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 295 | |
| 296 | // Device state #22 updated. |
| 297 | const char expect2[] = "{'state': 'done'}"; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 298 | EXPECT_CALL(cloud_updater_, UpdateCommand(kCmdID, MatchJson(expect2), _)) |
| 299 | .WillOnce(SaveArg<2>(&callback)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 300 | callbacks_.Notify(22); |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 301 | callback.Run(nullptr); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | TEST_F(CloudCommandProxyTest, CombineAllStates) { |
| 305 | current_state_update_id_ = 20; |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 306 | EXPECT_TRUE(command_instance_->SetProgress( |
| 307 | *CreateDictionaryValue("{'status': 'ready'}"), nullptr)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 308 | current_state_update_id_ = 21; |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 309 | EXPECT_TRUE(command_instance_->SetProgress( |
| 310 | *CreateDictionaryValue("{'status': 'busy'}"), nullptr)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 311 | current_state_update_id_ = 22; |
Vitaly Buka | 2f54897 | 2015-10-08 19:34:49 -0700 | [diff] [blame] | 312 | command_instance_->Complete({}, nullptr); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 313 | |
| 314 | // Device state 30 updated. |
| 315 | const char expected[] = R"({ |
| 316 | 'progress': {'status':'busy'}, |
| 317 | 'state':'done' |
| 318 | })"; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 319 | EXPECT_CALL(cloud_updater_, UpdateCommand(kCmdID, MatchJson(expected), _)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 320 | callbacks_.Notify(30); |
| 321 | } |
| 322 | |
| 323 | TEST_F(CloudCommandProxyTest, CoalesceUpdates) { |
| 324 | current_state_update_id_ = 20; |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 325 | EXPECT_TRUE(command_instance_->SetProgress( |
| 326 | *CreateDictionaryValue("{'status': 'ready'}"), nullptr)); |
| 327 | EXPECT_TRUE(command_instance_->SetProgress( |
| 328 | *CreateDictionaryValue("{'status': 'busy'}"), nullptr)); |
| 329 | EXPECT_TRUE(command_instance_->SetProgress( |
| 330 | *CreateDictionaryValue("{'status': 'finished'}"), nullptr)); |
Vitaly Buka | 2f54897 | 2015-10-08 19:34:49 -0700 | [diff] [blame] | 331 | EXPECT_TRUE(command_instance_->Complete(*CreateDictionaryValue("{'sum': 30}"), |
| 332 | nullptr)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 333 | |
| 334 | const char expected[] = R"({ |
| 335 | 'progress': {'status':'finished'}, |
| 336 | 'results': {'sum':30}, |
| 337 | 'state':'done' |
| 338 | })"; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 339 | EXPECT_CALL(cloud_updater_, UpdateCommand(kCmdID, MatchJson(expected), _)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 340 | callbacks_.Notify(30); |
| 341 | } |
| 342 | |
| 343 | TEST_F(CloudCommandProxyTest, EmptyStateChangeQueue) { |
| 344 | // Assume the device state update queue was empty and was at update ID 20. |
| 345 | current_state_update_id_ = 20; |
| 346 | |
| 347 | // Recreate the command instance and proxy with the new state change queue. |
| 348 | CreateCommandInstance(); |
| 349 | |
| 350 | // Empty queue will immediately call back with the state change notification. |
| 351 | callbacks_.Notify(20); |
| 352 | |
| 353 | // As soon as we change the command, the update to the server should be sent. |
| 354 | const char expected[] = "{'state':'done'}"; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 355 | EXPECT_CALL(cloud_updater_, UpdateCommand(kCmdID, MatchJson(expected), _)); |
Vitaly Buka | 2f54897 | 2015-10-08 19:34:49 -0700 | [diff] [blame] | 356 | command_instance_->Complete({}, nullptr); |
Vitaly Buka | ff1d186 | 2015-10-07 20:40:36 -0700 | [diff] [blame] | 357 | task_runner_.RunOnce(); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | TEST_F(CloudCommandProxyTest, NonEmptyStateChangeQueue) { |
| 361 | // Assume the device state update queue was NOT empty when the command |
| 362 | // instance was created. |
| 363 | current_state_update_id_ = 20; |
| 364 | |
| 365 | // Recreate the command instance and proxy with the new state change queue. |
| 366 | CreateCommandInstance(); |
| 367 | |
| 368 | // No command updates right now. |
Vitaly Buka | 2f54897 | 2015-10-08 19:34:49 -0700 | [diff] [blame] | 369 | command_instance_->Complete({}, nullptr); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 370 | |
| 371 | // Only when the state #20 is published we should update the command |
| 372 | const char expected[] = "{'state':'done'}"; |
Vitaly Buka | 7476342 | 2015-10-11 00:39:52 -0700 | [diff] [blame] | 373 | EXPECT_CALL(cloud_updater_, UpdateCommand(kCmdID, MatchJson(expected), _)); |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 374 | callbacks_.Notify(20); |
| 375 | } |
| 376 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 377 | } // namespace weave |