| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <memory> | 5 #include <memory> |
| 6 | 6 |
| 7 #include "tools/battor_agent/battor_agent.h" | 7 #include "tools/battor_agent/battor_agent.h" |
| 8 | 8 |
| 9 #include "base/test/test_simple_task_runner.h" | 9 #include "base/test/test_simple_task_runner.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 SET_GAIN_SENT, | 157 SET_GAIN_SENT, |
| 158 GAIN_ACKED, | 158 GAIN_ACKED, |
| 159 START_TRACING_SENT, | 159 START_TRACING_SENT, |
| 160 START_TRACING_COMPLETE, | 160 START_TRACING_COMPLETE, |
| 161 | 161 |
| 162 // States required to StopTracing. | 162 // States required to StopTracing. |
| 163 EEPROM_REQUEST_SENT, | 163 EEPROM_REQUEST_SENT, |
| 164 EEPROM_RECEIVED, | 164 EEPROM_RECEIVED, |
| 165 SAMPLES_REQUEST_SENT, | 165 SAMPLES_REQUEST_SENT, |
| 166 CALIBRATION_FRAME_RECEIVED, | 166 CALIBRATION_FRAME_RECEIVED, |
| 167 SAMPLES_END_FRAME_RECEIVED, |
| 167 | 168 |
| 168 // States required to RecordClockSyncMarker. | 169 // States required to RecordClockSyncMarker. |
| 169 CURRENT_SAMPLE_REQUEST_SENT, | 170 CURRENT_SAMPLE_REQUEST_SENT, |
| 170 RECORD_CLOCK_SYNC_MARKER_COMPLETE, | 171 RECORD_CLOCK_SYNC_MARKER_COMPLETE, |
| 171 | 172 |
| 172 // States required to GetFirmwareGitHash. | 173 // States required to GetFirmwareGitHash. |
| 173 GIT_FIRMWARE_HASH_REQUEST_SENT, | 174 GIT_FIRMWARE_HASH_REQUEST_SENT, |
| 174 READ_GIT_HASH_RECEIVED, | 175 READ_GIT_HASH_RECEIVED, |
| 175 }; | 176 }; |
| 176 | 177 |
| 177 // Runs BattOrAgent::StartTracing until it reaches the specified state by | 178 // Runs BattOrAgent::StartTracing until it reaches the specified state by |
| 178 // feeding it the callbacks it needs to progress. | 179 // feeding it the callbacks it needs to progress. |
| 179 void RunStartTracingTo(BattOrAgentState end_state) { | 180 void RunStartTracingTo(BattOrAgentState end_state) { |
| 180 is_command_complete_ = false; | |
| 181 | |
| 182 GetAgent()->StartTracing(); | |
| 183 GetTaskRunner()->RunUntilIdle(); | 181 GetTaskRunner()->RunUntilIdle(); |
| 184 | 182 |
| 185 GetAgent()->OnConnectionOpened(true); | 183 GetAgent()->OnConnectionOpened(true); |
| 186 GetTaskRunner()->RunUntilIdle(); | 184 GetTaskRunner()->RunUntilIdle(); |
| 187 | 185 |
| 188 if (end_state == BattOrAgentState::CONNECTED) | 186 if (end_state == BattOrAgentState::CONNECTED) |
| 189 return; | 187 return; |
| 190 | 188 |
| 191 OnBytesSent(true); | 189 OnBytesSent(true); |
| 192 if (end_state == BattOrAgentState::INIT_SENT) | 190 if (end_state == BattOrAgentState::INIT_SENT) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 214 // state machine. | 212 // state machine. |
| 215 DCHECK(end_state == BattOrAgentState::START_TRACING_COMPLETE); | 213 DCHECK(end_state == BattOrAgentState::START_TRACING_COMPLETE); |
| 216 | 214 |
| 217 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 215 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
| 218 ToCharVector(kStartTracingAck)); | 216 ToCharVector(kStartTracingAck)); |
| 219 } | 217 } |
| 220 | 218 |
| 221 // Runs BattOrAgent::StopTracing until it reaches the specified state by | 219 // Runs BattOrAgent::StopTracing until it reaches the specified state by |
| 222 // feeding it the callbacks it needs to progress. | 220 // feeding it the callbacks it needs to progress. |
| 223 void RunStopTracingTo(BattOrAgentState end_state) { | 221 void RunStopTracingTo(BattOrAgentState end_state) { |
| 224 is_command_complete_ = false; | |
| 225 | |
| 226 GetAgent()->StopTracing(); | |
| 227 GetTaskRunner()->RunUntilIdle(); | 222 GetTaskRunner()->RunUntilIdle(); |
| 228 | 223 |
| 229 GetAgent()->OnConnectionOpened(true); | 224 GetAgent()->OnConnectionOpened(true); |
| 230 GetTaskRunner()->RunUntilIdle(); | 225 GetTaskRunner()->RunUntilIdle(); |
| 231 | 226 |
| 232 if (end_state == BattOrAgentState::CONNECTED) | 227 if (end_state == BattOrAgentState::CONNECTED) |
| 233 return; | 228 return; |
| 234 | 229 |
| 235 OnBytesSent(true); | 230 OnBytesSent(true); |
| 236 if (end_state == BattOrAgentState::EEPROM_REQUEST_SENT) | 231 if (end_state == BattOrAgentState::EEPROM_REQUEST_SENT) |
| 237 return; | 232 return; |
| 238 | 233 |
| 239 BattOrEEPROM eeprom; | 234 BattOrEEPROM eeprom; |
| 240 eeprom.r1 = 1; | 235 eeprom.r1 = 1; |
| 241 eeprom.r2 = 1; | 236 eeprom.r2 = 1; |
| 242 eeprom.r3 = 1; | 237 eeprom.r3 = 1; |
| 243 eeprom.low_gain = 1; | 238 eeprom.low_gain = 1; |
| 244 eeprom.low_gain_correction_offset = 0; | 239 eeprom.low_gain_correction_offset = 0; |
| 245 eeprom.low_gain_correction_factor = 1; | 240 eeprom.low_gain_correction_factor = 1; |
| 246 eeprom.sd_sample_rate = 1000; | 241 eeprom.sd_sample_rate = 1000; |
| 247 | 242 |
| 248 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, ToCharVector(eeprom)); | 243 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, ToCharVector(eeprom)); |
| 249 if (end_state == BattOrAgentState::EEPROM_RECEIVED) | 244 if (end_state == BattOrAgentState::EEPROM_RECEIVED) |
| 250 return; | 245 return; |
| 251 | 246 |
| 252 OnBytesSent(true); | 247 OnBytesSent(true); |
| 253 if (end_state == BattOrAgentState::SAMPLES_REQUEST_SENT) | 248 if (end_state == BattOrAgentState::SAMPLES_REQUEST_SENT) |
| 254 return; | 249 return; |
| 255 | 250 |
| 256 DCHECK(end_state == BattOrAgentState::CALIBRATION_FRAME_RECEIVED); | |
| 257 | |
| 258 BattOrFrameHeader cal_frame_header{0, sizeof(RawBattOrSample)}; | 251 BattOrFrameHeader cal_frame_header{0, sizeof(RawBattOrSample)}; |
| 259 RawBattOrSample cal_frame[] = {RawBattOrSample{1, 1}}; | 252 RawBattOrSample cal_frame[] = {RawBattOrSample{1, 1}}; |
| 260 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 253 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 261 CreateFrame(cal_frame_header, cal_frame, 1)); | 254 CreateFrame(cal_frame_header, cal_frame, 1)); |
| 255 |
| 256 if (end_state == BattOrAgentState::CALIBRATION_FRAME_RECEIVED) |
| 257 return; |
| 258 |
| 259 DCHECK(end_state == BattOrAgentState::SAMPLES_END_FRAME_RECEIVED); |
| 260 |
| 261 BattOrFrameHeader frame_header{1, 0}; |
| 262 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 263 CreateFrame(frame_header, nullptr, 0)); |
| 262 } | 264 } |
| 263 | 265 |
| 264 // Runs BattOrAgent::RecordClockSyncMarker until it reaches the specified | 266 // Runs BattOrAgent::RecordClockSyncMarker until it reaches the specified |
| 265 // state by feeding it the callbacks it needs to progress. | 267 // state by feeding it the callbacks it needs to progress. |
| 266 void RunRecordClockSyncMarkerTo(BattOrAgentState end_state) { | 268 void RunRecordClockSyncMarkerTo(BattOrAgentState end_state) { |
| 267 is_command_complete_ = false; | |
| 268 | |
| 269 GetAgent()->RecordClockSyncMarker(kClockSyncId); | |
| 270 GetTaskRunner()->RunUntilIdle(); | 269 GetTaskRunner()->RunUntilIdle(); |
| 271 | 270 |
| 272 GetAgent()->OnConnectionOpened(true); | 271 GetAgent()->OnConnectionOpened(true); |
| 273 GetTaskRunner()->RunUntilIdle(); | 272 GetTaskRunner()->RunUntilIdle(); |
| 274 | 273 |
| 275 if (end_state == BattOrAgentState::CONNECTED) | 274 if (end_state == BattOrAgentState::CONNECTED) |
| 276 return; | 275 return; |
| 277 | 276 |
| 278 OnBytesSent(true); | 277 OnBytesSent(true); |
| 279 if (end_state == BattOrAgentState::CURRENT_SAMPLE_REQUEST_SENT) | 278 if (end_state == BattOrAgentState::CURRENT_SAMPLE_REQUEST_SENT) |
| 280 return; | 279 return; |
| 281 | 280 |
| 282 DCHECK(end_state == BattOrAgentState::RECORD_CLOCK_SYNC_MARKER_COMPLETE); | 281 DCHECK(end_state == BattOrAgentState::RECORD_CLOCK_SYNC_MARKER_COMPLETE); |
| 283 | 282 |
| 284 uint32_t current_sample = 1; | 283 uint32_t current_sample = 1; |
| 285 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 284 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
| 286 ToCharVector(current_sample)); | 285 ToCharVector(current_sample)); |
| 287 } | 286 } |
| 288 | 287 |
| 289 // Runs BattOrAgent::GetFirmwareGitHash until it reaches the specified | 288 // Runs BattOrAgent::GetFirmwareGitHash until it reaches the specified |
| 290 // state by feeding it the callbacks it needs to progress. | 289 // state by feeding it the callbacks it needs to progress. |
| 291 void RunGetFirmwareGitHashTo(BattOrAgentState end_state) { | 290 void RunGetFirmwareGitHashTo(BattOrAgentState end_state) { |
| 292 is_command_complete_ = false; | |
| 293 | |
| 294 GetAgent()->GetFirmwareGitHash(); | |
| 295 GetTaskRunner()->RunUntilIdle(); | 291 GetTaskRunner()->RunUntilIdle(); |
| 296 | 292 |
| 297 GetAgent()->OnConnectionOpened(true); | 293 GetAgent()->OnConnectionOpened(true); |
| 298 GetTaskRunner()->RunUntilIdle(); | 294 GetTaskRunner()->RunUntilIdle(); |
| 299 | 295 |
| 300 if (end_state == BattOrAgentState::CONNECTED) | 296 if (end_state == BattOrAgentState::CONNECTED) |
| 301 return; | 297 return; |
| 302 | 298 |
| 303 OnBytesSent(true); | 299 OnBytesSent(true); |
| 304 if (end_state == BattOrAgentState::INIT_SENT) | 300 if (end_state == BattOrAgentState::INIT_SENT) |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 bool is_command_complete_; | 337 bool is_command_complete_; |
| 342 BattOrError command_error_; | 338 BattOrError command_error_; |
| 343 std::string trace_; | 339 std::string trace_; |
| 344 std::string firmware_git_hash_; | 340 std::string firmware_git_hash_; |
| 345 }; | 341 }; |
| 346 | 342 |
| 347 TEST_F(BattOrAgentTest, StartTracing) { | 343 TEST_F(BattOrAgentTest, StartTracing) { |
| 348 testing::InSequence s; | 344 testing::InSequence s; |
| 349 EXPECT_CALL(*GetAgent()->GetConnection(), Open()); | 345 EXPECT_CALL(*GetAgent()->GetConnection(), Open()); |
| 350 | 346 |
| 351 EXPECT_CALL(*GetAgent()->GetConnection(), Flush()); | |
| 352 BattOrControlMessage init_msg{BATTOR_CONTROL_MESSAGE_TYPE_INIT, 0, 0}; | 347 BattOrControlMessage init_msg{BATTOR_CONTROL_MESSAGE_TYPE_INIT, 0, 0}; |
| 353 EXPECT_CALL( | 348 EXPECT_CALL( |
| 354 *GetAgent()->GetConnection(), | 349 *GetAgent()->GetConnection(), |
| 355 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, | 350 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, |
| 356 BufferEq(&init_msg, sizeof(init_msg)), sizeof(init_msg))); | 351 BufferEq(&init_msg, sizeof(init_msg)), sizeof(init_msg))); |
| 357 | 352 |
| 358 EXPECT_CALL(*GetAgent()->GetConnection(), | 353 EXPECT_CALL(*GetAgent()->GetConnection(), |
| 359 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK)); | 354 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK)); |
| 360 | 355 |
| 361 BattOrControlMessage set_gain_msg{BATTOR_CONTROL_MESSAGE_TYPE_SET_GAIN, | 356 BattOrControlMessage set_gain_msg{BATTOR_CONTROL_MESSAGE_TYPE_SET_GAIN, |
| 362 BATTOR_GAIN_LOW, 0}; | 357 BATTOR_GAIN_LOW, 0}; |
| 363 EXPECT_CALL(*GetAgent()->GetConnection(), | 358 EXPECT_CALL(*GetAgent()->GetConnection(), |
| 364 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, | 359 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, |
| 365 BufferEq(&set_gain_msg, sizeof(set_gain_msg)), | 360 BufferEq(&set_gain_msg, sizeof(set_gain_msg)), |
| 366 sizeof(set_gain_msg))); | 361 sizeof(set_gain_msg))); |
| 367 | 362 |
| 368 EXPECT_CALL(*GetAgent()->GetConnection(), | 363 EXPECT_CALL(*GetAgent()->GetConnection(), |
| 369 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK)); | 364 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK)); |
| 370 | 365 |
| 371 BattOrControlMessage start_tracing_msg{ | 366 BattOrControlMessage start_tracing_msg{ |
| 372 BATTOR_CONTROL_MESSAGE_TYPE_START_SAMPLING_SD, 0, 0}; | 367 BATTOR_CONTROL_MESSAGE_TYPE_START_SAMPLING_SD, 0, 0}; |
| 373 EXPECT_CALL(*GetAgent()->GetConnection(), | 368 EXPECT_CALL(*GetAgent()->GetConnection(), |
| 374 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, | 369 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, |
| 375 BufferEq(&start_tracing_msg, sizeof(start_tracing_msg)), | 370 BufferEq(&start_tracing_msg, sizeof(start_tracing_msg)), |
| 376 sizeof(start_tracing_msg))); | 371 sizeof(start_tracing_msg))); |
| 377 | 372 |
| 378 EXPECT_CALL(*GetAgent()->GetConnection(), | 373 EXPECT_CALL(*GetAgent()->GetConnection(), |
| 379 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK)); | 374 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK)); |
| 380 | 375 |
| 376 GetAgent()->StartTracing(); |
| 381 RunStartTracingTo(BattOrAgentState::START_TRACING_COMPLETE); | 377 RunStartTracingTo(BattOrAgentState::START_TRACING_COMPLETE); |
| 382 EXPECT_TRUE(IsCommandComplete()); | 378 EXPECT_TRUE(IsCommandComplete()); |
| 383 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 379 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 384 } | 380 } |
| 385 | 381 |
| 386 TEST_F(BattOrAgentTest, StartTracingFailsWithoutConnection) { | 382 TEST_F(BattOrAgentTest, StartTracingFailsWithoutConnection) { |
| 387 GetAgent()->StartTracing(); | 383 GetAgent()->StartTracing(); |
| 388 GetTaskRunner()->RunUntilIdle(); | 384 GetTaskRunner()->RunUntilIdle(); |
| 389 | 385 |
| 390 GetAgent()->OnConnectionOpened(false); | 386 GetAgent()->OnConnectionOpened(false); |
| 391 GetTaskRunner()->RunUntilIdle(); | 387 GetTaskRunner()->RunUntilIdle(); |
| 392 | 388 |
| 393 EXPECT_TRUE(IsCommandComplete()); | 389 EXPECT_TRUE(IsCommandComplete()); |
| 394 EXPECT_EQ(BATTOR_ERROR_CONNECTION_FAILED, GetCommandError()); | 390 EXPECT_EQ(BATTOR_ERROR_CONNECTION_FAILED, GetCommandError()); |
| 395 } | 391 } |
| 396 | 392 |
| 397 TEST_F(BattOrAgentTest, StartTracingFailsIfInitSendFails) { | 393 TEST_F(BattOrAgentTest, StartTracingFailsIfInitSendFails) { |
| 394 GetAgent()->StartTracing(); |
| 398 RunStartTracingTo(BattOrAgentState::CONNECTED); | 395 RunStartTracingTo(BattOrAgentState::CONNECTED); |
| 399 OnBytesSent(false); | 396 OnBytesSent(false); |
| 400 | 397 |
| 401 EXPECT_TRUE(IsCommandComplete()); | 398 EXPECT_TRUE(IsCommandComplete()); |
| 402 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); | 399 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); |
| 403 } | 400 } |
| 404 | 401 |
| 405 TEST_F(BattOrAgentTest, StartTracingFailsIfInitAckReadFails) { | 402 TEST_F(BattOrAgentTest, StartTracingSucceedsAfterInitAckReadFails) { |
| 403 GetAgent()->StartTracing(); |
| 404 |
| 406 RunStartTracingTo(BattOrAgentState::INIT_SENT); | 405 RunStartTracingTo(BattOrAgentState::INIT_SENT); |
| 406 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); |
| 407 | 407 |
| 408 for (int i = 0; i < 21; i++) { | 408 EXPECT_FALSE(IsCommandComplete()); |
| 409 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); | |
| 410 | 409 |
| 411 // Bytes will be sent because INIT will be retried. | 410 RunStartTracingTo(BattOrAgentState::START_TRACING_COMPLETE); |
| 412 OnBytesSent(true); | |
| 413 } | |
| 414 | 411 |
| 415 EXPECT_TRUE(IsCommandComplete()); | 412 EXPECT_TRUE(IsCommandComplete()); |
| 416 EXPECT_EQ(BATTOR_ERROR_TOO_MANY_INIT_RETRIES, GetCommandError()); | 413 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 417 } | 414 } |
| 418 | 415 |
| 419 TEST_F(BattOrAgentTest, StartTracingFailsIfInitWrongAckRead) { | 416 TEST_F(BattOrAgentTest, StartTracingSucceedsAfterInitWrongAckRead) { |
| 417 GetAgent()->StartTracing(); |
| 418 |
| 420 RunStartTracingTo(BattOrAgentState::INIT_SENT); | 419 RunStartTracingTo(BattOrAgentState::INIT_SENT); |
| 420 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
| 421 ToCharVector(kStartTracingAck)); |
| 421 | 422 |
| 422 for (int i = 0; i < 21; i++) { | 423 EXPECT_FALSE(IsCommandComplete()); |
| 423 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
| 424 ToCharVector(kStartTracingAck)); | |
| 425 | 424 |
| 426 // Bytes will be sent because INIT will be retried. | 425 RunStartTracingTo(BattOrAgentState::START_TRACING_COMPLETE); |
| 427 OnBytesSent(true); | |
| 428 } | |
| 429 | 426 |
| 430 EXPECT_TRUE(IsCommandComplete()); | 427 EXPECT_TRUE(IsCommandComplete()); |
| 431 EXPECT_EQ(BATTOR_ERROR_TOO_MANY_INIT_RETRIES, GetCommandError()); | 428 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 432 } | 429 } |
| 433 | 430 |
| 434 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainSendFails) { | 431 TEST_F(BattOrAgentTest, StartTracingFailsAfterSetGainSendFails) { |
| 432 GetAgent()->StartTracing(); |
| 433 |
| 435 RunStartTracingTo(BattOrAgentState::INIT_SENT); | 434 RunStartTracingTo(BattOrAgentState::INIT_SENT); |
| 436 OnBytesSent(false); | 435 OnBytesSent(false); |
| 437 | 436 |
| 438 EXPECT_TRUE(IsCommandComplete()); | 437 EXPECT_TRUE(IsCommandComplete()); |
| 439 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); | 438 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); |
| 440 } | 439 } |
| 441 | 440 |
| 442 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainAckReadFails) { | 441 TEST_F(BattOrAgentTest, StartTracingSucceedsAfterSetGainAckReadFails) { |
| 442 GetAgent()->StartTracing(); |
| 443 |
| 443 RunStartTracingTo(BattOrAgentState::SET_GAIN_SENT); | 444 RunStartTracingTo(BattOrAgentState::SET_GAIN_SENT); |
| 444 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); | 445 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); |
| 445 | 446 |
| 447 EXPECT_FALSE(IsCommandComplete()); |
| 448 |
| 449 RunStartTracingTo(BattOrAgentState::START_TRACING_COMPLETE); |
| 450 |
| 446 EXPECT_TRUE(IsCommandComplete()); | 451 EXPECT_TRUE(IsCommandComplete()); |
| 447 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetCommandError()); | 452 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 448 } | 453 } |
| 449 | 454 |
| 450 TEST_F(BattOrAgentTest, StartTracingFailsIfSetGainWrongAckRead) { | 455 TEST_F(BattOrAgentTest, StartTracingSucceedsAfterSetGainWrongAckRead) { |
| 456 GetAgent()->StartTracing(); |
| 457 |
| 451 RunStartTracingTo(BattOrAgentState::SET_GAIN_SENT); | 458 RunStartTracingTo(BattOrAgentState::SET_GAIN_SENT); |
| 452 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 459 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
| 453 ToCharVector(kStartTracingAck)); | 460 ToCharVector(kStartTracingAck)); |
| 454 | 461 |
| 462 EXPECT_FALSE(IsCommandComplete()); |
| 463 |
| 464 RunStartTracingTo(BattOrAgentState::START_TRACING_COMPLETE); |
| 465 |
| 455 EXPECT_TRUE(IsCommandComplete()); | 466 EXPECT_TRUE(IsCommandComplete()); |
| 456 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 467 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 457 } | 468 } |
| 458 | 469 |
| 459 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingSendFails) { | 470 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingSendFails) { |
| 471 GetAgent()->StartTracing(); |
| 460 RunStartTracingTo(BattOrAgentState::INIT_SENT); | 472 RunStartTracingTo(BattOrAgentState::INIT_SENT); |
| 461 OnBytesSent(false); | 473 OnBytesSent(false); |
| 462 | 474 |
| 463 EXPECT_TRUE(IsCommandComplete()); | 475 EXPECT_TRUE(IsCommandComplete()); |
| 464 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); | 476 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); |
| 465 } | 477 } |
| 466 | 478 |
| 467 TEST_F(BattOrAgentTest, StartTracingSucceedsAfterRetriesIfWrongAckRead) { | 479 TEST_F(BattOrAgentTest, StartTracingSucceedsAfterWrongAckRead) { |
| 468 RunStartTracingTo(BattOrAgentState::CONNECTED); | 480 GetAgent()->StartTracing(); |
| 469 | 481 |
| 470 for (int i = 0; i < 4; i++) { | 482 // Go through the correct init sequence, but give the wrong ack to |
| 471 // Go through the correct init sequence, but give the wrong ack to | 483 // START_TRACING. |
| 472 // START_TRACING. | 484 RunStartTracingTo(BattOrAgentState::START_TRACING_SENT); |
| 473 OnBytesSent(true); | 485 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, ToCharVector(kInitAck)); |
| 474 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
| 475 ToCharVector(kInitAck)); | |
| 476 OnBytesSent(true); | |
| 477 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
| 478 ToCharVector(kSetGainAck)); | |
| 479 OnBytesSent(true); | |
| 480 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
| 481 ToCharVector(kInitAck)); | |
| 482 } | |
| 483 | 486 |
| 484 // On the last attempt, give the correct ack to START_TRACING. | 487 EXPECT_FALSE(IsCommandComplete()); |
| 485 OnBytesSent(true); | 488 |
| 486 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, ToCharVector(kInitAck)); | 489 RunStartTracingTo(BattOrAgentState::START_TRACING_COMPLETE); |
| 487 OnBytesSent(true); | |
| 488 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
| 489 ToCharVector(kSetGainAck)); | |
| 490 OnBytesSent(true); | |
| 491 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
| 492 ToCharVector(kStartTracingAck)); | |
| 493 | 490 |
| 494 EXPECT_TRUE(IsCommandComplete()); | 491 EXPECT_TRUE(IsCommandComplete()); |
| 495 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 492 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 496 } | 493 } |
| 497 | 494 |
| 498 TEST_F(BattOrAgentTest, StartTracingSucceedsAfterRetriesWithReadFailure) { | 495 TEST_F(BattOrAgentTest, StartTracingSucceedsAfterReadFails) { |
| 499 RunStartTracingTo(BattOrAgentState::CONNECTED); | 496 GetAgent()->StartTracing(); |
| 500 | 497 |
| 501 for (int i = 0; i < 4; i++) { | 498 // Go through the correct init sequence, but indicate that we failed to read |
| 502 // Go through the correct init sequence, but indicate that we failed to read | 499 // the START_TRACING ack. |
| 503 // the START_TRACING ack. | 500 RunStartTracingTo(BattOrAgentState::START_TRACING_SENT); |
| 504 OnBytesSent(true); | 501 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); |
| 505 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 502 |
| 506 ToCharVector(kInitAck)); | 503 EXPECT_FALSE(IsCommandComplete()); |
| 507 OnBytesSent(true); | |
| 508 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
| 509 ToCharVector(kSetGainAck)); | |
| 510 OnBytesSent(true); | |
| 511 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); | |
| 512 } | |
| 513 | 504 |
| 514 // On the last attempt, give the correct ack to START_TRACING. | 505 // On the last attempt, give the correct ack to START_TRACING. |
| 515 OnBytesSent(true); | 506 RunStartTracingTo(BattOrAgentState::START_TRACING_SENT); |
| 516 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, ToCharVector(kInitAck)); | |
| 517 OnBytesSent(true); | |
| 518 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
| 519 ToCharVector(kSetGainAck)); | |
| 520 OnBytesSent(true); | |
| 521 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 507 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
| 522 ToCharVector(kStartTracingAck)); | 508 ToCharVector(kStartTracingAck)); |
| 523 | 509 |
| 524 EXPECT_TRUE(IsCommandComplete()); | 510 EXPECT_TRUE(IsCommandComplete()); |
| 525 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 511 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 526 } | 512 } |
| 527 | 513 |
| 528 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingWrongAckReadTooMuch) { | 514 TEST_F(BattOrAgentTest, StartTracingSucceedsAfterSamplesReadDuringInit) { |
| 529 RunStartTracingTo(BattOrAgentState::CONNECTED); | 515 GetAgent()->StartTracing(); |
| 530 | 516 |
| 531 for (int i = 0; i < 5; i++) { | |
| 532 // Go through the correct init sequence, but give the wrong ack to | |
| 533 // START_TRACING. | |
| 534 OnBytesSent(true); | |
| 535 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
| 536 ToCharVector(kInitAck)); | |
| 537 OnBytesSent(true); | |
| 538 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
| 539 ToCharVector(kSetGainAck)); | |
| 540 OnBytesSent(true); | |
| 541 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
| 542 ToCharVector(kInitAck)); | |
| 543 } | |
| 544 | |
| 545 EXPECT_TRUE(IsCommandComplete()); | |
| 546 EXPECT_EQ(BATTOR_ERROR_TOO_MANY_START_TRACING_RETRIES, GetCommandError()); | |
| 547 } | |
| 548 | |
| 549 TEST_F(BattOrAgentTest, StartTracingSucceedsWithOneInitFailure) { | |
| 550 RunStartTracingTo(BattOrAgentState::INIT_SENT); | 517 RunStartTracingTo(BattOrAgentState::INIT_SENT); |
| 551 | 518 |
| 552 // Send some samples instead of an INIT ACK. This will force an INIT retry. | 519 // Send some samples instead of an INIT ACK. This will force a command retry. |
| 553 BattOrFrameHeader frame_header{1, 3 * sizeof(RawBattOrSample)}; | 520 BattOrFrameHeader frame_header{1, 3 * sizeof(RawBattOrSample)}; |
| 554 RawBattOrSample frame[] = { | 521 RawBattOrSample frame[] = { |
| 555 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, RawBattOrSample{3, 3}, | 522 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, RawBattOrSample{3, 3}, |
| 556 }; | 523 }; |
| 557 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 524 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 558 CreateFrame(frame_header, frame, 3)); | 525 CreateFrame(frame_header, frame, 3)); |
| 559 | 526 |
| 560 OnBytesSent(true); | 527 EXPECT_FALSE(IsCommandComplete()); |
| 561 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, ToCharVector(kInitAck)); | |
| 562 | 528 |
| 563 OnBytesSent(true); | 529 RunStartTracingTo(BattOrAgentState::START_TRACING_COMPLETE); |
| 564 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
| 565 ToCharVector(kSetGainAck)); | |
| 566 | |
| 567 OnBytesSent(true); | |
| 568 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
| 569 ToCharVector(kStartTracingAck)); | |
| 570 | 530 |
| 571 EXPECT_TRUE(IsCommandComplete()); | 531 EXPECT_TRUE(IsCommandComplete()); |
| 572 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 532 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 573 } | 533 } |
| 574 | 534 |
| 535 TEST_F(BattOrAgentTest, StartTracingFailsAfterTooManyCumulativeFailures) { |
| 536 GetAgent()->StartTracing(); |
| 537 |
| 538 for (int i = 0; i < 9; i++) { |
| 539 RunStartTracingTo(BattOrAgentState::SET_GAIN_SENT); |
| 540 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); |
| 541 |
| 542 EXPECT_FALSE(IsCommandComplete()); |
| 543 } |
| 544 |
| 545 RunStartTracingTo(BattOrAgentState::SET_GAIN_SENT); |
| 546 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); |
| 547 |
| 548 EXPECT_TRUE(IsCommandComplete()); |
| 549 EXPECT_EQ(BATTOR_ERROR_TOO_MANY_COMMAND_RETRIES, GetCommandError()); |
| 550 } |
| 551 |
| 575 TEST_F(BattOrAgentTest, StopTracing) { | 552 TEST_F(BattOrAgentTest, StopTracing) { |
| 576 testing::InSequence s; | 553 testing::InSequence s; |
| 577 EXPECT_CALL(*GetAgent()->GetConnection(), Open()); | 554 EXPECT_CALL(*GetAgent()->GetConnection(), Open()); |
| 578 | 555 |
| 579 BattOrControlMessage request_eeprom_msg{ | 556 BattOrControlMessage request_eeprom_msg{ |
| 580 BATTOR_CONTROL_MESSAGE_TYPE_READ_EEPROM, sizeof(BattOrEEPROM), 0}; | 557 BATTOR_CONTROL_MESSAGE_TYPE_READ_EEPROM, sizeof(BattOrEEPROM), 0}; |
| 581 EXPECT_CALL( | 558 EXPECT_CALL( |
| 582 *GetAgent()->GetConnection(), | 559 *GetAgent()->GetConnection(), |
| 583 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, | 560 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, |
| 584 BufferEq(&request_eeprom_msg, sizeof(request_eeprom_msg)), | 561 BufferEq(&request_eeprom_msg, sizeof(request_eeprom_msg)), |
| 585 sizeof(request_eeprom_msg))); | 562 sizeof(request_eeprom_msg))); |
| 586 | 563 |
| 587 EXPECT_CALL(*GetAgent()->GetConnection(), | 564 EXPECT_CALL(*GetAgent()->GetConnection(), |
| 588 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK)); | 565 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK)); |
| 589 | 566 |
| 590 BattOrControlMessage request_samples_msg{ | 567 BattOrControlMessage request_samples_msg{ |
| 591 BATTOR_CONTROL_MESSAGE_TYPE_READ_SD_UART, 0, 0}; | 568 BATTOR_CONTROL_MESSAGE_TYPE_READ_SD_UART, 0, 0}; |
| 592 EXPECT_CALL( | 569 EXPECT_CALL( |
| 593 *GetAgent()->GetConnection(), | 570 *GetAgent()->GetConnection(), |
| 594 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, | 571 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, |
| 595 BufferEq(&request_samples_msg, sizeof(request_samples_msg)), | 572 BufferEq(&request_samples_msg, sizeof(request_samples_msg)), |
| 596 sizeof(request_samples_msg))); | 573 sizeof(request_samples_msg))); |
| 597 | 574 |
| 598 // We send the agent four frames: a calibration frame, and two real frames, | 575 // We send the agent four frames: a calibration frame, and two real frames, |
| 599 // and one zero-length frame to indicate that we're done. | 576 // and one zero-length frame to indicate that we're done. |
| 600 EXPECT_CALL(*GetAgent()->GetConnection(), | 577 EXPECT_CALL(*GetAgent()->GetConnection(), |
| 601 ReadMessage(BATTOR_MESSAGE_TYPE_SAMPLES)) | 578 ReadMessage(BATTOR_MESSAGE_TYPE_SAMPLES)) |
| 602 .Times(4); | 579 .Times(4); |
| 603 | 580 |
| 581 GetAgent()->StopTracing(); |
| 604 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); | 582 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); |
| 605 | 583 |
| 606 // Send the calibration frame. | 584 // Send the calibration frame. |
| 607 BattOrFrameHeader cal_frame_header{0, 2 * sizeof(RawBattOrSample)}; | 585 BattOrFrameHeader cal_frame_header{0, 2 * sizeof(RawBattOrSample)}; |
| 608 RawBattOrSample cal_frame[] = { | 586 RawBattOrSample cal_frame[] = { |
| 609 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, | 587 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, |
| 610 }; | 588 }; |
| 611 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 589 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 612 CreateFrame(cal_frame_header, cal_frame, 2)); | 590 CreateFrame(cal_frame_header, cal_frame, 2)); |
| 613 | 591 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 644 GetTaskRunner()->RunUntilIdle(); | 622 GetTaskRunner()->RunUntilIdle(); |
| 645 | 623 |
| 646 GetAgent()->OnConnectionOpened(false); | 624 GetAgent()->OnConnectionOpened(false); |
| 647 GetTaskRunner()->RunUntilIdle(); | 625 GetTaskRunner()->RunUntilIdle(); |
| 648 | 626 |
| 649 EXPECT_TRUE(IsCommandComplete()); | 627 EXPECT_TRUE(IsCommandComplete()); |
| 650 EXPECT_EQ(BATTOR_ERROR_CONNECTION_FAILED, GetCommandError()); | 628 EXPECT_EQ(BATTOR_ERROR_CONNECTION_FAILED, GetCommandError()); |
| 651 } | 629 } |
| 652 | 630 |
| 653 TEST_F(BattOrAgentTest, StopTracingFailsIfEEPROMRequestSendFails) { | 631 TEST_F(BattOrAgentTest, StopTracingFailsIfEEPROMRequestSendFails) { |
| 632 GetAgent()->StopTracing(); |
| 654 RunStopTracingTo(BattOrAgentState::CONNECTED); | 633 RunStopTracingTo(BattOrAgentState::CONNECTED); |
| 655 OnBytesSent(false); | 634 OnBytesSent(false); |
| 656 | 635 |
| 657 EXPECT_TRUE(IsCommandComplete()); | 636 EXPECT_TRUE(IsCommandComplete()); |
| 658 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); | 637 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); |
| 659 } | 638 } |
| 660 | 639 |
| 661 TEST_F(BattOrAgentTest, StopTracingRetriesEEPROMRead) { | 640 TEST_F(BattOrAgentTest, StopTracingSucceedsAfterEEPROMReadFails) { |
| 641 GetAgent()->StopTracing(); |
| 642 |
| 662 RunStopTracingTo(BattOrAgentState::EEPROM_REQUEST_SENT); | 643 RunStopTracingTo(BattOrAgentState::EEPROM_REQUEST_SENT); |
| 663 | |
| 664 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); | 644 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); |
| 665 | 645 |
| 666 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 646 EXPECT_FALSE(IsCommandComplete()); |
| 667 ToCharVector(BattOrEEPROM())); | |
| 668 | 647 |
| 669 // Give confirmation that the samples request was sent. | 648 RunStopTracingTo(BattOrAgentState::SAMPLES_END_FRAME_RECEIVED); |
| 670 OnBytesSent(true); | |
| 671 | |
| 672 BattOrFrameHeader cal_frame_header{0, sizeof(RawBattOrSample)}; | |
| 673 RawBattOrSample cal_frame[] = {RawBattOrSample{1, 1}}; | |
| 674 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | |
| 675 CreateFrame(cal_frame_header, cal_frame, 1)); | |
| 676 | |
| 677 BattOrFrameHeader frame_header{1, 0}; | |
| 678 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | |
| 679 CreateFrame(frame_header, nullptr, 0)); | |
| 680 | 649 |
| 681 EXPECT_TRUE(IsCommandComplete()); | 650 EXPECT_TRUE(IsCommandComplete()); |
| 682 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 651 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 683 } | 652 } |
| 684 | 653 |
| 685 TEST_F(BattOrAgentTest, StopTracingFailsIfEEPROMReadFails) { | 654 TEST_F(BattOrAgentTest, StopTracingSucceedsAfterEEPROMWrongAckRead) { |
| 686 RunStopTracingTo(BattOrAgentState::EEPROM_REQUEST_SENT); | 655 GetAgent()->StopTracing(); |
| 687 | 656 |
| 688 for (int i = 0; i < 21; i++) { | |
| 689 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); | |
| 690 } | |
| 691 | |
| 692 EXPECT_TRUE(IsCommandComplete()); | |
| 693 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetCommandError()); | |
| 694 } | |
| 695 | |
| 696 TEST_F(BattOrAgentTest, StopTracingFailsIfEEPROMWrongAckRead) { | |
| 697 RunStopTracingTo(BattOrAgentState::EEPROM_REQUEST_SENT); | 657 RunStopTracingTo(BattOrAgentState::EEPROM_REQUEST_SENT); |
| 698 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, ToCharVector(kInitAck)); | 658 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, ToCharVector(kInitAck)); |
| 699 | 659 |
| 660 EXPECT_FALSE(IsCommandComplete()); |
| 661 |
| 662 RunStopTracingTo(BattOrAgentState::SAMPLES_END_FRAME_RECEIVED); |
| 663 |
| 700 EXPECT_TRUE(IsCommandComplete()); | 664 EXPECT_TRUE(IsCommandComplete()); |
| 701 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 665 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 702 } | 666 } |
| 703 | 667 |
| 704 TEST_F(BattOrAgentTest, StopTracingFailsIfRequestSamplesFails) { | 668 TEST_F(BattOrAgentTest, StopTracingFailsIfSendamplesRequestFails) { |
| 669 GetAgent()->StopTracing(); |
| 670 |
| 705 RunStopTracingTo(BattOrAgentState::EEPROM_RECEIVED); | 671 RunStopTracingTo(BattOrAgentState::EEPROM_RECEIVED); |
| 706 OnBytesSent(false); | 672 OnBytesSent(false); |
| 707 | 673 |
| 708 EXPECT_TRUE(IsCommandComplete()); | 674 EXPECT_TRUE(IsCommandComplete()); |
| 709 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); | 675 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); |
| 710 } | 676 } |
| 711 | 677 |
| 712 TEST_F(BattOrAgentTest, StopTracingSucceedsWithOneCalibrationFrameReadFailure) { | 678 TEST_F(BattOrAgentTest, StopTracingSucceedsAfterCalibrationFrameReadFailure) { |
| 679 GetAgent()->StopTracing(); |
| 680 |
| 713 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); | 681 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); |
| 682 // Make a read fail in order to make sure that the agent will retry. |
| 683 OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); |
| 684 |
| 685 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); |
| 686 |
| 687 EXPECT_FALSE(IsCommandComplete()); |
| 688 |
| 689 RunStopTracingTo(BattOrAgentState::SAMPLES_END_FRAME_RECEIVED); |
| 690 |
| 691 EXPECT_TRUE(IsCommandComplete()); |
| 692 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 693 } |
| 694 |
| 695 TEST_F(BattOrAgentTest, StopTracingSucceedsAfterDataFrameReadFailure) { |
| 696 GetAgent()->StopTracing(); |
| 697 |
| 698 RunStopTracingTo(BattOrAgentState::CALIBRATION_FRAME_RECEIVED); |
| 714 | 699 |
| 715 // Make a read fail in order to make sure that the agent will retry. | 700 // Make a read fail in order to make sure that the agent will retry. |
| 716 OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); | 701 OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); |
| 717 | 702 |
| 718 BattOrFrameHeader cal_frame_header{0, sizeof(RawBattOrSample)}; | 703 EXPECT_FALSE(IsCommandComplete()); |
| 719 RawBattOrSample cal_frame[] = {RawBattOrSample{1, 1}}; | |
| 720 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | |
| 721 CreateFrame(cal_frame_header, cal_frame, 1)); | |
| 722 | 704 |
| 723 BattOrFrameHeader frame_header{1, 0}; | 705 RunStopTracingTo(BattOrAgentState::SAMPLES_END_FRAME_RECEIVED); |
| 724 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | |
| 725 CreateFrame(frame_header, nullptr, 0)); | |
| 726 | 706 |
| 727 EXPECT_TRUE(IsCommandComplete()); | 707 EXPECT_TRUE(IsCommandComplete()); |
| 728 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 708 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 729 } | 709 } |
| 730 | 710 |
| 731 TEST_F(BattOrAgentTest, StopTracingFailsWithManyCalibrationFrameReadFailures) { | 711 TEST_F(BattOrAgentTest, StopTracingFailsWithManyCalibrationFrameReadFailures) { |
| 732 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); | 712 GetAgent()->StopTracing(); |
| 733 | 713 |
| 734 // We attempt the read a max of 20 times: send that many failures. | 714 for (int i = 0; i < 9; i++) { |
| 735 for (int i = 0; i < 21; i++) { | 715 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); |
| 736 OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); | 716 OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); |
| 717 |
| 718 EXPECT_FALSE(IsCommandComplete()); |
| 737 } | 719 } |
| 738 | 720 |
| 721 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); |
| 722 OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); |
| 723 |
| 739 EXPECT_TRUE(IsCommandComplete()); | 724 EXPECT_TRUE(IsCommandComplete()); |
| 740 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetCommandError()); | 725 EXPECT_EQ(BATTOR_ERROR_TOO_MANY_COMMAND_RETRIES, GetCommandError()); |
| 741 } | 726 } |
| 742 | 727 |
| 743 TEST_F(BattOrAgentTest, StopTracingSucceedsWithOneDataFrameReadFailure) { | 728 TEST_F(BattOrAgentTest, StopTracingFailsWithManyDataFrameReadFailures) { |
| 729 GetAgent()->StopTracing(); |
| 730 |
| 731 for (int i = 0; i < 9; i++) { |
| 732 RunStopTracingTo(BattOrAgentState::CALIBRATION_FRAME_RECEIVED); |
| 733 OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); |
| 734 |
| 735 EXPECT_FALSE(IsCommandComplete()); |
| 736 } |
| 737 |
| 744 RunStopTracingTo(BattOrAgentState::CALIBRATION_FRAME_RECEIVED); | 738 RunStopTracingTo(BattOrAgentState::CALIBRATION_FRAME_RECEIVED); |
| 745 | |
| 746 // Make a read fail in order to make sure that the agent will retry. | |
| 747 OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); | 739 OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); |
| 748 | 740 |
| 749 BattOrFrameHeader frame_header{1, 0}; | 741 EXPECT_TRUE(IsCommandComplete()); |
| 742 EXPECT_EQ(BATTOR_ERROR_TOO_MANY_COMMAND_RETRIES, GetCommandError()); |
| 743 } |
| 744 |
| 745 TEST_F(BattOrAgentTest, StopTracingSucceedsWithFewDataFrameReadFailures) { |
| 746 BattOrFrameHeader frame_header{1, 1 * sizeof(RawBattOrSample)}; |
| 747 RawBattOrSample frame[] = {RawBattOrSample{1, 1}}; |
| 748 |
| 749 GetAgent()->StopTracing(); |
| 750 |
| 751 RunStopTracingTo(BattOrAgentState::CALIBRATION_FRAME_RECEIVED); |
| 752 OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); |
| 753 |
| 754 EXPECT_FALSE(IsCommandComplete()); |
| 755 |
| 756 RunStopTracingTo(BattOrAgentState::CALIBRATION_FRAME_RECEIVED); |
| 750 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 757 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 751 CreateFrame(frame_header, nullptr, 0)); | 758 CreateFrame(frame_header, frame, 1)); |
| 759 OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); |
| 760 |
| 761 EXPECT_FALSE(IsCommandComplete()); |
| 762 |
| 763 RunStopTracingTo(BattOrAgentState::SAMPLES_END_FRAME_RECEIVED); |
| 752 | 764 |
| 753 EXPECT_TRUE(IsCommandComplete()); | 765 EXPECT_TRUE(IsCommandComplete()); |
| 754 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 766 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 755 } | 767 } |
| 756 | 768 |
| 757 TEST_F(BattOrAgentTest, StopTracingFailsWithManyDataFrameReadFailures) { | 769 TEST_F(BattOrAgentTest, StopTracingSucceedsAfterSamplesReadHasWrongType) { |
| 758 RunStopTracingTo(BattOrAgentState::CALIBRATION_FRAME_RECEIVED); | 770 GetAgent()->StopTracing(); |
| 759 | 771 |
| 760 // We attempt the read a max of 20 times: send that many failures. | 772 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); |
| 761 for (int i = 0; i < 21; i++) { | 773 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, ToCharVector(kInitAck)); |
| 762 OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); | |
| 763 } | |
| 764 | 774 |
| 765 EXPECT_TRUE(IsCommandComplete()); | 775 EXPECT_FALSE(IsCommandComplete()); |
| 766 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetCommandError()); | |
| 767 } | |
| 768 | 776 |
| 769 TEST_F(BattOrAgentTest, StopTracingRetriesResetEachFrame) { | 777 RunStopTracingTo(BattOrAgentState::SAMPLES_END_FRAME_RECEIVED); |
| 770 RunStopTracingTo(BattOrAgentState::CALIBRATION_FRAME_RECEIVED); | |
| 771 | |
| 772 // Send 11 failures on two different reads: because the retry count should | |
| 773 // reset after a successful read, this should still be okay. | |
| 774 for (int i = 0; i < 11; i++) { | |
| 775 OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); | |
| 776 } | |
| 777 | |
| 778 BattOrFrameHeader frame_header1{1, 1 * sizeof(RawBattOrSample)}; | |
| 779 RawBattOrSample frame1[] = {RawBattOrSample{1, 1}}; | |
| 780 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | |
| 781 CreateFrame(frame_header1, frame1, 1)); | |
| 782 | |
| 783 for (int i = 0; i < 11; i++) { | |
| 784 OnMessageRead(false, BATTOR_MESSAGE_TYPE_SAMPLES, nullptr); | |
| 785 } | |
| 786 | |
| 787 BattOrFrameHeader frame_header2{2, 0}; | |
| 788 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | |
| 789 CreateFrame(frame_header2, nullptr, 0)); | |
| 790 | 778 |
| 791 EXPECT_TRUE(IsCommandComplete()); | 779 EXPECT_TRUE(IsCommandComplete()); |
| 792 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 780 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 793 } | 781 } |
| 794 | 782 |
| 795 TEST_F(BattOrAgentTest, StopTracingFailsIfSamplesReadHasWrongType) { | 783 TEST_F(BattOrAgentTest, StopTracingSucceedsAfterCalibrationFrameWrongLength) { |
| 796 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); | 784 GetAgent()->StopTracing(); |
| 797 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, ToCharVector(kInitAck)); | |
| 798 | |
| 799 EXPECT_TRUE(IsCommandComplete()); | |
| 800 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | |
| 801 } | |
| 802 | |
| 803 TEST_F(BattOrAgentTest, StopTracingFailsIfCalibrationFrameHasWrongLength) { | |
| 804 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); | |
| 805 | 785 |
| 806 // Send a calibration frame with a mismatch between the frame length in the | 786 // Send a calibration frame with a mismatch between the frame length in the |
| 807 // header and the actual frame length. | 787 // header and the actual frame length. |
| 808 BattOrFrameHeader cal_frame_header{0, 1 * sizeof(RawBattOrSample)}; | 788 BattOrFrameHeader cal_frame_header{0, 1 * sizeof(RawBattOrSample)}; |
| 809 RawBattOrSample cal_frame[] = { | 789 RawBattOrSample cal_frame[] = { |
| 810 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, | 790 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, |
| 811 }; | 791 }; |
| 792 |
| 793 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); |
| 812 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 794 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 813 CreateFrame(cal_frame_header, cal_frame, 2)); | 795 CreateFrame(cal_frame_header, cal_frame, 2)); |
| 814 | 796 |
| 797 EXPECT_FALSE(IsCommandComplete()); |
| 798 |
| 799 RunStopTracingTo(BattOrAgentState::SAMPLES_END_FRAME_RECEIVED); |
| 800 |
| 815 EXPECT_TRUE(IsCommandComplete()); | 801 EXPECT_TRUE(IsCommandComplete()); |
| 816 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 802 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 817 } | 803 } |
| 818 | 804 |
| 819 TEST_F(BattOrAgentTest, StopTracingFailsIfDataFrameHasWrongLength) { | 805 TEST_F(BattOrAgentTest, StopTracingSucceedsAfterDataFrameHasWrongLength) { |
| 820 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); | 806 GetAgent()->StopTracing(); |
| 821 | |
| 822 BattOrFrameHeader cal_frame_header{0, 1 * sizeof(RawBattOrSample)}; | |
| 823 RawBattOrSample cal_frame[] = { | |
| 824 RawBattOrSample{1, 1}, | |
| 825 }; | |
| 826 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | |
| 827 CreateFrame(cal_frame_header, cal_frame, 1)); | |
| 828 | 807 |
| 829 // Send a data frame with a mismatch between the frame length in the | 808 // Send a data frame with a mismatch between the frame length in the |
| 830 // header and the actual frame length. | 809 // header and the actual frame length. |
| 831 BattOrFrameHeader frame_header{1, 2 * sizeof(RawBattOrSample)}; | 810 BattOrFrameHeader frame_header{1, 2 * sizeof(RawBattOrSample)}; |
| 832 RawBattOrSample frame[] = {RawBattOrSample{1, 1}}; | 811 RawBattOrSample frame[] = {RawBattOrSample{1, 1}}; |
| 812 |
| 813 RunStopTracingTo(BattOrAgentState::CALIBRATION_FRAME_RECEIVED); |
| 833 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 814 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 834 CreateFrame(frame_header, frame, 1)); | 815 CreateFrame(frame_header, frame, 1)); |
| 835 | 816 |
| 817 EXPECT_FALSE(IsCommandComplete()); |
| 818 |
| 819 RunStopTracingTo(BattOrAgentState::SAMPLES_END_FRAME_RECEIVED); |
| 820 |
| 836 EXPECT_TRUE(IsCommandComplete()); | 821 EXPECT_TRUE(IsCommandComplete()); |
| 837 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 822 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 838 } | 823 } |
| 839 | 824 |
| 840 TEST_F(BattOrAgentTest, StopTracingFailsIfCalibrationFrameMissingByte) { | 825 TEST_F(BattOrAgentTest, StopTracingSucceedsAfterCalibrationFrameMissingByte) { |
| 841 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); | 826 GetAgent()->StopTracing(); |
| 842 | 827 |
| 843 BattOrFrameHeader cal_frame_header{0, 2 * sizeof(RawBattOrSample)}; | 828 BattOrFrameHeader cal_frame_header{0, 2 * sizeof(RawBattOrSample)}; |
| 844 RawBattOrSample cal_frame[] = { | 829 RawBattOrSample cal_frame[] = { |
| 845 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, | 830 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, |
| 846 }; | 831 }; |
| 847 | 832 |
| 848 // Remove the last byte from the frame to make it invalid. | 833 // Remove the last byte from the frame to make it invalid. |
| 849 std::unique_ptr<vector<char>> cal_frame_bytes = | 834 std::unique_ptr<vector<char>> cal_frame_bytes = |
| 850 CreateFrame(cal_frame_header, cal_frame, 2); | 835 CreateFrame(cal_frame_header, cal_frame, 2); |
| 851 cal_frame_bytes->pop_back(); | 836 cal_frame_bytes->pop_back(); |
| 852 | 837 |
| 838 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); |
| 853 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, std::move(cal_frame_bytes)); | 839 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, std::move(cal_frame_bytes)); |
| 854 | 840 |
| 841 EXPECT_FALSE(IsCommandComplete()); |
| 842 |
| 843 RunStopTracingTo(BattOrAgentState::SAMPLES_END_FRAME_RECEIVED); |
| 844 |
| 855 EXPECT_TRUE(IsCommandComplete()); | 845 EXPECT_TRUE(IsCommandComplete()); |
| 856 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 846 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 857 } | 847 } |
| 858 | 848 |
| 859 TEST_F(BattOrAgentTest, StopTracingFailsIfDataFrameMissingByte) { | 849 TEST_F(BattOrAgentTest, StopTracingSucceedsAfterDataFrameMissingByte) { |
| 860 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); | 850 GetAgent()->StopTracing(); |
| 861 | |
| 862 BattOrFrameHeader cal_frame_header{0, 1 * sizeof(RawBattOrSample)}; | |
| 863 RawBattOrSample cal_frame[] = { | |
| 864 RawBattOrSample{1, 1}, | |
| 865 }; | |
| 866 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | |
| 867 CreateFrame(cal_frame_header, cal_frame, 1)); | |
| 868 | 851 |
| 869 BattOrFrameHeader frame_header{1, 1 * sizeof(RawBattOrSample)}; | 852 BattOrFrameHeader frame_header{1, 1 * sizeof(RawBattOrSample)}; |
| 870 RawBattOrSample frame[] = {RawBattOrSample{1, 1}}; | 853 RawBattOrSample frame[] = {RawBattOrSample{1, 1}}; |
| 871 | 854 |
| 872 // Remove the last byte from the frame to make it invalid. | 855 // Remove the last byte from the frame to make it invalid. |
| 873 std::unique_ptr<vector<char>> frame_bytes = | 856 std::unique_ptr<vector<char>> frame_bytes = |
| 874 CreateFrame(frame_header, frame, 1); | 857 CreateFrame(frame_header, frame, 1); |
| 875 frame_bytes->pop_back(); | 858 frame_bytes->pop_back(); |
| 876 | 859 |
| 860 RunStopTracingTo(BattOrAgentState::CALIBRATION_FRAME_RECEIVED); |
| 877 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, std::move(frame_bytes)); | 861 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, std::move(frame_bytes)); |
| 878 | 862 |
| 863 EXPECT_FALSE(IsCommandComplete()); |
| 864 |
| 865 RunStopTracingTo(BattOrAgentState::SAMPLES_END_FRAME_RECEIVED); |
| 866 |
| 879 EXPECT_TRUE(IsCommandComplete()); | 867 EXPECT_TRUE(IsCommandComplete()); |
| 880 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 868 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 881 } | 869 } |
| 882 | 870 |
| 883 TEST_F(BattOrAgentTest, StopTracingFailsIfFrameArrivesOutOfOrder) { | 871 TEST_F(BattOrAgentTest, StopTracingSucceedsAfterDataFrameArrivesOutOfOrder) { |
| 872 GetAgent()->StopTracing(); |
| 873 |
| 874 // Frame with sequence number 1. |
| 875 BattOrFrameHeader frame_header1{1, 1 * sizeof(RawBattOrSample)}; |
| 876 RawBattOrSample frame1[] = {RawBattOrSample{1, 1}}; |
| 877 |
| 878 // Skip frame with sequence number 2. |
| 879 BattOrFrameHeader frame_header3{3, 1 * sizeof(RawBattOrSample)}; |
| 880 RawBattOrSample frame3[] = {RawBattOrSample{1, 1}}; |
| 881 |
| 884 RunStopTracingTo(BattOrAgentState::CALIBRATION_FRAME_RECEIVED); | 882 RunStopTracingTo(BattOrAgentState::CALIBRATION_FRAME_RECEIVED); |
| 885 | 883 |
| 886 BattOrFrameHeader frame_header{1, 1 * sizeof(RawBattOrSample)}; | 884 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 887 RawBattOrSample frame[] = {RawBattOrSample{1, 1}}; | 885 CreateFrame(frame_header1, frame1, 1)); |
| 886 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 887 CreateFrame(frame_header3, frame3, 1)); |
| 888 | 888 |
| 889 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 889 EXPECT_FALSE(IsCommandComplete()); |
| 890 CreateFrame(frame_header, frame, 1)); | |
| 891 | 890 |
| 892 // Skip frame with sequence number 2. | 891 RunStopTracingTo(BattOrAgentState::SAMPLES_END_FRAME_RECEIVED); |
| 893 frame_header = BattOrFrameHeader{3, 1 * sizeof(RawBattOrSample)}; | |
| 894 frame[0] = RawBattOrSample{1, 1}; | |
| 895 | |
| 896 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | |
| 897 CreateFrame(frame_header, frame, 1)); | |
| 898 | 892 |
| 899 EXPECT_TRUE(IsCommandComplete()); | 893 EXPECT_TRUE(IsCommandComplete()); |
| 900 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 894 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 901 } | 895 } |
| 902 | 896 |
| 903 TEST_F(BattOrAgentTest, RecordClockSyncMarker) { | 897 TEST_F(BattOrAgentTest, RecordClockSyncMarker) { |
| 904 testing::InSequence s; | 898 testing::InSequence s; |
| 905 EXPECT_CALL(*GetAgent()->GetConnection(), Open()); | 899 EXPECT_CALL(*GetAgent()->GetConnection(), Open()); |
| 906 | 900 |
| 907 BattOrControlMessage request_current_sample_msg{ | 901 BattOrControlMessage request_current_sample_msg{ |
| 908 BATTOR_CONTROL_MESSAGE_TYPE_READ_SAMPLE_COUNT, 0, 0}; | 902 BATTOR_CONTROL_MESSAGE_TYPE_READ_SAMPLE_COUNT, 0, 0}; |
| 909 EXPECT_CALL(*GetAgent()->GetConnection(), | 903 EXPECT_CALL(*GetAgent()->GetConnection(), |
| 910 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, | 904 SendBytes(BATTOR_MESSAGE_TYPE_CONTROL, |
| 911 BufferEq(&request_current_sample_msg, | 905 BufferEq(&request_current_sample_msg, |
| 912 sizeof(request_current_sample_msg)), | 906 sizeof(request_current_sample_msg)), |
| 913 sizeof(request_current_sample_msg))); | 907 sizeof(request_current_sample_msg))); |
| 914 | 908 |
| 915 EXPECT_CALL(*GetAgent()->GetConnection(), | 909 EXPECT_CALL(*GetAgent()->GetConnection(), |
| 916 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK)); | 910 ReadMessage(BATTOR_MESSAGE_TYPE_CONTROL_ACK)); |
| 917 | 911 |
| 912 GetAgent()->RecordClockSyncMarker(kClockSyncId); |
| 918 RunRecordClockSyncMarkerTo( | 913 RunRecordClockSyncMarkerTo( |
| 919 BattOrAgentState::RECORD_CLOCK_SYNC_MARKER_COMPLETE); | 914 BattOrAgentState::RECORD_CLOCK_SYNC_MARKER_COMPLETE); |
| 920 | 915 |
| 921 EXPECT_TRUE(IsCommandComplete()); | 916 EXPECT_TRUE(IsCommandComplete()); |
| 922 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 917 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 923 } | 918 } |
| 924 | 919 |
| 925 TEST_F(BattOrAgentTest, RecordClockSyncMarkerPrintsInStopTracingResult) { | 920 TEST_F(BattOrAgentTest, RecordClockSyncMarkerPrintsInStopTracingResult) { |
| 926 // Record a clock sync marker that says CLOCK_SYNC_ID happened at sample #2. | 921 // Record a clock sync marker that says CLOCK_SYNC_ID happened at sample #2. |
| 922 GetAgent()->RecordClockSyncMarker(kClockSyncId); |
| 923 |
| 927 RunRecordClockSyncMarkerTo(BattOrAgentState::CURRENT_SAMPLE_REQUEST_SENT); | 924 RunRecordClockSyncMarkerTo(BattOrAgentState::CURRENT_SAMPLE_REQUEST_SENT); |
| 928 | 925 |
| 929 uint32_t current_sample = 1; | 926 uint32_t current_sample = 1; |
| 930 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 927 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
| 931 ToCharVector(current_sample)); | 928 ToCharVector(current_sample)); |
| 932 | 929 |
| 933 EXPECT_TRUE(IsCommandComplete()); | 930 EXPECT_TRUE(IsCommandComplete()); |
| 934 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 931 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 935 | 932 |
| 933 GetAgent()->StopTracing(); |
| 936 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); | 934 RunStopTracingTo(BattOrAgentState::SAMPLES_REQUEST_SENT); |
| 937 | 935 |
| 938 // Now run StopTracing, and make sure that CLOCK_SYNC_ID gets printed out with | 936 // Now run StopTracing, and make sure that CLOCK_SYNC_ID gets printed out with |
| 939 // sample #2 (including calibration frame samples). | 937 // sample #2 (including calibration frame samples). |
| 940 BattOrFrameHeader cal_frame_header{0, 1 * sizeof(RawBattOrSample)}; | 938 BattOrFrameHeader cal_frame_header{0, 1 * sizeof(RawBattOrSample)}; |
| 941 RawBattOrSample cal_frame[] = {RawBattOrSample{1, 1}}; | 939 RawBattOrSample cal_frame[] = {RawBattOrSample{1, 1}}; |
| 942 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, | 940 OnMessageRead(true, BATTOR_MESSAGE_TYPE_SAMPLES, |
| 943 CreateFrame(cal_frame_header, cal_frame, 1)); | 941 CreateFrame(cal_frame_header, cal_frame, 1)); |
| 944 | 942 |
| 945 BattOrFrameHeader frame_header1{1, 2 * sizeof(RawBattOrSample)}; | 943 BattOrFrameHeader frame_header1{1, 2 * sizeof(RawBattOrSample)}; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 969 GetTaskRunner()->RunUntilIdle(); | 967 GetTaskRunner()->RunUntilIdle(); |
| 970 | 968 |
| 971 GetAgent()->OnConnectionOpened(false); | 969 GetAgent()->OnConnectionOpened(false); |
| 972 GetTaskRunner()->RunUntilIdle(); | 970 GetTaskRunner()->RunUntilIdle(); |
| 973 | 971 |
| 974 EXPECT_TRUE(IsCommandComplete()); | 972 EXPECT_TRUE(IsCommandComplete()); |
| 975 EXPECT_EQ(BATTOR_ERROR_CONNECTION_FAILED, GetCommandError()); | 973 EXPECT_EQ(BATTOR_ERROR_CONNECTION_FAILED, GetCommandError()); |
| 976 } | 974 } |
| 977 | 975 |
| 978 TEST_F(BattOrAgentTest, RecordClockSyncMarkerFailsIfSampleRequestSendFails) { | 976 TEST_F(BattOrAgentTest, RecordClockSyncMarkerFailsIfSampleRequestSendFails) { |
| 977 GetAgent()->RecordClockSyncMarker(kClockSyncId); |
| 978 |
| 979 RunRecordClockSyncMarkerTo(BattOrAgentState::CONNECTED); | 979 RunRecordClockSyncMarkerTo(BattOrAgentState::CONNECTED); |
| 980 OnBytesSent(false); | 980 OnBytesSent(false); |
| 981 | 981 |
| 982 EXPECT_TRUE(IsCommandComplete()); | 982 EXPECT_TRUE(IsCommandComplete()); |
| 983 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); | 983 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); |
| 984 } | 984 } |
| 985 | 985 |
| 986 TEST_F(BattOrAgentTest, RecordClockSyncMarkerRetriesCurrentSampleRead) { | 986 TEST_F(BattOrAgentTest, RecordClockSyncMarkerFailsIfCurrentSampleReadFails) { |
| 987 GetAgent()->RecordClockSyncMarker(kClockSyncId); |
| 988 |
| 987 RunRecordClockSyncMarkerTo(BattOrAgentState::CURRENT_SAMPLE_REQUEST_SENT); | 989 RunRecordClockSyncMarkerTo(BattOrAgentState::CURRENT_SAMPLE_REQUEST_SENT); |
| 988 | |
| 989 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); | 990 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); |
| 990 | 991 |
| 991 uint32_t current_sample = 1; | |
| 992 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | |
| 993 ToCharVector(current_sample)); | |
| 994 | |
| 995 EXPECT_TRUE(IsCommandComplete()); | 992 EXPECT_TRUE(IsCommandComplete()); |
| 996 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 993 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetCommandError()); |
| 997 } | 994 } |
| 998 | 995 |
| 999 TEST_F(BattOrAgentTest, | 996 TEST_F(BattOrAgentTest, |
| 1000 RecordClockSyncMarkerFailsIfCurrentSampleReadHasWrongType) { | 997 RecordClockSyncMarkerFailsIfCurrentSampleReadHasWrongType) { |
| 998 GetAgent()->RecordClockSyncMarker(kClockSyncId); |
| 999 |
| 1001 RunRecordClockSyncMarkerTo(BattOrAgentState::CURRENT_SAMPLE_REQUEST_SENT); | 1000 RunRecordClockSyncMarkerTo(BattOrAgentState::CURRENT_SAMPLE_REQUEST_SENT); |
| 1002 | 1001 |
| 1003 uint32_t current_sample = 1; | 1002 uint32_t current_sample = 1; |
| 1004 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL, | 1003 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL, |
| 1005 ToCharVector(current_sample)); | 1004 ToCharVector(current_sample)); |
| 1006 | 1005 |
| 1007 EXPECT_TRUE(IsCommandComplete()); | 1006 EXPECT_TRUE(IsCommandComplete()); |
| 1008 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 1007 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); |
| 1009 } | 1008 } |
| 1010 | 1009 |
| 1011 TEST_F(BattOrAgentTest, GetFirmwareGitHash) { | 1010 TEST_F(BattOrAgentTest, GetFirmwareGitHash) { |
| 1011 GetAgent()->GetFirmwareGitHash(); |
| 1012 |
| 1012 RunGetFirmwareGitHashTo(BattOrAgentState::READ_GIT_HASH_RECEIVED); | 1013 RunGetFirmwareGitHashTo(BattOrAgentState::READ_GIT_HASH_RECEIVED); |
| 1014 |
| 1013 EXPECT_TRUE(IsCommandComplete()); | 1015 EXPECT_TRUE(IsCommandComplete()); |
| 1014 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); | 1016 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 1015 EXPECT_EQ("GITHASH", GetGitHash()); | 1017 EXPECT_EQ("GITHASH", GetGitHash()); |
| 1016 } | 1018 } |
| 1017 | 1019 |
| 1018 TEST_F(BattOrAgentTest, GetFirmwareGitHashFailsWithoutConnection) { | 1020 TEST_F(BattOrAgentTest, GetFirmwareGitHashFailsWithoutConnection) { |
| 1019 GetAgent()->GetFirmwareGitHash(); | 1021 GetAgent()->GetFirmwareGitHash(); |
| 1022 |
| 1020 GetTaskRunner()->RunUntilIdle(); | 1023 GetTaskRunner()->RunUntilIdle(); |
| 1021 | 1024 |
| 1022 GetAgent()->OnConnectionOpened(false); | 1025 GetAgent()->OnConnectionOpened(false); |
| 1023 GetTaskRunner()->RunUntilIdle(); | 1026 GetTaskRunner()->RunUntilIdle(); |
| 1024 | 1027 |
| 1025 EXPECT_TRUE(IsCommandComplete()); | 1028 EXPECT_TRUE(IsCommandComplete()); |
| 1026 EXPECT_EQ(BATTOR_ERROR_CONNECTION_FAILED, GetCommandError()); | 1029 EXPECT_EQ(BATTOR_ERROR_CONNECTION_FAILED, GetCommandError()); |
| 1027 } | 1030 } |
| 1028 | 1031 |
| 1029 TEST_F(BattOrAgentTest, GetFirmwareGitHashFailsIfReadHasWrongType) { | 1032 TEST_F(BattOrAgentTest, GetFirmwareGitHashSucceedsReadHasWrongType) { |
| 1033 GetAgent()->GetFirmwareGitHash(); |
| 1034 |
| 1030 RunGetFirmwareGitHashTo(BattOrAgentState::GIT_FIRMWARE_HASH_REQUEST_SENT); | 1035 RunGetFirmwareGitHashTo(BattOrAgentState::GIT_FIRMWARE_HASH_REQUEST_SENT); |
| 1031 | 1036 |
| 1032 uint32_t current_sample = 1; | 1037 uint32_t current_sample = 1; |
| 1033 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL, | 1038 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL, |
| 1034 ToCharVector(current_sample)); | 1039 ToCharVector(current_sample)); |
| 1035 | 1040 |
| 1041 EXPECT_FALSE(IsCommandComplete()); |
| 1042 |
| 1043 RunGetFirmwareGitHashTo(BattOrAgentState::READ_GIT_HASH_RECEIVED); |
| 1044 |
| 1036 EXPECT_TRUE(IsCommandComplete()); | 1045 EXPECT_TRUE(IsCommandComplete()); |
| 1037 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); | 1046 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 1038 } | 1047 } |
| 1039 | 1048 |
| 1040 TEST_F(BattOrAgentTest, GetFirmwareGitHashFailsIfInitSendFails) { | 1049 TEST_F(BattOrAgentTest, GetFirmwareGitHashFailsIfInitSendFails) { |
| 1050 GetAgent()->GetFirmwareGitHash(); |
| 1051 |
| 1041 RunGetFirmwareGitHashTo(BattOrAgentState::CONNECTED); | 1052 RunGetFirmwareGitHashTo(BattOrAgentState::CONNECTED); |
| 1053 |
| 1042 OnBytesSent(false); | 1054 OnBytesSent(false); |
| 1043 | 1055 |
| 1044 EXPECT_TRUE(IsCommandComplete()); | 1056 EXPECT_TRUE(IsCommandComplete()); |
| 1045 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); | 1057 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); |
| 1046 } | 1058 } |
| 1047 | 1059 |
| 1048 TEST_F(BattOrAgentTest, GetFirmwareGitHashFailsIfInitAckReadFails) { | 1060 TEST_F(BattOrAgentTest, GetFirmwareGitHashSucceedsAfterInitAckReadFails) { |
| 1061 GetAgent()->GetFirmwareGitHash(); |
| 1062 |
| 1049 RunGetFirmwareGitHashTo(BattOrAgentState::INIT_SENT); | 1063 RunGetFirmwareGitHashTo(BattOrAgentState::INIT_SENT); |
| 1064 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); |
| 1050 | 1065 |
| 1051 for (int i =0; i < 21; i++) { | 1066 EXPECT_FALSE(IsCommandComplete()); |
| 1052 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); | |
| 1053 | 1067 |
| 1054 // Bytes will be sent because INIT will be retried. | 1068 RunGetFirmwareGitHashTo(BattOrAgentState::READ_GIT_HASH_RECEIVED); |
| 1055 OnBytesSent(true); | |
| 1056 } | |
| 1057 | 1069 |
| 1058 EXPECT_TRUE(IsCommandComplete()); | 1070 EXPECT_TRUE(IsCommandComplete()); |
| 1059 EXPECT_EQ(BATTOR_ERROR_TOO_MANY_INIT_RETRIES, GetCommandError()); | 1071 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 1060 } | 1072 } |
| 1061 | 1073 |
| 1062 TEST_F(BattOrAgentTest, GetFirmwareGithashFailsIfInitWrongAckRead) { | 1074 TEST_F(BattOrAgentTest, GetFirmwareGitHashSucceedsAfterInitWrongAckRead) { |
| 1075 GetAgent()->GetFirmwareGitHash(); |
| 1076 |
| 1063 RunGetFirmwareGitHashTo(BattOrAgentState::INIT_SENT); | 1077 RunGetFirmwareGitHashTo(BattOrAgentState::INIT_SENT); |
| 1064 for (int i = 0; i < 21; i++) { | 1078 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, |
| 1065 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, | 1079 ToCharVector(kStartTracingAck)); |
| 1066 ToCharVector(kStartTracingAck)); | |
| 1067 | 1080 |
| 1068 // Bytes will be sent because INIT will be retried. | 1081 EXPECT_FALSE(IsCommandComplete()); |
| 1069 OnBytesSent(true); | 1082 |
| 1070 } | 1083 RunGetFirmwareGitHashTo(BattOrAgentState::READ_GIT_HASH_RECEIVED); |
| 1071 | 1084 |
| 1072 EXPECT_TRUE(IsCommandComplete()); | 1085 EXPECT_TRUE(IsCommandComplete()); |
| 1073 EXPECT_EQ(BATTOR_ERROR_TOO_MANY_INIT_RETRIES, GetCommandError()); | 1086 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); |
| 1074 } | 1087 } |
| 1075 } // namespace battor | 1088 } // namespace battor |
| OLD | NEW |