blob: 2c46073e86b35656926690bee65c8b120fc5629b [file] [log] [blame]
Jordan Halterman2bf177c2017-06-29 01:49:08 -07001/*
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 */
16package org.onosproject.store.primitives.resources.impl;
17
18import java.util.Arrays;
19import java.util.Collection;
Jordan Halterman035231e2017-07-18 08:39:07 -070020import java.util.concurrent.ScheduledExecutorService;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070021
22import io.atomix.protocols.raft.ReadConsistency;
23import io.atomix.protocols.raft.cluster.MemberId;
Jordan Halterman035231e2017-07-18 08:39:07 -070024import io.atomix.protocols.raft.impl.RaftContext;
Jordan Halterman2bf177c2017-06-29 01:49:08 -070025import io.atomix.protocols.raft.protocol.RaftServerProtocol;
26import io.atomix.protocols.raft.service.ServiceId;
27import io.atomix.protocols.raft.service.ServiceType;
28import io.atomix.protocols.raft.service.impl.DefaultCommit;
29import io.atomix.protocols.raft.service.impl.DefaultServiceContext;
30import io.atomix.protocols.raft.session.SessionId;
31import 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;
39import io.atomix.utils.concurrent.ThreadContext;
40import org.junit.Test;
41import org.onosproject.store.service.Task;
42
43import static org.easymock.EasyMock.expect;
44import static org.easymock.EasyMock.mock;
45import static org.easymock.EasyMock.replay;
46import static org.junit.Assert.assertArrayEquals;
47import static org.junit.Assert.assertEquals;
48import static org.junit.Assert.assertNotNull;
49import static org.onosproject.store.primitives.resources.impl.AtomixWorkQueueOperations.ADD;
50import static org.onosproject.store.primitives.resources.impl.AtomixWorkQueueOperations.TAKE;
51import static org.onosproject.store.service.DistributedPrimitive.Type.WORK_QUEUE;
52
53/**
54 * Work queue service test.
55 */
56public 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 Halterman035231e2017-07-18 08:39:07 -070071 RaftContext server = mock(RaftContext.class);
Jordan Halterman2bf177c2017-06-29 01:49:08 -070072 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 Halterman035231e2017-07-18 08:39:07 -070084 server,
85 mock(ScheduledExecutorService.class));
Jordan Halterman2bf177c2017-06-29 01:49:08 -070086
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}