Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.store.primitives.resources.impl; |
| 17 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 18 | import java.util.Collection; |
Yuta HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 19 | import java.util.Collections; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 20 | |
Jordan Halterman | 15f3371 | 2018-06-21 00:00:15 -0700 | [diff] [blame] | 21 | import io.atomix.protocols.raft.ReadConsistency; |
| 22 | import io.atomix.protocols.raft.cluster.MemberId; |
| 23 | import io.atomix.protocols.raft.impl.RaftContext; |
| 24 | import io.atomix.protocols.raft.protocol.RaftServerProtocol; |
| 25 | import io.atomix.protocols.raft.service.ServiceId; |
| 26 | import io.atomix.protocols.raft.service.ServiceType; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 27 | import io.atomix.protocols.raft.service.impl.DefaultCommit; |
Jordan Halterman | 15f3371 | 2018-06-21 00:00:15 -0700 | [diff] [blame] | 28 | import io.atomix.protocols.raft.service.impl.DefaultServiceContext; |
| 29 | import io.atomix.protocols.raft.session.RaftSession; |
| 30 | import io.atomix.protocols.raft.session.SessionId; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 31 | import io.atomix.protocols.raft.session.impl.RaftSessionContext; |
| 32 | import io.atomix.protocols.raft.storage.RaftStorage; |
| 33 | import io.atomix.protocols.raft.storage.snapshot.Snapshot; |
| 34 | import io.atomix.protocols.raft.storage.snapshot.SnapshotReader; |
| 35 | import io.atomix.protocols.raft.storage.snapshot.SnapshotStore; |
| 36 | import io.atomix.protocols.raft.storage.snapshot.SnapshotWriter; |
| 37 | import io.atomix.storage.StorageLevel; |
| 38 | import io.atomix.time.WallClockTimestamp; |
Jordan Halterman | 15f3371 | 2018-06-21 00:00:15 -0700 | [diff] [blame] | 39 | import io.atomix.utils.concurrent.AtomixThreadFactory; |
| 40 | import io.atomix.utils.concurrent.SingleThreadContextFactory; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 41 | import org.junit.Test; |
| 42 | import org.onlab.util.Match; |
| 43 | import org.onosproject.store.service.Versioned; |
| 44 | |
Jordan Halterman | 15f3371 | 2018-06-21 00:00:15 -0700 | [diff] [blame] | 45 | import static org.easymock.EasyMock.expect; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 46 | import static org.easymock.EasyMock.mock; |
Jordan Halterman | 15f3371 | 2018-06-21 00:00:15 -0700 | [diff] [blame] | 47 | import static org.easymock.EasyMock.replay; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 48 | import static org.junit.Assert.assertArrayEquals; |
| 49 | import static org.junit.Assert.assertEquals; |
| 50 | import static org.junit.Assert.assertNotNull; |
| 51 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.GET; |
Jordan Halterman | 15f3371 | 2018-06-21 00:00:15 -0700 | [diff] [blame] | 52 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.NEXT; |
| 53 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.OPEN_ITERATOR; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 54 | import static org.onosproject.store.primitives.resources.impl.AtomixConsistentSetMultimapOperations.PUT; |
Jordan Halterman | 15f3371 | 2018-06-21 00:00:15 -0700 | [diff] [blame] | 55 | import static org.onosproject.store.service.DistributedPrimitive.Type.LEADER_ELECTOR; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * Consistent set multimap service test. |
| 59 | */ |
| 60 | public class AtomixConsistentSetMultimapServiceTest { |
| 61 | @Test |
| 62 | @SuppressWarnings("unchecked") |
| 63 | public void testSnapshot() throws Exception { |
| 64 | SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder() |
Jordan Halterman | 15f3371 | 2018-06-21 00:00:15 -0700 | [diff] [blame] | 65 | .withPrefix("test") |
| 66 | .withStorageLevel(StorageLevel.MEMORY) |
| 67 | .build()); |
Jordan Halterman | 4ce65e8 | 2018-02-15 18:09:38 -0800 | [diff] [blame] | 68 | Snapshot snapshot = store.newSnapshot(2, new WallClockTimestamp()); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 69 | |
Jordan Halterman | 15f3371 | 2018-06-21 00:00:15 -0700 | [diff] [blame] | 70 | 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 Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 93 | AtomixConsistentSetMultimapService service = new AtomixConsistentSetMultimapService(); |
| 94 | service.put(new DefaultCommit<>( |
Jordan Halterman | 15f3371 | 2018-06-21 00:00:15 -0700 | [diff] [blame] | 95 | 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 Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 107 | |
| 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 Halterman | 15f3371 | 2018-06-21 00:00:15 -0700 | [diff] [blame] | 120 | 3, |
| 121 | GET, |
| 122 | new AtomixConsistentSetMultimapOperations.Get("foo"), |
| 123 | session, |
| 124 | System.currentTimeMillis())); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 125 | assertNotNull(value); |
| 126 | assertEquals(1, value.value().size()); |
| 127 | assertArrayEquals("Hello world!".getBytes(), value.value().iterator().next()); |
Jordan Halterman | 15f3371 | 2018-06-21 00:00:15 -0700 | [diff] [blame] | 128 | |
| 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 Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 135 | } |
| 136 | } |