From 0c7e0b3e4865271493adbd2c3d75e59302f508a1 Mon Sep 17 00:00:00 2001
From: William Escande <wescande@google.com>
Date: Sat, 7 Jan 2023 22:58:36 -0800
Subject: [PATCH] Ensure future are reset before being set

This avoid async flakiness when test assert the future is null before
enqueue next packet

Test: atest --host
Bug: 264572812
Change-Id: Iac9e2f0722db5d6d4c148358d96bb1df1f00aebf
---
 .../acl_manager/classic_acl_connection_test.cc | 10 ++++++----
 .../hci/acl_manager/le_acl_connection_test.cc  | 10 ++++++----
 system/gd/hci/acl_manager/le_impl_test.cc      | 10 ++++++----
 system/gd/hci/acl_manager_unittest.cc          | 10 ++++++----
 system/gd/hci/hci_layer_test.cc                | 18 +++++++++---------
 system/gd/hci/le_address_manager_test.cc       | 10 ++++++----
 system/gd/hci/le_periodic_sync_manager_test.cc | 10 ++++++----
 system/gd/hci/le_scanning_manager_test.cc      | 10 ++++++----
 system/gd/os/android/wakelock_native_test.cc   |  5 +++--
 9 files changed, 54 insertions(+), 39 deletions(-)

diff --git a/system/gd/hci/acl_manager/classic_acl_connection_test.cc b/system/gd/hci/acl_manager/classic_acl_connection_test.cc
index 628a37a9370..af062020a1b 100644
--- a/system/gd/hci/acl_manager/classic_acl_connection_test.cc
+++ b/system/gd/hci/acl_manager/classic_acl_connection_test.cc
@@ -124,8 +124,9 @@ class TestAclConnectionInterface : public hci::AclConnectionInterface {
     command_queue_.push(std::move(command));
     command_status_callbacks.push_back(std::move(on_status));
     if (command_promise_ != nullptr) {
-      command_promise_->set_value();
-      command_promise_.reset();
+      std::promise<void>* prom = command_promise_.release();
+      prom->set_value();
+      delete prom;
     }
   }
 
@@ -136,8 +137,9 @@ class TestAclConnectionInterface : public hci::AclConnectionInterface {
     command_queue_.push(std::move(command));
     command_complete_callbacks.push_back(std::move(on_complete));
     if (command_promise_ != nullptr) {
-      command_promise_->set_value();
-      command_promise_.reset();
+      std::promise<void>* prom = command_promise_.release();
+      prom->set_value();
+      delete prom;
     }
   }
 
diff --git a/system/gd/hci/acl_manager/le_acl_connection_test.cc b/system/gd/hci/acl_manager/le_acl_connection_test.cc
index a210c2227b4..52d4c77fcad 100644
--- a/system/gd/hci/acl_manager/le_acl_connection_test.cc
+++ b/system/gd/hci/acl_manager/le_acl_connection_test.cc
@@ -102,8 +102,9 @@ class TestLeAclConnectionInterface : public hci::LeAclConnectionInterface {
     command_queue_.push(std::move(command));
     command_status_callbacks.push_back(std::move(on_status));
     if (command_promise_ != nullptr) {
-      command_promise_->set_value();
-      command_promise_.reset();
+      std::promise<void>* prom = command_promise_.release();
+      prom->set_value();
+      delete prom;
     }
   }
 
@@ -114,8 +115,9 @@ class TestLeAclConnectionInterface : public hci::LeAclConnectionInterface {
     command_queue_.push(std::move(command));
     command_complete_callbacks.push_back(std::move(on_complete));
     if (command_promise_ != nullptr) {
-      command_promise_->set_value();
-      command_promise_.reset();
+      std::promise<void>* prom = command_promise_.release();
+      prom->set_value();
+      delete prom;
     }
   }
 
diff --git a/system/gd/hci/acl_manager/le_impl_test.cc b/system/gd/hci/acl_manager/le_impl_test.cc
index d75f7ce16ee..f2779d20a22 100644
--- a/system/gd/hci/acl_manager/le_impl_test.cc
+++ b/system/gd/hci/acl_manager/le_impl_test.cc
@@ -256,8 +256,9 @@ class TestHciLayer : public HciLayer {
     command_queue_.push(std::move(command));
     command_status_callbacks.push_back(std::move(on_status));
     if (command_promise_ != nullptr) {
-      command_promise_->set_value();
-      command_promise_.reset();
+      std::promise<void>* prom = command_promise_.release();
+      prom->set_value();
+      delete prom;
     }
   }
 
@@ -268,8 +269,9 @@ class TestHciLayer : public HciLayer {
     command_queue_.push(std::move(command));
     command_complete_callbacks.push_back(std::move(on_complete));
     if (command_promise_ != nullptr) {
-      command_promise_->set_value();
-      command_promise_.reset();
+      std::promise<void>* prom = command_promise_.release();
+      prom->set_value();
+      delete prom;
     }
   }
 
diff --git a/system/gd/hci/acl_manager_unittest.cc b/system/gd/hci/acl_manager_unittest.cc
index 79d083480cb..9b296669518 100644
--- a/system/gd/hci/acl_manager_unittest.cc
+++ b/system/gd/hci/acl_manager_unittest.cc
@@ -320,8 +320,9 @@ class TestHciLayer : public HciLayer {
  private:
   void Notify() {
     if (hci_command_promise_ != nullptr) {
-      hci_command_promise_->set_value();
-      hci_command_promise_.reset();
+      std::promise<void>* prom = hci_command_promise_.release();
+      prom->set_value();
+      delete prom;
     }
   }
 
@@ -372,8 +373,9 @@ class MockLeConnectionCallbacks : public LeConnectionCallbacks {
   void OnLeConnectSuccess(AddressWithType address_with_type, std::unique_ptr<LeAclConnection> connection) override {
     le_connections_.push_back(std::move(connection));
     if (le_connection_promise_ != nullptr) {
-      le_connection_promise_->set_value();
-      le_connection_promise_.reset();
+      std::promise<void>* prom = le_connection_promise_.release();
+      prom->set_value();
+      delete prom;
     }
   }
   MOCK_METHOD(void, OnLeConnectFail, (AddressWithType, ErrorCode reason, bool locally_initiated), (override));
diff --git a/system/gd/hci/hci_layer_test.cc b/system/gd/hci/hci_layer_test.cc
index ff7a729dbc1..1d79e9bb114 100644
--- a/system/gd/hci/hci_layer_test.cc
+++ b/system/gd/hci/hci_layer_test.cc
@@ -84,18 +84,18 @@ class TestHciHal : public hal::HciHal {
   void sendHciCommand(hal::HciPacket command) override {
     outgoing_commands_.push_back(std::move(command));
     if (sent_command_promise_ != nullptr) {
-      auto promise = std::move(sent_command_promise_);
-      sent_command_promise_.reset();
-      promise->set_value();
+      std::promise<void>* prom = sent_command_promise_.release();
+      prom->set_value();
+      delete prom;
     }
   }
 
   void sendAclData(hal::HciPacket data) override {
     outgoing_acl_.push_back(std::move(data));
     if (sent_acl_promise_ != nullptr) {
-      auto promise = std::move(sent_acl_promise_);
-      sent_acl_promise_.reset();
-      promise->set_value();
+      std::promise<void>* prom = sent_acl_promise_.release();
+      prom->set_value();
+      delete prom;
     }
   }
 
@@ -106,9 +106,9 @@ class TestHciHal : public hal::HciHal {
   void sendIsoData(hal::HciPacket data) override {
     outgoing_iso_.push_back(std::move(data));
     if (sent_iso_promise_ != nullptr) {
-      auto promise = std::move(sent_iso_promise_);
-      sent_iso_promise_.reset();
-      promise->set_value();
+      std::promise<void>* prom = sent_iso_promise_.release();
+      prom->set_value();
+      delete prom;
     }
   }
 
diff --git a/system/gd/hci/le_address_manager_test.cc b/system/gd/hci/le_address_manager_test.cc
index e2000cfeafa..b1ede37b37d 100644
--- a/system/gd/hci/le_address_manager_test.cc
+++ b/system/gd/hci/le_address_manager_test.cc
@@ -57,8 +57,9 @@ class TestHciLayer : public HciLayer {
     command_queue_.push(std::move(command));
     command_complete_callbacks.push_back(std::move(on_complete));
     if (command_promise_ != nullptr) {
-      command_promise_->set_value();
-      command_promise_.reset();
+      std::promise<void>* prom = command_promise_.release();
+      prom->set_value();
+      delete prom;
     }
   }
 
@@ -136,8 +137,9 @@ class RotatorClient : public LeAddressManagerCallback {
     paused = false;
     le_address_manager_->AckResume(this);
     if (resume_promise_ != nullptr) {
-      resume_promise_->set_value();
-      resume_promise_.reset();
+      std::promise<void>* prom = resume_promise_.release();
+      prom->set_value();
+      delete prom;
     }
   }
 
diff --git a/system/gd/hci/le_periodic_sync_manager_test.cc b/system/gd/hci/le_periodic_sync_manager_test.cc
index 1d61a2649a9..2bd90382a40 100644
--- a/system/gd/hci/le_periodic_sync_manager_test.cc
+++ b/system/gd/hci/le_periodic_sync_manager_test.cc
@@ -47,8 +47,9 @@ class TestLeScanningInterface : public LeScanningInterface {
     command_queue_.push(std::move(command));
     command_complete_callbacks.push_back(std::move(on_complete));
     if (command_promise_ != nullptr) {
-      command_promise_->set_value();
-      command_promise_.reset();
+      std::promise<void>* prom = command_promise_.release();
+      prom->set_value();
+      delete prom;
     }
   }
 
@@ -58,8 +59,9 @@ class TestLeScanningInterface : public LeScanningInterface {
     command_queue_.push(std::move(command));
     command_status_callbacks.push_back(std::move(on_status));
     if (command_promise_ != nullptr) {
-      command_promise_->set_value();
-      command_promise_.reset();
+      std::promise<void>* prom = command_promise_.release();
+      prom->set_value();
+      delete prom;
     }
   }
 
diff --git a/system/gd/hci/le_scanning_manager_test.cc b/system/gd/hci/le_scanning_manager_test.cc
index 31d608d3017..8c2831938df 100644
--- a/system/gd/hci/le_scanning_manager_test.cc
+++ b/system/gd/hci/le_scanning_manager_test.cc
@@ -146,8 +146,9 @@ class TestHciLayer : public HciLayer {
     command_status_callbacks.push_back(std::move(on_status));
     command_count_--;
     if (command_promise_ != nullptr && command_count_ == 0) {
-      command_promise_->set_value();
-      command_promise_.reset();
+      std::promise<void>* prom = command_promise_.release();
+      prom->set_value();
+      delete prom;
     }
   }
 
@@ -159,8 +160,9 @@ class TestHciLayer : public HciLayer {
     command_complete_callbacks.push_back(std::move(on_complete));
     command_count_--;
     if (command_promise_ != nullptr && command_count_ == 0) {
-      command_promise_->set_value();
-      command_promise_.reset();
+      std::promise<void>* prom = command_promise_.release();
+      prom->set_value();
+      delete prom;
     }
   }
 
diff --git a/system/gd/os/android/wakelock_native_test.cc b/system/gd/os/android/wakelock_native_test.cc
index 66be7229636..36f8af87d72 100644
--- a/system/gd/os/android/wakelock_native_test.cc
+++ b/system/gd/os/android/wakelock_native_test.cc
@@ -53,8 +53,9 @@ class PromiseFutureContext {
   static void FulfilPromise(std::unique_ptr<std::promise<void>>& promise) {
     std::lock_guard<std::recursive_mutex> lock_guard(mutex);
     if (promise != nullptr) {
-      promise->set_value();
-      promise = nullptr;
+      std::promise<void>* prom = promise.release();
+      prom->set_value();
+      delete prom;
     }
   }
 
-- 
GitLab