Yuta HIGUCHI | 6a64313 | 2014-03-18 22:39:27 -0700 | [diff] [blame^] | 1 | package net.onrc.onos.datastore.hazelcast; |
| 2 | |
| 3 | import static org.junit.Assert.*; |
| 4 | |
| 5 | import java.nio.charset.StandardCharsets; |
| 6 | import java.util.Map; |
| 7 | import java.util.TreeMap; |
| 8 | |
| 9 | import net.onrc.onos.datastore.IKVTable.IKVEntry; |
| 10 | import net.onrc.onos.datastore.ObjectDoesntExistException; |
| 11 | import net.onrc.onos.datastore.ObjectExistsException; |
| 12 | import net.onrc.onos.datastore.WrongVersionException; |
| 13 | import net.onrc.onos.datastore.hazelcast.HZTable.VersionedValue; |
| 14 | import net.onrc.onos.datastore.utils.ByteArrayComparator; |
| 15 | |
| 16 | import org.junit.After; |
| 17 | import org.junit.Before; |
| 18 | import org.junit.Ignore; |
| 19 | import org.junit.Rule; |
| 20 | import org.junit.Test; |
| 21 | import org.junit.rules.TestName; |
| 22 | |
| 23 | public class HZTableTest { |
| 24 | @Rule |
| 25 | public TestName name= new TestName(); |
| 26 | |
| 27 | static final String TEST_TABLE_NAME = "TableForUnitTest"; |
| 28 | HZTable table; |
| 29 | |
| 30 | @Before |
| 31 | public void setUp() throws Exception { |
| 32 | table = (HZTable) HZClient.getClient().getTable(TEST_TABLE_NAME); |
| 33 | } |
| 34 | |
| 35 | @After |
| 36 | public void tearDown() throws Exception { |
| 37 | HZClient.getClient().dropTable(table); |
| 38 | } |
| 39 | |
| 40 | public void assertEntryInTable(final byte[] key, final byte[] value, final long version) { |
| 41 | VersionedValue valueblob = table.getBackendMap().get(key); |
| 42 | assertNotNull(valueblob); |
| 43 | assertArrayEquals(value, valueblob.getValue()); |
| 44 | assertEquals(version, valueblob.getVersion()); |
| 45 | } |
| 46 | |
| 47 | public void assertKeyNotInTable(final byte[] key) { |
| 48 | VersionedValue valueblob = table.getBackendMap().get(key); |
| 49 | assertNull(valueblob); |
| 50 | } |
| 51 | |
| 52 | @Test |
| 53 | public void testGetInitialVersion() { |
| 54 | final long version1 = HZTable.getInitialVersion(); |
| 55 | assertNotEquals(HZClient.VERSION_NONEXISTENT,version1); |
| 56 | |
| 57 | final long version2 = HZTable.getInitialVersion(); |
| 58 | assertNotEquals(HZClient.VERSION_NONEXISTENT, version2); |
| 59 | assertNotEquals(version1, version2); |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void testGetNextVersion() { |
| 64 | final long nextVersion = HZTable.getNextVersion(1); |
| 65 | assertNotEquals(nextVersion, HZClient.VERSION_NONEXISTENT); |
| 66 | assertNotEquals(nextVersion, 1); |
| 67 | |
| 68 | final long nextVersion1 = HZTable.getNextVersion(Long.MAX_VALUE); |
| 69 | assertNotEquals(nextVersion1, HZClient.VERSION_NONEXISTENT); |
| 70 | assertNotEquals(nextVersion1, Long.MAX_VALUE); |
| 71 | |
| 72 | final long nextVersion11 = HZTable.getNextVersion(HZClient.VERSION_NONEXISTENT-1); |
| 73 | assertNotEquals(nextVersion11, HZClient.VERSION_NONEXISTENT); |
| 74 | assertNotEquals(nextVersion11, HZClient.VERSION_NONEXISTENT-1); |
| 75 | } |
| 76 | |
| 77 | @Ignore // nothing to test for now |
| 78 | @Test |
| 79 | public void testHZTable() { |
| 80 | fail("Not yet implemented"); |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void testGetTableName() { |
| 85 | assertEquals(TEST_TABLE_NAME, table.getTableName()); |
| 86 | } |
| 87 | |
| 88 | @Test |
| 89 | public void testVERSION_NONEXISTENT() { |
| 90 | assertEquals(HZClient.VERSION_NONEXISTENT, table.VERSION_NONEXISTENT()); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testGetTableId() { |
| 95 | // for Hazelcast implementation IKVTableID is table itself |
| 96 | assertEquals(table, table.getTableId()); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void testCreate() throws ObjectExistsException { |
| 101 | final byte[] key = name.getMethodName().getBytes(StandardCharsets.UTF_8); |
| 102 | final byte[] value = "SomeValue".getBytes(StandardCharsets.UTF_8); |
| 103 | |
| 104 | final long version = table.create(key, value); |
| 105 | assertNotEquals(HZClient.VERSION_NONEXISTENT,version); |
| 106 | |
| 107 | assertEntryInTable(key, value, version); |
| 108 | } |
| 109 | |
| 110 | @Test(expected = ObjectExistsException.class) |
| 111 | public void testCreateConflict() throws ObjectExistsException { |
| 112 | final byte[] key = name.getMethodName().getBytes(StandardCharsets.UTF_8); |
| 113 | final byte[] value = "SomeValue".getBytes(StandardCharsets.UTF_8); |
| 114 | |
| 115 | final long version = table.create(key, value); |
| 116 | assertNotEquals(HZClient.VERSION_NONEXISTENT,version); |
| 117 | |
| 118 | assertEntryInTable(key, value, version); |
| 119 | |
| 120 | table.create(key, value); |
| 121 | fail("Should have thrown exception"); |
| 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void testForceCreate() { |
| 126 | final byte[] key = name.getMethodName().getBytes(StandardCharsets.UTF_8); |
| 127 | final byte[] value = "SomeValue".getBytes(StandardCharsets.UTF_8); |
| 128 | |
| 129 | final long version = table.forceCreate(key, value); |
| 130 | assertNotEquals(HZClient.VERSION_NONEXISTENT,version); |
| 131 | assertEntryInTable(key, value, version); |
| 132 | |
| 133 | |
| 134 | final long version1 = table.forceCreate(key, value); |
| 135 | assertNotEquals(HZClient.VERSION_NONEXISTENT,version1); |
| 136 | assertNotEquals(version, version1); |
| 137 | assertEntryInTable(key, value, version1); |
| 138 | } |
| 139 | |
| 140 | @Test |
| 141 | public void testRead() throws ObjectDoesntExistException { |
| 142 | final byte[] key = name.getMethodName().getBytes(StandardCharsets.UTF_8); |
| 143 | final byte[] value = "SomeValue".getBytes(StandardCharsets.UTF_8); |
| 144 | |
| 145 | // put data to read |
| 146 | final long version = table.forceCreate(key, value); |
| 147 | assertNotEquals(HZClient.VERSION_NONEXISTENT,version); |
| 148 | assertEntryInTable(key, value, version); |
| 149 | |
| 150 | // test body |
| 151 | final IKVEntry readValue = table.read(key); |
| 152 | assertArrayEquals(key, readValue.getKey()); |
| 153 | assertArrayEquals(value, readValue.getValue()); |
| 154 | assertEquals(version, readValue.getVersion()); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | @Test(expected = ObjectDoesntExistException.class) |
| 159 | public void testReadNotExist() throws ObjectDoesntExistException { |
| 160 | final byte[] key = name.getMethodName().getBytes(StandardCharsets.UTF_8); |
| 161 | |
| 162 | table.read(key); |
| 163 | fail("Should have thrown exception"); |
| 164 | } |
| 165 | |
| 166 | @Test |
| 167 | public void testUpdateByteArrayByteArrayLongSuccess() throws ObjectDoesntExistException, WrongVersionException { |
| 168 | final byte[] key = name.getMethodName().getBytes(StandardCharsets.UTF_8); |
| 169 | final byte[] oldValue = "OldValue".getBytes(StandardCharsets.UTF_8); |
| 170 | final byte[] value = "SomeValue".getBytes(StandardCharsets.UTF_8); |
| 171 | |
| 172 | // put data to update |
| 173 | final long oldVersion = table.forceCreate(key, oldValue); |
| 174 | assertNotEquals(HZClient.VERSION_NONEXISTENT,oldVersion); |
| 175 | assertEntryInTable(key, oldValue, oldVersion); |
| 176 | |
| 177 | final long version = table.update(key, value, oldVersion); |
| 178 | assertNotEquals(HZClient.VERSION_NONEXISTENT,version); |
| 179 | assertEntryInTable(key, value, version); |
| 180 | } |
| 181 | |
| 182 | @Test(expected = ObjectDoesntExistException.class) |
| 183 | public void testUpdateByteArrayByteArrayLongFailNoOldValue() throws ObjectDoesntExistException, WrongVersionException { |
| 184 | final byte[] key = name.getMethodName().getBytes(StandardCharsets.UTF_8); |
| 185 | final byte[] value = "SomeValue".getBytes(StandardCharsets.UTF_8); |
| 186 | |
| 187 | final long oldVersion = 0xDEAD; |
| 188 | |
| 189 | final long version = table.update(key, value, oldVersion); |
| 190 | assertNotEquals(HZClient.VERSION_NONEXISTENT,version); |
| 191 | assertEntryInTable(key, value, version); |
| 192 | } |
| 193 | |
| 194 | @Test(expected = WrongVersionException.class) |
| 195 | public void testUpdateByteArrayByteArrayLongFailWrongVersion() throws ObjectDoesntExistException, WrongVersionException { |
| 196 | final byte[] key = name.getMethodName().getBytes(StandardCharsets.UTF_8); |
| 197 | final byte[] oldValue = "OldValue".getBytes(StandardCharsets.UTF_8); |
| 198 | final byte[] value = "SomeValue".getBytes(StandardCharsets.UTF_8); |
| 199 | |
| 200 | // put data to update |
| 201 | final long oldVersion = table.forceCreate(key, oldValue); |
| 202 | assertNotEquals(HZClient.VERSION_NONEXISTENT,oldVersion); |
| 203 | assertEntryInTable(key, oldValue, oldVersion); |
| 204 | // some one updates (from different thread/process in reality) |
| 205 | table.forceCreate(key, oldValue); |
| 206 | assertNotEquals(HZClient.VERSION_NONEXISTENT,oldVersion); |
| 207 | |
| 208 | |
| 209 | table.update(key, value, oldVersion); |
| 210 | fail("Should have thrown exception"); |
| 211 | } |
| 212 | |
| 213 | @Test |
| 214 | public void testUpdateByteArrayByteArraySuccess() throws ObjectDoesntExistException { |
| 215 | final byte[] key = name.getMethodName().getBytes(StandardCharsets.UTF_8); |
| 216 | final byte[] oldValue = "OldValue".getBytes(StandardCharsets.UTF_8); |
| 217 | final byte[] value = "SomeValue".getBytes(StandardCharsets.UTF_8); |
| 218 | |
| 219 | // put data to update |
| 220 | final long oldVersion = table.forceCreate(key, oldValue); |
| 221 | assertNotEquals(HZClient.VERSION_NONEXISTENT,oldVersion); |
| 222 | assertEntryInTable(key, oldValue, oldVersion); |
| 223 | |
| 224 | final long version = table.update(key, value); |
| 225 | assertNotEquals(HZClient.VERSION_NONEXISTENT,version); |
| 226 | assertEntryInTable(key, value, version); |
| 227 | } |
| 228 | |
| 229 | @Test(expected = ObjectDoesntExistException.class) |
| 230 | public void testUpdateByteArrayByteArrayFailNoOldValue() throws ObjectDoesntExistException, WrongVersionException { |
| 231 | final byte[] key = name.getMethodName().getBytes(StandardCharsets.UTF_8); |
| 232 | final byte[] value = "SomeValue".getBytes(StandardCharsets.UTF_8); |
| 233 | |
| 234 | final long version = table.update(key, value); |
| 235 | assertNotEquals(HZClient.VERSION_NONEXISTENT,version); |
| 236 | assertEntryInTable(key, value, version); |
| 237 | fail("Should have thrown exception"); |
| 238 | } |
| 239 | |
| 240 | @Test |
| 241 | public void testUpdateByteArrayByteArraySuccessIgnoreVersion() throws ObjectDoesntExistException, WrongVersionException { |
| 242 | final byte[] key = name.getMethodName().getBytes(StandardCharsets.UTF_8); |
| 243 | final byte[] oldValue = "OldValue".getBytes(StandardCharsets.UTF_8); |
| 244 | final byte[] value = "SomeValue".getBytes(StandardCharsets.UTF_8); |
| 245 | |
| 246 | // put data to update |
| 247 | final long oldVersion = table.forceCreate(key, oldValue); |
| 248 | assertNotEquals(HZClient.VERSION_NONEXISTENT,oldVersion); |
| 249 | assertEntryInTable(key, oldValue, oldVersion); |
| 250 | // someone updates (from different thread/process in reality) |
| 251 | table.forceCreate(key, oldValue); |
| 252 | assertNotEquals(HZClient.VERSION_NONEXISTENT,oldVersion); |
| 253 | |
| 254 | |
| 255 | final long version = table.update(key, value); |
| 256 | assertNotEquals(HZClient.VERSION_NONEXISTENT,version); |
| 257 | assertEntryInTable(key, value, version); |
| 258 | } |
| 259 | |
| 260 | @Test |
| 261 | public void testDelete() throws ObjectDoesntExistException, WrongVersionException { |
| 262 | final byte[] key = name.getMethodName().getBytes(StandardCharsets.UTF_8); |
| 263 | final byte[] oldValue = "OldValue".getBytes(StandardCharsets.UTF_8); |
| 264 | |
| 265 | // put data to delete |
| 266 | final long oldVersion = table.forceCreate(key, oldValue); |
| 267 | assertNotEquals(HZClient.VERSION_NONEXISTENT,oldVersion); |
| 268 | assertEntryInTable(key, oldValue, oldVersion); |
| 269 | |
| 270 | long version = table.delete(key, oldVersion); |
| 271 | assertNotEquals(HZClient.VERSION_NONEXISTENT,oldVersion); |
| 272 | assertEquals(oldVersion, version); |
| 273 | assertKeyNotInTable(key); |
| 274 | } |
| 275 | |
| 276 | @Test(expected = ObjectDoesntExistException.class) |
| 277 | public void testDeleteFailNoEntry() throws ObjectDoesntExistException, WrongVersionException { |
| 278 | final byte[] key = name.getMethodName().getBytes(StandardCharsets.UTF_8); |
| 279 | |
| 280 | final long oldVersion = 0xDEAD; |
| 281 | |
| 282 | try { |
| 283 | table.delete(key, oldVersion); |
| 284 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 285 | assertKeyNotInTable(key); |
| 286 | throw e; |
| 287 | } |
| 288 | fail("Should have thrown exception"); |
| 289 | } |
| 290 | |
| 291 | @Test(expected = WrongVersionException.class) |
| 292 | public void testDeleteFailWrongVersion() throws ObjectDoesntExistException, WrongVersionException { |
| 293 | final byte[] key = name.getMethodName().getBytes(StandardCharsets.UTF_8); |
| 294 | final byte[] oldValue = "OldValue".getBytes(StandardCharsets.UTF_8); |
| 295 | |
| 296 | // put data to delete |
| 297 | final long oldVersion = table.forceCreate(key, oldValue); |
| 298 | assertNotEquals(HZClient.VERSION_NONEXISTENT,oldVersion); |
| 299 | assertEntryInTable(key, oldValue, oldVersion); |
| 300 | // someone updates (from different thread/process in reality) |
| 301 | final long latestVersion = table.forceCreate(key, oldValue); |
| 302 | assertNotEquals(HZClient.VERSION_NONEXISTENT,latestVersion); |
| 303 | |
| 304 | try { |
| 305 | table.delete(key, oldVersion); |
| 306 | } catch (ObjectDoesntExistException | WrongVersionException e) { |
| 307 | assertEntryInTable(key, oldValue, latestVersion); |
| 308 | throw e; |
| 309 | } |
| 310 | fail("Should have thrown exception"); |
| 311 | } |
| 312 | |
| 313 | |
| 314 | |
| 315 | @Test |
| 316 | public void testForceDelete() { |
| 317 | final byte[] key = name.getMethodName().getBytes(StandardCharsets.UTF_8); |
| 318 | final byte[] oldValue = "OldValue".getBytes(StandardCharsets.UTF_8); |
| 319 | |
| 320 | // put data to delete |
| 321 | final long oldVersion = table.forceCreate(key, oldValue); |
| 322 | assertNotEquals(HZClient.VERSION_NONEXISTENT,oldVersion); |
| 323 | assertEntryInTable(key, oldValue, oldVersion); |
| 324 | |
| 325 | long version = table.forceDelete(key); |
| 326 | assertNotEquals(HZClient.VERSION_NONEXISTENT,oldVersion); |
| 327 | assertEquals(oldVersion, version); |
| 328 | assertKeyNotInTable(key); |
| 329 | } |
| 330 | |
| 331 | @Test |
| 332 | public void testGetAllEntries() { |
| 333 | final int DATASETSIZE = 100; |
| 334 | final Map<byte[], VersionedValue> testdata = new TreeMap<>(ByteArrayComparator.BYTEARRAY_COMPARATOR); |
| 335 | for(int i = 0 ; i < DATASETSIZE ; ++i) { |
| 336 | final byte[] key = (name.getMethodName()+i).getBytes(StandardCharsets.UTF_8); |
| 337 | final byte[] value = ("Value"+i).getBytes(StandardCharsets.UTF_8); |
| 338 | |
| 339 | // put data to delete |
| 340 | final long version = table.forceCreate(key, value); |
| 341 | assertNotEquals(HZClient.VERSION_NONEXISTENT, version); |
| 342 | assertEntryInTable(key, value, version); |
| 343 | |
| 344 | testdata.put(key, new VersionedValue(value, version)); |
| 345 | } |
| 346 | |
| 347 | Iterable<IKVEntry> datastore = table.getAllEntries(); |
| 348 | for( IKVEntry entry : datastore ) { |
| 349 | VersionedValue expectedValue = testdata.get(entry.getKey()); |
| 350 | assertNotNull(expectedValue); |
| 351 | assertArrayEquals(expectedValue.getValue(), entry.getValue()); |
| 352 | assertEquals(expectedValue.getVersion(), entry.getVersion()); |
| 353 | |
| 354 | testdata.remove(entry.getKey()); |
| 355 | } |
| 356 | |
| 357 | assertTrue(testdata.isEmpty()); |
| 358 | } |
| 359 | |
| 360 | @Test |
| 361 | public void testToString() { |
| 362 | assertEquals("[HZTable " + TEST_TABLE_NAME + "]", table.toString()); |
| 363 | } |
| 364 | |
| 365 | } |