Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present 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.primitives.resources.impl; |
| 17 | |
| 18 | import io.atomix.protocols.raft.ReadConsistency; |
| 19 | import io.atomix.protocols.raft.cluster.MemberId; |
| 20 | import io.atomix.protocols.raft.impl.RaftServerContext; |
| 21 | import io.atomix.protocols.raft.protocol.RaftServerProtocol; |
| 22 | import io.atomix.protocols.raft.service.ServiceId; |
| 23 | import io.atomix.protocols.raft.service.ServiceType; |
| 24 | import io.atomix.protocols.raft.service.impl.DefaultCommit; |
| 25 | import io.atomix.protocols.raft.service.impl.DefaultServiceContext; |
| 26 | import io.atomix.protocols.raft.session.SessionId; |
| 27 | import io.atomix.protocols.raft.session.impl.RaftSessionContext; |
| 28 | import io.atomix.protocols.raft.storage.RaftStorage; |
| 29 | import io.atomix.protocols.raft.storage.snapshot.Snapshot; |
| 30 | import io.atomix.protocols.raft.storage.snapshot.SnapshotReader; |
| 31 | import io.atomix.protocols.raft.storage.snapshot.SnapshotStore; |
| 32 | import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter; |
| 33 | import io.atomix.storage.StorageLevel; |
| 34 | import io.atomix.time.WallClockTimestamp; |
| 35 | import io.atomix.utils.concurrent.ThreadContext; |
| 36 | import org.junit.Test; |
| 37 | import org.onosproject.cluster.Leadership; |
| 38 | import org.onosproject.cluster.NodeId; |
| 39 | |
| 40 | import static org.easymock.EasyMock.expect; |
| 41 | import static org.easymock.EasyMock.mock; |
| 42 | import static org.easymock.EasyMock.replay; |
| 43 | import static org.junit.Assert.assertEquals; |
| 44 | import static org.junit.Assert.assertNotNull; |
| 45 | import static org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.GET_LEADERSHIP; |
| 46 | import static org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.RUN; |
| 47 | import static org.onosproject.store.service.DistributedPrimitive.Type.LEADER_ELECTOR; |
| 48 | |
| 49 | /** |
| 50 | * Leader elector service test. |
| 51 | */ |
| 52 | public class AtomixLeaderElectorServiceTest { |
| 53 | @Test |
| 54 | public void testSnapshot() throws Exception { |
| 55 | SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder() |
| 56 | .withPrefix("test") |
| 57 | .withStorageLevel(StorageLevel.MEMORY) |
| 58 | .build()); |
| 59 | Snapshot snapshot = store.newSnapshot(ServiceId.from(1), 2, new WallClockTimestamp()); |
| 60 | |
| 61 | DefaultServiceContext context = mock(DefaultServiceContext.class); |
| 62 | expect(context.serviceType()).andReturn(ServiceType.from(LEADER_ELECTOR.name())).anyTimes(); |
| 63 | expect(context.serviceName()).andReturn("test").anyTimes(); |
| 64 | expect(context.serviceId()).andReturn(ServiceId.from(1)).anyTimes(); |
| 65 | expect(context.executor()).andReturn(mock(ThreadContext.class)).anyTimes(); |
| 66 | |
| 67 | RaftServerContext server = mock(RaftServerContext.class); |
| 68 | expect(server.getProtocol()).andReturn(mock(RaftServerProtocol.class)); |
| 69 | |
| 70 | replay(context, server); |
| 71 | |
| 72 | AtomixLeaderElectorService service = new AtomixLeaderElectorService(); |
| 73 | service.init(context); |
| 74 | |
| 75 | NodeId nodeId = NodeId.nodeId("1"); |
| 76 | service.run(new DefaultCommit<>( |
| 77 | 2, |
| 78 | RUN, |
| 79 | new AtomixLeaderElectorOperations.Run("test", nodeId), |
| 80 | new RaftSessionContext( |
| 81 | SessionId.from(1), |
| 82 | MemberId.from("1"), |
| 83 | "test", |
| 84 | ServiceType.from(LEADER_ELECTOR.name()), |
| 85 | ReadConsistency.LINEARIZABLE, |
| 86 | 5000, |
| 87 | context, |
| 88 | server), |
| 89 | System.currentTimeMillis())); |
| 90 | |
| 91 | try (SnapshotWriter writer = snapshot.openWriter()) { |
| 92 | service.snapshot(writer); |
| 93 | } |
| 94 | |
| 95 | snapshot.complete(); |
| 96 | |
| 97 | service = new AtomixLeaderElectorService(); |
| 98 | service.init(context); |
| 99 | |
| 100 | try (SnapshotReader reader = snapshot.openReader()) { |
| 101 | service.install(reader); |
| 102 | } |
| 103 | |
| 104 | Leadership value = service.getLeadership(new DefaultCommit<>( |
| 105 | 2, |
| 106 | GET_LEADERSHIP, |
| 107 | new AtomixLeaderElectorOperations.GetLeadership("test"), |
| 108 | mock(RaftSessionContext.class), |
| 109 | System.currentTimeMillis())); |
| 110 | assertNotNull(value); |
| 111 | assertEquals(value.leader().nodeId(), nodeId); |
| 112 | } |
| 113 | } |