Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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 | |
| 18 | import java.util.Arrays; |
| 19 | import java.util.Collection; |
Jordan Halterman | 035231e | 2017-07-18 08:39:07 -0700 | [diff] [blame] | 20 | import java.util.concurrent.ScheduledExecutorService; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 21 | |
| 22 | import io.atomix.protocols.raft.ReadConsistency; |
| 23 | import io.atomix.protocols.raft.cluster.MemberId; |
Jordan Halterman | 035231e | 2017-07-18 08:39:07 -0700 | [diff] [blame] | 24 | import io.atomix.protocols.raft.impl.RaftContext; |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 25 | import io.atomix.protocols.raft.protocol.RaftServerProtocol; |
| 26 | import io.atomix.protocols.raft.service.ServiceId; |
| 27 | import io.atomix.protocols.raft.service.ServiceType; |
| 28 | import io.atomix.protocols.raft.service.impl.DefaultCommit; |
| 29 | import io.atomix.protocols.raft.service.impl.DefaultServiceContext; |
| 30 | import io.atomix.protocols.raft.session.SessionId; |
| 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; |
| 39 | import io.atomix.utils.concurrent.ThreadContext; |
| 40 | import org.junit.Test; |
| 41 | import org.onosproject.store.service.Task; |
| 42 | |
| 43 | import static org.easymock.EasyMock.expect; |
| 44 | import static org.easymock.EasyMock.mock; |
| 45 | import static org.easymock.EasyMock.replay; |
| 46 | import static org.junit.Assert.assertArrayEquals; |
| 47 | import static org.junit.Assert.assertEquals; |
| 48 | import static org.junit.Assert.assertNotNull; |
| 49 | import static org.onosproject.store.primitives.resources.impl.AtomixWorkQueueOperations.ADD; |
| 50 | import static org.onosproject.store.primitives.resources.impl.AtomixWorkQueueOperations.TAKE; |
| 51 | import static org.onosproject.store.service.DistributedPrimitive.Type.WORK_QUEUE; |
| 52 | |
| 53 | /** |
| 54 | * Work queue service test. |
| 55 | */ |
| 56 | public class AtomixWorkQueueServiceTest { |
| 57 | @Test |
| 58 | public void testSnapshot() throws Exception { |
| 59 | SnapshotStore store = new SnapshotStore(RaftStorage.newBuilder() |
| 60 | .withPrefix("test") |
| 61 | .withStorageLevel(StorageLevel.MEMORY) |
| 62 | .build()); |
| 63 | Snapshot snapshot = store.newSnapshot(ServiceId.from(1), 2, new WallClockTimestamp()); |
| 64 | |
| 65 | DefaultServiceContext context = mock(DefaultServiceContext.class); |
| 66 | expect(context.serviceType()).andReturn(ServiceType.from(WORK_QUEUE.name())).anyTimes(); |
| 67 | expect(context.serviceName()).andReturn("test").anyTimes(); |
| 68 | expect(context.serviceId()).andReturn(ServiceId.from(1)).anyTimes(); |
| 69 | expect(context.executor()).andReturn(mock(ThreadContext.class)).anyTimes(); |
| 70 | |
Jordan Halterman | 035231e | 2017-07-18 08:39:07 -0700 | [diff] [blame] | 71 | RaftContext server = mock(RaftContext.class); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 72 | expect(server.getProtocol()).andReturn(mock(RaftServerProtocol.class)); |
| 73 | |
| 74 | replay(context, server); |
| 75 | |
| 76 | RaftSessionContext session = new RaftSessionContext( |
| 77 | SessionId.from(1), |
| 78 | MemberId.from("1"), |
| 79 | "test", |
| 80 | ServiceType.from(WORK_QUEUE.name()), |
| 81 | ReadConsistency.LINEARIZABLE, |
| 82 | 5000, |
| 83 | context, |
Jordan Halterman | 035231e | 2017-07-18 08:39:07 -0700 | [diff] [blame] | 84 | server, |
| 85 | mock(ScheduledExecutorService.class)); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 86 | |
| 87 | AtomixWorkQueueService service = new AtomixWorkQueueService(); |
| 88 | service.init(context); |
| 89 | |
| 90 | service.add(new DefaultCommit<>( |
| 91 | 2, |
| 92 | ADD, |
| 93 | new AtomixWorkQueueOperations.Add(Arrays.asList("Hello world!".getBytes())), |
| 94 | session, |
| 95 | System.currentTimeMillis())); |
| 96 | |
| 97 | try (SnapshotWriter writer = snapshot.openWriter()) { |
| 98 | service.snapshot(writer); |
| 99 | } |
| 100 | |
| 101 | snapshot.complete(); |
| 102 | |
| 103 | service = new AtomixWorkQueueService(); |
| 104 | service.init(context); |
| 105 | |
| 106 | try (SnapshotReader reader = snapshot.openReader()) { |
| 107 | service.install(reader); |
| 108 | } |
| 109 | |
| 110 | Collection<Task<byte[]>> value = service.take(new DefaultCommit<>( |
| 111 | 2, |
| 112 | TAKE, |
| 113 | new AtomixWorkQueueOperations.Take(1), |
| 114 | session, |
| 115 | System.currentTimeMillis())); |
| 116 | assertNotNull(value); |
| 117 | assertEquals(1, value.size()); |
| 118 | assertArrayEquals("Hello world!".getBytes(), value.iterator().next().payload()); |
| 119 | } |
| 120 | } |