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 | |
Yuta HIGUCHI | 8f18219 | 2014-08-02 18:47:42 -0700 | [diff] [blame] | 26 | private LogAtomicLong along; |
| 27 | |
| 28 | /** |
| 29 | * Create LogAtomicLong instance. |
| 30 | */ |
| 31 | @Before |
| 32 | public void setUp() { |
| 33 | final HZClient cl = HZClient.getClient(); |
| 34 | HazelcastInstance hz = TestUtils.callMethod(cl, "getHZInstance", null); |
| 35 | |
| 36 | SequencerRuntime sequencerRuntime = new HazelcastSequencerRuntime(hz); |
| 37 | LogBasedRuntime runtime = new HazelcastRuntime(hz, sequencerRuntime); |
| 38 | |
| 39 | String counterName = UUID.randomUUID().toString(); |
| 40 | along = new LogAtomicLong(runtime, counterName); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Test unconditional write. |
| 45 | */ |
| 46 | @Test |
| 47 | public void testSet() { |
| 48 | along.set(42); |
| 49 | assertEquals(42, along.get()); |
| 50 | along.set(0); |
| 51 | assertEquals(0, along.get()); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Test conditional write. |
| 56 | */ |
| 57 | @Test |
| 58 | public void testCompareAndSet() { |
| 59 | along.set(42); |
| 60 | assertEquals(42, along.get()); |
| 61 | |
| 62 | along.compareAndSet(42, 43); |
| 63 | assertEquals(43, along.get()); |
| 64 | |
| 65 | // should remain unchanged if expectation not met |
| 66 | along.compareAndSet(42, 45); |
| 67 | assertEquals(43, along.get()); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Confirm initial value is 0. |
| 72 | */ |
| 73 | @Test |
| 74 | public void testGet() { |
| 75 | assertEquals(0, along.get()); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Confirm another instance with same ID observes the same value. |
| 80 | */ |
| 81 | @Test |
| 82 | public void testOtherInstance() { |
| 83 | along.set(42); |
| 84 | assertEquals(42, along.get()); |
| 85 | |
| 86 | final HZClient cl = HZClient.getClient(); |
| 87 | HazelcastInstance hz = TestUtils.callMethod(cl, "getHZInstance", null); |
| 88 | |
| 89 | SequencerRuntime sequencerRuntime = new HazelcastSequencerRuntime(hz); |
| 90 | LogBasedRuntime runtime = new HazelcastRuntime(hz, sequencerRuntime); |
| 91 | |
| 92 | LogAtomicLong anotherInstance = new LogAtomicLong(runtime, |
| 93 | along.getObjectID()); |
| 94 | assertEquals(42, anotherInstance.get()); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Confirm another instance with same ID initializes using snapshot. |
| 99 | */ |
| 100 | @Category(IntegrationTest.class) |
| 101 | @Test |
| 102 | public void testOtherInstanceFromSnapshot() { |
| 103 | along.set(42); |
| 104 | assertEquals(42, along.get()); |
| 105 | |
| 106 | final HZClient cl = HZClient.getClient(); |
| 107 | HazelcastInstance hz = TestUtils.callMethod(cl, "getHZInstance", null); |
| 108 | |
| 109 | SequencerRuntime sequencerRuntime = new HazelcastSequencerRuntime(hz); |
| 110 | LogBasedRuntime runtime = new HazelcastRuntime(hz, sequencerRuntime); |
| 111 | |
| 112 | // FIXME SNAPSHOT_INTERVAL should be customized to smaller values |
| 113 | // write multiple times to trigger snapshot |
| 114 | for (int i = 0; i < HazelcastRuntime.SNAPSHOT_INTERVAL + 2; ++i) { |
| 115 | along.set(i); |
| 116 | } |
| 117 | along.set(99); |
| 118 | Thread.yield(); |
| 119 | |
| 120 | // this instance might start from latest snap shot then replay |
| 121 | LogAtomicLong anotherInstance2 = new LogAtomicLong(runtime, |
| 122 | along.getObjectID()); |
| 123 | assertEquals(99, anotherInstance2.get()); |
| 124 | |
| 125 | } |
| 126 | } |