blob: 4f7aa72004372dadec344700e1ba43553e3d6009 [file] [log] [blame]
Jordan Halterman2bf177c2017-06-29 01:49:08 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jordan Halterman2bf177c2017-06-29 01:49:08 -07003 *
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 */
16package org.onosproject.store.primitives.resources.impl;
17
18import io.atomix.protocols.raft.ReadConsistency;
19import io.atomix.protocols.raft.cluster.MemberId;
Jordan Halterman035231e2017-07-18 08:39:07 -070020import io.atomix.protocols.raft.impl.RaftContext;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070021import io.atomix.protocols.raft.protocol.RaftServerProtocol;
22import io.atomix.protocols.raft.service.ServiceId;
23import io.atomix.protocols.raft.service.ServiceType;
24import io.atomix.protocols.raft.service.impl.DefaultCommit;
25import io.atomix.protocols.raft.service.impl.DefaultServiceContext;
26import io.atomix.protocols.raft.session.SessionId;
27import io.atomix.protocols.raft.session.impl.RaftSessionContext;
28import io.atomix.protocols.raft.storage.RaftStorage;
29import io.atomix.protocols.raft.storage.snapshot.Snapshot;
30import io.atomix.protocols.raft.storage.snapshot.SnapshotReader;
31import io.atomix.protocols.raft.storage.snapshot.SnapshotStore;
32import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter;
33import io.atomix.storage.StorageLevel;
34import io.atomix.time.WallClockTimestamp;
Jordan Haltermane372d852017-11-02 15:00:06 -070035import io.atomix.utils.concurrent.AtomixThreadFactory;
36import io.atomix.utils.concurrent.SingleThreadContextFactory;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070037import org.junit.Test;
38import org.onosproject.cluster.Leadership;
39import org.onosproject.cluster.NodeId;
40
41import static org.easymock.EasyMock.expect;
42import static org.easymock.EasyMock.mock;
43import static org.easymock.EasyMock.replay;
44import static org.junit.Assert.assertEquals;
45import static org.junit.Assert.assertNotNull;
46import static org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.GET_LEADERSHIP;
47import static org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.RUN;
48import static org.onosproject.store.service.DistributedPrimitive.Type.LEADER_ELECTOR;
49
50/**
51 * Leader elector service test.
52 */
53public class AtomixLeaderElectorServiceTest {
54 @Test
55 public void testSnapshot() throws Exception {
56 SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder()
57 .withPrefix("test")
58 .withStorageLevel(StorageLevel.MEMORY)
59 .build());
Jordan Haltermane636d762018-02-15 18:09:38 -080060 Snapshot snapshot = store.newSnapshot(2, new WallClockTimestamp());
Jordan Halterman2bf177c2017-06-29 01:49:08 -070061
62 DefaultServiceContext context = mock(DefaultServiceContext.class);
63 expect(context.serviceType()).andReturn(ServiceType.from(LEADER_ELECTOR.name())).anyTimes();
64 expect(context.serviceName()).andReturn("test").anyTimes();
65 expect(context.serviceId()).andReturn(ServiceId.from(1)).anyTimes();
Jordan Halterman2bf177c2017-06-29 01:49:08 -070066
Jordan Halterman035231e2017-07-18 08:39:07 -070067 RaftContext server = mock(RaftContext.class);
Jordan Halterman2bf177c2017-06-29 01:49:08 -070068 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,
Jordan Haltermane372d852017-11-02 15:00:06 -070086 100,
Jordan Halterman2bf177c2017-06-29 01:49:08 -070087 5000,
Jordan Halterman067e11b2017-12-08 20:39:09 -080088 System.currentTimeMillis(),
Jordan Halterman2bf177c2017-06-29 01:49:08 -070089 context,
Jordan Halterman035231e2017-07-18 08:39:07 -070090 server,
Jordan Haltermane372d852017-11-02 15:00:06 -070091 new SingleThreadContextFactory(new AtomixThreadFactory())),
Jordan Halterman2bf177c2017-06-29 01:49:08 -070092 System.currentTimeMillis()));
93
94 try (SnapshotWriter writer = snapshot.openWriter()) {
95 service.snapshot(writer);
96 }
97
98 snapshot.complete();
99
100 service = new AtomixLeaderElectorService();
101 service.init(context);
102
103 try (SnapshotReader reader = snapshot.openReader()) {
104 service.install(reader);
105 }
106
107 Leadership value = service.getLeadership(new DefaultCommit<>(
108 2,
109 GET_LEADERSHIP,
110 new AtomixLeaderElectorOperations.GetLeadership("test"),
111 mock(RaftSessionContext.class),
112 System.currentTimeMillis()));
113 assertNotNull(value);
114 assertEquals(value.leader().nodeId(), nodeId);
115 }
116}