blob: a7cd94adfbde13e3f7908051a94bed91360437b0 [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
Jordan Haltermandae11602018-07-03 00:00:47 -070018import io.atomix.protocols.raft.ReadConsistency;
19import io.atomix.protocols.raft.cluster.MemberId;
20import io.atomix.protocols.raft.impl.RaftContext;
21import io.atomix.protocols.raft.protocol.RaftServerProtocol;
22import io.atomix.protocols.raft.service.ServiceId;
23import io.atomix.protocols.raft.service.ServiceType;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070024import io.atomix.protocols.raft.service.impl.DefaultCommit;
Jordan Haltermandae11602018-07-03 00:00:47 -070025import io.atomix.protocols.raft.service.impl.DefaultServiceContext;
26import io.atomix.protocols.raft.session.RaftSession;
27import io.atomix.protocols.raft.session.SessionId;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070028import io.atomix.protocols.raft.session.impl.RaftSessionContext;
29import io.atomix.protocols.raft.storage.RaftStorage;
30import io.atomix.protocols.raft.storage.snapshot.Snapshot;
31import io.atomix.protocols.raft.storage.snapshot.SnapshotReader;
32import io.atomix.protocols.raft.storage.snapshot.SnapshotStore;
33import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter;
34import io.atomix.storage.StorageLevel;
35import io.atomix.time.WallClockTimestamp;
Jordan Haltermandae11602018-07-03 00:00:47 -070036import io.atomix.utils.concurrent.AtomixThreadFactory;
37import io.atomix.utils.concurrent.SingleThreadContextFactory;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070038import org.junit.Test;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070039import org.onosproject.store.service.Versioned;
40
Jordan Haltermandae11602018-07-03 00:00:47 -070041import static org.easymock.EasyMock.expect;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070042import static org.easymock.EasyMock.mock;
Jordan Haltermandae11602018-07-03 00:00:47 -070043import static org.easymock.EasyMock.replay;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070044import static org.junit.Assert.assertArrayEquals;
Jordan Haltermandae11602018-07-03 00:00:47 -070045import static org.junit.Assert.assertEquals;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070046import static org.junit.Assert.assertNotNull;
47import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.GET;
Jordan Haltermandae11602018-07-03 00:00:47 -070048import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.NEXT;
49import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.OPEN_ITERATOR;
Jordan Halterman71635ae2017-07-28 10:35:43 -070050import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.PUT;
51import static org.onosproject.store.primitives.resources.impl.AtomixConsistentMapOperations.Put;
Jordan Haltermandae11602018-07-03 00:00:47 -070052import static org.onosproject.store.service.DistributedPrimitive.Type.LEADER_ELECTOR;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070053
54/**
55 * Consistent map service test.
56 */
57public class AtomixConsistentMapServiceTest {
58 @Test
59 @SuppressWarnings("unchecked")
60 public void testSnapshot() throws Exception {
61 SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder()
62 .withPrefix("test")
63 .withStorageLevel(StorageLevel.MEMORY)
64 .build());
Jordan Halterman4ce65e82018-02-15 18:09:38 -080065 Snapshot snapshot = store.newSnapshot(2, new WallClockTimestamp());
Jordan Halterman2bf177c2017-06-29 01:49:08 -070066
Jordan Haltermandae11602018-07-03 00:00:47 -070067 DefaultServiceContext context = mock(DefaultServiceContext.class);
68 expect(context.serviceType()).andReturn(ServiceType.from(LEADER_ELECTOR.name())).anyTimes();
69 expect(context.serviceName()).andReturn("test").anyTimes();
70 expect(context.serviceId()).andReturn(ServiceId.from(1)).anyTimes();
71
72 RaftContext server = mock(RaftContext.class);
73 expect(server.getProtocol()).andReturn(mock(RaftServerProtocol.class));
74
75 replay(context, server);
76
77 RaftSession 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
Jordan Halterman2bf177c2017-06-29 01:49:08 -070090 AtomixConsistentMapService service = new AtomixConsistentMapService();
Jordan Halterman71635ae2017-07-28 10:35:43 -070091 service.put(new DefaultCommit<>(
Jordan Halterman2bf177c2017-06-29 01:49:08 -070092 2,
Jordan Halterman71635ae2017-07-28 10:35:43 -070093 PUT,
94 new Put("foo", "Hello world!".getBytes()),
Jordan Haltermandae11602018-07-03 00:00:47 -070095 session,
Jordan Halterman2bf177c2017-06-29 01:49:08 -070096 System.currentTimeMillis()));
Jordan Haltermandae11602018-07-03 00:00:47 -070097 service.openIterator(new DefaultCommit<>(
98 3,
99 OPEN_ITERATOR,
100 null,
101 session,
102 System.currentTimeMillis()));
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700103
104 try (SnapshotWriter writer = snapshot.openWriter()) {
105 service.snapshot(writer);
106 }
107
108 snapshot.complete();
109
110 service = new AtomixConsistentMapService();
111 try (SnapshotReader reader = snapshot.openReader()) {
112 service.install(reader);
113 }
114
115 Versioned<byte[]> value = service.get(new DefaultCommit<>(
116 2,
117 GET,
118 new AtomixConsistentMapOperations.Get("foo"),
119 mock(RaftSessionContext.class),
120 System.currentTimeMillis()));
121 assertNotNull(value);
122 assertArrayEquals("Hello world!".getBytes(), value.value());
Jordan Haltermandae11602018-07-03 00:00:47 -0700123
124 assertEquals(1, service.next(new DefaultCommit<>(
125 4,
126 NEXT,
127 new AtomixConsistentMapOperations.IteratorPosition(3L, 0),
128 session,
129 System.currentTimeMillis())).entries().size());
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700130 }
131}