Yuta HIGUCHI | 8f18219 | 2014-08-02 18:47:42 -0700 | [diff] [blame^] | 1 | package net.onrc.onos.core.util.distributed.sharedlog.example; |
| 2 | |
| 3 | import static org.junit.Assert.*; |
| 4 | |
| 5 | import java.util.UUID; |
| 6 | |
| 7 | import net.onrc.onos.core.datastore.hazelcast.HZClient; |
| 8 | import net.onrc.onos.core.util.IntegrationTest; |
| 9 | import net.onrc.onos.core.util.TestUtils; |
| 10 | import net.onrc.onos.core.util.distributed.sharedlog.hazelcast.HazelcastRuntime; |
| 11 | import net.onrc.onos.core.util.distributed.sharedlog.hazelcast.HazelcastSequencerRuntime; |
| 12 | import net.onrc.onos.core.util.distributed.sharedlog.runtime.LogBasedRuntime; |
| 13 | import net.onrc.onos.core.util.distributed.sharedlog.runtime.SequencerRuntime; |
| 14 | |
| 15 | import org.junit.Before; |
| 16 | import org.junit.Test; |
| 17 | import org.junit.experimental.categories.Category; |
| 18 | |
| 19 | import com.hazelcast.core.HazelcastInstance; |
| 20 | |
| 21 | /** |
| 22 | * Unit test to run LogAtomicLong example. |
| 23 | */ |
| 24 | public class LogAtomicLongTest { |
| 25 | |
| 26 | static { |
| 27 | // configuration to quickly fall back to instance mode for faster test run |
| 28 | System.setProperty("net.onrc.onos.core.datastore.hazelcast.client.attemptLimit", "0"); |
| 29 | } |
| 30 | |
| 31 | |
| 32 | private LogAtomicLong along; |
| 33 | |
| 34 | /** |
| 35 | * Create LogAtomicLong instance. |
| 36 | */ |
| 37 | @Before |
| 38 | public void setUp() { |
| 39 | final HZClient cl = HZClient.getClient(); |
| 40 | HazelcastInstance hz = TestUtils.callMethod(cl, "getHZInstance", null); |
| 41 | |
| 42 | SequencerRuntime sequencerRuntime = new HazelcastSequencerRuntime(hz); |
| 43 | LogBasedRuntime runtime = new HazelcastRuntime(hz, sequencerRuntime); |
| 44 | |
| 45 | String counterName = UUID.randomUUID().toString(); |
| 46 | along = new LogAtomicLong(runtime, counterName); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Test unconditional write. |
| 51 | */ |
| 52 | @Test |
| 53 | public void testSet() { |
| 54 | along.set(42); |
| 55 | assertEquals(42, along.get()); |
| 56 | along.set(0); |
| 57 | assertEquals(0, along.get()); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Test conditional write. |
| 62 | */ |
| 63 | @Test |
| 64 | public void testCompareAndSet() { |
| 65 | along.set(42); |
| 66 | assertEquals(42, along.get()); |
| 67 | |
| 68 | along.compareAndSet(42, 43); |
| 69 | assertEquals(43, along.get()); |
| 70 | |
| 71 | // should remain unchanged if expectation not met |
| 72 | along.compareAndSet(42, 45); |
| 73 | assertEquals(43, along.get()); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Confirm initial value is 0. |
| 78 | */ |
| 79 | @Test |
| 80 | public void testGet() { |
| 81 | assertEquals(0, along.get()); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Confirm another instance with same ID observes the same value. |
| 86 | */ |
| 87 | @Test |
| 88 | public void testOtherInstance() { |
| 89 | along.set(42); |
| 90 | assertEquals(42, along.get()); |
| 91 | |
| 92 | final HZClient cl = HZClient.getClient(); |
| 93 | HazelcastInstance hz = TestUtils.callMethod(cl, "getHZInstance", null); |
| 94 | |
| 95 | SequencerRuntime sequencerRuntime = new HazelcastSequencerRuntime(hz); |
| 96 | LogBasedRuntime runtime = new HazelcastRuntime(hz, sequencerRuntime); |
| 97 | |
| 98 | LogAtomicLong anotherInstance = new LogAtomicLong(runtime, |
| 99 | along.getObjectID()); |
| 100 | assertEquals(42, anotherInstance.get()); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Confirm another instance with same ID initializes using snapshot. |
| 105 | */ |
| 106 | @Category(IntegrationTest.class) |
| 107 | @Test |
| 108 | public void testOtherInstanceFromSnapshot() { |
| 109 | along.set(42); |
| 110 | assertEquals(42, along.get()); |
| 111 | |
| 112 | final HZClient cl = HZClient.getClient(); |
| 113 | HazelcastInstance hz = TestUtils.callMethod(cl, "getHZInstance", null); |
| 114 | |
| 115 | SequencerRuntime sequencerRuntime = new HazelcastSequencerRuntime(hz); |
| 116 | LogBasedRuntime runtime = new HazelcastRuntime(hz, sequencerRuntime); |
| 117 | |
| 118 | // FIXME SNAPSHOT_INTERVAL should be customized to smaller values |
| 119 | // write multiple times to trigger snapshot |
| 120 | for (int i = 0; i < HazelcastRuntime.SNAPSHOT_INTERVAL + 2; ++i) { |
| 121 | along.set(i); |
| 122 | } |
| 123 | along.set(99); |
| 124 | Thread.yield(); |
| 125 | |
| 126 | // this instance might start from latest snap shot then replay |
| 127 | LogAtomicLong anotherInstance2 = new LogAtomicLong(runtime, |
| 128 | along.getObjectID()); |
| 129 | assertEquals(99, anotherInstance2.get()); |
| 130 | |
| 131 | } |
| 132 | } |