Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 84cb6c15 authored by Chienyuan's avatar Chienyuan Committed by android-build-merger
Browse files

Fix BlockingQueue bug when using vector

am: 988b802b

Change-Id: I665ed4ef6ad47ad4b5b5f42c226d68b872fade7e
parents 7edfe044 988b802b
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -34,12 +34,12 @@ class BlockingQueue {
    }
  };

  const T& take() {
  T take() {
    std::unique_lock<std::mutex> lock(mutex_);
    while (queue_.empty()) {
      not_empty_.wait(lock);
    }
    const T& data = queue_.front();
    T data = queue_.front();
    queue_.pop();
    return data;
  };
+22 −0
Original line number Diff line number Diff line
@@ -99,6 +99,28 @@ TEST_F(BlockingQueueTest, wait_for_non_empty_batch) {
  EXPECT_TRUE(queue_.empty());
}

class VectorBlockingQueueTest : public ::testing::Test {
 protected:
  void SetUp() override {
    EXPECT_TRUE(queue_.empty());
  }

  // Postcondition for each test case: clear the blocking queue
  void TearDown() override {
    EXPECT_TRUE(queue_.empty());
  }

  BlockingQueue<std::vector<uint8_t>> queue_;
};

TEST_F(VectorBlockingQueueTest, same_thread_push_and_pop) {
  std::vector<uint8_t> data = {1, 2, 3, 4, 5, 6};
  queue_.push(data);
  EXPECT_FALSE(queue_.empty());
  EXPECT_EQ(queue_.take(), data);
  EXPECT_TRUE(queue_.empty());
}

}  // namespace
}  // namespace common
}  // namespace bluetooth