/Users/deen/code/yugabyte-db/src/yb/server/logical_clock-test.cc
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #include <glog/logging.h> |
34 | | #include <gtest/gtest.h> |
35 | | |
36 | | #include "yb/server/logical_clock.h" |
37 | | #include "yb/util/monotime.h" |
38 | | #include "yb/util/test_util.h" |
39 | | |
40 | | namespace yb { |
41 | | namespace server { |
42 | | |
43 | | class LogicalClockTest : public YBTest { |
44 | | public: |
45 | | LogicalClockTest() |
46 | 3 | : clock_(LogicalClock::CreateStartingAt(HybridTime::kInitial)) { |
47 | 3 | } |
48 | | |
49 | | protected: |
50 | | scoped_refptr<LogicalClock> clock_; |
51 | | }; |
52 | | |
53 | | // Test that two subsequent time reads are monotonically increasing. |
54 | 1 | TEST_F(LogicalClockTest, TestNow_ValuesIncreaseMonotonically) { |
55 | 1 | const HybridTime now1 = clock_->Now(); |
56 | 1 | const HybridTime now2 = clock_->Now(); |
57 | 1 | ASSERT_EQ(now1.value() + 1, now2.value()); |
58 | 1 | } |
59 | | |
60 | | // Tests that the clock gets updated if the incoming value is higher. |
61 | 1 | TEST_F(LogicalClockTest, TestUpdate_LogicalValueIncreasesByAmount) { |
62 | 1 | HybridTime initial = clock_->Now(); |
63 | 1 | HybridTime future(initial.value() + 10); |
64 | 1 | clock_->Update(future); |
65 | 1 | HybridTime now = clock_->Now(); |
66 | | // now should be 1 after future |
67 | 1 | ASSERT_EQ(initial.value() + 11, now.value()); |
68 | 1 | } |
69 | | |
70 | | // Tests that the clock doesn't get updated if the incoming value is lower. |
71 | 1 | TEST_F(LogicalClockTest, TestUpdate_LogicalValueDoesNotIncrease) { |
72 | 1 | HybridTime ht(1); |
73 | | // update the clock to 1, the initial value, should do nothing |
74 | 1 | clock_->Update(ht); |
75 | 1 | HybridTime now = clock_->Now(); |
76 | 1 | ASSERT_EQ(now.value(), 2); |
77 | 1 | } |
78 | | |
79 | | } // namespace server |
80 | | } // namespace yb |
81 | | |