ONOS-2330: Fixes bug in MapValue.isNewerThan
Change-Id: I2a1bb9299e69254754d4ad6b500a8be0b511211a
Showing
2 changed files
with
127 additions
and
1 deletions
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
1 | package org.onosproject.store.ecmap; | 16 | package org.onosproject.store.ecmap; |
2 | 17 | ||
3 | import org.onosproject.store.Timestamp; | 18 | import org.onosproject.store.Timestamp; |
19 | + | ||
4 | import com.google.common.base.MoreObjects; | 20 | import com.google.common.base.MoreObjects; |
21 | +import com.google.common.base.Objects; | ||
5 | 22 | ||
6 | /** | 23 | /** |
7 | * Representation of a value in EventuallyConsistentMap. | 24 | * Representation of a value in EventuallyConsistentMap. |
... | @@ -54,7 +71,7 @@ public class MapValue<V> implements Comparable<MapValue<V>> { | ... | @@ -54,7 +71,7 @@ public class MapValue<V> implements Comparable<MapValue<V>> { |
54 | } | 71 | } |
55 | 72 | ||
56 | public boolean isNewerThan(Timestamp timestamp) { | 73 | public boolean isNewerThan(Timestamp timestamp) { |
57 | - return timestamp.isNewerThan(timestamp); | 74 | + return this.timestamp.isNewerThan(timestamp); |
58 | } | 75 | } |
59 | 76 | ||
60 | public Digest digest() { | 77 | public Digest digest() { |
... | @@ -62,6 +79,21 @@ public class MapValue<V> implements Comparable<MapValue<V>> { | ... | @@ -62,6 +79,21 @@ public class MapValue<V> implements Comparable<MapValue<V>> { |
62 | } | 79 | } |
63 | 80 | ||
64 | @Override | 81 | @Override |
82 | + public int hashCode() { | ||
83 | + return Objects.hashCode(timestamp, value); | ||
84 | + } | ||
85 | + | ||
86 | + @Override | ||
87 | + public boolean equals(Object other) { | ||
88 | + if (other instanceof MapValue) { | ||
89 | + MapValue<V> that = (MapValue) other; | ||
90 | + return Objects.equal(this.timestamp, that.timestamp) && | ||
91 | + Objects.equal(this.value, that.value); | ||
92 | + } | ||
93 | + return false; | ||
94 | + } | ||
95 | + | ||
96 | + @Override | ||
65 | public String toString() { | 97 | public String toString() { |
66 | return MoreObjects.toStringHelper(getClass()) | 98 | return MoreObjects.toStringHelper(getClass()) |
67 | .add("timestamp", timestamp) | 99 | .add("timestamp", timestamp) |
... | @@ -100,6 +132,21 @@ public class MapValue<V> implements Comparable<MapValue<V>> { | ... | @@ -100,6 +132,21 @@ public class MapValue<V> implements Comparable<MapValue<V>> { |
100 | } | 132 | } |
101 | 133 | ||
102 | @Override | 134 | @Override |
135 | + public int hashCode() { | ||
136 | + return Objects.hashCode(timestamp, isTombstone); | ||
137 | + } | ||
138 | + | ||
139 | + @Override | ||
140 | + public boolean equals(Object other) { | ||
141 | + if (other instanceof Digest) { | ||
142 | + Digest that = (Digest) other; | ||
143 | + return Objects.equal(this.timestamp, that.timestamp) && | ||
144 | + Objects.equal(this.isTombstone, that.isTombstone); | ||
145 | + } | ||
146 | + return false; | ||
147 | + } | ||
148 | + | ||
149 | + @Override | ||
103 | public String toString() { | 150 | public String toString() { |
104 | return MoreObjects.toStringHelper(getClass()) | 151 | return MoreObjects.toStringHelper(getClass()) |
105 | .add("timestamp", timestamp) | 152 | .add("timestamp", timestamp) | ... | ... |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.store.ecmap; | ||
17 | + | ||
18 | +import static org.junit.Assert.assertEquals; | ||
19 | +import static org.junit.Assert.assertFalse; | ||
20 | +import static org.junit.Assert.assertNull; | ||
21 | +import static org.junit.Assert.assertTrue; | ||
22 | + | ||
23 | +import org.junit.Test; | ||
24 | +import org.onosproject.store.Timestamp; | ||
25 | +import org.onosproject.store.ecmap.MapValue.Digest; | ||
26 | +import org.onosproject.store.impl.LogicalTimestamp; | ||
27 | + | ||
28 | +/** | ||
29 | + * Unit tests for MapValue. | ||
30 | + */ | ||
31 | +public class MapValueTest { | ||
32 | + | ||
33 | + @Test | ||
34 | + public void testConstruction() { | ||
35 | + Timestamp ts = new LogicalTimestamp(10); | ||
36 | + MapValue<String> mv = new MapValue<>("foo", ts); | ||
37 | + assertEquals("foo", mv.get()); | ||
38 | + assertEquals(ts, mv.timestamp()); | ||
39 | + assertTrue(mv.isAlive()); | ||
40 | + } | ||
41 | + | ||
42 | + @Test | ||
43 | + public void testDigest() { | ||
44 | + Timestamp ts = new LogicalTimestamp(10); | ||
45 | + MapValue<String> mv = new MapValue<>("foo", ts); | ||
46 | + Digest actual = mv.digest(); | ||
47 | + Digest expected = new MapValue.Digest(ts, false); | ||
48 | + assertEquals(actual, expected); | ||
49 | + } | ||
50 | + | ||
51 | + @Test | ||
52 | + public void testComparison() { | ||
53 | + Timestamp ts1 = new LogicalTimestamp(9); | ||
54 | + Timestamp ts2 = new LogicalTimestamp(10); | ||
55 | + Timestamp ts3 = new LogicalTimestamp(11); | ||
56 | + MapValue<String> mv1 = new MapValue<>("foo", ts1); | ||
57 | + MapValue<String> mv2 = new MapValue<>("foo", ts2); | ||
58 | + MapValue<String> mv3 = new MapValue<>("foo", ts3); | ||
59 | + assertTrue(mv2.isNewerThan(mv1)); | ||
60 | + assertFalse(mv1.isNewerThan(mv3)); | ||
61 | + | ||
62 | + assertTrue(mv3.isNewerThan(ts2)); | ||
63 | + assertFalse(mv1.isNewerThan(ts2)); | ||
64 | + | ||
65 | + assertTrue(mv1.compareTo(mv2) < 0); | ||
66 | + assertTrue(mv1.compareTo(mv1) == 0); | ||
67 | + assertTrue(mv3.compareTo(mv2) > 0); | ||
68 | + } | ||
69 | + | ||
70 | + @Test | ||
71 | + public void testTombstone() { | ||
72 | + Timestamp ts1 = new LogicalTimestamp(9); | ||
73 | + MapValue<String> mv = MapValue.tombstone(ts1); | ||
74 | + assertTrue(mv.isTombstone()); | ||
75 | + assertFalse(mv.isAlive()); | ||
76 | + assertNull(mv.get()); | ||
77 | + assertEquals(ts1, mv.timestamp()); | ||
78 | + } | ||
79 | +} |
-
Please register or login to post a comment