blob: fb92ebaebd98259acb0dbbfe3be706a517516fd1 [file] [log] [blame]
Jordan Halterman47432582018-01-25 16:56:45 -08001/*
2 * Copyright 2018-present Open Networking Foundation
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
18import java.util.concurrent.atomic.AtomicLong;
19
20import io.atomix.protocols.raft.ReadConsistency;
21import io.atomix.protocols.raft.cluster.MemberId;
22import io.atomix.protocols.raft.impl.RaftContext;
Jordan Halterman4ce65e82018-02-15 18:09:38 -080023import io.atomix.protocols.raft.impl.RaftServiceManager;
Jordan Halterman47432582018-01-25 16:56:45 -080024import io.atomix.protocols.raft.operation.OperationType;
25import io.atomix.protocols.raft.protocol.RaftServerProtocol;
26import io.atomix.protocols.raft.service.ServiceId;
27import io.atomix.protocols.raft.service.ServiceType;
28import io.atomix.protocols.raft.service.impl.DefaultCommit;
29import io.atomix.protocols.raft.service.impl.DefaultServiceContext;
30import io.atomix.protocols.raft.session.SessionId;
31import io.atomix.protocols.raft.session.impl.RaftSessionContext;
32import io.atomix.protocols.raft.storage.RaftStorage;
33import io.atomix.protocols.raft.storage.snapshot.Snapshot;
34import io.atomix.protocols.raft.storage.snapshot.SnapshotReader;
35import io.atomix.protocols.raft.storage.snapshot.SnapshotStore;
36import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter;
37import io.atomix.storage.StorageLevel;
38import io.atomix.time.WallClockTimestamp;
39import io.atomix.utils.concurrent.AtomixThreadFactory;
40import io.atomix.utils.concurrent.SingleThreadContextFactory;
41import io.atomix.utils.concurrent.ThreadContext;
42import org.junit.Test;
43
44import static org.easymock.EasyMock.expect;
45import static org.easymock.EasyMock.mock;
46import static org.easymock.EasyMock.replay;
47import static org.onosproject.store.service.DistributedPrimitive.Type.LEADER_ELECTOR;
48
49/**
50 * Distributed lock service test.
51 */
52public class AtomixDistributedLockServiceTest {
53 @Test
54 public void testSnapshot() throws Exception {
55 SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder()
56 .withPrefix("test")
57 .withStorageLevel(StorageLevel.MEMORY)
58 .build());
Jordan Halterman4ce65e82018-02-15 18:09:38 -080059 Snapshot snapshot = store.newSnapshot(2, new WallClockTimestamp());
Jordan Halterman47432582018-01-25 16:56:45 -080060
61 AtomicLong index = new AtomicLong();
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 Halterman47432582018-01-25 16:56:45 -080066 expect(context.currentIndex()).andReturn(index.get()).anyTimes();
67 expect(context.currentOperation()).andReturn(OperationType.COMMAND).anyTimes();
68
69 RaftContext server = mock(RaftContext.class);
Jordan Halterman4ce65e82018-02-15 18:09:38 -080070 expect(server.getProtocol()).andReturn(mock(RaftServerProtocol.class)).anyTimes();
71 RaftServiceManager manager = mock(RaftServiceManager.class);
72 expect(manager.executor()).andReturn(mock(ThreadContext.class)).anyTimes();
73 expect(server.getServiceManager()).andReturn(manager).anyTimes();
Jordan Halterman47432582018-01-25 16:56:45 -080074
75 replay(context, server);
76
77 AtomixDistributedLockService service = new AtomixDistributedLockService();
78 service.init(context);
79
80 RaftSessionContext session = new RaftSessionContext(
81 SessionId.from(1),
82 MemberId.from("1"),
83 "test",
84 ServiceType.from(LEADER_ELECTOR.name()),
85 ReadConsistency.LINEARIZABLE,
86 100,
87 5000,
88 System.currentTimeMillis(),
89 context,
90 server,
91 new SingleThreadContextFactory(new AtomixThreadFactory()));
92 session.open();
93
94 service.lock(new DefaultCommit<>(
95 index.incrementAndGet(),
96 AtomixDistributedLockOperations.LOCK,
97 new AtomixDistributedLockOperations.Lock(1, 0),
98 session,
99 System.currentTimeMillis()));
100
101 try (SnapshotWriter writer = snapshot.openWriter()) {
102 service.snapshot(writer);
103 }
104
105 snapshot.complete();
106
107 service = new AtomixDistributedLockService();
108 try (SnapshotReader reader = snapshot.openReader()) {
109 service.install(reader);
110 }
111
112 service.unlock(new DefaultCommit<>(
113 index.incrementAndGet(),
114 AtomixDistributedLockOperations.UNLOCK,
115 new AtomixDistributedLockOperations.Unlock(1),
116 session,
117 System.currentTimeMillis()));
118 }
119}