aboutsummaryrefslogtreecommitdiff
path: root/gd/common/bidi_queue_unittest.cc
blob: 7a5503c80a254de59e64d834757b828f72d2aadf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "common/bidi_queue.h"

#include <future>

#include "common/bind.h"
#include "gtest/gtest.h"
#include "os/handler.h"
#include "os/thread.h"

using ::bluetooth::os::Thread;
using ::bluetooth::os::Handler;

namespace bluetooth {
namespace common {
namespace {

class BidiQueueTest : public ::testing::Test {
 protected:
  void SetUp() override {
    up_thread_ = new Thread("up_thread", Thread::Priority::NORMAL);
    up_handler_ = new Handler(up_thread_);
    down_thread_ = new Thread("down_thread", Thread::Priority::NORMAL);
    down_handler_ = new Handler(down_thread_);
  }

  void TearDown() override {
    delete up_handler_;
    delete up_thread_;
    delete down_handler_;
    delete down_thread_;
  }

  Thread* up_thread_;
  Handler* up_handler_;
  Thread* down_thread_;
  Handler* down_handler_;
};

class A {
};

class B {
};

template <typename TA, typename TB>
class TestBidiQueueEnd {
 public:
  explicit TestBidiQueueEnd(BidiQueueEnd<TA, TB>* end, Handler* handler)
      : handler_(handler), end_(end) {}

  ~TestBidiQueueEnd() {
    handler_->Clear();
  }

  std::promise<void>* Send(TA* value) {
    std::promise<void>* promise = new std::promise<void>();
    handler_->Post(BindOnce(&TestBidiQueueEnd<TA, TB>::handle_send, common::Unretained(this), common::Unretained(value),
                            common::Unretained(promise)));
    return promise;
  }

  std::promise<TB*>* Receive() {
    std::promise<TB*>* promise = new std::promise<TB*>();
    handler_->Post(
        BindOnce(&TestBidiQueueEnd<TA, TB>::handle_receive, common::Unretained(this), common::Unretained(promise)));

    return promise;
  }

  void handle_send(TA* value, std::promise<void>* promise) {
    end_->RegisterEnqueue(handler_, Bind(&TestBidiQueueEnd<TA, TB>::handle_register_enqueue, common::Unretained(this),
                                         common::Unretained(value), common::Unretained(promise)));
  }

  std::unique_ptr<TA> handle_register_enqueue(TA* value, std::promise<void>* promise) {
    end_->UnregisterEnqueue();
    promise->set_value();
    return std::unique_ptr<TA>(value);
  }

  void handle_receive(std::promise<TB*>* promise) {
    end_->RegisterDequeue(handler_, Bind(&TestBidiQueueEnd<TA, TB>::handle_register_dequeue, common::Unretained(this),
                                         common::Unretained(promise)));
  }

  void handle_register_dequeue(std::promise<TB*>* promise) {
    end_->UnregisterDequeue();
    promise->set_value(end_->TryDequeue().get());
  }

 private:
  Handler* handler_;
  BidiQueueEnd<TA, TB>* end_;
};

TEST_F(BidiQueueTest, simple_test) {
  BidiQueue<A, B> queue(100);
  TestBidiQueueEnd<B, A> test_up(queue.GetUpEnd(), up_handler_);
  TestBidiQueueEnd<A, B> test_down(queue.GetDownEnd(), down_handler_);

  auto sending_b = new B();
  auto promise_sending_b = test_up.Send(sending_b);
  promise_sending_b->get_future().wait();
  auto promise_receive_b = test_down.Receive();
  EXPECT_EQ(promise_receive_b->get_future().get(), sending_b);
  delete promise_receive_b;
  delete promise_sending_b;

  auto sending_a = new A();
  auto promise_sending_a = test_down.Send(sending_a);
  promise_sending_a->get_future().wait();
  auto promise_receive_a = test_up.Receive();
  EXPECT_EQ(promise_receive_a->get_future().get(), sending_a);
  delete promise_receive_a;
  delete promise_sending_a;
}

}  // namespace
}  // namespace os
}  // namespace bluetooth