Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(632)

Side by Side Diff: tools/battor_agent/battor_agent_unittest.cc

Issue 2563033002: Make the BattOr agent retry start tracing when we fail to read an ack (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/battor_agent/battor_agent.cc ('k') | tools/battor_agent/battor_error.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 command_error_ = error; 113 command_error_ = error;
114 trace_ = trace; 114 trace_ = trace;
115 } 115 }
116 116
117 void OnRecordClockSyncMarkerComplete(BattOrError error) override { 117 void OnRecordClockSyncMarkerComplete(BattOrError error) override {
118 is_command_complete_ = true; 118 is_command_complete_ = true;
119 command_error_ = error; 119 command_error_ = error;
120 } 120 }
121 121
122 void OnGetFirmwareGitHashComplete(const std::string& firmware_git_hash, 122 void OnGetFirmwareGitHashComplete(const std::string& firmware_git_hash,
123 BattOrError error) override { 123 BattOrError error) override {
124 is_command_complete_ = true; 124 is_command_complete_ = true;
125 command_error_ = error; 125 command_error_ = error;
126 firmware_git_hash_ = firmware_git_hash; 126 firmware_git_hash_ = firmware_git_hash;
127 } 127 }
128 128
129 void OnBytesSent(bool success) { 129 void OnBytesSent(bool success) {
130 agent_->OnBytesSent(success); 130 agent_->OnBytesSent(success);
131 task_runner_->RunUntilIdle(); 131 task_runner_->RunUntilIdle();
132 } 132 }
133 133
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 } 394 }
395 395
396 TEST_F(BattOrAgentTest, StartTracingFailsIfInitAckReadFails) { 396 TEST_F(BattOrAgentTest, StartTracingFailsIfInitAckReadFails) {
397 RunStartTracingTo(BattOrAgentState::INIT_SENT); 397 RunStartTracingTo(BattOrAgentState::INIT_SENT);
398 398
399 for (int i = 0; i < 21; i++) { 399 for (int i = 0; i < 21; i++) {
400 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); 400 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr);
401 401
402 // Bytes will be sent because INIT will be retried. 402 // Bytes will be sent because INIT will be retried.
403 OnBytesSent(true); 403 OnBytesSent(true);
404 } 404 }
405 405
406 EXPECT_TRUE(IsCommandComplete()); 406 EXPECT_TRUE(IsCommandComplete());
407 EXPECT_EQ(BATTOR_ERROR_TOO_MANY_INIT_RETRIES, GetCommandError()); 407 EXPECT_EQ(BATTOR_ERROR_TOO_MANY_INIT_RETRIES, GetCommandError());
408 } 408 }
409 409
410 TEST_F(BattOrAgentTest, StartTracingFailsIfInitWrongAckRead) { 410 TEST_F(BattOrAgentTest, StartTracingFailsIfInitWrongAckRead) {
411 RunStartTracingTo(BattOrAgentState::INIT_SENT); 411 RunStartTracingTo(BattOrAgentState::INIT_SENT);
412 412
413 for (int i = 0; i < 21; i++) { 413 for (int i = 0; i < 21; i++) {
414 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, 414 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK,
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 } 448 }
449 449
450 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingSendFails) { 450 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingSendFails) {
451 RunStartTracingTo(BattOrAgentState::INIT_SENT); 451 RunStartTracingTo(BattOrAgentState::INIT_SENT);
452 OnBytesSent(false); 452 OnBytesSent(false);
453 453
454 EXPECT_TRUE(IsCommandComplete()); 454 EXPECT_TRUE(IsCommandComplete());
455 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError()); 455 EXPECT_EQ(BATTOR_ERROR_SEND_ERROR, GetCommandError());
456 } 456 }
457 457
458 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingAckReadFails) { 458 TEST_F(BattOrAgentTest, StartTracingSucceedsAfterRetriesIfWrongAckRead) {
459 RunStartTracingTo(BattOrAgentState::START_TRACING_SENT); 459 RunStartTracingTo(BattOrAgentState::CONNECTED);
460 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr); 460
461 for (int i = 0; i < 4; i++) {
462 // Go through the correct init sequence, but give the wrong ack to
463 // START_TRACING.
464 OnBytesSent(true);
465 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK,
466 ToCharVector(kInitAck));
467 OnBytesSent(true);
468 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK,
469 ToCharVector(kSetGainAck));
470 OnBytesSent(true);
471 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK,
472 ToCharVector(kInitAck));
473 }
474
475 // On the last attempt, give the correct ack to START_TRACING.
476 OnBytesSent(true);
477 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, ToCharVector(kInitAck));
478 OnBytesSent(true);
479 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK,
480 ToCharVector(kSetGainAck));
481 OnBytesSent(true);
482 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK,
483 ToCharVector(kStartTracingAck));
461 484
462 EXPECT_TRUE(IsCommandComplete()); 485 EXPECT_TRUE(IsCommandComplete());
463 EXPECT_EQ(BATTOR_ERROR_RECEIVE_ERROR, GetCommandError()); 486 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError());
464 } 487 }
465 488
466 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingWrongAckRead) { 489 TEST_F(BattOrAgentTest, StartTracingSucceedsAfterRetriesWithReadFailure) {
467 RunStartTracingTo(BattOrAgentState::START_TRACING_SENT); 490 RunStartTracingTo(BattOrAgentState::CONNECTED);
491
492 for (int i = 0; i < 4; i++) {
493 // Go through the correct init sequence, but indicate that we failed to read
494 // the START_TRACING ack.
495 OnBytesSent(true);
496 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK,
497 ToCharVector(kInitAck));
498 OnBytesSent(true);
499 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK,
500 ToCharVector(kSetGainAck));
501 OnBytesSent(true);
502 OnMessageRead(false, BATTOR_MESSAGE_TYPE_CONTROL_ACK, nullptr);
503 }
504
505 // On the last attempt, give the correct ack to START_TRACING.
506 OnBytesSent(true);
468 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, ToCharVector(kInitAck)); 507 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK, ToCharVector(kInitAck));
508 OnBytesSent(true);
509 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK,
510 ToCharVector(kSetGainAck));
511 OnBytesSent(true);
512 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK,
513 ToCharVector(kStartTracingAck));
469 514
470 EXPECT_TRUE(IsCommandComplete()); 515 EXPECT_TRUE(IsCommandComplete());
471 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); 516 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError());
517 }
518
519 TEST_F(BattOrAgentTest, StartTracingFailsIfStartTracingWrongAckReadTooMuch) {
520 RunStartTracingTo(BattOrAgentState::CONNECTED);
521
522 for (int i = 0; i < 5; i++) {
523 // Go through the correct init sequence, but give the wrong ack to
524 // START_TRACING.
525 OnBytesSent(true);
526 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK,
527 ToCharVector(kInitAck));
528 OnBytesSent(true);
529 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK,
530 ToCharVector(kSetGainAck));
531 OnBytesSent(true);
532 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL_ACK,
533 ToCharVector(kInitAck));
534 }
535
536 EXPECT_TRUE(IsCommandComplete());
537 EXPECT_EQ(BATTOR_ERROR_TOO_MANY_START_TRACING_RETRIES, GetCommandError());
472 } 538 }
473 539
474 TEST_F(BattOrAgentTest, StartTracingSucceedsWithOneInitFailure) { 540 TEST_F(BattOrAgentTest, StartTracingSucceedsWithOneInitFailure) {
475 RunStartTracingTo(BattOrAgentState::INIT_SENT); 541 RunStartTracingTo(BattOrAgentState::INIT_SENT);
476 542
477 // Send some samples instead of an INIT ACK. This will force an INIT retry. 543 // Send some samples instead of an INIT ACK. This will force an INIT retry.
478 BattOrFrameHeader frame_header{1, 3 * sizeof(RawBattOrSample)}; 544 BattOrFrameHeader frame_header{1, 3 * sizeof(RawBattOrSample)};
479 RawBattOrSample frame[] = { 545 RawBattOrSample frame[] = {
480 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, RawBattOrSample{3, 3}, 546 RawBattOrSample{1, 1}, RawBattOrSample{2, 2}, RawBattOrSample{3, 3},
481 }; 547 };
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after
880 946
881 EXPECT_TRUE(IsCommandComplete()); 947 EXPECT_TRUE(IsCommandComplete());
882 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError()); 948 EXPECT_EQ(BATTOR_ERROR_NONE, GetCommandError());
883 EXPECT_EQ( 949 EXPECT_EQ(
884 "# BattOr\n# voltage_range [-2401.2, 2398.8] mV\n# " 950 "# BattOr\n# voltage_range [-2401.2, 2398.8] mV\n# "
885 "current_range [-1200.6, 1199.4] mA\n" 951 "current_range [-1200.6, 1199.4] mA\n"
886 "# sample_rate 1000 Hz, gain 1.0x\n" 952 "# sample_rate 1000 Hz, gain 1.0x\n"
887 "0.00 0.0 0.0 <MY_MARKER>\n" 953 "0.00 0.0 0.0 <MY_MARKER>\n"
888 "1.00 0.6 1.2\n", 954 "1.00 0.6 1.2\n",
889 GetTrace()); 955 GetTrace());
890
891 } 956 }
892 957
893 TEST_F(BattOrAgentTest, RecordClockSyncMarkerFailsWithoutConnection) { 958 TEST_F(BattOrAgentTest, RecordClockSyncMarkerFailsWithoutConnection) {
894 GetAgent()->RecordClockSyncMarker("my_marker"); 959 GetAgent()->RecordClockSyncMarker("my_marker");
895 GetTaskRunner()->RunUntilIdle(); 960 GetTaskRunner()->RunUntilIdle();
896 961
897 GetAgent()->OnConnectionOpened(false); 962 GetAgent()->OnConnectionOpened(false);
898 GetTaskRunner()->RunUntilIdle(); 963 GetTaskRunner()->RunUntilIdle();
899 964
900 EXPECT_TRUE(IsCommandComplete()); 965 EXPECT_TRUE(IsCommandComplete());
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
956 RunGetFirmwareGitHashTo(BattOrAgentState::GIT_FIRMWARE_HASH_REQUEST_SENT); 1021 RunGetFirmwareGitHashTo(BattOrAgentState::GIT_FIRMWARE_HASH_REQUEST_SENT);
957 1022
958 uint32_t current_sample = 1; 1023 uint32_t current_sample = 1;
959 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL, 1024 OnMessageRead(true, BATTOR_MESSAGE_TYPE_CONTROL,
960 ToCharVector(current_sample)); 1025 ToCharVector(current_sample));
961 1026
962 EXPECT_TRUE(IsCommandComplete()); 1027 EXPECT_TRUE(IsCommandComplete());
963 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError()); 1028 EXPECT_EQ(BATTOR_ERROR_UNEXPECTED_MESSAGE, GetCommandError());
964 } 1029 }
965 } // namespace battor 1030 } // namespace battor
OLDNEW
« no previous file with comments | « tools/battor_agent/battor_agent.cc ('k') | tools/battor_agent/battor_error.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698