Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
promise-test.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <boost/thread.hpp>
16 #include <gtest/gtest.h>
18 #include <sys/resource.h>
19 
20 #include "util/promise.h"
21 #include "util/time.h"
22 
23 #include "common/names.h"
24 
25 namespace impala {
26 
28  public:
30  getrlimit(RLIMIT_CORE, &limit_before_);
31  rlimit limit;
32  limit.rlim_cur = limit.rlim_max = 0;
33  setrlimit(RLIMIT_CORE, &limit);
34  }
35 
37  setrlimit(RLIMIT_CORE, &limit_before_);
38  }
39 
40  private:
41  rlimit limit_before_;
42 };
43 
44 void RunThread(Promise<int64_t>* promise) {
45  promise->Set(100);
46 }
47 
48 TEST(PromiseTest, BasicTest) {
49  Promise<int64_t> promise;
50  thread promise_setter(RunThread, &promise);
51 
52  DCHECK_EQ(promise.Get(), 100);
53 }
54 
55 TEST(PromiseTest, TimeoutTest) {
56  // Test that the promise can be fulfilled by setting a value.
57  bool timed_out = true;
58  Promise<int64_t> fulfilled_promise;
59  thread promise_setter(RunThread, &fulfilled_promise);
60  DCHECK_EQ(fulfilled_promise.Get(10000, &timed_out), 100);
61  DCHECK_EQ(timed_out, false);
62 
63  // Test that the promise times out properly.
64  int64_t start_time, end_time;
65  start_time = MonotonicMillis();
66  timed_out = false;
67  Promise<int64_t> timedout_promise;
68  timedout_promise.Get(1000, &timed_out);
69  DCHECK_EQ(timed_out, true);
70  end_time = MonotonicMillis();
71  DCHECK_GE(end_time - start_time, 1000);
72 }
73 
74 TEST(PromiseDeathTest, RepeatedSetTest) {
75  // This test intentionally causes a crash. Don't generate core files for it.
76  ScopedLimitResetter resetter;
77 
78  // Hint to gtest that only one thread is being used here. Multiple threads are unsafe
79  // for 'death' tests, see
80  // https://code.google.com/p/googletest/wiki/AdvancedGuide#Death_Tests for more detail
81  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
82  Promise<int64_t> promise;
83  promise.Set(100);
84  ASSERT_DEATH(promise.Set(150), "Called Set\\(\\.\\.\\) twice on the same Promise");
85 }
86 
87 }
88 
89 int main(int argc, char **argv) {
90  google::InitGoogleLogging(argv[0]);
91  ::testing::InitGoogleTest(&argc, argv);
92  return RUN_ALL_TESTS();
93 }
void Set(const T &val)
Definition: promise.h:38
TEST(AtomicTest, Basic)
Definition: atomic-test.cc:28
void RunThread(Promise< int64_t > *promise)
Definition: promise-test.cc:44
int main(int argc, char **argv)
Definition: promise-test.cc:89
int64_t MonotonicMillis()
Definition: time.h:35
const T & Get()
Definition: promise.h:59