aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/common_runtime/buf_rendezvous_test.cc
blob: 0e798235bf0649428409a2fa72ac3067736c347a (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

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 "tensorflow/core/common_runtime/buf_rendezvous.h"

#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/framework/types.pb.h"
#include "tensorflow/core/lib/core/notification.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/core/status_test_util.h"
#include "tensorflow/core/platform/test.h"

namespace tensorflow {
namespace {

#define NUM_DEVS 3

class BufRendezvousTest : public ::testing::Test {
 protected:
  BufRendezvousTest() {
    br_.reset(new BufRendezvous(123));
    fake_dev_ptr_ = reinterpret_cast<Device*>(512LLU);
    fake_dev_ctx_ = reinterpret_cast<DeviceContext*>(1024LLU);
    a_ = Tensor(DT_FLOAT, TensorShape({24}));
    b_ = Tensor(DT_FLOAT, TensorShape({24}));
  }

  Device* fake_dev_ptr_ = nullptr;
  DeviceContext* fake_dev_ctx_ = nullptr;
  Tensor a_;
  Tensor b_;
  AllocatorAttributes aa_;
  std::unique_ptr<BufRendezvous> br_;
};

TEST_F(BufRendezvousTest, CorrectUseProducerFirst) {
  Status prod_status;
  Status cons_status;
  bool prod_callback_called = false;
  bool cons_callback_called = false;
  Notification note;
  br_->ProvideBuf(
      "key0", fake_dev_ptr_, fake_dev_ctx_, &a_, aa_,
      [&note, &prod_status, &prod_callback_called](const Status& s) {
        prod_status = s;
        prod_callback_called = true;
        note.Notify();
      });
  EXPECT_FALSE(prod_callback_called);
  br_->ConsumeBuf("key0", [this, &cons_status, &cons_callback_called](
                              const Status& s, BufRendezvous::Hook* h) {
    cons_status = s;
    cons_callback_called = true;
    ASSERT_TRUE(h != nullptr);
    EXPECT_EQ(h->prod_dev, fake_dev_ptr_);
    EXPECT_EQ(h->prod_ctx, fake_dev_ctx_);
    EXPECT_EQ(h->prod_value, &a_);
    br_->DoneWithHook(h);
  });
  EXPECT_TRUE(cons_callback_called);
  note.WaitForNotification();
  EXPECT_TRUE(prod_callback_called);
  TF_EXPECT_OK(cons_status);
  TF_EXPECT_OK(prod_status);
}

TEST_F(BufRendezvousTest, CorrectUseConsumerFirst) {
  Status prod_status;
  Status cons_status;
  bool prod_callback_called = false;
  bool cons_callback_called = false;
  Notification note;
  br_->ConsumeBuf("key0", [this, &cons_status, &cons_callback_called](
                              const Status& s, BufRendezvous::Hook* h) {
    cons_status = s;
    cons_callback_called = true;
    ASSERT_TRUE(h != nullptr);
    EXPECT_EQ(h->prod_dev, fake_dev_ptr_);
    EXPECT_EQ(h->prod_ctx, fake_dev_ctx_);
    EXPECT_EQ(h->prod_value, &a_);
    br_->DoneWithHook(h);
  });
  EXPECT_FALSE(cons_callback_called);
  br_->ProvideBuf(
      "key0", fake_dev_ptr_, fake_dev_ctx_, &a_, aa_,
      [&note, &prod_status, &prod_callback_called](const Status& s) {
        prod_status = s;
        prod_callback_called = true;
        note.Notify();
      });
  EXPECT_TRUE(cons_callback_called);
  note.WaitForNotification();
  EXPECT_TRUE(prod_callback_called);
  TF_EXPECT_OK(cons_status);
  TF_EXPECT_OK(prod_status);
}

TEST_F(BufRendezvousTest, ErrorDuplicatePut) {
  bool prod_callback_called = false;
  br_->ProvideBuf("key0", fake_dev_ptr_, fake_dev_ctx_, &a_, aa_,
                  [this, &prod_callback_called](const Status& s) {
                    prod_callback_called = true;
                  });
  Status bad_status;
  Notification note;
  br_->ProvideBuf("key0", fake_dev_ptr_, fake_dev_ctx_, &a_, aa_,
                  [&bad_status, &note](const Status& s) {
                    bad_status = s;
                    note.Notify();
                  });
  note.WaitForNotification();
  EXPECT_FALSE(bad_status.ok());
  EXPECT_EQ("BufRendezvous::ProvideBuf already called for key key0",
            bad_status.error_message());
  EXPECT_FALSE(prod_callback_called);
  br_.reset();
}

TEST_F(BufRendezvousTest, ErrorDeleteNonEmpty) {
  Status cons_status;
  br_->ConsumeBuf(
      "key0", [this, &cons_status](const Status& s, BufRendezvous::Hook* h) {
        cons_status = s;
        EXPECT_EQ(h, nullptr);
      });
  EXPECT_TRUE(cons_status.ok());
  br_.reset();
  EXPECT_FALSE(cons_status.ok());
  EXPECT_EQ("Delete called on non-empty BufRendezvous",
            cons_status.error_message());
}

TEST_F(BufRendezvousTest, AbortNonEmpty) {
  Status cons_status;
  Status prod_status;
  Notification prod_note;
  Notification cons_note;
  br_->ConsumeBuf("key0", [this, &cons_note, &cons_status](
                              const Status& s, BufRendezvous::Hook* h) {
    cons_status = s;
    cons_note.Notify();
  });
  br_->ProvideBuf("key1", fake_dev_ptr_, fake_dev_ctx_, &a_, aa_,
                  [this, &prod_note, &prod_status](const Status& s) {
                    prod_status = s;
                    prod_note.Notify();
                  });
  br_->StartAbort(errors::Internal("Falling sky detected"));
  prod_note.WaitForNotification();
  cons_note.WaitForNotification();
  EXPECT_FALSE(prod_status.ok());
  EXPECT_EQ(prod_status.error_message(), "Falling sky detected");
  EXPECT_FALSE(cons_status.ok());
  EXPECT_EQ(cons_status.error_message(), "Falling sky detected");
}

TEST_F(BufRendezvousTest, AbortEmpty) {
  br_->StartAbort(errors::Internal("Falling sky detected"));
}

TEST_F(BufRendezvousTest, UseAfterAbort) {
  br_->StartAbort(errors::Internal("Falling sky detected"));
  Status cons_status;
  Status prod_status;
  Notification prod_note;
  Notification cons_note;
  br_->ConsumeBuf("key0", [this, &cons_note, &cons_status](
                              const Status& s, BufRendezvous::Hook* h) {
    cons_status = s;
    cons_note.Notify();
  });
  br_->ProvideBuf("key1", fake_dev_ptr_, fake_dev_ctx_, &a_, aa_,
                  [this, &prod_note, &prod_status](const Status& s) {
                    prod_status = s;
                    prod_note.Notify();
                  });
  prod_note.WaitForNotification();
  cons_note.WaitForNotification();
  EXPECT_FALSE(prod_status.ok());
  EXPECT_EQ(prod_status.error_message(), "Falling sky detected");
  EXPECT_FALSE(cons_status.ok());
  EXPECT_EQ(cons_status.error_message(), "Falling sky detected");
}

}  // namespace
}  // namespace tensorflow