blob: e42243618e9a9c1bfb82589f536b00a32c2ce117 [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 io.atomix.utils.concurrent.ThreadContext;
38import org.junit.Test;
39import org.onosproject.cluster.Leadership;
40import org.onosproject.cluster.NodeId;
41
42import static org.easymock.EasyMock.expect;
43import static org.easymock.EasyMock.mock;
44import static org.easymock.EasyMock.replay;
45import static org.junit.Assert.assertEquals;
46import static org.junit.Assert.assertNotNull;
47import static org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.GET_LEADERSHIP;
48import static org.onosproject.store.primitives.resources.impl.AtomixLeaderElectorOperations.RUN;
49import static org.onosproject.store.service.DistributedPrimitive.Type.LEADER_ELECTOR;
50
51/**
52 * Leader elector service test.
53 */
54public class AtomixLeaderElectorServiceTest {
55 @Test
56 public void testSnapshot() throws Exception {
57 SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder()
58 .withPrefix("test")
59 .withStorageLevel(StorageLevel.MEMORY)
60 .build());
Jordan Haltermane372d852017-11-02 15:00:06 -070061 Snapshot snapshot = store.newSnapshot(ServiceId.from(1), "test", 2, new WallClockTimestamp());
Jordan Halterman2bf177c2017-06-29 01:49:08 -070062
63 DefaultServiceContext context = mock(DefaultServiceContext.class);
64 expect(context.serviceType()).andReturn(ServiceType.from(LEADER_ELECTOR.name())).anyTimes();
65 expect(context.serviceName()).andReturn("test").anyTimes();
66 expect(context.serviceId()).andReturn(ServiceId.from(1)).anyTimes();
67 expect(context.executor()).andReturn(mock(ThreadContext.class)).anyTimes();
68
Jordan Halterman035231e2017-07-18 08:39:07 -070069 RaftContext server = mock(RaftContext.class);
Jordan Halterman2bf177c2017-06-29 01:49:08 -070070 expect(server.getProtocol()).andReturn(mock(RaftServerProtocol.class));
71
72 replay(context, server);
73
74 AtomixLeaderElectorService service = new AtomixLeaderElectorService();
75 service.init(context);
76
77 NodeId nodeId = NodeId.nodeId("1");
78 service.run(new DefaultCommit<>(
79 2,
80 RUN,
81 new AtomixLeaderElectorOperations.Run("test", nodeId),
82 new RaftSessionContext(
83 SessionId.from(1),
84 MemberId.from("1"),
85 "test",
86 ServiceType.from(LEADER_ELECTOR.name()),
87 ReadConsistency.LINEARIZABLE,
Jordan Haltermane372d852017-11-02 15:00:06 -070088 100,
Jordan Halterman2bf177c2017-06-29 01:49:08 -070089 5000,
Jordan Halterman067e11b2017-12-08 20:39:09 -080090 System.currentTimeMillis(),
Jordan Halterman2bf177c2017-06-29 01:49:08 -070091 context,
Jordan Halterman035231e2017-07-18 08:39:07 -070092 server,
Jordan Haltermane372d852017-11-02 15:00:06 -070093 new SingleThreadContextFactory(new AtomixThreadFactory())),
Jordan Halterman2bf177c2017-06-29 01:49:08 -070094 System.currentTimeMillis()));
95
96 try (SnapshotWriter writer = snapshot.openWriter()) {
97 service.snapshot(writer);
98 }
99
100 snapshot.complete();
101
102 service = new AtomixLeaderElectorService();
103 service.init(context);
104
105 try (SnapshotReader reader = snapshot.openReader()) {
106 service.install(reader);
107 }
108
109 Leadership value = service.getLeadership(new DefaultCommit<>(
110 2,
111 GET_LEADERSHIP,
112 new AtomixLeaderElectorOperations.GetLeadership("test"),
113 mock(RaftSessionContext.class),
114 System.currentTimeMillis()));
115 assertNotNull(value);
116 assertEquals(value.leader().nodeId(), nodeId);
117 }
118}