blob: f640a81debab222a479713cfd57eeb87e3d3a0c5 [file] [log] [blame]
Jordan Halterman2bf177c2017-06-29 01:49:08 -07001/*
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 */
16package org.onosproject.store.primitives.resources.impl;
17
Jordan Halterman035231e2017-07-18 08:39:07 -070018import java.util.concurrent.ScheduledExecutorService;
19
Jordan Halterman2bf177c2017-06-29 01:49:08 -070020import io.atomix.protocols.raft.ReadConsistency;
21import io.atomix.protocols.raft.cluster.MemberId;
Jordan Halterman035231e2017-07-18 08:39:07 -070022import io.atomix.protocols.raft.impl.RaftContext;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070023import io.atomix.protocols.raft.protocol.RaftServerProtocol;
24import io.atomix.protocols.raft.service.ServiceId;
25import io.atomix.protocols.raft.service.ServiceType;
26import io.atomix.protocols.raft.service.impl.DefaultCommit;
27import io.atomix.protocols.raft.service.impl.DefaultServiceContext;
28import io.atomix.protocols.raft.session.SessionId;
29import io.atomix.protocols.raft.session.impl.RaftSessionContext;
30import io.atomix.protocols.raft.storage.RaftStorage;
31import io.atomix.protocols.raft.storage.snapshot.Snapshot;
32import io.atomix.protocols.raft.storage.snapshot.SnapshotReader;
33import io.atomix.protocols.raft.storage.snapshot.SnapshotStore;
34import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter;
35import io.atomix.storage.StorageLevel;
36import io.atomix.time.WallClockTimestamp;
37import 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());
61 Snapshot snapshot = store.newSnapshot(ServiceId.from(1), 2, new WallClockTimestamp());
62
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,
88 5000,
89 context,
Jordan Halterman035231e2017-07-18 08:39:07 -070090 server,
91 mock(ScheduledExecutorService.class)),
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}