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

Commit 988b802b authored by Chienyuan's avatar Chienyuan Committed by Hansong Zhang
Browse files

Fix BlockingQueue bug when using vector

return value instead of reference when take

Test: run unittest
Change-Id: I268f5022306353493a348a52ed363cf60a125661
parent 44c1d2d7
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