blob: 5efdeb326dcc7df8b872a03ab7e638cbec734d9d [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 Halterman2bf177c2017-06-29 01:49:08 -070018import java.util.Collection;
Yuta HIGUCHIfbd9ae92018-01-24 23:39:06 -080019import java.util.Collections;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070020
Jordan Halterman15f33712018-06-21 00:00:15 -070021import io.atomix.protocols.raft.ReadConsistency;
22import io.atomix.protocols.raft.cluster.MemberId;
23import io.atomix.protocols.raft.impl.RaftContext;
24import io.atomix.protocols.raft.protocol.RaftServerProtocol;
25import io.atomix.protocols.raft.service.ServiceId;
26import io.atomix.protocols.raft.service.ServiceType;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070027import io.atomix.protocols.raft.service.impl.DefaultCommit;
Jordan Halterman15f33712018-06-21 00:00:15 -070028import io.atomix.protocols.raft.service.impl.DefaultServiceContext;
29import io.atomix.protocols.raft.session.RaftSession;
30import io.atomix.protocols.raft.session.SessionId;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070031import 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;
Jordan Halterman15f33712018-06-21 00:00:15 -070039import io.atomix.utils.concurrent.AtomixThreadFactory;
40import io.atomix.utils.concurrent.SingleThreadContextFactory;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070041import org.junit.Test;
42import org.onlab.util.Match;
43import org.onosproject.store.service.Versioned;
44
Jordan Halterman15f33712018-06-21 00:00:15 -070045import static org.easymock.EasyMock.expect;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070046import static org.easymock.EasyMock.mock;
Jordan Halterman15f33712018-06-21 00:00:15 -070047import static org.easymock.EasyMock.replay;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070048import static org.junit.Assert.assertArrayEquals;
49import static org.junit.Assert.assertEquals;
50import static org.junit.Assert.assertNotNull;
51import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.GET;
Jordan Halterman15f33712018-06-21 00:00:15 -070052import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.NEXT;
53import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.OPEN_ITERATOR;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070054import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.PUT;
Jordan Halterman15f33712018-06-21 00:00:15 -070055import static org.onosproject.store.service.DistributedPrimitive.Type.LEADER_ELECTOR;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070056
57/**
58 * Consistent set multimap service test.
59 */
60public class AtomixConsistentSetMultimapServiceTest {
61 @Test
62 @SuppressWarnings("unchecked")
63 public void testSnapshot() throws Exception {
64 SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder()
Jordan Halterman15f33712018-06-21 00:00:15 -070065 .withPrefix("test")
66 .withStorageLevel(StorageLevel.MEMORY)
67 .build());
Jordan Halterman4ce65e82018-02-15 18:09:38 -080068 Snapshot snapshot = store.newSnapshot(2, new WallClockTimestamp());
Jordan Halterman2bf177c2017-06-29 01:49:08 -070069
Jordan Halterman15f33712018-06-21 00:00:15 -070070 DefaultServiceContext context = mock(DefaultServiceContext.class);
71 expect(context.serviceType()).andReturn(ServiceType.from(LEADER_ELECTOR.name())).anyTimes();
72 expect(context.serviceName()).andReturn("test").anyTimes();
73 expect(context.serviceId()).andReturn(ServiceId.from(1)).anyTimes();
74
75 RaftContext server = mock(RaftContext.class);
76 expect(server.getProtocol()).andReturn(mock(RaftServerProtocol.class));
77
78 replay(context, server);
79
80 RaftSession 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
Jordan Halterman2bf177c2017-06-29 01:49:08 -070093 AtomixConsistentSetMultimapService service = new AtomixConsistentSetMultimapService();
94 service.put(new DefaultCommit<>(
Jordan Halterman15f33712018-06-21 00:00:15 -070095 2,
96 PUT,
97 new AtomixConsistentSetMultimapOperations.Put(
98 "foo", Collections.singletonList("Hello world!".getBytes()), Match.ANY),
99 session,
100 System.currentTimeMillis()));
101 service.openIterator(new DefaultCommit<>(
102 3,
103 OPEN_ITERATOR,
104 null,
105 session,
106 System.currentTimeMillis()));
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700107
108 try (SnapshotWriter writer = snapshot.openWriter()) {
109 service.snapshot(writer);
110 }
111
112 snapshot.complete();
113
114 service = new AtomixConsistentSetMultimapService();
115 try (SnapshotReader reader = snapshot.openReader()) {
116 service.install(reader);
117 }
118
119 Versioned<Collection<? extends byte[]>> value = service.get(new DefaultCommit<>(
Jordan Halterman15f33712018-06-21 00:00:15 -0700120 3,
121 GET,
122 new AtomixConsistentSetMultimapOperations.Get("foo"),
123 session,
124 System.currentTimeMillis()));
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700125 assertNotNull(value);
126 assertEquals(1, value.value().size());
127 assertArrayEquals("Hello world!".getBytes(), value.value().iterator().next());
Jordan Halterman15f33712018-06-21 00:00:15 -0700128
129 assertEquals(1, service.next(new DefaultCommit<>(
130 4,
131 NEXT,
132 new AtomixConsistentSetMultimapOperations.IteratorPosition(3L, 0),
133 session,
134 System.currentTimeMillis())).entries().size());
Jordan Halterman2bf177c2017-06-29 01:49:08 -0700135 }
136}