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